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.ui; |
---|
43 | |
---|
44 | import org.eclipse.jface.resource.ImageDescriptor; |
---|
45 | import org.eclipse.ui.ISharedImages; |
---|
46 | import org.eclipse.ui.PlatformUI; |
---|
47 | import org.eclipse.ui.model.IWorkbenchAdapter; |
---|
48 | import org.eclipse.ui.model.WorkbenchAdapter; |
---|
49 | import org.modelica.mdt.core.IModelicaClass; |
---|
50 | import org.modelica.mdt.core.IModelicaComponent; |
---|
51 | import org.modelica.mdt.core.IModelicaElement; |
---|
52 | import org.modelica.mdt.core.IModelicaFile; |
---|
53 | import org.modelica.mdt.core.IModelicaSourceFile; |
---|
54 | import org.modelica.mdt.core.IModelicaFolder; |
---|
55 | import org.modelica.mdt.core.IModelicaProject; |
---|
56 | import org.modelica.mdt.core.IStandardLibrary; |
---|
57 | import org.modelica.mdt.core.compiler.CompilerException; |
---|
58 | import org.modelica.mdt.internal.core.ErrorManager; |
---|
59 | |
---|
60 | /** |
---|
61 | * This class mapps modelica objects (Modelica packages, classes, etc) to icons |
---|
62 | * and labels via IWorkbenchAdapter interface. This icon and lables are used in |
---|
63 | * for example Modelica Projects view. |
---|
64 | * |
---|
65 | * @author Elmir Jagudin |
---|
66 | */ |
---|
67 | |
---|
68 | public class ModelicaElementAdapter extends WorkbenchAdapter |
---|
69 | { |
---|
70 | |
---|
71 | @Override |
---|
72 | public String getLabel(Object object) |
---|
73 | { |
---|
74 | return ((IModelicaElement)object).getElementName(); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public ImageDescriptor getImageDescriptor(Object object) |
---|
79 | { |
---|
80 | if (object instanceof IModelicaProject) |
---|
81 | { |
---|
82 | /* |
---|
83 | * Isn't patterns beautifull ? |
---|
84 | */ |
---|
85 | IModelicaProject mproj = (IModelicaProject) object; |
---|
86 | IWorkbenchAdapter wadap = |
---|
87 | (IWorkbenchAdapter) mproj.getWrappedProject().getAdapter(IWorkbenchAdapter.class); |
---|
88 | return wadap.getImageDescriptor(mproj.getWrappedProject()); |
---|
89 | |
---|
90 | } |
---|
91 | else if (object instanceof IModelicaClass) |
---|
92 | { |
---|
93 | String imgTag = null; |
---|
94 | |
---|
95 | try |
---|
96 | { |
---|
97 | switch (((IModelicaClass)object).getRestrictionType()) |
---|
98 | { |
---|
99 | case PACKAGE: |
---|
100 | imgTag = ModelicaImages.IMG_OBJS_PACKAGE; |
---|
101 | break; |
---|
102 | case CLASS: |
---|
103 | imgTag = ModelicaImages.IMG_OBJS_CLASS; |
---|
104 | break; |
---|
105 | case MODEL: |
---|
106 | imgTag = ModelicaImages.IMG_OBJS_MODEL; |
---|
107 | break; |
---|
108 | case FUNCTION: |
---|
109 | imgTag = ModelicaImages.IMG_OBJS_FUNCTION; |
---|
110 | break; |
---|
111 | case RECORD: |
---|
112 | imgTag = ModelicaImages.IMG_OBJS_RECORD; |
---|
113 | break; |
---|
114 | case CONNECTOR: |
---|
115 | imgTag = ModelicaImages.IMG_OBJS_CONNECTOR; |
---|
116 | break; |
---|
117 | case BLOCK: |
---|
118 | imgTag = ModelicaImages.IMG_OBJS_BLOCK; |
---|
119 | break; |
---|
120 | case TYPE: |
---|
121 | imgTag = ModelicaImages.IMG_OBJS_TYPE; |
---|
122 | break; |
---|
123 | default: |
---|
124 | ErrorManager.logBug(UIPlugin.getSymbolicName(), |
---|
125 | "IModelicaClass object of unexpected restriction " + |
---|
126 | "type " + |
---|
127 | ((IModelicaClass)object).getRestrictionType() + |
---|
128 | " encountered."); |
---|
129 | imgTag = ""; |
---|
130 | } |
---|
131 | } |
---|
132 | catch (CompilerException e) |
---|
133 | { |
---|
134 | ErrorManager.logError(e); |
---|
135 | /* |
---|
136 | * hmm, let the class icon be default |
---|
137 | * 'i don't know your type' image |
---|
138 | */ |
---|
139 | imgTag = ModelicaImages.IMG_OBJS_CLASS; |
---|
140 | } |
---|
141 | return ModelicaImages.getImageDescriptor(imgTag); |
---|
142 | } |
---|
143 | else if (object instanceof IModelicaComponent) |
---|
144 | { |
---|
145 | switch (((IModelicaComponent)object).getVisbility()) |
---|
146 | { |
---|
147 | case PUBLIC: |
---|
148 | return ModelicaImages.getImageDescriptor |
---|
149 | (ModelicaImages.IMG_OBJS_PUBLIC_COMPONENT); |
---|
150 | case PROTECTED: |
---|
151 | return ModelicaImages.getImageDescriptor |
---|
152 | (ModelicaImages.IMG_OBJS_PROTECTED_COMPONENT); |
---|
153 | } |
---|
154 | } |
---|
155 | else if (object instanceof IModelicaSourceFile) |
---|
156 | { |
---|
157 | return ModelicaImages.getImageDescriptor(ModelicaImages.IMG_OBJS_MO_FILE); |
---|
158 | } |
---|
159 | /* |
---|
160 | * this check must be done after IModelicaSourceFile couse |
---|
161 | * IModelciaFile is superclass of IModelicaSourceFile |
---|
162 | */ |
---|
163 | else if (object instanceof IModelicaFile) |
---|
164 | { |
---|
165 | /* |
---|
166 | * pattern beauty continued... |
---|
167 | */ |
---|
168 | IModelicaFile mfile = (IModelicaFile) object; |
---|
169 | IWorkbenchAdapter wadap = |
---|
170 | (IWorkbenchAdapter) mfile.getResource().getAdapter(IWorkbenchAdapter.class); |
---|
171 | return wadap.getImageDescriptor(mfile.getResource()); |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | else if (object instanceof IModelicaFolder) |
---|
176 | { |
---|
177 | return PlatformUI.getWorkbench().getSharedImages(). |
---|
178 | getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER); |
---|
179 | } |
---|
180 | else if (object instanceof IStandardLibrary) |
---|
181 | { |
---|
182 | return ModelicaImages.getImageDescriptor(ModelicaImages.IMG_OBJS_LIBRARY); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | return super.getImageDescriptor(object); |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|