View Javadoc

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   * Panel que representa la vista transformada
24   * 
25   * 
26   *
27   */
28  public class TransformedViewPane extends JPanel{
29  
30  	/**
31  	 * Version
32  	 */
33  	private static final long serialVersionUID = 1L;
34  	/**
35  	 * Longitud maxima de una etiqueta
36  	 */
37  	private static final int MAX_LENGTH_TAG = 24;
38  	/**
39  	 * Ancho de un nodo hoja
40  	 */
41  	public static final int LEAF_NODE_WIDTH = 140;
42  	/**
43  	 * Alto de un nodo hoja
44  	 */
45  	private static final int LEAF_NODE_HEIGHT = 13;
46  	/**
47  	 * Espacio entre nodos
48  	 */
49  	private static final int SPACE_NODES = 10;
50  	/**
51  	 * Ancho del arco de los nodos
52  	 */
53  	private static final int ARCW_NODE = 10;
54  	/**
55  	 * Alto del arco de los nodos
56  	 */
57  	private static final int ARCH_NODE = 10;
58  	/**
59  	 * Color del fondo de los nodos
60  	 */
61  	private static final Color BACKGROUND_NODE_COLOR = Color.WHITE;
62  
63  	/***
64  	 * Lista de colores de los nodos
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  	 * Arbol
79  	 */
80  	private XmlTree xmlTree;
81  	/**
82  	 * Ventana del validador
83  	 */
84  	private ENIValidator eniValidator;
85  	/**
86  	 * Posiciones de los nodos
87  	 */
88  	private List<XmlNodePosition> xmlNodePositions;
89  
90  	/**
91  	 * Constructor
92  	 * @param eniValidator Ventana del validador
93  	 */
94  	public TransformedViewPane(ENIValidator eniValidator){
95  		this.eniValidator = eniValidator;
96  		addMouseListener(new TransformedViewMouseListener(eniValidator, this));
97  	}
98  
99  	/**
100 	 * 
101 	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
102 	 */
103 	protected void paintComponent(Graphics g) {
104 		super.paintComponent(g);
105 
106 		//Inicializamos los nodos
107 		xmlNodePositions = new ArrayList<XmlNodePosition>();
108 		if(xmlTree!=null){
109 			//Pintamos los nodos
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 	 * Pinta un nodo XML en la vista transformada
131 	 * @param xmlNode Nodo xml
132 	 * @param graphics2 g
133 	 * @param parentRectangle Rectangulo que representa el nodo padre
134 	 * @param acumulativeLeafDescendent
135 	 * @return Nodo pintado
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 		//Color
145 		int colorIndex = (xmlNode.getDeep() -1) % COLOR_LIST.size();
146 		Color lineColor = COLOR_LIST.get(colorIndex);
147 		
148 		
149 		graphics2.setColor(lineColor);
150 		//Marcamos los elementos que representan ficheros
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 		//Añadimos nodo xml y su posicion
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 	 * @return Devuelve el valor del campo xmlTree
179 	 */
180 	public XmlTree getXmlTree() {
181 		return xmlTree;
182 	}
183 
184 	/**
185 	 * @param Asigna al campo xmlTree el valor del parámetro xmlTree
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 		//Establece el tamaño de la vista transformada en función de los nodos del árbol
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 	 * @return Devuelve el valor del campo eniValidator
200 	 */
201 	public ENIValidator getEniValidator() {
202 		return eniValidator;
203 	}
204 
205 	/**
206 	 * @param Asigna al campo eniValidator el valor del parámetro eniValidator
207 	 */
208 	public void setEniValidator(ENIValidator eniValidator) {
209 		this.eniValidator = eniValidator;
210 	}
211 
212 	/**
213 	 * @return Devuelve el valor del campo xmlNodePositions
214 	 */
215 	public List<XmlNodePosition> getXmlNodePositions() {
216 		return xmlNodePositions;
217 	}
218 
219 	/**
220 	 * @param Asigna al campo xmlNodePositions el valor del parámetro xmlNodePositions
221 	 */
222 	public void setXmlNodePositions(List<XmlNodePosition> xmlNodePositions) {
223 		this.xmlNodePositions = xmlNodePositions;
224 	}
225 }