1 package es.inteco.eni.swing;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Dimension;
6 import java.awt.Font;
7 import java.awt.Graphics;
8 import java.awt.Graphics2D;
9 import java.awt.geom.RoundRectangle2D;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import javax.swing.JPanel;
17
18 import es.inteco.eni.commons.EniConstants;
19 import es.inteco.eni.xml.XmlNode;
20 import es.inteco.eni.xml.XmlTree;
21
22
23
24
25
26
27
28 public class TransformedViewPane extends JPanel{
29
30
31
32
33 private static final long serialVersionUID = 1L;
34
35
36
37 private static final int MAX_LENGTH_TAG = 24;
38
39
40
41 public static final int LEAF_NODE_WIDTH = 140;
42
43
44
45 private static final int LEAF_NODE_HEIGHT = 13;
46
47
48
49 private static final int SPACE_NODES = 10;
50
51
52
53 private static final int ARCW_NODE = 10;
54
55
56
57 private static final int ARCH_NODE = 10;
58
59
60
61 private static final Color BACKGROUND_NODE_COLOR = Color.WHITE;
62
63
64
65
66 private static final List<Color> COLOR_LIST = new ArrayList<Color>();
67 static{
68 COLOR_LIST.add(new Color(0,38,100));
69 COLOR_LIST.add(new Color(0,90,140));
70 COLOR_LIST.add(new Color(0,123,165));
71 COLOR_LIST.add(new Color(0,185,228));
72 COLOR_LIST.add(new Color(192,192,192));
73 }
74
75
76
77
78
79
80 private XmlTree xmlTree;
81
82
83
84 private ENIValidator eniValidator;
85
86
87
88 private List<XmlNodePosition> xmlNodePositions;
89
90
91
92
93
94 public TransformedViewPane(ENIValidator eniValidator){
95 this.eniValidator = eniValidator;
96 addMouseListener(new TransformedViewMouseListener(eniValidator, this));
97 }
98
99
100
101
102
103 protected void paintComponent(Graphics g) {
104 super.paintComponent(g);
105
106
107 xmlNodePositions = new ArrayList<XmlNodePosition>();
108 if(xmlTree!=null){
109
110 paintNodes(xmlTree.getXmlNodes(), (Graphics2D)g, null);
111 }
112 }
113
114
115 private void paintNodes(Map<String, XmlNode> xmlNodes, Graphics2D graphics2, RoundRectangle2D parentRectangle) {
116 Iterator<Entry<String, XmlNode>> it = xmlNodes.entrySet().iterator();
117 int acumulativeLeafDescendent = 0;
118 while(it.hasNext()){
119 Entry<String, XmlNode> entry = it.next();
120 XmlNode xmlNode = entry.getValue();
121 RoundRectangle2D rectangle = paintXmlNode(xmlNode, graphics2, parentRectangle,acumulativeLeafDescendent);
122 paintNodes(xmlNode.getChilds(), graphics2, rectangle);
123 acumulativeLeafDescendent += xmlNode.getTotalLeafDescendants();
124 }
125
126 }
127
128
129
130
131
132
133
134
135
136
137 private RoundRectangle2D paintXmlNode(XmlNode xmlNode, Graphics2D graphics2, RoundRectangle2D parentRectangle, int acumulativeLeafDescendent){
138 int x = (LEAF_NODE_WIDTH + SPACE_NODES) * (xmlNode.getDeep()-1) + SPACE_NODES;
139 int parentY = parentRectangle!=null ? Double.valueOf(parentRectangle.getY()).intValue() : SPACE_NODES;
140 int y = parentY + (LEAF_NODE_HEIGHT + SPACE_NODES) * acumulativeLeafDescendent;
141 int width = LEAF_NODE_WIDTH;
142 int height = (LEAF_NODE_HEIGHT + SPACE_NODES) * xmlNode.getTotalLeafDescendants() - SPACE_NODES;
143
144
145 int colorIndex = (xmlNode.getDeep() -1) % COLOR_LIST.size();
146 Color lineColor = COLOR_LIST.get(colorIndex);
147
148
149 graphics2.setColor(lineColor);
150
151 if(FileOpener.isFile(xmlNode)){
152 graphics2.setStroke(new BasicStroke(8.0f));
153 }else{
154 graphics2.setStroke(new BasicStroke(3.0f));
155 }
156 RoundRectangle2D roundedRectangleBorder = new RoundRectangle2D.Float(x, y, width, height, ARCW_NODE, ARCH_NODE);
157 graphics2.draw(roundedRectangleBorder);
158
159 graphics2.setColor(BACKGROUND_NODE_COLOR);
160 RoundRectangle2D roundedRectangleContent = new RoundRectangle2D.Float(x, y, width, height, ARCW_NODE, ARCH_NODE);
161 graphics2.fill(roundedRectangleContent);
162
163 graphics2.setFont(new Font("Arial", Font.PLAIN, 10));
164 graphics2.setColor(Color.black);
165 graphics2.drawString(getLName(xmlNode.getlName()), x+5 ,y+10);
166
167
168 xmlNodePositions.add(new XmlNodePosition(x, y, width, height, xmlNode));
169 return roundedRectangleBorder;
170 }
171
172
173 private String getLName(String lName){
174 return lName.length()>MAX_LENGTH_TAG ? lName.substring(0, MAX_LENGTH_TAG-3) + "..." : lName;
175 }
176
177
178
179
180 public XmlTree getXmlTree() {
181 return xmlTree;
182 }
183
184
185
186
187 public void setXmlTree(XmlTree xmlTree) {
188 this.xmlTree = xmlTree;
189 int deep = xmlTree!=null ? xmlTree.getDeep() : 0;
190 int leafsNum = xmlTree!=null ? xmlTree.getLeafsNum() : 0;
191
192 int width = (LEAF_NODE_WIDTH + SPACE_NODES ) * deep + SPACE_NODES;
193 int height = (LEAF_NODE_HEIGHT + SPACE_NODES ) * leafsNum + SPACE_NODES;
194 setPreferredSize(new Dimension(width, height));
195 setSize(new Dimension(width, height));
196 }
197
198
199
200
201 public ENIValidator getEniValidator() {
202 return eniValidator;
203 }
204
205
206
207
208 public void setEniValidator(ENIValidator eniValidator) {
209 this.eniValidator = eniValidator;
210 }
211
212
213
214
215 public List<XmlNodePosition> getXmlNodePositions() {
216 return xmlNodePositions;
217 }
218
219
220
221
222 public void setXmlNodePositions(List<XmlNodePosition> xmlNodePositions) {
223 this.xmlNodePositions = xmlNodePositions;
224 }
225 }