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