Revision 17088 trunk/extensions/extRemoteSensing/src/org/gvsig/remotesensing/decisiontrees/gui/DecisionTreePanel.java

View differences:

DecisionTreePanel.java
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
	 */
1 40
package org.gvsig.remotesensing.decisiontrees.gui;
2 41

  
3 42
import java.awt.Color;
......
2 41
import java.awt.Dimension;
3
import java.awt.event.MouseAdapter;
4
import java.awt.event.MouseEvent;
5
import java.awt.geom.Rectangle2D;
6 42

  
7 43
import javax.swing.BorderFactory;
8
import javax.swing.JOptionPane;
9 44
import javax.swing.JScrollPane;
10 45

  
11 46
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
47
import org.gvsig.remotesensing.decisiontrees.DecisionTreeNode;
48
import org.gvsig.remotesensing.decisiontrees.gui.listener.GraphListener;
12 49
import org.jgraph.JGraph;
......
19 56
import org.jgraph.graph.GraphConstants;
20 57
import org.jgraph.graph.GraphModel;
21 58

  
59
/**
60
 * Panel para la herramienta de ?rboles de decisi?n.
61
 * 
62
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
63
 *
64
 */
22 65
public class DecisionTreePanel extends DefaultButtonsPanel {
23 66

  
24 67
	/**
......
29 72
	private JGraph 			  		jGraph     	   		= null;
30 73
	private DecisionTreeDialog  	decisionTreeDialog 	= null;
31 74
	private JScrollPane				scrollPane			= null;
75
	private DecisionTreeNode 		root				= null;
32 76

  
33 77

  
34 78
	public DecisionTreePanel(DecisionTreeDialog decisionTreeDialog) {
......
38 82
	}
39 83
	
40 84
	private void initialize() {
41
		
85
		initTree();
86
		reloadGraph();
42 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);
43 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
		}
44 130
	}
45 131

  
46 132
	public DefaultGraphCell createVertex(Object name, double x,
......
50 136
			DefaultGraphCell cell = new DefaultGraphCell(name);
51 137

  
52 138
			// Set bounds
53
			GraphConstants.setBounds(cell.getAttributes(),
54
					new Rectangle2D.Double(x, y, w, h));
139
		//	GraphConstants.setBounds(cell.getAttributes(),
140
			//		new Rectangle2D.Double(x, y, w, h));
55 141

  
56 142
			// Set fill color
57 143
			if (bg != null) {
58 144
				GraphConstants.setGradientColor(
59
					cell.getAttributes(), Color.orange);
145
					cell.getAttributes(), bg);
60 146
				GraphConstants.setOpaque(
61 147
					cell.getAttributes(), true);
62 148
			}
......
75 161
			DefaultPort port = new DefaultPort();
76 162
			cell.add(port);
77 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);
78 169

  
79 170
			return cell;
80 171
		}
......
82 173
	public JScrollPane getScrollPane() {
83 174
		if (scrollPane == null){
84 175
			scrollPane = new JScrollPane(getJGraph());
176
			scrollPane.setPreferredSize(new Dimension(500,500));
85 177
		}
86
			
87 178
		return scrollPane;
88 179
	}
89 180

  
......
99 190
	        jGraph.setDisconnectable(false);
100 191
	        jGraph.setEditable(false);
101 192
	        jGraph.setEnabled(true);
102
	        jGraph.setMoveable(false);
193
	        
194
	        jGraph.setSize(300, 300);
103 195
			
104
			DefaultGraphCell[] cells = new DefaultGraphCell[3];
105

  
106
			// Create Hello Vertex
107
			cells[0] = createVertex("Adios", 20, 20, 40, 20, 
108
						null, false);
109

  
110
			// Create World Vertex
111
			cells[1] = createVertex("HOla", 140, 140, 40, 20, 
112
						Color.ORANGE, true);
113

  
114
			// Create Edge
115
			DefaultEdge edge = new DefaultEdge();
116
			// Fetch the ports from the new vertices, 
117
			// and connect them with the edge
118
			edge.setSource(cells[0].getChildAt(0));
119
			edge.setTarget(cells[1].getChildAt(0));
120
			cells[2] = edge;
121

  
122
			// Set Arrow Style for edge
123
			int arrow = GraphConstants.ARROW_CLASSIC;
124
			GraphConstants.setLineEnd(edge.getAttributes(), arrow);
125
			GraphConstants.setEndFill(edge.getAttributes(), true);
126

  
127
			// Insert the cells via the cache, so they get selected
128
			jGraph.getGraphLayoutCache().insert(cells);
129
			
130
			jGraph.addMouseListener(new MouseAdapter() {
131
	        	public void mousePressed(MouseEvent e) {
132
	        		if (e.getButton() ==  MouseEvent.BUTTON3){
133
	        			JOptionPane.showMessageDialog(null, "Esto es un pop up men?. ?Que no?", "popup Menu", JOptionPane.INFORMATION_MESSAGE);
134
	        		}
135
	        		if (e.getClickCount() == 2) {
136
	        			JOptionPane.showMessageDialog(null, "Ay Gal?n", "doble clicki", JOptionPane.INFORMATION_MESSAGE);
137
	        		}
138
	        	}
139
	        });
196
			GraphListener listener = new GraphListener();
197
			jGraph.addMouseListener(listener);
140 198
		}
141 199
		return jGraph;
142 200
	}

Also available in: Unified diff