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.plugin.AbstractUIPlugin; |
---|
52 | import org.osgi.framework.BundleContext; |
---|
53 | |
---|
54 | public class MdtPlugin extends AbstractUIPlugin |
---|
55 | { |
---|
56 | |
---|
57 | public static final String MODELICA_NATURE = |
---|
58 | "org.modelica.mdt.ModelicaNature"; |
---|
59 | |
---|
60 | private static final int INTERNAL_ERROR = 0; |
---|
61 | |
---|
62 | //The shared instance. |
---|
63 | private static MdtPlugin plugin; |
---|
64 | |
---|
65 | /** |
---|
66 | * The constructor. |
---|
67 | */ |
---|
68 | public MdtPlugin() |
---|
69 | { |
---|
70 | plugin = this; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | public static void addModelicaNature(IProject project) throws CoreException |
---|
75 | { |
---|
76 | if (project.hasNature(MODELICA_NATURE)) |
---|
77 | return; |
---|
78 | |
---|
79 | IProjectDescription description = project.getDescription(); |
---|
80 | String[] ids= description.getNatureIds(); |
---|
81 | String[] newIds= new String[ids.length + 1]; |
---|
82 | System.arraycopy(ids, 0, newIds, 0, ids.length); |
---|
83 | newIds[ids.length]= MODELICA_NATURE; |
---|
84 | description.setNatureIds(newIds); |
---|
85 | project.setDescription(description, null); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Returns the shared instance. |
---|
90 | */ |
---|
91 | public static MdtPlugin getDefault() |
---|
92 | { |
---|
93 | return plugin; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * This method is called upon plug-in activation |
---|
98 | */ |
---|
99 | public void start(BundleContext context) throws Exception |
---|
100 | { |
---|
101 | super.start(context); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * This method is called when the plug-in is stopped |
---|
106 | */ |
---|
107 | public void stop(BundleContext context) throws Exception |
---|
108 | { |
---|
109 | super.stop(context); |
---|
110 | plugin = null; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * @return returns this plugins symbolic name e.g. stuff like org.foo.bar |
---|
115 | */ |
---|
116 | public static String getSymbolicName() |
---|
117 | { |
---|
118 | return getDefault().getBundle().getSymbolicName(); |
---|
119 | } |
---|
120 | |
---|
121 | public static void tag(Widget widget, String tag) |
---|
122 | { |
---|
123 | widget.setData("name", tag); |
---|
124 | } |
---|
125 | |
---|
126 | // /** |
---|
127 | // * convinience wrapper method for loggin to plugin logger |
---|
128 | // * @param exception the exception to log |
---|
129 | // */ |
---|
130 | // public static void log(CoreException exception) |
---|
131 | // { |
---|
132 | // plugin.getLog().log(exception.getStatus()); |
---|
133 | // } |
---|
134 | |
---|
135 | /** |
---|
136 | * convinience wrapper method for loggin to plugin logger |
---|
137 | */ |
---|
138 | public static void log(IStatus stat) |
---|
139 | { |
---|
140 | plugin.getLog().log(stat); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * Returns the standard display to be used. The method first checks, if |
---|
146 | * the thread calling this method has an associated display. If so, this |
---|
147 | * display is returned. Otherwise the method returns the default display. |
---|
148 | */ |
---|
149 | public static Display getStandardDisplay() |
---|
150 | { |
---|
151 | Display display; |
---|
152 | display= Display.getCurrent(); |
---|
153 | if (display == null) |
---|
154 | display= Display.getDefault(); |
---|
155 | return display; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Logs an internal error with the specified throwable |
---|
160 | * |
---|
161 | * @param e the exception to be logged |
---|
162 | */ |
---|
163 | public static void log(Throwable e) |
---|
164 | { |
---|
165 | log(new Status(IStatus.ERROR, getSymbolicName(), |
---|
166 | INTERNAL_ERROR, |
---|
167 | "Internal Error", e)); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Note: This method is for internal use only. Clients should not call this method. |
---|
172 | */ |
---|
173 | public static Object[] concatenate(Object[] a1, Object[] a2) |
---|
174 | { |
---|
175 | int a1Len = 0; |
---|
176 | int a2Len = 0; |
---|
177 | if (a1 != null) |
---|
178 | { |
---|
179 | a1Len= a1.length; |
---|
180 | } |
---|
181 | if (a2 != null) |
---|
182 | { |
---|
183 | a2Len= a2.length; |
---|
184 | } |
---|
185 | |
---|
186 | Object[] res= new Object[a1Len + a2Len]; |
---|
187 | |
---|
188 | if (a1 != null) |
---|
189 | { |
---|
190 | System.arraycopy(a1, 0, res, 0, a1Len); |
---|
191 | } |
---|
192 | if (a2 != null) |
---|
193 | { |
---|
194 | System.arraycopy(a2, 0, res, a1Len, a2Len); |
---|
195 | } |
---|
196 | return res; |
---|
197 | } |
---|
198 | } |
---|