Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extRemoteSensing / src / org / gvsig / remotesensing / calculator / gui / listener / CalculatorPanelListener.java @ 26348

History | View | Annotate | Download (8.27 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.calculator.gui.listener;
42

    
43
import java.awt.Component;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
46
import java.awt.event.KeyEvent;
47
import java.awt.event.KeyListener;
48
import java.io.File;
49
import java.io.FileInputStream;
50
import java.io.FileNotFoundException;
51
import java.io.FileOutputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54
import java.io.OutputStream;
55
import java.util.HashMap;
56
import java.util.Iterator;
57

    
58
import javax.swing.JFileChooser;
59
import javax.swing.JOptionPane;
60
import javax.swing.JTable;
61
import javax.swing.filechooser.FileFilter;
62
import javax.swing.table.DefaultTableModel;
63

    
64
import org.gvsig.remotesensing.calculator.gui.CalculatorPanel;
65
import org.nfunk.jep.Variable;
66

    
67
import com.iver.andami.PluginServices;
68

    
69

    
70
/**
71
 * Listener para el panel de la calculadora de bandas
72
 * 
73
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es)
74
 * @author Alejandro Mu?oz S?nchez        (alejandro.munoz@uclm.es)
75
 * @version 19/10/2007 
76
 */
77
public class CalculatorPanelListener implements ActionListener, KeyListener{
78
        
79
        private CalculatorPanel calculatorPanel = null;
80
        private boolean canClose = false;
81

    
82
        
83
        /**
84
         * Constructor
85
         * @param calculatorPanel
86
         */
87
        public CalculatorPanelListener(CalculatorPanel calculatorPanel) {
88
                this.calculatorPanel = calculatorPanel;
89
        
90
        }
91

    
92
        /**
93
        * Fija las acciones que se realizan cuando se produce un evento
94
        * @param e Objeto que genera el evento
95
        */
96
        public void actionPerformed(ActionEvent e) {
97
                
98
                if (e.getSource().equals(calculatorPanel.getSalvar())){
99
                        //realizar acciones de salvar
100
                        JFileChooser openFileChooser;
101
                        
102
                        openFileChooser = new JFileChooser();                        
103
                        openFileChooser.setEnabled(false);
104
                        openFileChooser.addChoosableFileFilter(new ExpresionFileFilter());
105
                        int returnVal = openFileChooser.showSaveDialog(calculatorPanel);
106
                if (returnVal == JFileChooser.APPROVE_OPTION) {
107
                        String fileName = openFileChooser.getSelectedFile().toString();
108
                        if (!fileName.endsWith(".exp"))
109
                                fileName = fileName + ".exp";
110
                        File outFile = new File(fileName);
111
                        if (outFile.exists()){
112
                                int resp = JOptionPane.showConfirmDialog(
113
                                                        (Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"),
114
                                                        PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
115
                                        if (resp != JOptionPane.YES_OPTION) {
116
                                                return;
117
                                        }
118
                        }
119
                    try {
120
                            OutputStream outputStream = new FileOutputStream(outFile);
121
                                        outputStream.write(calculatorPanel.getJTextExpression().getText().getBytes());
122
                                        outputStream.close();
123
                                } catch (FileNotFoundException e1) {
124
                                        e1.printStackTrace();
125
                                } catch (IOException e1) {
126
                                        e1.printStackTrace();
127
                                }
128
                }
129
                        //Desactiva click sobre tabla 
130
                calculatorPanel.getJTableVariables().setEnEspera(false);                
131
                }else if (e.getSource().equals(calculatorPanel.getRecuperar())){
132
                        //realizar acciones de recuperar                        
133
                        JFileChooser openFileChooser;
134
                        openFileChooser = new JFileChooser();                        
135
                        openFileChooser.setEnabled(false);
136
                        openFileChooser.addChoosableFileFilter(new ExpresionFileFilter());
137
                        int returnVal = openFileChooser.showOpenDialog(calculatorPanel);
138
                if (returnVal == JFileChooser.APPROVE_OPTION) {
139
                    File inFile = openFileChooser.getSelectedFile();
140
                    InputStream inputStream = null;
141
                    byte[] buf = new byte[1];
142
                    calculatorPanel.getJTextExpression().setText("");
143
                    try {
144
                                        inputStream = new FileInputStream(inFile);
145
                                        while (inputStream.read(buf)> 0) {
146
                                                calculatorPanel.getJTextExpression().setText(calculatorPanel.getJTextExpression().getText()+new String(buf));
147
                                        }
148
                                        inputStream.close();
149
                                } catch (FileNotFoundException e1) {
150
                                        // TODO Auto-generated catch block
151
                                        e1.printStackTrace();
152
                                } catch (IOException e1) {
153
                                        // TODO Auto-generated catch block
154
                                        e1.printStackTrace();
155
                                }
156
                                KeyListener[] kls = (KeyListener[])(calculatorPanel.getJTextExpression().getListeners(KeyListener.class));
157
                                kls[0].keyReleased(new KeyEvent(calculatorPanel,1,1L,1,1,(char)1));
158
                }
159
                
160
                //        Desactiva click sobre tabla
161
                calculatorPanel.getJTableVariables().setEnEspera(false);
162
                }
163
                
164
                if(e.getSource().equals(calculatorPanel.getJCheckExtent())){
165
                                        
166
                        int cont=((DefaultTableModel)calculatorPanel.getJTableVariables().getTableFormat().getModel()).getRowCount();
167
                        if (cont>0){
168
                        
169
                                for(int row=0;row<cont;row++)
170
                                        ((DefaultTableModel)calculatorPanel.getJTableVariables().getTableFormat().getModel()).removeRow(0);
171
                
172
                                calculatorPanel.getQWindowsHash().clear();
173
                                calculatorPanel.initializeParser();
174
                                calculatorPanel.getJTextExpression().setText("");
175
                                calculatorPanel.getJTableVariables().getTableFormat().updateUI();
176
                        }        
177
                }        
178
        }        
179
        
180
        public void keyPressed(KeyEvent e) {
181

    
182
        }
183

    
184
        
185
        public void keyReleased(KeyEvent e) {
186
                /*
187
                 * Se Actualiza la tabla de variables de acuerdo a la exprexi?n.
188
                 */
189
                JTable table = null;
190
                HashMap qWindowsHash = null;
191
                String expression = null;
192
                
193
                expression= calculatorPanel.getJTextExpression().getText();
194
                table = calculatorPanel.getJTableVariables().getTableFormat();
195
                qWindowsHash = calculatorPanel.getQWindowsHash();
196
                
197
                calculatorPanel.getParser().getSymbolTable().clear();
198
                        calculatorPanel.getParser().parseExpression(expression);
199
                        //if(!panel.getParser().hasError()){
200
                                //Actualizar la tabla de variables:
201
                                for (Iterator vars = calculatorPanel.getParser().getSymbolTable().values().iterator(); vars.hasNext();) {
202
                                        Variable var = (Variable) vars.next();
203
                                        if (!qWindowsHash.containsKey(var.getName())){
204
                                                qWindowsHash.put(var.getName(),null);
205
                                                calculatorPanel.getJTableVariables().InsertRow(var.getName(),"");
206
                                        }
207
                                }
208
                                
209
                                for(int i=0;i<table.getRowCount();i++){
210
                                        String var = table.getValueAt(i,0).toString();
211
                                        if         (!calculatorPanel.getParser().getSymbolTable().keySet().contains(var))
212
                                                /*
213
                                                 * Si la variable se encuentra en el HashMap de persistenciacc c(*) no se elimina de la tabla.
214
                                                 */
215
                                                if(calculatorPanel.getPersistentVarTable()== null || !calculatorPanel.getPersistentVarTable().containsKey(var)){
216
                                                        ((DefaultTableModel)table.getModel()).removeRow(i);
217
                                                        qWindowsHash.remove(var);
218
                                                        i--;
219
                                        }
220
                                }
221
                                table.updateUI();
222
        }
223

    
224
        public void keyTyped(KeyEvent e) {}
225
}
226

    
227

    
228
/**
229
 * Filtro para el selector de expresiones matem?ticas.
230
 * @author Alejandro Mu?oz (alejandro.munoz@uclm.es)
231
 *
232
 */
233
class ExpresionFileFilter extends FileFilter {
234

    
235
        final static String exp = "exp";
236
        public boolean accept(File f) {
237
                if (f.isDirectory()) {
238
           return true;
239
       }
240
       String s = f.getName();
241
       int i = s.lastIndexOf('.');
242

    
243
       if (i > 0 &&  i < s.length() - 1) {
244
           String extension = s.substring(i+1).toLowerCase();
245
           if (exp.equals(extension)){
246
                   return true;
247
           } else {
248
               return false;
249
           }
250
       }
251
       return false;
252
        }
253

    
254
        public String getDescription() {
255
                 return "Archivos .exp";
256
        }
257
        
258
}