1 package de.fzi.wim.guibase.graphview.view;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.Point;
6 import java.awt.Rectangle;
7 import java.awt.Font;
8 import java.awt.FontMetrics;
9
10 import de.fzi.wim.guibase.graphview.graph.*;
11 import de.fzi.wim.guibase.graphview.controller.*;
12
13 /***
14 * The painter drawing the node as a rectangle.
15 */
16 public class RectangleNodePainter implements NodePainter {
17 /*** An instance. */
18 public static final NodePainter INSTANCE=new RectangleNodePainter();
19
20 /***
21 * Paints the supplied node.
22 *
23 * @param graphPane the graph pane
24 * @param g the graphics
25 * @param node the node to paint
26 */
27 public void paintNode(JGraphPane graphPane,Graphics2D g,Node node) {
28 HighlightingManipulator highlightingManipulator=(HighlightingManipulator)graphPane.getManipulator(HighlightingManipulator.NAME);
29 boolean isHighlighted=highlightingManipulator!=null && highlightingManipulator.getHighlightedNode()==node;
30 SelectionManipulator selectionManipulator=(SelectionManipulator)graphPane.getManipulator(SelectionManipulator.NAME);
31 boolean isSelected=selectionManipulator!=null && selectionManipulator.getNodeSelectionModel().isNodeSelected(node);
32 DraggingManipulator draggingManipulator=(DraggingManipulator)graphPane.getManipulator(DraggingManipulator.NAME);
33 boolean isDragging=draggingManipulator!=null && draggingManipulator.getDraggedNode()==node;
34 Point nodePoint=graphPane.getScreenPointForNode(node);
35 int width=1;
36 int height=1;
37 int textX=0;
38 int textY=0;
39 String label=node.getLabel();
40 if (label!=null) {
41 FontMetrics fontMetrics=graphPane.getFontMetrics(graphPane.getFont());
42 int stringWidth=fontMetrics.stringWidth(label);
43 width+=stringWidth+6;
44 height+=fontMetrics.getAscent()+fontMetrics.getDescent()+4;
45 textX=nodePoint.x-stringWidth/2;
46 textY=nodePoint.y+(fontMetrics.getAscent()-fontMetrics.getDescent())/2;
47 }
48 else {
49 width+=40;
50 height+=20;
51 }
52 Color oldColor=g.getColor();
53 g.setColor(getBackgroundColor(isHighlighted,isSelected,isDragging));
54 g.fillRect(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
55 if (label!=null) {
56 Font oldFont=g.getFont();
57 g.setFont(graphPane.getFont());
58 g.setColor(getTextColor(isHighlighted,isSelected,isDragging));
59 g.drawString(label,textX,textY);
60 g.setFont(oldFont);
61 }
62 g.setColor(getBorderColor(isHighlighted,isSelected,isDragging));
63 g.drawRect(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
64 g.setColor(oldColor);
65 }
66 /***
67 * Returns the border color of the node.
68 *
69 * @param isHighlighted <code>true</code> if the node is highlighted
70 * @param isSelected <code>true</code> if the node is selected
71 * @param isDragging <code>true</code> if the node is being dragged
72 * @return the border color
73 */
74 protected Color getBorderColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
75 return isHighlighted || isDragging ? Color.red : Color.black;
76 }
77 /***
78 * Returns the background color of the node.
79 *
80 * @param isHighlighted <code>true</code> if the node is highlighted
81 * @param isSelected <code>true</code> if the node is selected
82 * @param isDragging <code>true</code> if the node is being dragged
83 * @return the background color
84 */
85 protected Color getBackgroundColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
86 if (isSelected)
87 return Color.cyan;
88 else
89 return Color.yellow;
90 }
91 /***
92 * Returns the text color of the node
93 *
94 * @param isHighlighted <code>true</code> if the node is highlighted
95 * @param isSelected <code>true</code> if the node is selected
96 * @param isDragging <code>true</code> if the node is being dragged
97 * @return the background color
98 */
99 protected Color getTextColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
100 return Color.black;
101 }
102 /***
103 * Checks whether given point is inside the node.
104 *
105 * @param graphPane the graph pane
106 * @param node the node
107 * @param point the point
108 * @return <code>true</code> if the point is in the node
109 */
110 public boolean isInNode(JGraphPane graphPane,Node node,Point point) {
111 Rectangle nodeScreenRectangle=new Rectangle();
112 getNodeScreenBounds(graphPane,node,nodeScreenRectangle);
113 return nodeScreenRectangle.contains(point);
114 }
115 /***
116 * Returns the outer rectangle of the node on screen.
117 *
118 * @param graphPane the graph pane
119 * @param node the node
120 * @param nodeScreenRectangle the rectangle receiving the node's coordinates
121 */
122 public void getNodeScreenBounds(JGraphPane graphPane,Node node,Rectangle nodeScreenRectangle) {
123 Point nodePoint=graphPane.getScreenPointForNode(node);
124 String label=node.getLabel();
125 int width=1;
126 int height=1;
127 if (label!=null) {
128 FontMetrics fontMetrics=graphPane.getFontMetrics(graphPane.getFont());
129 width+=fontMetrics.stringWidth(label)+6;
130 height+=fontMetrics.getAscent()+fontMetrics.getDescent()+4;
131 }
132 else {
133 width+=40;
134 height+=20;
135 }
136 nodeScreenRectangle.setBounds(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
137 }
138 /***
139 * Retruns the tool-tip for given point.
140 *
141 * @param graphPane the graph pane
142 * @param node the node
143 * @param point the point
144 * @return the tool-tip at given point (or <code>null</code>)
145 */
146 public String getToolTipText(JGraphPane graphPane,Node node,Point point) {
147 return null;
148 }
149 }