1 | /* |
---|
2 | * This file is part of Modelica Development Tooling. |
---|
3 | * |
---|
4 | * Copyright (c) 2005, Linköpings universitet, Department of |
---|
5 | * Computer and Information Science, PELAB |
---|
6 | * |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * (The new BSD license, see also |
---|
10 | * http://www.opensource.org/licenses/bsd-license.php) |
---|
11 | * |
---|
12 | * |
---|
13 | * Redistribution and use in source and binary forms, with or without |
---|
14 | * modification, are permitted provided that the following conditions are |
---|
15 | * met: |
---|
16 | * |
---|
17 | * * Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * |
---|
20 | * * Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in |
---|
22 | * the documentation and/or other materials provided with the |
---|
23 | * distribution. |
---|
24 | * |
---|
25 | * * Neither the name of Linköpings universitet nor the names of its |
---|
26 | * contributors may be used to endorse or promote products derived from |
---|
27 | * this software without specific prior written permission. |
---|
28 | * |
---|
29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
32 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
33 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
34 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
35 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
39 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
40 | */ |
---|
41 | |
---|
42 | package org.modelica.mdt; |
---|
43 | |
---|
44 | import org.eclipse.core.resources.IProject; |
---|
45 | import org.eclipse.core.resources.IProjectDescription; |
---|
46 | import org.eclipse.core.runtime.CoreException; |
---|
47 | import org.eclipse.core.runtime.IStatus; |
---|
48 | import org.eclipse.core.runtime.Status; |
---|
49 | import org.eclipse.swt.widgets.Display; |
---|
50 | import org.eclipse.swt.widgets.Widget; |
---|
51 | import org.eclipse.ui.IWorkbenchPage; |
---|
52 | import org.eclipse.ui.IWorkbenchWindow; |
---|
53 | import org.eclipse.ui.plugin.AbstractUIPlugin; |
---|
54 | import org.modelica.mdt.core.ModelicaCore; |
---|
55 | import org.osgi.framework.BundleContext; |
---|
56 | |
---|
57 | public class MdtPlugin extends AbstractUIPlugin |
---|
58 | { |
---|
59 | |
---|
60 | public static final String MODELICA_NATURE = |
---|
61 | "org.modelica.mdt.ModelicaNature"; |
---|
62 | |
---|
63 | |
---|
64 | /* status codes used for logging */ |
---|
65 | private static final int INTERNAL_ERROR = 0; |
---|
66 | private static final int INTERNAL_WARNING = 1; |
---|
67 | |
---|
68 | /* The shared instance. */ |
---|
69 | private static MdtPlugin plugin; |
---|
70 | |
---|
71 | /** |
---|
72 | * The constructor. |
---|
73 | */ |
---|
74 | public MdtPlugin() |
---|
75 | { |
---|
76 | plugin = this; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | public static void addModelicaNature(IProject project) throws CoreException |
---|
81 | { |
---|
82 | if (project.hasNature(MODELICA_NATURE)) |
---|
83 | return; |
---|
84 | |
---|
85 | IProjectDescription description = project.getDescription(); |
---|
86 | String[] ids = description.getNatureIds(); |
---|
87 | String[] newIds = new String[ids.length + 1]; |
---|
88 | System.arraycopy(ids, 0, newIds, 0, ids.length); |
---|
89 | newIds[ids.length] = MODELICA_NATURE; |
---|
90 | description.setNatureIds(newIds); |
---|
91 | project.setDescription(description, null); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Returns the shared instance. |
---|
96 | */ |
---|
97 | public static MdtPlugin getDefault() |
---|
98 | { |
---|
99 | return plugin; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * This method is called upon plug-in activation |
---|
104 | */ |
---|
105 | public void start(BundleContext context) throws Exception |
---|
106 | { |
---|
107 | super.start(context); |
---|
108 | ModelicaCore.start(); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * This method is called when the plug-in is stopped |
---|
113 | */ |
---|
114 | public void stop(BundleContext context) throws Exception |
---|
115 | { |
---|
116 | super.stop(context); |
---|
117 | ModelicaCore.stop(); |
---|
118 | plugin = null; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @return returns this plugins symbolic name e.g. stuff like org.foo.bar |
---|
123 | */ |
---|
124 | public static String getSymbolicName() |
---|
125 | { |
---|
126 | return getDefault().getBundle().getSymbolicName(); |
---|
127 | } |
---|
128 | |
---|
129 | public static void tag(Widget widget, String tag) |
---|
130 | { |
---|
131 | widget.setData("name", tag); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * convinience wrapper method for loggin to plugin logger |
---|
136 | */ |
---|
137 | public static void log(IStatus stat) |
---|
138 | { |
---|
139 | plugin.getLog().log(stat); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * Returns the standard display to be used. The method first checks, if |
---|
145 | * the thread calling this method has an associated display. If so, this |
---|
146 | * display is returned. Otherwise the method returns the default display. |
---|
147 | */ |
---|
148 | public static Display getStandardDisplay() |
---|
149 | { |
---|
150 | Display display; |
---|
151 | display= Display.getCurrent(); |
---|
152 | if (display == null) |
---|
153 | display= Display.getDefault(); |
---|
154 | return display; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Logs an internal error with the specified throwable |
---|
159 | * |
---|
160 | * @param e the exception to be logged |
---|
161 | */ |
---|
162 | public static void log(Throwable e) |
---|
163 | { |
---|
164 | log(new Status(IStatus.ERROR, getSymbolicName(), |
---|
165 | INTERNAL_ERROR, |
---|
166 | e.getMessage(), e)); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Logs a runtime warning. A message that can be helpfull while |
---|
171 | * investigating possible problems. |
---|
172 | * |
---|
173 | * @param message warning message |
---|
174 | */ |
---|
175 | public static void logWarning(String message) |
---|
176 | { |
---|
177 | log(new Status(IStatus.WARNING, getSymbolicName(), |
---|
178 | INTERNAL_WARNING, message, null)); |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | private IWorkbenchPage internalGetActivePage() |
---|
183 | { |
---|
184 | IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow(); |
---|
185 | if (window == null) |
---|
186 | { |
---|
187 | return null; |
---|
188 | } |
---|
189 | return getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | public static IWorkbenchPage getActivePage() |
---|
194 | { |
---|
195 | return getDefault().internalGetActivePage(); |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | /** |
---|
200 | * Note: This method is for internal use only. Clients should not |
---|
201 | * call this method. |
---|
202 | */ |
---|
203 | public static Object[] concatenate(Object[] a1, Object[] a2) |
---|
204 | { |
---|
205 | int a1Len = 0; |
---|
206 | int a2Len = 0; |
---|
207 | if (a1 != null) |
---|
208 | { |
---|
209 | a1Len= a1.length; |
---|
210 | } |
---|
211 | if (a2 != null) |
---|
212 | { |
---|
213 | a2Len= a2.length; |
---|
214 | } |
---|
215 | |
---|
216 | Object[] res= new Object[a1Len + a2Len]; |
---|
217 | |
---|
218 | if (a1 != null) |
---|
219 | { |
---|
220 | System.arraycopy(a1, 0, res, 0, a1Len); |
---|
221 | } |
---|
222 | if (a2 != null) |
---|
223 | { |
---|
224 | System.arraycopy(a2, 0, res, a1Len, a2Len); |
---|
225 | } |
---|
226 | return res; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | } |
---|