Package: VdmUILabelProvider$1

VdmUILabelProvider$1

nameinstructionbranchcomplexitylinemethod
run()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.ui
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.ui.internal.viewsupport;
23:
24: import java.util.ArrayList;
25:
26: import org.eclipse.core.runtime.ListenerList;
27: import org.eclipse.core.runtime.SafeRunner;
28: import org.eclipse.jface.util.SafeRunnable;
29: import org.eclipse.jface.viewers.IColorProvider;
30: import org.eclipse.jface.viewers.ILabelDecorator;
31: import org.eclipse.jface.viewers.ILabelProvider;
32: import org.eclipse.jface.viewers.ILabelProviderListener;
33: import org.eclipse.jface.viewers.LabelProviderChangedEvent;
34: import org.eclipse.jface.viewers.StyledCellLabelProvider;
35: import org.eclipse.jface.viewers.StyledString;
36: import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
37: import org.eclipse.swt.graphics.Color;
38: import org.eclipse.swt.graphics.Image;
39: import org.overture.ide.ui.VdmUIPlugin;
40:
41: public class VdmUILabelProvider implements ILabelProvider, IColorProvider,
42:                 IStyledLabelProvider
43: {
44: //        private List<Image> images = new Vector<Image>();
45:
46:         protected ListenerList fListeners = new ListenerList();
47:
48:         protected VdmElementImageProvider fImageLabelProvider;
49:         // protected StorageLabelProvider fStorageLabelProvider;
50:
51:         private ArrayList<ILabelDecorator> fLabelDecorators;
52:
53:         private int fImageFlags;
54:         private long fTextFlags;
55:
56:         /**
57:          * Creates a new label provider with default flags.
58:          */
59:         public VdmUILabelProvider()
60:         {
61:                 this(VdmElementLabels.ALL_DEFAULT, VdmElementImageProvider.OVERLAY_ICONS);
62:         }
63:
64:         /**
65:          * @param textFlags
66:          * Flags defined in <code>JavaElementLabels</code>.
67:          * @param imageFlags
68:          * Flags defined in <code>JavaElementImageProvider</code>.
69:          */
70:         public VdmUILabelProvider(long textFlags, int imageFlags)
71:         {
72:                 fImageLabelProvider = new VdmElementImageProvider();
73:                 fLabelDecorators = null;
74:
75:                 // fStorageLabelProvider= new StorageLabelProvider();
76:                 fImageFlags = imageFlags;
77:                 fTextFlags = textFlags;
78:         }
79:
80:         /**
81:          * Adds a decorator to the label provider
82:          *
83:          * @param decorator
84:          * the decorator to add
85:          */
86:         public void addLabelDecorator(ILabelDecorator decorator)
87:         {
88:                 if (fLabelDecorators == null)
89:                 {
90:                         fLabelDecorators = new ArrayList<ILabelDecorator>(2);
91:                 }
92:                 fLabelDecorators.add(decorator);
93:         }
94:
95:         /**
96:          * Sets the textFlags.
97:          *
98:          * @param textFlags
99:          * The textFlags to set
100:          */
101:         public final void setTextFlags(long textFlags)
102:         {
103:                 fTextFlags = textFlags;
104:         }
105:
106:         /**
107:          * Sets the imageFlags
108:          *
109:          * @param imageFlags
110:          * The imageFlags to set
111:          */
112:         public final void setImageFlags(int imageFlags)
113:         {
114:                 fImageFlags = imageFlags;
115:         }
116:
117:         /**
118:          * Gets the image flags. Can be overwritten by super classes.
119:          *
120:          * @return Returns a int
121:          */
122:         public final int getImageFlags()
123:         {
124:                 return fImageFlags;
125:         }
126:
127:         /**
128:          * Gets the text flags.
129:          *
130:          * @return Returns a int
131:          */
132:         public final long getTextFlags()
133:         {
134:                 return fTextFlags;
135:         }
136:
137:         /**
138:          * Evaluates the image flags for a element. Can be overwritten by super classes.
139:          *
140:          * @param element
141:          * the element to compute the image flags for
142:          * @return Returns a int
143:          */
144:         protected int evaluateImageFlags(Object element)
145:         {
146:                 return getImageFlags();
147:         }
148:
149:         /**
150:          * Evaluates the text flags for a element. Can be overwritten by super classes.
151:          *
152:          * @param element
153:          * the element to compute the text flags for
154:          * @return Returns a int
155:          */
156:         protected long evaluateTextFlags(Object element)
157:         {
158:                 return getTextFlags();
159:         }
160:
161:         protected Image decorateImage(Image image, Object element)
162:         {
163:                 if (fLabelDecorators != null && image != null)
164:                 {
165:                         for (int i = 0; i < fLabelDecorators.size(); i++)
166:                         {
167:                                 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators.get(i);
168:                                 image = decorator.decorateImage(image, element);
169:                         }
170:                 }
171:                 return image;
172:         }
173:
174:         /*
175:          * (non-Javadoc)
176:          * @see ILabelProvider#getImage
177:          */
178:         public Image getImage(Object element)
179:         {
180:                 try
181:                 {
182:                         Image resultB = fImageLabelProvider.getImageLabel(element, evaluateImageFlags(element));
183:                         // if (result == null && (element instanceof IStorage)) {
184:                         // result= fStorageLabelProvider.getImage(element);
185:                         // }
186:                         Image result = decorateImage(resultB, element);
187:
188:                         
189:                         return result;
190:                 } catch (Exception e)
191:                 {
192:                         VdmUIPlugin.log(e);
193:                         return null;
194:                 }
195:         }
196:
197:         protected String decorateText(String text, Object element)
198:         {
199:                 if (fLabelDecorators != null && text.length() > 0)
200:                 {
201:                         for (int i = 0; i < fLabelDecorators.size(); i++)
202:                         {
203:                                 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators.get(i);
204:                                 String decorated = decorator.decorateText(text, element);
205:                                 if (decorated != null)
206:                                 {
207:                                         text = decorated;
208:                                 }
209:                         }
210:                 }
211:                 return text;
212:         }
213:
214:         /*
215:          * (non-Javadoc)
216:          * @see ILabelProvider#getText
217:          */
218:         public String getText(Object element)
219:         {
220:                 String result = VdmElementLabels.getTextLabel(element, evaluateTextFlags(element));
221:                 // if (result.length() == 0 && (element instanceof IStorage)) {
222:                 // result= fStorageLabelProvider.getText(element);
223:                 // }
224:                 return decorateText(result, element);
225:         }
226:
227:         public StyledString getStyledText(Object element)
228:         {
229:                 StyledString string = VdmElementLabels.getStyledTextLabel(element, (evaluateTextFlags(element) | VdmElementLabels.COLORIZE));
230:                 // if (string.length() == 0 && (element instanceof IStorage)) {
231:                 // string= new StyledString(fStorageLabelProvider.getText(element));
232:                 // }
233:                 String decorated = (string!=null?decorateText(string.getString(), element):null);
234:                 if (decorated != null)
235:                 {
236:                         return StyledCellLabelProvider.styleDecoratedString(decorated, StyledString.DECORATIONS_STYLER, string);
237:                 }
238:                 return string;
239:         }
240:
241:         /*
242:          * (non-Javadoc)
243:          * @see IBaseLabelProvider#dispose
244:          */
245:         public void dispose()
246:         {
247:                 if (fLabelDecorators != null)
248:                 {
249:                         for (int i = 0; i < fLabelDecorators.size(); i++)
250:                         {
251:                                 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators.get(i);
252:                                 decorator.dispose();
253:                         }
254:                         fLabelDecorators = null;
255:                 }
256:                 // fStorageLabelProvider.dispose();
257:                 fImageLabelProvider.dispose();
258:
259:                 // for (Image img : images)
260:                 // {
261:                 // if (!img.isDisposed())
262:                 // {
263:                 // img.dispose();
264:                 // }
265:                 // }
266:         }
267:
268:         /*
269:          * (non-Javadoc)
270:          * @see IBaseLabelProvider#addListener(ILabelProviderListener)
271:          */
272:         public void addListener(ILabelProviderListener listener)
273:         {
274:                 if (fLabelDecorators != null)
275:                 {
276:                         for (int i = 0; i < fLabelDecorators.size(); i++)
277:                         {
278:                                 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators.get(i);
279:                                 decorator.addListener(listener);
280:                         }
281:                 }
282:                 fListeners.add(listener);
283:         }
284:
285:         /*
286:          * (non-Javadoc)
287:          * @see IBaseLabelProvider#isLabelProperty(Object, String)
288:          */
289:         public boolean isLabelProperty(Object element, String property)
290:         {
291:                 return true;
292:         }
293:
294:         /*
295:          * (non-Javadoc)
296:          * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
297:          */
298:         public void removeListener(ILabelProviderListener listener)
299:         {
300:                 if (fLabelDecorators != null)
301:                 {
302:                         for (int i = 0; i < fLabelDecorators.size(); i++)
303:                         {
304:                                 ILabelDecorator decorator = (ILabelDecorator) fLabelDecorators.get(i);
305:                                 decorator.removeListener(listener);
306:                         }
307:                 }
308:                 fListeners.remove(listener);
309:         }
310:
311:         public static ILabelDecorator[] getDecorators(boolean errortick,
312:                         ILabelDecorator extra)
313:         {
314:                 if (errortick)
315:                 {
316:                         if (extra == null)
317:                         {
318:                                 return new ILabelDecorator[] {};
319:                         } else
320:                         {
321:                                 return new ILabelDecorator[] { extra };
322:                         }
323:                 }
324:                 if (extra != null)
325:                 {
326:                         return new ILabelDecorator[] { extra };
327:                 }
328:                 return null;
329:         }
330:
331:         /*
332:          * (non-Javadoc)
333:          * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
334:          */
335:         public Color getForeground(Object element)
336:         {
337:                 return null;
338:         }
339:
340:         /*
341:          * (non-Javadoc)
342:          * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
343:          */
344:         public Color getBackground(Object element)
345:         {
346:                 return null;
347:         }
348:
349:         /**
350:          * Fires a label provider changed event to all registered listeners Only listeners registered at the time this
351:          * method is called are notified.
352:          *
353:          * @param event
354:          * a label provider changed event
355:          * @see ILabelProviderListener#labelProviderChanged
356:          */
357:         protected void fireLabelProviderChanged(
358:                         final LabelProviderChangedEvent event)
359:         {
360:                 Object[] listeners = fListeners.getListeners();
361:                 for (int i = 0; i < listeners.length; ++i)
362:                 {
363:                         final ILabelProviderListener l = (ILabelProviderListener) listeners[i];
364:                         SafeRunner.run(new SafeRunnable()
365:                         {
366:                                 public void run()
367:                                 {
368:                                         l.labelProviderChanged(event);
369:                                 }
370:                         });
371:                 }
372:         }
373: }