View Javadoc

1   package de.matthias_burbach.mosaique4eclipse.views;
2   
3   import org.eclipse.core.resources.IFile;
4   import org.eclipse.core.resources.IFolder;
5   import org.eclipse.core.resources.IResource;
6   import org.eclipse.jface.resource.ImageDescriptor;
7   import org.eclipse.jface.viewers.LabelProvider;
8   import org.eclipse.swt.graphics.Image;
9   import org.eclipse.ui.ISharedImages;
10  import org.eclipse.ui.PlatformUI;
11  
12  import de.matthias_burbach.mosaique.core.model.AbstractFileItem;
13  import de.matthias_burbach.mosaique.core.model.Put;
14  import de.matthias_burbach.mosaique.swing.AbstractFileItemNode;
15  import de.matthias_burbach.mosaique.swing.BaseMutableTreeNode;
16  import de.matthias_burbach.mosaique.swing.DefinitionNode;
17  import de.matthias_burbach.mosaique.swing.InsertNode;
18  import de.matthias_burbach.mosaique.swing.JspNode;
19  import de.matthias_burbach.mosaique.swing.LogicalPageNode;
20  import de.matthias_burbach.mosaique.swing.PutNode;
21  import de.matthias_burbach.mosaique.swing.PutsNode;
22  import de.matthias_burbach.mosaique4eclipse.MosaiquePlugin;
23  import de.matthias_burbach.mosaique4eclipse.util.OverlayImageIcon;
24  import de.matthias_burbach.mosaique4eclipse.util.Util;
25  
26  /***
27   * @author Matthias Burbach
28   */
29  public class MosaiqueLabelProvider extends LabelProvider {
30  
31      /***
32       * The S icon for Struts configs.
33       */
34      private Image strutsImage =
35          createImage("struts.gif");
36  
37      /***
38       * The icon for logical pages.
39       */
40      private Image logicalPageImage =
41          createImage("logical-page.gif");
42  
43      /***
44       * The icon for Tiles definitions.
45       */
46      private Image definitionImage =
47          createImage("definition.gif");
48  
49      /***
50       * The icon for JSPs.
51       */
52      private Image jspImage =
53          createImage("jsp.gif");
54  
55      /***
56       * The icon for insert tags.
57       */
58      private Image insertImage =
59          createImage("insert.gif");
60  
61      /***
62       * The icon for the puts node.
63       */
64      private Image putsImage =
65          createImage("puts.gif");
66  
67      /***
68       * The icon for put nodes of an even definition hierarchy level.
69       */
70      private Image evenPutImage =
71          createImage("green-bullet.gif");
72  
73      /***
74       * The icon for put nodes of an odd definition hierarchy level.
75       */
76      private Image oddPutImage =
77          createImage("empty-bullet.gif");
78  
79      /***
80       * The icon for put nodes defined dynamically on a JSP.
81       */
82      private Image dynamicPutImage =
83          createImage("red-bullet.gif");
84  
85      /***
86       * The content provider of the tree nodes this class provides lables for.
87       */
88      private MosaiqueTreeContentProvider mosaiqueTreeContentProvider;
89  
90      /***
91       * @param mosaiqueTreeContentProvider The content provider of the tree nodes
92       *                                    this class provides lables for.
93       */
94      public MosaiqueLabelProvider(
95              final MosaiqueTreeContentProvider mosaiqueTreeContentProvider) {
96          this.mosaiqueTreeContentProvider = mosaiqueTreeContentProvider;
97      }
98  
99      /*
100      * (non-Javadoc)
101      * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
102      */
103     /***
104      * {@inheritDoc}
105      */
106     public String getText(final Object obj) {
107         String result = "";
108         if (obj instanceof IResource) {
109             IResource resource = (IResource) obj;
110             result = resource.getName();
111         } else if (obj != null) {
112             result = obj.toString();
113         }
114         return decorateText(obj, result);
115     }
116 
117     /***
118      * @param element The element whose text to decorate.
119      * @param text The text to decorate.
120      * @return The decorated text.
121      */
122     private String decorateText(final Object element, final String text) {
123         String result = text;
124         if (element instanceof AbstractFileItemNode) {
125             AbstractFileItemNode fileItemNode = (AbstractFileItemNode) element;
126             AbstractFileItem fileItem = fileItemNode.getFileItem();
127             if (fileItem != null) {
128                 String filePath = fileItem.getFilePath();
129                 if (filePath != null
130                         && filePath.indexOf("vrp-localdeploy") != -1) {
131                     result = "[->] " + result;
132                 }
133             }
134         }
135         return result;
136     }
137 
138     /*
139      * (non-Javadoc)
140      * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
141      */
142     /***
143      * {@inheritDoc}
144      */
145     public Image getImage(final Object obj) {
146         Image result = null;
147         String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
148         if (obj instanceof IFolder) {
149            imageKey = ISharedImages.IMG_OBJ_FOLDER;
150         }
151         result = PlatformUI.getWorkbench().getSharedImages().getImage(
152                     imageKey);
153 
154         if (Util.isStrutsConfigFile(obj)) {
155             result = strutsImage;
156         } else if (obj instanceof LogicalPageNode) {
157             result = logicalPageImage;
158         } else if (obj instanceof DefinitionNode) {
159             result = definitionImage;
160         } else if (obj instanceof JspNode) {
161             result = jspImage;
162         } else if (obj instanceof InsertNode) {
163             result = insertImage;
164         } else if (obj instanceof PutsNode) {
165             result = putsImage;
166         } else if (obj instanceof PutNode) {
167             Put put = ((PutNode) obj).getPut();
168             if (put.isDynamicPut()) {
169                 result = dynamicPutImage;
170             } else {
171                 int level = put.getLevel();
172                 boolean isEven = (level % 2) == 0;
173                 if (isEven) {
174                     result = evenPutImage;
175                 } else {
176                     result = oddPutImage;
177                 }
178             }
179         }
180         result = decorateImage(result, obj);
181         return result;
182     }
183 
184     /***
185      * @param baseImage The base image to decorate.
186      * @param object The object whose image to decorate.
187      * @return The decorated image.
188      */
189     private Image decorateImage(final Image baseImage, final Object object) {
190        Image result = baseImage;
191        boolean isErroneous = false;
192        if (object instanceof BaseMutableTreeNode) {
193            BaseMutableTreeNode node = (BaseMutableTreeNode) object;
194            if (!(node instanceof PutsNode)) {
195                isErroneous = node.isErroneous();
196            }
197        } else if (Util.isStrutsConfigFile(object)) {
198            isErroneous = mosaiqueTreeContentProvider.isErroneousStrutsConfig(
199                        (IFile) object);
200        }
201        if (isErroneous) {
202            OverlayImageIcon overlay = new OverlayImageIcon(baseImage);
203            result = overlay.getImage();
204        }
205        return result;
206     }
207 
208     /***
209      * @param name The file name of the image to create.
210      * @return The image created.
211      */
212     private Image createImage(final String name) {
213         ImageDescriptor descriptor =
214             MosaiquePlugin.getImageDescriptor("icons/" + name);
215         Image result = descriptor.createImage();
216         return result;
217     }
218 }