Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / decisiontrees / gui / DecisionTreePanel.java @ 17773

History | View | Annotate | Download (12.4 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.Component;
44
import java.awt.Dimension;
45
import java.awt.geom.Rectangle2D;
46
import java.util.ArrayList;
47
import java.util.HashMap;
48
import java.util.Iterator;
49

    
50
import javax.swing.BorderFactory;
51
import javax.swing.JMenuItem;
52
import javax.swing.JPopupMenu;
53
import javax.swing.JScrollPane;
54
import javax.swing.JTabbedPane;
55

    
56
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
57
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
58
import org.gvsig.remotesensing.decisiontrees.DecisionTreeNode;
59
import org.gvsig.remotesensing.decisiontrees.gui.listener.DecisionTreePanelListener;
60
import org.gvsig.remotesensing.gui.beans.OptionsPanel;
61
import org.jgraph.JGraph;
62
import org.jgraph.graph.DefaultEdge;
63
import org.jgraph.graph.DefaultGraphCell;
64
import org.jgraph.graph.DefaultGraphModel;
65
import org.jgraph.graph.DefaultPort;
66
import org.jgraph.graph.GraphConstants;
67
import org.jgraph.graph.GraphModel;
68

    
69
import com.iver.andami.PluginServices;
70
import com.iver.cit.gvsig.project.documents.view.gui.View;
71

    
72
/**
73
 * Panel para la herramienta de ?rboles de decisi?n.
74
 * 
75
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
76
 *
77
 */
78
public class DecisionTreePanel extends DefaultButtonsPanel {
79

    
80
        /**
81
         * 
82
         */
83
        private static final long                 serialVersionUID         = 2800193252856199039L;
84
        private static final Color[]        colors  = new Color[] {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.MAGENTA, Color.CYAN,
85
                Color.ORANGE, Color.PINK, Color.WHITE, Color.BLACK};
86
        public  static final int                 POSITION_LEFT                 = 0;
87
        public  static final int                 POSITION_RIGHT                 = 1;
88
        
89
        private JGraph                                                   jGraph                                = null;
90
        private DecisionTreeDialog                  decisionTreeDialog         = null;
91
        private JScrollPane                                        scrollPane                        = null;
92
        private JPopupMenu                                        jPopupMenu                        = null;
93
        private JMenuItem                                         addMenuItem                        = null;
94
        private JMenuItem                                        deleteMenuItem                = null;
95
        private JMenuItem                                        executeMenuItem                = null;
96
        private DecisionTreeNode                         root                                = null;
97
        private DecisionTreePanelListener         listener                         = null;
98
        private DefaultGraphCell                         selectedCell                 = null;
99
        private View                                                view                                = null;
100
        private JTabbedPane                                        tabbedPanel                 = null; 
101
        private OptionsPanel                                outputOptionsPanel  = null;
102
        
103
        /**
104
         * Tabla de variables y sus asociaciones con las bandas(layerName[bandN]).
105
         */
106
        private HashMap                                                varTable                        = null;
107
        private HashMap                                                classColors                        = null;
108
        
109

    
110

    
111
        public DecisionTreePanel(DecisionTreeDialog decisionTreeDialog, View view) {
112
                super(ButtonsPanel.BUTTONS_APPLYCLOSE);
113
                this.view = view;
114
                this.decisionTreeDialog = decisionTreeDialog;
115
                initialize();
116
        }
117
        
118
        private void initialize() {
119
                initTree();
120
                reloadGraph();
121
//                add(getScrollPane());
122
                add(getTabbedPanel());
123
                listener = new DecisionTreePanelListener(this);
124
                getAddMenuItem().addActionListener(listener);
125
                getDeleteMenuItem().addActionListener(listener);
126
                getExecuteMenuItem().addActionListener(listener);
127
                getJGraph().addMouseListener(listener);
128
                this.addButtonPressedListener(listener);
129
        }
130

    
131
        private Component getTabbedPanel() {
132
                if (tabbedPanel==null){
133
                        tabbedPanel = new JTabbedPane();
134
                        tabbedPanel.addTab(PluginServices.getText(this, "arbol_decision"), getScrollPane());
135
                        tabbedPanel.addTab("opciones_salida", getOutputOptionsPanel());
136
                }
137
                return tabbedPanel;
138
        }
139

    
140
        /**
141
         * Crea el ?rbol de decisi?n inicial
142
         *
143
         */
144
        private void initTree() {
145
                root = new DecisionTreeNode();
146
                root.addChildren();
147
                root.setExpression("");
148
                root.getLeftChild().setClassID(generateClassId());
149
                root.getRightChild().setClassID(generateClassId());
150
        }
151
        
152
        public void initJGraph(){
153
                jGraph = null;
154
        }
155
        
156
        /**
157
         * Reconstruye el gr?fico a partir del ?rbol (root).
158
         *
159
         */
160
        public void reloadGraph(){
161
                /*
162
                 * Limpiar el gr?fico.
163
                 */
164
                getJGraph().getGraphLayoutCache().remove(getJGraph().getGraphLayoutCache().getCells(false, true, false, true),true,true);
165
                getJGraph().getGraphLayoutCache().reload();
166
                /*
167
                 * Construir el gr?fico a partir el ?rbol.
168
                 */
169
                insertCells(root,null,-1);
170
        }
171

    
172
        /**
173
         * Inserta recursivamente en el gr?fico los nodos y arcos el ?rbol que radica en <code>node</node>
174
         * en el gr?fico.
175
         * 
176
         * @param node nodo raiz del sub?rbol.
177
         * @param parent nodo antecesor del sub?rbol a insertar
178
         * @param position indica si <code>node</node> es el hijo izquierdo (0) o derecho (1) de <code>parent</code>
179
         */
180
        public void insertCells(DecisionTreeNode node, DefaultGraphCell parent, int position) {
181
                int x = 0;
182
                int y = 0;
183
                int w = 40;
184
                int h = 40;
185
                if(parent==null){
186
                        y = 10;
187
                        x = getScrollPane().getPreferredSize().width/2 - 20;
188
                }
189
                else{
190
                        int leftLevels = ((DecisionTreeNode)parent.getUserObject()).getLeftChild().getLevelsCount();
191
                        int rightLevels = ((DecisionTreeNode)parent.getUserObject()).getRightChild().getLevelsCount();
192
                        int minLevels = Math.min(leftLevels, rightLevels);
193
                        y = ((int)GraphConstants.getBounds(parent.getAttributes()).getMaxY())+50;
194
                        if(position==POSITION_LEFT)
195
                                x = ((int)GraphConstants.getBounds(parent.getAttributes()).getMinX())-w*minLevels;
196
                        else
197
                                x = ((int)GraphConstants.getBounds(parent.getAttributes()).getMinX())+(w)*minLevels;
198
                }
199
                DefaultGraphCell nodeVertex = createVertex(node, x, y, w, h, null, false);
200
                
201
                /*
202
                 * Si es un nodo final:
203
                 */
204
                if (node.isFinal()){
205
                        Color color = (Color)getClassColors().get(new Integer(node.getClassID()));
206
                        GraphConstants.setBackground(nodeVertex.getAttributes(), color);
207
                        GraphConstants.setOpaque(nodeVertex.getAttributes(), true);
208
                }
209
                
210
                getJGraph().getGraphLayoutCache().insert(nodeVertex);
211
                if (parent!=null){
212
                        String label = PluginServices.getText(this, "si");
213
                        if(position==POSITION_LEFT)
214
                                label = PluginServices.getText(this, "no");
215
                        DefaultEdge edge = createEdge(nodeVertex, parent, label);
216
                        getJGraph().getGraphLayoutCache().insert(edge);
217
                }
218
                        
219
                if (node.getLeftChild()!=null){
220
                        insertCells(node.getLeftChild(), nodeVertex, POSITION_LEFT);
221
                }
222
                if (node.getRightChild()!=null){
223
                        insertCells(node.getRightChild(), nodeVertex, POSITION_RIGHT);
224
                }
225
        }
226

    
227
        private DefaultGraphCell createVertex(Object name, double x,
228
                        double y, double w, double h, Color bg, boolean raised) {
229

    
230
                        // Create vertex with the given name
231
                        DefaultGraphCell cell = new DefaultGraphCell(name);
232

    
233
                        // Set bounds
234
                        GraphConstants.setBounds(cell.getAttributes(),
235
                                        new Rectangle2D.Double(x, y, w, h));
236

    
237
                        // Set fill color
238
                        if (bg != null) {
239
                                GraphConstants.setGradientColor(
240
                                        cell.getAttributes(), bg);
241
                                GraphConstants.setOpaque(
242
                                        cell.getAttributes(), true);
243
                        }
244

    
245
                        // Set raised border
246
                        if (raised)
247
                                GraphConstants.setBorder(
248
                                        cell.getAttributes(), 
249
                                        BorderFactory.createRaisedBevelBorder());
250
                        else
251
                                // Set black border
252
                                GraphConstants.setBorderColor(
253
                                        cell.getAttributes(), Color.black);
254

    
255
                        // Add a Port
256
                        DefaultPort port = new DefaultPort();
257
                        cell.add(port);
258
                        port.setParent(cell);
259
                        
260
                        GraphConstants.setBorder(cell.getAttributes(), BorderFactory.createEtchedBorder());
261
                    GraphConstants.setAutoSize(cell.getAttributes(),false);
262
                    GraphConstants.setResize(cell.getAttributes(),false);
263
                    GraphConstants.setInset(cell.getAttributes(), 10);
264
                    GraphConstants.setMoveable(cell.getAttributes(), false);
265

    
266
                        return cell;
267
                }
268
        
269
        private DefaultEdge createEdge(DefaultGraphCell cell, DefaultGraphCell parentCell, String label){
270
                DefaultEdge edge = new DefaultEdge(label);
271
                edge.setSource(parentCell);
272
                edge.setTarget(cell);
273
                GraphConstants.setLineEnd(edge.getAttributes(), GraphConstants.ARROW_CLASSIC);
274
                GraphConstants.setEndFill(edge.getAttributes(), true);
275
                GraphConstants.setAutoSize(edge.getAttributes(), true);
276
                getJGraph().getGraphLayoutCache().insert(edge);
277
                return edge;
278
        }
279

    
280
        public JScrollPane getScrollPane() {
281
                if (scrollPane == null){
282
                        scrollPane = new JScrollPane(getJGraph());
283
                        scrollPane.setPreferredSize(new Dimension(500,400));
284
                }
285
                return scrollPane;
286
        }
287

    
288
        public JGraph getJGraph() {
289
                if (jGraph == null){
290
                        GraphModel model = new DefaultGraphModel();
291
                        jGraph = new JGraph(model);
292
                        
293
                        jGraph.setCloneable(false);
294
                jGraph.setInvokesStopCellEditing(true);
295
                jGraph.setJumpToDefaultPort(true);
296
                jGraph.setConnectable(false);
297
                jGraph.setDisconnectable(false);
298
                jGraph.setEditable(false);
299
                jGraph.setEnabled(true);
300
                }
301
                return jGraph;
302
        }
303

    
304
        public JPopupMenu getJPopupMenu() {
305
                if (jPopupMenu==null){
306
                        jPopupMenu = new JPopupMenu("");
307
                        jPopupMenu.add(getAddMenuItem());
308
                        jPopupMenu.add(getDeleteMenuItem());
309
                        jPopupMenu.add(getExecuteMenuItem());
310
                }
311
                return jPopupMenu;
312
        }
313

    
314
        public JMenuItem getAddMenuItem() {
315
                if (addMenuItem==null){
316
                        addMenuItem = new JMenuItem(PluginServices.getText(this, "addChild"));
317
                }
318
                return addMenuItem;
319
        }
320

    
321
        public JMenuItem getDeleteMenuItem() {
322
                if (deleteMenuItem==null){
323
                        deleteMenuItem = new JMenuItem(PluginServices.getText(this, "deleteChild"));
324
                }
325
                return deleteMenuItem;
326
        }
327

    
328
        public JMenuItem getExecuteMenuItem() {
329
                if (executeMenuItem == null)
330
                        executeMenuItem = new JMenuItem(PluginServices.getText(this, "ejecutar"));
331
                return executeMenuItem;
332
        }
333
        
334
        public void setSelectedCell(DefaultGraphCell cell) {
335
                selectedCell = cell;
336
        }
337

    
338
        public DefaultGraphCell getSelectedCell() {
339
                return selectedCell;
340
        }
341
        
342

    
343
        public View getView() {
344
                return view;
345
        }
346

    
347
        public DecisionTreeDialog getDecisionTreeDialog() {
348
                return decisionTreeDialog;
349
        }
350

    
351
        public HashMap getVarTable() {
352
                if (varTable == null)
353
                        varTable = new HashMap();
354
                return varTable;
355
        }
356

    
357
        public void setVarTable(HashMap varTable) {
358
                this.varTable = varTable;
359
        }
360

    
361
        public OptionsPanel getOutputOptionsPanel() {
362
                if (outputOptionsPanel == null){
363
                        outputOptionsPanel = new OptionsPanel(view);
364
                }
365
                return outputOptionsPanel;
366
        }
367

    
368
        /**
369
         * 
370
         * @return Nodo raiz del ?rbol de decisi?n.
371
         */
372
        public DecisionTreeNode getRoot() {
373
                return root;
374
        }
375
        
376
        /**
377
         * Devuelve un color para asignar a un nuevo nodo final
378
         * 
379
         * @return Color
380
         */
381
        private Color generateColor(){
382
                return null;
383
        }
384
        
385
        /**
386
         * Devuelve un ID para asignar a un nuevo nodo final
387
         * 
388
         * @return int
389
         */
390
        public int generateClassId(){
391
                Color color = null;
392
                int classColorsCount = getClassColors().size();
393
                for (int i=0; i<classColorsCount;i++)
394
                        if(!getClassColors().containsKey(new Integer(i))){
395
                                if (i<colors.length)
396
                                        color = colors[i];
397
                                else
398
                                        color = new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
399
                                getClassColors().put(new Integer(i), color);
400
                                return i;
401
                        }
402
                if (classColorsCount<colors.length)
403
                        color = colors[classColorsCount];
404
                else
405
                        color = new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
406
                getClassColors().put(new Integer(classColorsCount), color);
407
                return classColorsCount;
408
        }
409

    
410
        public HashMap getClassColors() {
411
                if (classColors == null)
412
                        classColors = new HashMap();
413
                return classColors;
414
        }
415

    
416
        /**
417
         * Elimina las entradas de <code>classColors</code> que se est?n
418
         * usando en el ?rbol.
419
         *
420
         */
421
        public void cleanClassColors() {
422
                ArrayList ids = new ArrayList();
423
                root.getFinals(ids);
424
                Object keys[] = getClassColors().keySet().toArray();
425
                for (int i=0; i < keys.length; i++){
426
                        if (!ids.contains(keys[i]))
427
                                        getClassColors().remove(keys[i]);
428
                }
429
        }
430

    
431
}