Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / gridmath / gui / CalculatorPanel.java @ 16088

History | View | Annotate | Download (24.2 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 Ib??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

    
41
package org.gvsig.remotesensing.gridmath.gui;
42

    
43
import info.clearthought.layout.TableLayout;
44

    
45
import java.awt.BorderLayout;
46
import java.awt.GridBagConstraints;
47
import java.awt.GridBagLayout;
48
import java.awt.Insets;
49
import java.awt.event.FocusEvent;
50
import java.awt.event.FocusListener;
51
import java.awt.event.MouseAdapter;
52
import java.awt.event.MouseEvent;
53
import java.awt.event.MouseListener;
54
import java.util.HashMap;
55
import java.util.Iterator;
56
import java.util.Set;
57

    
58
import javax.swing.BorderFactory;
59
import javax.swing.JButton;
60
import javax.swing.JCheckBox;
61
import javax.swing.JPanel;
62
import javax.swing.JScrollPane;
63
import javax.swing.JTabbedPane;
64
import javax.swing.JTextArea;
65
import javax.swing.JTree;
66
import javax.swing.border.BevelBorder;
67
import javax.swing.border.CompoundBorder;
68
import javax.swing.border.EmptyBorder;
69
import javax.swing.border.TitledBorder;
70
import javax.swing.tree.DefaultMutableTreeNode;
71
import javax.swing.tree.DefaultTreeModel;
72
import javax.swing.tree.TreePath;
73

    
74
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
75
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
76
import org.gvsig.raster.grid.GridExtent;
77
import org.gvsig.remotesensing.gridmath.GridMathProcess;
78
import org.gvsig.remotesensing.gridmath.gui.listener.CalculatorPanelListener;
79
import org.gvsig.remotesensing.gridmath.gui.listener.GridMathListener;
80
import org.nfunk.jep.JEP;
81

    
82
import com.iver.andami.PluginServices;
83
import com.iver.andami.ui.mdiManager.IWindow;
84
import com.iver.cit.gvsig.fmap.MapContext;
85
import com.iver.cit.gvsig.fmap.layers.FLayers;
86
import com.iver.cit.gvsig.project.documents.view.gui.View;
87

    
88
/**
89
 * Clase que implenta la interfaz de la calculadora de mapas.
90
 * 
91
 * @author Alejandro Mu?oz Sanchez        (alejandro.munoz@uclm.es)
92
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
93
 * @version 19/10/2007 
94
 */
95

    
96
public class CalculatorPanel extends DefaultButtonsPanel implements FocusListener{
97

    
98
        private static final long serialVersionUID = 1L;
99
        
100
        //-------JBUTTON---------//
101
        private JButton salvar;
102
        private JButton recuperar;
103
        
104
        //-------PANEL---------//
105

    
106
        private JPanel centerPanel;
107
        private JPanel expresionPanel;
108
        OutputOptionsPanel optionsPanel = null;
109
        
110
        //-------JTEXTAREA---------//
111
        private JTextArea jTextExpresion;
112
        
113
        //-------JSCROLLPANE---------//
114
        private JScrollPane scrollExpresion;
115
        private JScrollPane scrollTree;
116
        private JScrollPane scrollVariables;
117
        
118
        //--------JTREE--------------//
119
        private JTree jtree; 
120
        
121
        // ------ JCheckBox----------//        
122
        private JCheckBox jCheckExtent;
123
        
124
        
125
        //----------------------------------------//
126
        private CalculatorDialog calculatorDialog = null;
127
        private KeysPanel kp;
128
        private TableFormat jTableVariables;
129
        private MapContext m_MapContext = null;
130
        private View view = null;
131
        private HashMap m_Constants;
132
        private HashMap qWindowsHash = null;
133
        private GridExtent outputExtent;
134
        GridMathProcess gridMath = null;
135
        
136
        private final String nombreBandas="Band";
137
        private JEP parser = null;
138
        private CalculatorPanelListener listener = null;
139
                 
140
        /**
141
         * @param calculatorDialog
142
         * @param view        vista de la aplicacion 
143
         */
144
        public CalculatorPanel(CalculatorDialog calculatorDialog, View view) {
145
                super();
146
                this.view = view;
147
                this.calculatorDialog = calculatorDialog;
148
                if (view != null)
149
                        m_MapContext = view.getModel().getMapContext();
150
                
151
                listener = new CalculatorPanelListener(this);
152
                Inicializar();
153
        }
154
        
155
        /**
156
         * Inicializar los elementos del Panel CalculatorPanel
157
         */
158
        private void Inicializar(){
159
                
160
                JPanel principalPanel = new JPanel();
161
                BorderLayout bd=new BorderLayout();
162
                principalPanel.setLayout(bd);
163
                kp=new KeysPanel(getJTextExpression()); 
164
                //agregar el panel central
165
                principalPanel.add(getCenterPanel(), BorderLayout.CENTER);
166
                //this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
167
                principalPanel.setBorder( new EmptyBorder(3, 3, 3, 3));
168
                
169
                MouseListener m = new MouseAdapter() {
170
                       public void mousePressed(MouseEvent e) {
171
                              jTableVariables.setEnEspera(false);        
172
                               }
173
                      
174
                  };
175
                 kp.getJButton0().addMouseListener(m);
176
                 kp.getJButton1().addMouseListener(m);
177
                 kp.getJButton2().addMouseListener(m);
178
                 kp.getJButton3().addMouseListener(m);
179
                 kp.getJButton4().addMouseListener(m);
180
                 kp.getJButton5().addMouseListener(m);
181
                 kp.getJButton6().addMouseListener(m);
182
                 kp.getJButton7().addMouseListener(m);
183
                 kp.getJButton8().addMouseListener(m);
184
                 kp.getJButton9().addMouseListener(m);
185
                 kp.getJButtonMinus().addMouseListener(m);
186
                 kp.getJButtonPlus().addMouseListener(m);
187
                 kp.getJButtonBrackets().addMouseListener(m);
188
                 kp.getJButtonDivide().addMouseListener(m);
189
                 kp.getJButtonMultiply().addMouseListener(m);
190
                 kp.getJButtonDot().addMouseListener(m);
191
                 
192
                 JTabbedPane tabbedPane = new JTabbedPane();
193
                 tabbedPane.addTab(PluginServices.getText(this, "operacion"), principalPanel);
194
                tabbedPane.addTab(PluginServices.getText(this, "opciones"), getOptionsPanel());
195
                this.setLayout(new BorderLayout());
196
                this.add(tabbedPane, BorderLayout.CENTER);
197
         
198
                 this.addButtonPressedListener(listener);
199
        }
200
        
201
        
202
        /**
203
         * Define los elementos del panel central: JTree, Tabla de Variables y calculadora
204
         * @return  panel central
205
         */
206
        public JPanel getCenterPanel() {
207
                
208
                if (centerPanel==null){
209
                        centerPanel=new JPanel();
210
                        centerPanel.setLayout(new GridBagLayout());
211
                        GridBagConstraints c = new GridBagConstraints();
212
                        c.fill = GridBagConstraints.BOTH;
213
                        
214
                        c.insets=new Insets(2,2,0,2);
215
                        c.weightx = 0.75;
216
                        c.gridx = 0;
217
                        c.gridy = 0;
218
                        // Agregar jtree
219
                        getScrollTree().setSize(50,70);
220
                        centerPanel.add(getScrollTree(), c);
221
                
222
                        c.insets=new Insets(2,2,0,2);
223
                        c.weightx = 1.0;
224
                        c.gridx = 1;
225
                        c.gridy = 0;
226
                        getScrollVariables().setSize(50,70);
227
                        getJTableVariables().getTableFormat().getColumn(PluginServices.getText(this, "valor")).setPreferredWidth(180);
228
                        // Agregar la tabla de variables
229
                        centerPanel.add(getScrollVariables(), c);
230

    
231
                        c.weightx = 0.0;
232
                        c.gridx = 2;
233
                        c.gridy = 0;
234
                        //Agregar el KeyPanel
235
                        centerPanel.add(kp, c);
236
                
237
                        c.insets=new Insets(2,2,0,2);
238
                        c.weightx = 1.0;
239
                        c.gridwidth=2;
240
                        c.gridx = 0;
241
                        c.gridy = 1;
242
                        //Agregar check  habilitar distinta extension
243
                        centerPanel.add(getJCheckExtent(), c);
244
                        
245
                        c.fill = GridBagConstraints.BOTH;
246
                        c.insets=new Insets(2,2,0,2);
247
                        c.weightx = 1.0;
248
                        c.gridwidth=3;
249
                        c.gridx = 0;
250
                        c.gridy = 2;
251
                        //Agergar el panel que contiene la expresion
252
                        centerPanel.add(getExpresionPanel(), c);
253
                }                
254
                
255
                return centerPanel;
256
        }
257

    
258
        /**
259
         * Crea un panel en el que se agrega el area de texto donde se define la expresi?n.
260
         * @return panel con el area de texto donde se define la expresi?n
261
         */
262
        public JPanel getExpresionPanel() {
263
                
264
                if (expresionPanel==null){
265
                        expresionPanel=new JPanel();                                
266
                }
267
                expresionPanel.setLayout(new BorderLayout());
268
                expresionPanel.add(getScrollExpresion(),BorderLayout.CENTER);
269
                //Este panel contine los botones de salvar y recuperar
270
                JPanel p=new JPanel();
271
                TableLayout thisLayout = new TableLayout(new double[][] {
272
                                 {5.0,TableLayout.PREFERRED},         
273
                                {TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}});
274
                               
275
                                //Establece la separaci?n entre los elementos
276
                                thisLayout.setHGap(3);
277
                                thisLayout.setVGap(3);
278
                                
279
                p.setLayout(thisLayout);        
280
                TitledBorder topBorder = BorderFactory.createTitledBorder("Expresi?n");
281
            topBorder.setTitlePosition(TitledBorder.TOP);
282
            //creamos un borde compuesto
283
                expresionPanel.setBorder(new CompoundBorder(topBorder,new EmptyBorder(0,0,0,0)));
284
                p.add(getSalvar(),"1,0");
285
                p.add(getRecuperar(),"1,1");
286
                
287
                expresionPanel.add(p,BorderLayout.EAST);
288
                return expresionPanel;
289
        }
290

    
291
        
292
        /**
293
         * @return checkbox permitir distinto extend
294
         */
295
        public JCheckBox getJCheckExtent() {
296
                
297
                if (jCheckExtent==null){
298
                        jCheckExtent=new JCheckBox( PluginServices.getText(this, "distinto_extent"),false);
299
                        jCheckExtent.addActionListener(listener);
300
                }
301
                
302
                return jCheckExtent;
303
        }
304
        
305
        
306
        /**
307
         * @return arbol con capas, funciones, operadores y constantes
308
         */
309
        public JTree getJtree() {
310
                
311
                if (jtree==null){
312
                        jtree=new JTree();
313
                        jtree.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
314
                        // Rellenar el arbol con los items correspondientes
315
                        if (m_MapContext!=null)
316
                                this.InicizlizaJTree(jtree);
317
                }
318
                 return jtree;
319
        }
320

    
321
        
322
        /**
323
         * @return area de texto donde se intoduce la expresi?n
324
         */
325
        public JTextArea getJTextExpression() {
326
                        
327
                if (jTextExpresion==null){
328
                        jTextExpresion=new JTextArea();                                
329
                                jTextExpresion.setLineWrap(true);
330
                                jTextExpresion.setWrapStyleWord(true);
331
                                jTextExpresion.addFocusListener(this);
332
                                jTextExpresion.addKeyListener(new GridMathListener(this));
333
                        }
334
        
335
                        return jTextExpresion;
336
                }
337
        
338
        
339
        /**
340
         *  @return formato de la tabla
341
         */
342
        public TableFormat getJTableVariables() {        
343
                if (jTableVariables==null){
344
                        jTableVariables=new TableFormat(jTextExpresion);
345
                        jTableVariables.getTableFormat().getModel().addTableModelListener(listener);
346
                }
347
                return jTableVariables;
348
        }
349
                
350
        
351
        /**
352
        * @return  scroll con el arbol
353
        */
354
        public JScrollPane getScrollTree() {
355
                if (scrollTree==null){
356
                                scrollTree=new JScrollPane();
357
                                scrollTree.setViewportView(getJtree());
358
                                scrollTree.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
359
                }
360
                return scrollTree;
361
        }
362
        
363
        
364
        /**
365
        *  @return scroll con el area de expresi?n
366
        */
367
        public JScrollPane getScrollExpresion() {
368
                if (scrollExpresion==null){
369
                        scrollExpresion=new JScrollPane();
370
                        scrollExpresion.setViewportView(getJTextExpression());
371
                        scrollExpresion.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
372
                }
373
                return scrollExpresion;
374
        }
375
                
376
        /**
377
         * @return  scroll con la tabla de variables
378
         */        
379
        public JScrollPane getScrollVariables() {
380
                if (scrollVariables==null){
381
                        scrollVariables=new JScrollPane();                                
382
                }
383
                scrollVariables.setViewportView(getJTableVariables().getTableFormat());
384
                scrollVariables.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
385
                return scrollVariables;
386
        }
387
        
388
        /**
389
         * @return  button cargar expresi?n
390
         */        
391
        public JButton getRecuperar() {
392
                if(recuperar==null){
393
                        recuperar=new JButton();
394
                        recuperar.setText(PluginServices.getText(this,"cargar"));
395
                        recuperar.addActionListener(listener);
396
                }
397
                return recuperar;
398
                }
399

    
400
        /**
401
         * @return  button salvar expresi?n
402
         */        
403
        public JButton getSalvar() {
404
                if(salvar==null){
405
                        salvar=new JButton();
406
                        salvar.setText(PluginServices.getText(this,"salvar"));
407
                        salvar.addActionListener(listener);                
408
                }
409
                return salvar;
410
        }
411
        
412
        
413
        /**
414
         * Metodo que rellena el Jtree con las las capas cargadas en la vista y sus
415
         * bandas correspondientes. Ademas con las funciones, operadores y constantes 
416
         * estandar.
417
         */
418
        void InicizlizaJTree(JTree jTree){
419
                int i;
420
            double dCellsize;
421
            String sName;
422
            //Eliminar el contenido del ?rbol con el que se inicializa por defecto
423
            jTree.setModel(null);
424
            FLayers layers = m_MapContext.getLayers();          
425
            //Nodo principal
426
            DefaultMutableTreeNode main = new DefaultMutableTreeNode(PluginServices.getText(this,"elementos_jtree"));
427
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(PluginServices.getText(this,"capas_jtree"));
428
            DefaultMutableTreeNode child;
429
            DefaultMutableTreeNode bandas;
430
            for (i = 0; i < layers.getLayersCount(); i++){
431
                    //obtiene las capas raster de la vista actual
432
                    if (layers.getLayer(i) instanceof FLyrRasterSE){
433
                            //Nombre por defecto de las bandas
434
                            child = new DefaultMutableTreeNode(layers.getLayer(i).getName());
435
                                //Obtener las todas las bandas de cada capa raster de la vista
436
                                // tomamos el nombre de cada una de las bandas
437
                                for (int j=0; j<((FLyrRasterSE) layers.getLayer(i)).getBandCount(); j++){
438
                                        String s=nombreBandas+(j+1);
439
                                        bandas= new DefaultMutableTreeNode(s);
440
                                        child.add(bandas);
441
                                }
442
                                node.add(child);
443
                    }
444
            }
445
            main.add(node);
446
            
447
            //Funciones
448
            String sFunctions[] ={ "sin","cos","tan","asin", "acos","atan","atan2","sinh",
449
                                                            "cosh","tanh","asinh", "acosh","atanh","ln","log","exp",
450
                                                            "abs","rand","mod","sqrt","if" };
451
            node = new DefaultMutableTreeNode(PluginServices.getText(this,"funciones_jtree"));
452
            for (i = 0; i < sFunctions.length; i++){
453
                    //agregar los elementos al subarbol
454
                    child = new DefaultMutableTreeNode(" " + sFunctions[i] + "() ");
455
                        node.add(child);
456
            }
457
            main.add(node);
458
            //Operadores
459
            String sOperators[] = {"+","-","*","/","%","!","^","&&","||","<",">","<=",">=", "==","!="};
460
            node = new DefaultMutableTreeNode(PluginServices.getText(this,"operadores_jtree"));
461
            for (i = 0; i < sOperators.length; i++){
462
                    child = new DefaultMutableTreeNode(" " + sOperators[i] + " ");
463
                        node.add(child);
464
            }
465
            main.add(node);
466
            //Constantes
467
            node = new DefaultMutableTreeNode(PluginServices.getText(this,"constantes_jtree"));
468
            m_Constants = new HashMap();
469
            m_Constants.put("e", " " + Double.toString(Math.E) + " ");
470
            m_Constants.put("Pi", " " + Double.toString(Math.PI)+ " ");
471
            for (i = 0; i < layers.getLayersCount(); i++){
472
                    if (layers.getLayer(i) instanceof FLyrRasterSE){
473
                            sName = "Tama?o celda [" + layers.getLayer(i).getName() + "]";
474
                            dCellsize =((FLyrRasterSE)layers.getLayer(i)).getBufferFactory().getXCellSize();
475
                            m_Constants.put(sName, " " + Double.toString(dCellsize) + " ");
476
                    }
477
            }
478
            Set set= m_Constants.keySet();
479
            Iterator iter = set.iterator();
480
                while(iter.hasNext()){                    
481
                        child = new DefaultMutableTreeNode((String)iter.next());
482
                        node.add(child);
483
                }
484
                main.add(node);
485
            //asociar el conenido del arbol al jTree           
486
            jTree.setModel(new DefaultTreeModel(main));  
487
            //GENERAR LOS EVENTOS DEL RAT?N CORRESPONDIENTES
488
            MouseListener ml = new MouseAdapter() {
489
                public void mousePressed(MouseEvent e) {
490
                       int iRow=jtree.getRowForLocation(e.getX(),e.getY());
491
                       TreePath path = jtree.getPathForLocation(e.getX(), e.getY());
492
                        if( iRow != -1 && e.getClickCount() == 2) {
493
                                //al hacer doble click sobre un elemento del arbol
494
                                insertTextFromTree(path);
495
                        }
496
                }
497
           };
498
           jTree.addMouseListener(ml);
499
     
500
        }
501
        
502
        /**
503
         * Agrega el texto del ?rbol sobre el que se ha hecho doble click en el cuadro de la expresion
504
         */
505
   private void insertTextFromTree(TreePath path){
506
                    String  var;
507
                        boolean yaAsignada=false;
508
                    //Devuelve la ruta de acceso primaria de la ruta de acceso proporcionada.
509
                    TreePath parent = path.getParentPath();
510
                    if (parent != null && !parent.toString().equals("["+ PluginServices.getText(this,"elementos_jtree")+"]") && !parent.toString().equals("["+PluginServices.getText(this,"elementos_jtree")+", "+ PluginServices.getText(this,"capas_jtree")+"]")){
511
                            String sParentName = parent.toString();
512
                            //String s = path.getLastPathComponent().toString();;
513
                            String s = null;
514
                            if (parent.getParentPath().toString().equals("["+PluginServices.getText(this,"elementos_jtree")+", "+ PluginServices.getText(this,"capas_jtree")+"]")){
515
                                    s =path.getParentPath().getLastPathComponent().toString()+"["+ path.getLastPathComponent().toString()+"]";
516
                                    if(jTableVariables.isEnEspera()){                                            
517
                                            // Asigna la variable al nuevo valor
518
                                            int row= jTableVariables.getFilaSeleccionada();
519
                                            
520
                                            // Tratamiento de la asignacion incluyendo posibilidad de distinto extent
521
                                            
522
                                            // Tomo la primera que haya asignada
523
                                            if(!getJCheckExtent().isSelected()){
524
                                            
525
                                                            String layerBand,layerName;
526
                                                            FLyrRasterSE rasterLayer1, rasterLayer2;
527
                                                            int filaAsignada=0;
528
                                                    
529
                                                            for (int i=0; i< getJTableVariables().getTableFormat().getRowCount(); i++)
530
                                                                    {
531
                                                            
532
                                                                            layerBand= getJTableVariables().getTableFormat().getValueAt(i,1).toString();
533
                                                                 
534
                                                                            if (!layerBand.equals("")){
535
                                                                                    // primera variable asignada
536
                                                                                    filaAsignada=i;
537
                                                                                    break;
538
                                                                 }
539
                                                             
540
                                                            // si no hay ninguna asignada
541
                                                             if (i==getJTableVariables().getTableFormat().getRowCount()-1){
542
                                                                 
543
                                                                 //insertar normalmente
544
                                                                 jTableVariables.actualizarVariable(row,s);
545
                                                            jTableVariables.setEnEspera(false);
546
                                                            yaAsignada=true;
547
                                                            s="";        
548
                                                            }        
549
                                                                            
550
                                                    }
551
                                                    
552
                                                    if (yaAsignada==false){
553
                                                    
554
                                                            // Se comparan
555
                                                            layerBand= getJTableVariables().getTableFormat().getValueAt(filaAsignada,1).toString();
556
                                                            layerName = layerBand.substring(0,layerBand.indexOf("["));
557
                                                            rasterLayer1 = (FLyrRasterSE)m_MapContext.getLayers().getLayer(layerName);
558
                                                 
559
                                                            // Variable pasan como argumento
560
                                                            layerName = s.substring(0,s.indexOf("["));
561
                                                            rasterLayer2 = (FLyrRasterSE)m_MapContext.getLayers().getLayer(layerName);
562
                                                         
563
                                                            if ((rasterLayer1.getBufferFactory().getExtent().height() == rasterLayer2.getBufferFactory().getExtent().height()) && (rasterLayer1.getBufferFactory().getExtent().width() == rasterLayer2.getBufferFactory().getExtent().width())){// iguales){
564
                                                         
565
                                                                    jTableVariables.actualizarVariable(row,s);
566
                                                                    jTableVariables.setEnEspera(false);
567
                                                                    s="";        
568
                                                         
569
                                                            } else  s="";
570
                                                    }
571
                                            }
572
                                            //         Permitiendo  tama?os diferentes
573
                                            else{
574
                                                            jTableVariables.actualizarVariable(row,s);
575
                                                            jTableVariables.setEnEspera(false);
576
                                                       s="";        
577
                                            }                 
578
                                    }else{                                    
579
                                            //        var: almacena el nombre de la variable asociada al valor
580
                                               var=consultarVariable(s);
581
                                            jTextExpresion.insert(var,jTextExpresion.getCaretPosition());
582
                                            s="";
583
                                            }
584
                            }
585
                            else
586
                                    s = path.getLastPathComponent().toString();    
587
                           if (sParentName.equals("["+PluginServices.getText(this,"elementos_jtree")+", "+ PluginServices.getText(this,"constantes_jtree")+"]")){
588
                             if (m_Constants.containsKey(s)){
589
                                     s = (String) m_Constants.get(s);
590
                             }else{
591
                                     s = "";
592
                             }
593
                            }        
594
                            jTextExpresion.insert(s,jTextExpresion.getCaretPosition());
595
                            if (sParentName.equals("["+PluginServices.getText(this,"elementos_jtree")+", "+ PluginServices.getText(this,"funciones_jtree")+"]")){
596
                                    jTextExpresion.setCaretPosition(jTextExpresion.getCaretPosition() - 2);
597
                            }
598
                    }
599
         }
600
   
601
           /**
602
          * Agregar la variable y el valor a la tabla, haciendo la comprobacion del extent
603
          * @param valor
604
          * @return la variable asociada al valor-->capa[banda]
605
          */
606
         private String insertVariableToJTable(String valor){
607
                 
608
                
609
                String layerBand,layerName;
610
                FLyrRasterSE rasterLayer1, rasterLayer2;
611
                String s;
612
                int filaAsignada = 0; 
613
                boolean yaAsignada=false;
614
                
615
                /* Antes de insertar la variable hay que comprobar si no esta marcada la casilla que no hay diferente extent
616
                caso no permitir distinto extent*/
617
                        
618
                if (getJCheckExtent().isSelected()==false){                 
619
                                 if (getJTableVariables().getTableFormat().getRowCount()>0){
620
                                                         for (int i=0; i< getJTableVariables().getTableFormat().getRowCount(); i++)
621
                                                         {
622
                                                                 layerBand= getJTableVariables().getTableFormat().getValueAt(i,1).toString();
623
                                                                 if (!layerBand.equals("")){
624
                                                                         // primera variable asignada
625
                                                                         filaAsignada=i;
626
                                                                         break;
627
                                                                 }
628
                                                                 // si no hay ninguna asignada
629
                                                                 if (i==getJTableVariables().getTableFormat().getRowCount()-1){
630
                                                                          s=jTableVariables.InsertRow(null,valor);        
631
                                                                           getQWindowsHash().put(s,null);
632
                                                                           return s;
633
                                                                 }        
634
                                                         } // fin for
635
                                                 
636
                                                         // Si la tabla no estaba vacia y habia algun elemento asignado
637
                                                         if (yaAsignada==false){
638
                                                                 // Primera variable de la tabla
639
                                                                 layerBand= getJTableVariables().getTableFormat().getValueAt(filaAsignada,1).toString();
640
                                                                 layerName = layerBand.substring(0,layerBand.indexOf("["));
641
                                                                 rasterLayer1 = (FLyrRasterSE)m_MapContext.getLayers().getLayer(layerName);
642
                                 
643
                                                                 // Variable que me pasan como argumento
644
                                                                 layerName = valor.substring(0,valor.indexOf("["));
645
                                                                 rasterLayer2 = (FLyrRasterSE)m_MapContext.getLayers().getLayer(layerName);
646
                                                                 // Se compara si tienen las mismas dimensiones
647
                                                                 if ((rasterLayer1.getBufferFactory().getExtent().height() == rasterLayer2.getBufferFactory().getExtent().height()) && (rasterLayer1.getBufferFactory().getExtent().width() == rasterLayer2.getBufferFactory().getExtent().width())    )// && (qWindow.getNY()== qWindow1.getNY())
648
                                                                 {
649
                                                                         s=jTableVariables.InsertRow(null,valor);
650
                                                                         getQWindowsHash().put(s,null);
651
                                                                         return s;
652
                                                                 }
653
                                                                 // no tienen las mismas dimensiones
654
                                                                 else return "";
655
                                                         } 
656
                                                 }
657
                                 else { 
658
                                                 // Inserto primera variable
659
                                         s=jTableVariables.InsertRow(null,valor);        
660
                                         getQWindowsHash().put(s,null);
661
                                         return s;
662
                                         }
663
                         } // Si la casilla esta seleccionada.
664
                else {
665
                         
666
                         // Inserto la variable, no hay reestricciones de extent.
667
                         s=jTableVariables.InsertRow(null,valor);        
668
                          getQWindowsHash().put(s,null);
669
                          return s; 
670
                 }
671
                return "";
672
         }
673
         
674
         
675
         /**
676
         * Se utiliza para conocer si un determinado valor-->(capa[banda])
677
         * tiene una variable asociada, si no es as? se introduce y 
678
         * se le asigna una nueva variable
679
         *  
680
         * @param valor
681
         * @return el nombre de la variable asignada a ese valor
682
         */
683
         private String consultarVariable(String valor){
684
                 //obtiene la variable asociada a un valor
685
                 String s=jTableVariables.getVariableOf(valor);
686
                 //si es s=="" indica que ese valor no est? introducido en la tabla
687
                 if (s.equals("")){
688
                         String aux;
689
                         //aux contiene el nombre de la variable asignada a valor
690
                         aux=insertVariableToJTable(valor);
691
                         return aux;
692
                 } 
693
                 return s;
694
         }
695
        
696
         
697
         public void focusLost(FocusEvent e) {
698
        
699
                 if(e.getSource()==jTextExpresion){
700
                         jTableVariables.setEnEspera(false);
701
                         System.out.println("Evento del textarea:   "+jTableVariables.isEnEspera());
702
                 }
703
         }
704
        
705
         
706
         
707
         public void focusGained(FocusEvent e) {
708
                
709
        }
710

    
711
        
712
        /**
713
         * inicializa el parser y lo devuelve
714
         * @return parser
715
         */
716
        public JEP initializeParser(){
717
                parser=null;
718
                parser=getParser();
719
                return parser;
720
                
721
        }
722
        
723
        
724
        /**
725
         * @return parser 
726
         */
727
        public JEP getParser() {
728
                if (parser==null){
729
                        parser = new JEP();
730
                        parser.setAllowUndeclared(true);
731
                        parser.addStandardFunctions();
732
                }
733
                return parser;
734
        }
735
        
736
        /**
737
         * @return tabla hash con las variables definidas
738
         */
739
        public HashMap getQWindowsHash() {
740
                if (qWindowsHash==null)
741
                        qWindowsHash = new HashMap();
742
                return qWindowsHash;
743
        }
744
        
745
        
746
        /**
747
         * @return extent de salida
748
         */
749
        public GridExtent getOutputExtent() {
750
                if (outputExtent==null)
751
                        outputExtent= new GridExtent();
752
                return outputExtent;
753
        }
754
        
755
        
756
        /**
757
         * @return proceso de calculo
758
         */
759
        public GridMathProcess getGridMath() {
760
                if (gridMath==null)
761
                        gridMath = new GridMathProcess();
762
                return gridMath;
763
        }
764
        
765
        /**
766
         * @return calculatorDialog
767
         */
768
        public IWindow getCalculatorDialog() {
769
                return calculatorDialog;
770
        }
771
        
772
        /**
773
         * @return vista actual de la aplicacion
774
         */
775
        public View getView() {
776
                return view;
777
        }
778
        
779
        /**
780
         * @return panel de configuracion de salida
781
         */
782
        public OutputOptionsPanel getOptionsPanel() {
783
                if (optionsPanel==null)
784
                        optionsPanel = new OutputOptionsPanel(getView(),this);
785
                return optionsPanel;
786
        }        
787
}
788

    
789