View Javadoc

1   package de.matthias_burbach.deputy.swing;
2   
3   import java.awt.Component;
4   
5   import javax.swing.Icon;
6   import javax.swing.JTree;
7   import javax.swing.tree.DefaultTreeCellRenderer;
8   
9   import de.matthias_burbach.deputy.core.Deputy;
10  import de.matthias_burbach.deputy.core.project.Project;
11  import de.matthias_burbach.deputy.core.project.ProjectConflictGroup;
12  import de.matthias_burbach.deputy.core.repository.RepositoryArtifact;
13  
14  /***
15   * The tree cell renderer responsible for displaying tree node specific
16   * icons in both the project and the repositories tree.
17   *
18   * @author Matthias Burbach
19   */
20  public class DeputyTreeCellRenderer extends DefaultTreeCellRenderer {
21      /***
22       * For some reason it doesn't work to use a non-empty path for icons.
23       * Icons would not be found when running Deputy outside of Eclipse.
24       */
25      private String iconPath = ""; // "icons" + File.separator;
26  
27      /*
28       * Dependency icons --------------------------------------------------------
29       */
30      /***
31       * The white component icon for dependencies not being part of the
32       * top level project and for artifacts in the repositories tree.
33       */
34      private Icon whiteComponentIcon =
35          SwingHelper.createImageIcon(iconPath + "white-component.gif", "");
36  
37      /***
38       * The green component icon for dependencies being part of the top level
39       * project.
40       */
41      private Icon greenComponentIcon =
42          SwingHelper.createImageIcon(iconPath + "green-component.gif", "");
43  
44      /***
45       * The grey arrow up icon for used by dependencies.
46       */
47      private Icon greyArrowUpIcon =
48          SwingHelper.createImageIcon(iconPath + "grey-arrow-up.gif", "");
49  
50      /***
51       * The blue arrow down for direct dependencies.
52       */
53      private Icon blueArrowDownIcon =
54          SwingHelper.createImageIcon(iconPath + "blue-arrow-down.gif", "");
55  
56      /***
57       * The blue red arrow down for indirect dependencies.
58       */
59      private Icon blueRedArrowDownIcon =
60          SwingHelper.createImageIcon(iconPath + "blue-red-arrow-down.gif", "");
61  
62      /*
63       * Conflicts icons ---------------------------------------------------------
64       */
65      /***
66       * The conflicts icon.
67       */
68      private Icon conflictsIcon =
69          SwingHelper.createImageIcon(iconPath + "lightning.gif", "");
70  
71      /***
72       * The icon for conflicts of high severity.
73       */
74      private Icon errorIcon =
75          SwingHelper.createImageIcon(iconPath + "error.gif", "");
76  
77      /***
78       * The icon for conflicts of mid severity.
79       */
80      private Icon warningIcon =
81          SwingHelper.createImageIcon(iconPath + "warning.gif", "");
82  
83      /***
84       * The icon for conflicts of low severity.
85       */
86      private Icon infoIcon =
87          SwingHelper.createImageIcon(iconPath + "info.gif", "");
88  
89      /*
90       * Rules icons -------------------------------------------------------------
91       */
92      /***
93       * The rules icon.
94       */
95      private Icon rulesIcon =
96          SwingHelper.createImageIcon(iconPath + "paragraph.gif", "");
97  
98      /***
99       * The icon for enforcement rules.
100      */
101     private Icon enforcementIcon =
102         SwingHelper.createImageIcon(iconPath + "enforcement.gif", "");
103 
104     /***
105      * The icon for deprecation rules.
106      */
107     private Icon deprecationIcon =
108         SwingHelper.createImageIcon(iconPath + "deprecation.gif", "");
109 
110     /***
111      * The icon for replacement rules.
112      */
113     private Icon replacementIcon =
114         SwingHelper.createImageIcon(iconPath + "replacement.gif", "");
115 
116     /***
117      * The icon for removal rules.
118      */
119     private Icon removalIcon =
120         SwingHelper.createImageIcon(iconPath + "removal.gif", "");
121 
122     /***
123      * The icon for retention rules.
124      */
125     private Icon retentionIcon =
126         SwingHelper.createImageIcon(iconPath + "ampersand.gif", "");
127 
128     /***
129      * The Deputy core application. Needed to figure out properties of nodes
130      * which are relevant for choosing the appropriate icon.
131      */
132     private Deputy deputy;
133 
134     /***
135      * @param deputy The Deputy core application.
136      */
137     public DeputyTreeCellRenderer(final Deputy deputy) {
138         this.deputy = deputy;
139     }
140 
141     /*(non-Javadoc)
142      * @see javax.swing.tree.TreeCellRenderer
143      *          #getTreeCellRendererComponent(
144      *              javax.swing.JTree,
145      *              java.lang.Object,
146      *              boolean,
147      *              boolean,
148      *              boolean,
149      *              int,
150      *              boolean)
151      */
152     /***
153      * {@inheritDoc}
154      */
155     public Component getTreeCellRendererComponent(
156             final JTree tree,
157             final Object value,
158             final boolean sel,
159             final boolean expanded,
160             final boolean leaf,
161             final int row,
162             final boolean havingFocus) {
163         Component component = super.getTreeCellRendererComponent(
164             tree,
165             value,
166             sel,
167             expanded,
168             leaf,
169             row,
170             havingFocus);
171         if (value instanceof DependenciesTreeNode) {
172             setIcon(blueArrowDownIcon);
173         } else if (value instanceof UsedByTreeNode) {
174             setIcon(greyArrowUpIcon);
175         } else if (value instanceof IndirectDependenciesTreeNode) {
176             setIcon(blueRedArrowDownIcon);
177         } else if (value instanceof ConflictsTreeNode) {
178             setIcon(conflictsIcon);
179         } else if (value instanceof RuleSetTreeNode) {
180             setIcon(rulesIcon);
181         } else if (value instanceof EnforcementRuleTreeNode) {
182             setIcon(enforcementIcon);
183         } else if (value instanceof DeprecationRuleTreeNode) {
184             setIcon(deprecationIcon);
185         } else if (value instanceof ReplacementRuleTreeNode) {
186             setIcon(replacementIcon);
187         } else if (value instanceof RemovalRuleTreeNode) {
188             setIcon(removalIcon);
189         } else if (value instanceof RetentionRuleTreeNode) {
190             setIcon(retentionIcon);
191         } else if (value instanceof ConflictTreeNode) {
192             int severity =
193                 ((ConflictTreeNode) value).getConflictGroup().getSeverity();
194             switch (severity) {
195                 case ProjectConflictGroup.SEVERITY_INFO:
196                     setIcon(infoIcon);
197                     break;
198                 case ProjectConflictGroup.SEVERITY_WARNING:
199                     setIcon(warningIcon);
200                     break;
201                 default:
202                     setIcon(errorIcon);
203             }
204         } else if (value instanceof ProjectTreeNode) {
205             setIcon(whiteComponentIcon);
206             ProjectTreeNode node = (ProjectTreeNode) value;
207             Project topProject = deputy.getRootProject();
208             if ((topProject.isAssembly()
209                     && topProject.hasDirectOrIndirectDependency(
210                             node.getProject()))
211                     || (!topProject.isAssembly()
212                             && topProject.hasDependency(node.getProject()))) {
213                 //mark node as direct or indirect dependency of top project
214                 setIcon(greenComponentIcon);
215             }
216         } else if (value instanceof RepositoryArtifact) {
217             setIcon(whiteComponentIcon);
218         }
219         return component;
220     }
221 }