svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / decisiontrees / gui / DecisionTreePanel.java @ 17088
History | View | Annotate | Download (5.61 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Iba?ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
|
34 |
* Campus Universitario s/n
|
35 |
* 02071 Alabacete
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 967 599 200
|
39 |
*/
|
40 |
package org.gvsig.remotesensing.decisiontrees.gui; |
41 |
|
42 |
import java.awt.Color; |
43 |
import java.awt.Dimension; |
44 |
|
45 |
import javax.swing.BorderFactory; |
46 |
import javax.swing.JScrollPane; |
47 |
|
48 |
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel; |
49 |
import org.gvsig.remotesensing.decisiontrees.DecisionTreeNode; |
50 |
import org.gvsig.remotesensing.decisiontrees.gui.listener.GraphListener; |
51 |
import org.jgraph.JGraph; |
52 |
import org.jgraph.graph.DefaultEdge; |
53 |
import org.jgraph.graph.DefaultGraphCell; |
54 |
import org.jgraph.graph.DefaultGraphModel; |
55 |
import org.jgraph.graph.DefaultPort; |
56 |
import org.jgraph.graph.GraphConstants; |
57 |
import org.jgraph.graph.GraphModel; |
58 |
|
59 |
/**
|
60 |
* Panel para la herramienta de ?rboles de decisi?n.
|
61 |
*
|
62 |
* @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
|
63 |
*
|
64 |
*/
|
65 |
public class DecisionTreePanel extends DefaultButtonsPanel { |
66 |
|
67 |
/**
|
68 |
*
|
69 |
*/
|
70 |
private static final long serialVersionUID = 2800193252856199039L; |
71 |
|
72 |
private JGraph jGraph = null; |
73 |
private DecisionTreeDialog decisionTreeDialog = null; |
74 |
private JScrollPane scrollPane = null; |
75 |
private DecisionTreeNode root = null; |
76 |
|
77 |
|
78 |
public DecisionTreePanel(DecisionTreeDialog decisionTreeDialog) {
|
79 |
super();
|
80 |
this.decisionTreeDialog = decisionTreeDialog;
|
81 |
initialize(); |
82 |
} |
83 |
|
84 |
private void initialize() { |
85 |
initTree(); |
86 |
reloadGraph(); |
87 |
add(getScrollPane()); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* Crea el ?rbol de decisi?n inicial
|
92 |
*
|
93 |
*/
|
94 |
private void initTree() { |
95 |
root = new DecisionTreeNode();
|
96 |
root.addChildren(); |
97 |
root.setExpression("a<b");
|
98 |
root.getLeftChild().setClassID(0);
|
99 |
root.getRightChild().setClassID(1);
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* Reconstruye el gr?fico a partir del ?rbol (root).
|
104 |
*
|
105 |
*/
|
106 |
private void reloadGraph(){ |
107 |
insertCells(root,null);
|
108 |
} |
109 |
|
110 |
private void insertCells(DecisionTreeNode node, DefaultGraphCell parent) { |
111 |
DefaultGraphCell nodeVertex = createVertex(node, 20, 20, 40, 20, null, false); |
112 |
|
113 |
getJGraph().getGraphLayoutCache().insert(nodeVertex); |
114 |
if (parent!=null){ |
115 |
DefaultEdge edge = new DefaultEdge();
|
116 |
edge.setSource(parent); |
117 |
edge.setTarget(nodeVertex); |
118 |
GraphConstants.setLineEnd(edge.getAttributes(), GraphConstants.ARROW_CLASSIC); |
119 |
GraphConstants.setEndFill(edge.getAttributes(), true);
|
120 |
GraphConstants.setAutoSize(edge.getAttributes(), true);
|
121 |
getJGraph().getGraphLayoutCache().insert(edge); |
122 |
} |
123 |
|
124 |
if (node.getLeftChild()!=null){ |
125 |
insertCells(node.getLeftChild(), nodeVertex); |
126 |
} |
127 |
if (node.getRightChild()!=null){ |
128 |
insertCells(node.getRightChild(), nodeVertex); |
129 |
} |
130 |
} |
131 |
|
132 |
public DefaultGraphCell createVertex(Object name, double x, |
133 |
double y, double w, double h, Color bg, boolean raised) { |
134 |
|
135 |
// Create vertex with the given name
|
136 |
DefaultGraphCell cell = new DefaultGraphCell(name);
|
137 |
|
138 |
// Set bounds
|
139 |
// GraphConstants.setBounds(cell.getAttributes(),
|
140 |
// new Rectangle2D.Double(x, y, w, h));
|
141 |
|
142 |
// Set fill color
|
143 |
if (bg != null) { |
144 |
GraphConstants.setGradientColor( |
145 |
cell.getAttributes(), bg); |
146 |
GraphConstants.setOpaque( |
147 |
cell.getAttributes(), true);
|
148 |
} |
149 |
|
150 |
// Set raised border
|
151 |
if (raised)
|
152 |
GraphConstants.setBorder( |
153 |
cell.getAttributes(), |
154 |
BorderFactory.createRaisedBevelBorder());
|
155 |
else
|
156 |
// Set black border
|
157 |
GraphConstants.setBorderColor( |
158 |
cell.getAttributes(), Color.black);
|
159 |
|
160 |
// Add a Port
|
161 |
DefaultPort port = new DefaultPort();
|
162 |
cell.add(port); |
163 |
port.setParent(cell); |
164 |
|
165 |
GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createEtchedBorder());
|
166 |
GraphConstants.setAutoSize(cell.getAttributes(),true);
|
167 |
GraphConstants.setResize(cell.getAttributes(),true);
|
168 |
GraphConstants.setInset(cell.getAttributes(), 10);
|
169 |
|
170 |
return cell;
|
171 |
} |
172 |
|
173 |
public JScrollPane getScrollPane() { |
174 |
if (scrollPane == null){ |
175 |
scrollPane = new JScrollPane(getJGraph()); |
176 |
scrollPane.setPreferredSize(new Dimension(500,500)); |
177 |
} |
178 |
return scrollPane;
|
179 |
} |
180 |
|
181 |
public JGraph getJGraph() {
|
182 |
if (jGraph == null){ |
183 |
GraphModel model = new DefaultGraphModel();
|
184 |
jGraph = new JGraph(model);
|
185 |
|
186 |
jGraph.setCloneable(false);
|
187 |
jGraph.setInvokesStopCellEditing(true);
|
188 |
jGraph.setJumpToDefaultPort(true);
|
189 |
jGraph.setConnectable(false);
|
190 |
jGraph.setDisconnectable(false);
|
191 |
jGraph.setEditable(false);
|
192 |
jGraph.setEnabled(true);
|
193 |
|
194 |
jGraph.setSize(300, 300); |
195 |
|
196 |
GraphListener listener = new GraphListener();
|
197 |
jGraph.addMouseListener(listener); |
198 |
} |
199 |
return jGraph;
|
200 |
} |
201 |
} |