View Javadoc

1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gwui.graphview;
7   
8   import de.fzi.wim.guibase.graphview.graph.Node;
9   import de.fzi.wim.guibase.graphview.view.JGraphPane;
10  import de.fzi.wim.guibase.graphview.view.NodePainter;
11  import net.kwfgrid.gworkflowdl.structure.Operation;
12  import net.kwfgrid.gwui.workflow.XMLUtilities;
13  import org.apache.log4j.Logger;
14  import org.glassbox.Theme;
15  import org.glassbox.graphview.NodeBorder;
16  import org.glassbox.graphview.TextNodePainter;
17  
18  import java.awt.*;
19  
20  /***
21   * The painter for <code>TransitionNodes</code>.<br>
22   * This painter has the following properties which can be configured via the application's theme:
23   * <ul>
24   * <li>kwfgrid.TransitionNodePainter.insets</li>
25   * <li>kwfgrid.TransitionNodePainter.default.font</li>
26   * <li>kwfgrid.TransitionNodePainter.bold.font</li>
27   * <li>kwfgrid.TransitionNodePainter.default.border.stroke</li>
28   * <li>kwfgrid.TransitionNodePainter.black.border.stroke</li>
29   * <li>kwfgrid.TransitionNodePainter.black.border.color</li>
30   * <li>kwfgrid.TransitionNodePainter.black.paint</li>
31   * <li>kwfgrid.TransitionNodePainter.black.text.color</li>
32   * <li>kwfgrid.TransitionNodePainter.black.size</li>
33   * <li>kwfgrid.TransitionNodePainter.blue.border.color</li>
34   * <li>kwfgrid.TransitionNodePainter.blue.paint</li>
35   * <li>kwfgrid.TransitionNodePainter.blue.text.color</li>
36   * <li>kwfgrid.TransitionNodePainter.red.border.color</li>
37   * <li>kwfgrid.TransitionNodePainter.red.paint</li>
38   * <li>kwfgrid.TransitionNodePainter.red.text.color</li>
39   * <li>kwfgrid.TransitionNodePainter.yellow.border.color</li>
40   * <li>kwfgrid.TransitionNodePainter.yellow.paint</li>
41   * <li>kwfgrid.TransitionNodePainter.yellow.text.color</li>
42   * <li>kwfgrid.TransitionNodePainter.green.border.color</li>
43   * <li>kwfgrid.TransitionNodePainter.green.paint</li>
44   * <li>kwfgrid.TransitionNodePainter.green.text.color</li>
45   * <li>kwfgrid.TransitionNodePainter.grey.border.color</li>
46   * <li>kwfgrid.TransitionNodePainter.grey.paint</li>
47   * <li>kwfgrid.TransitionNodePainter.grey.text.color</li>
48   * </ul>
49   */
50  public class TransitionNodePainter implements NodePainter {
51      private static final Logger logger = Logger.getLogger(TransitionNodePainter.class);
52  
53      protected static final String INSETS_KEY = "kwfgrid.TransitionNodePainter.insets";
54      protected static final Insets INSETS = Theme.getInsets(INSETS_KEY);
55      protected static final String DEFAULT_FONT_KEY = "kwfgrid.TransitionNodePainter.default.font";
56      protected static final Font DEFAULT_FONT = Theme.getFont(DEFAULT_FONT_KEY);
57      protected static final String BOLD_FONT_KEY = "kwfgrid.TransitionNodePainter.bold.font";
58      protected static final Font BOLD_FONT = Theme.getFont(BOLD_FONT_KEY);
59      protected static final String DEFAULT_BORDER_STROKE_KEY = "kwfgrid.TransitionNodePainter.default.border.stroke";
60      protected static final Stroke DEFAULT_BORDER_STROKE = Theme.getStroke(DEFAULT_BORDER_STROKE_KEY);
61  
62      protected static final String BLACK_STROKE_KEY = "kwfgrid.TransitionNodePainter.black.border.stroke";
63      protected static final String BLACK_BORDER_COLOR_KEY = "kwfgrid.TransitionNodePainter.black.border.color";
64      protected static final String BLACK_PAINT_KEY = "kwfgrid.TransitionNodePainter.black.paint";
65      protected static final String BLACK_TEXT_COLOR_KEY = "kwfgrid.TransitionNodePainter.black.text.color";
66      protected static final String BLACK_SIZE_KEY = "kwfgrid.TransitionNodePainter.black.size";
67      protected static final Stroke BLACK_STROKE = Theme.getStroke(BLACK_STROKE_KEY);
68      protected static final Color BLACK_BORDER_COLOR = Theme.getColor(BLACK_BORDER_COLOR_KEY);
69      protected static final Paint BLACK_PAINT = Theme.getPaint(BLACK_PAINT_KEY);
70      protected static final Color BLACK_TEXT_COLOR = Theme.getColor(BLACK_TEXT_COLOR_KEY);
71      protected static final Dimension BLACK_SIZE = Theme.getSize(BLACK_SIZE_KEY);
72  
73      protected static final String RED_BORDER_COLOR_KEY = "kwfgrid.TransitionNodePainter.red.border.color";
74      protected static final String RED_PAINT_KEY = "kwfgrid.TransitionNodePainter.red.paint";
75      protected static final String RED_TEXT_COLOR_KEY = "kwfgrid.TransitionNodePainter.red.text.color";
76      protected static final Color RED_BORDER_COLOR = Theme.getColor(RED_BORDER_COLOR_KEY);
77      protected static final Paint RED_PAINT = Theme.getPaint(RED_PAINT_KEY);
78      protected static final Color RED_TEXT_COLOR = Theme.getColor(RED_TEXT_COLOR_KEY);
79  
80      protected static final String YELLOW_BORDER_COLOR_KEY = "kwfgrid.TransitionNodePainter.yellow.border.color";
81      protected static final String YELLOW_PAINT_KEY = "kwfgrid.TransitionNodePainter.yellow.paint";
82      protected static final String YELLOW_TEXT_COLOR_KEY = "kwfgrid.TransitionNodePainter.yellow.text.color";
83      protected static final Color YELLOW_BORDER_COLOR = Theme.getColor(YELLOW_BORDER_COLOR_KEY);
84      protected static final Paint YELLOW_PAINT = Theme.getPaint(YELLOW_PAINT_KEY);
85      protected static final Color YELLOW_TEXT_COLOR = Theme.getColor(YELLOW_TEXT_COLOR_KEY);
86  
87      protected static final String GREEN_BORDER_COLOR_KEY = "kwfgrid.TransitionNodePainter.green.border.color";
88      protected static final String GREEN_PAINT_KEY = "kwfgrid.TransitionNodePainter.green.paint";
89      protected static final String GREEN_TEXT_COLOR_KEY = "kwfgrid.TransitionNodePainter.green.text.color";
90      protected static final Color GREEN_BORDER_COLOR = Theme.getColor(GREEN_BORDER_COLOR_KEY);
91      protected static final Paint GREEN_PAINT = Theme.getPaint(GREEN_PAINT_KEY);
92      protected static final Color GREEN_TEXT_COLOR = Theme.getColor(GREEN_TEXT_COLOR_KEY);
93  
94      protected static final String BLUE_BORDER_COLOR_KEY = "kwfgrid.TransitionNodePainter.blue.border.color";
95      protected static final String BLUE_PAINT_KEY = "kwfgrid.TransitionNodePainter.blue.paint";
96      protected static final String BLUE_TEXT_COLOR_KEY = "kwfgrid.TransitionNodePainter.blue.text.color";
97      protected static final Color BLUE_BORDER_COLOR = Theme.getColor(BLUE_BORDER_COLOR_KEY);
98      protected static final Paint BLUE_PAINT = Theme.getPaint(BLUE_PAINT_KEY);
99      protected static final Color BLUE_TEXT_COLOR = Theme.getColor(BLUE_TEXT_COLOR_KEY);
100 
101     protected static final String GREY_BORDER_COLOR_KEY = "kwfgrid.TransitionNodePainter.grey.border.color";
102     protected static final String GREY_PAINT_KEY = "kwfgrid.TransitionNodePainter.grey.paint";
103     protected static final String GREY_TEXT_COLOR_KEY = "kwfgrid.TransitionNodePainter.grey.text.color";
104     protected static final Color GREY_BORDER_COLOR = Theme.getColor(GREY_BORDER_COLOR_KEY);
105     protected static final Paint GREY_PAINT = Theme.getPaint(GREY_PAINT_KEY);
106     protected static final Color GREY_TEXT_COLOR = Theme.getColor(GREY_TEXT_COLOR_KEY);
107 
108     protected class TransitionTextPainter extends TextNodePainter {
109         public TransitionTextPainter(Font font, Color color) {
110             super(font, color);
111         }
112 
113         protected String[] getText(Node node) {
114             TransitionNode tnode = (TransitionNode) node;
115             return tnode.getText();
116         }
117     }
118 
119     /***
120      * An instance.
121      */
122     public static final TransitionNodePainter INSTANCE = new TransitionNodePainter();
123 
124     public TransitionNodePainter() {
125         //
126         logger.debug("TransitionNodePainter instantiated.");
127     }
128 
129     public void paintNode(JGraphPane gpane, Graphics2D g, Node node) {
130         logger.debug("Painting node: " + node);
131 
132         Stroke oldstroke = g.getStroke();
133         Color oldcolor = g.getColor();
134         TransitionNode tn = (TransitionNode) node;
135         NodePainter state = getState(tn);
136         state.paintNode(gpane, g, node);
137         g.setStroke(oldstroke);
138         g.setColor(oldcolor);
139     }
140 
141     public boolean isInNode(JGraphPane gpane, Node node, Point point) {
142         TransitionNode tn = (TransitionNode) node;
143         NodePainter state = getState(tn);
144         return state.isInNode(gpane, node, point);
145     }
146 
147     public void getNodeScreenBounds(JGraphPane gpane, Node node, Rectangle bounds) {
148         TransitionNode tn = (TransitionNode) node;
149         NodePainter state = getState(tn);
150         state.getNodeScreenBounds(gpane, node, bounds);
151     }
152 
153     public String getToolTipText(JGraphPane gpane, Node node, Point point) {
154         TransitionNode tnode = (TransitionNode) node;
155         StringBuffer t = new StringBuffer();
156         String level = XMLUtilities.getAbstractionLevelDescription(tnode.getTransition());
157         t.append("<html>").append(level).append("<br>ID: <b>").append(tnode.getTransition().getID()).append("</b>");
158         String description = (String) tnode.getTransition().getDescription();
159         if (description != null && description.length() > 0) {
160             t.append("<br><i>").append(description).append("</i>");
161         }
162         String lastResource = tnode.getTransition().getProperties().get("last.resource.name");
163         if (lastResource != null && lastResource.length() >0)
164             t.append("<br><br>last used resource: <b>").append(lastResource).append("</b>");
165         t.append(tnode.getOperationToolTipText());
166         t.append("</html>");
167         return t.toString();
168     }
169 
170     protected NodePainter getState(TransitionNode node) {
171         int level = node.getTransition().getAbstractionLevel();
172         if (Operation.RED == level) {
173             return RED;
174         } else if (Operation.BLUE == level) {
175             return BLUE;
176         } else if (Operation.GREEN == level) {
177             return GREEN;
178         } else if (Operation.YELLOW == level) {
179             return YELLOW;
180         } else {
181             return BLACK;
182         }
183     }
184 
185     /***
186      * Painter for abstraction level BLACK.
187      */
188     protected NodePainter BLACK = new NodePainter() {
189         protected Rectangle _bounds = new Rectangle();
190 
191         protected void setCurrentNodeBounds(JGraphPane gpane, Node node) {
192             Point p = gpane.getScreenPointForNode(node);
193             _bounds.setSize(BLACK_SIZE);
194             _bounds.setLocation(p.x - BLACK_SIZE.width / 2, p.y - BLACK_SIZE.height / 2);
195         }
196 
197         public void paintNode(JGraphPane gpane, Graphics2D g, Node node) {
198             gpane.getNodeScreenBounds(node, _bounds);
199             g.setPaint(BLACK_PAINT);
200             g.fillRect(_bounds.x, _bounds.y, _bounds.width, _bounds.height);
201             g.setStroke(BLACK_STROKE);
202             g.setColor(BLACK_BORDER_COLOR);
203             g.drawRect(_bounds.x, _bounds.y, _bounds.width - 1, _bounds.height - 1);
204         }
205 
206         public boolean isInNode(JGraphPane gpane, Node node, Point point) {
207             setCurrentNodeBounds(gpane, node);
208             return _bounds.contains(point);
209         }
210 
211         public void getNodeScreenBounds(JGraphPane gpane, Node node, Rectangle bounds) {
212             setCurrentNodeBounds(gpane, node);
213             bounds.setBounds(_bounds);
214         }
215 
216         public String getToolTipText(JGraphPane gpane, Node node, Point point) {
217             return null;
218         }
219     };
220 
221     /***
222      * Painter for abstraction level RED.
223      */
224     protected NodePainter RED = new NodeBorder(new TransitionTextPainter(DEFAULT_FONT, RED_TEXT_COLOR),
225             INSETS,
226             RED_BORDER_COLOR,
227             RED_PAINT,
228             DEFAULT_BORDER_STROKE,
229             new Dimension(0, 0));
230     /***
231      * Painter for abstraction level YELLOW.
232      */
233     protected NodePainter YELLOW = new NodeBorder(new TransitionTextPainter(DEFAULT_FONT, YELLOW_TEXT_COLOR),
234             INSETS,
235             YELLOW_BORDER_COLOR,
236             YELLOW_PAINT,
237             DEFAULT_BORDER_STROKE,
238             new Dimension(0, 0));
239     /***
240      * Painter for abstraction level BLUE.
241      */
242     protected NodePainter BLUE = new NodeBorder(new TransitionTextPainter(DEFAULT_FONT, BLUE_TEXT_COLOR),
243             INSETS,
244             BLUE_BORDER_COLOR,
245             BLUE_PAINT,
246             DEFAULT_BORDER_STROKE,
247             new Dimension(0, 0));
248     /***
249      * Painter for abstraction level GREEN.
250      */
251     protected NodePainter GREEN = new NodeBorder(new TransitionTextPainter(DEFAULT_FONT, GREEN_TEXT_COLOR),
252             INSETS,
253             GREEN_BORDER_COLOR,
254             GREEN_PAINT,
255             DEFAULT_BORDER_STROKE,
256             new Dimension(0, 0));
257 
258 }