Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAddEventTheme / src / com / iver / gvsig / addeventtheme / gui / AddEventThemePanel.java @ 10661

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * Created on 09-nov-2005
3
 *
4
 * gvSIG. Sistema de Informacin Geogrfica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ibez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package com.iver.gvsig.addeventtheme.gui;
45

    
46
import java.util.ArrayList;
47

    
48
import javax.swing.DefaultComboBoxModel;
49
import javax.swing.JComboBox;
50
import javax.swing.JDialog;
51
import javax.swing.JLabel;
52
import javax.swing.JPanel;
53

    
54
import org.cresques.cts.IProjection;
55
import org.gvsig.gui.beans.AcceptCancelPanel;
56

    
57
import com.hardcode.driverManager.DriverLoadException;
58
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
59
import com.hardcode.gdbms.engine.data.DataSource;
60
import com.iver.andami.PluginServices;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.ui.mdiManager.IWindow;
63
import com.iver.andami.ui.mdiManager.WindowInfo;
64
import com.iver.cit.gvsig.fmap.MapContext;
65
import com.iver.cit.gvsig.fmap.layers.FLayer;
66
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
67
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
68
import com.iver.cit.gvsig.project.documents.table.ProjectTable;
69
import com.iver.gvsig.addeventtheme.AddEventThemListener;
70
import com.iver.gvsig.addeventtheme.AddEventThemeDriver;
71

    
72
/**
73
 * The AddEventThemePanel class creates a JPanel where the
74
 * user can input the name of the gvSIG Table from that the
75
 * plugin will create the new point Layer, and the fields
76
 * corresponding with the point coordinates.
77
 *
78
 * @author jmorell
79
 */
80
public class AddEventThemePanel extends JPanel implements IWindow {
81
    private static final long serialVersionUID = 1L;
82
    private WindowInfo viewInfo = null;
83
    private JLabel tableLabel = null;
84
    private JLabel xLabel = null;
85
    private JLabel yLabel = null;
86
    private JComboBox tableComboBox = null;
87
    private JComboBox xComboBox = null;
88
    private JComboBox yComboBox = null;
89
    //private JButton okButton = null;
90
    private AcceptCancelPanel acceptPanel = null;
91
    private MapContext mapContext;
92
    private ArrayList tableList;
93
    private String firstCoordinate;
94
    private String secondCoordinate;
95

    
96
    /**
97
     * This is the default constructor
98
     */
99
    public AddEventThemePanel(MapContext mapContext, ArrayList tableList) {
100
        super();
101
        this.mapContext = mapContext;
102
        this.tableList = tableList;
103
        initializeCoordinates();
104
        initialize();
105
    }
106

    
107
    private void initializeCoordinates() {
108
        IProjection proj = mapContext.getProjection();
109
        String projName = proj.getAbrev();
110
        if (projName.equals("EPSG:23030") || projName.equals("EPSG:27492") || projName.equals("EPSG:42101") || projName.equals("EPSG:42304")) {
111
            firstCoordinate = "X";
112
            secondCoordinate = "Y";
113
        } else if (projName.equals("EPSG:4230") || projName.equals("EPSG:4326")) {
114
            firstCoordinate = "Lon";
115
            secondCoordinate = "Lat";
116
        } else {
117
            System.out.println("Proyeccin: " + projName);
118
            System.out.println("Proyeccin no soportada.");
119
        }
120
    }
121

    
122
    /**
123
     * This method initializes this
124
     *
125
     * @return void
126
     */
127
    private void initialize() {
128
        yLabel = new JLabel();
129
        yLabel.setBounds(6, 68, 90, 23);
130
        yLabel.setText(secondCoordinate + ":");
131
        xLabel = new JLabel();
132
        xLabel.setBounds(6, 37, 90, 23);
133
        xLabel.setText(firstCoordinate + ":");
134
        tableLabel = new JLabel();
135
        tableLabel.setBounds(6, 6, 90, 23);
136
        tableLabel.setText(PluginServices.getText(this,"Tabla") + ":");
137
        this.setLayout(null);
138
        this.setSize(400, 135);
139
        this.add(tableLabel, null);
140
        this.add(xLabel, null);
141
        this.add(yLabel, null);
142
        this.add(getTableComboBox(), null);
143
        this.add(getXComboBox(), null);
144
        this.add(getYComboBox(), null);
145
        this.add(getAcceptPanel(), null);
146
    }
147

    
148
    private String[] getTableNames() {
149
        String[] tableNames = new String[tableList.size()];
150
        for (int i=0;i<tableList.size();i++) {
151
            tableNames[i] = ((ProjectTable)tableList.get(i)).getName();
152
        }
153
        return tableNames;
154
    }
155
    private String[] getFieldNames() {
156
        String tableName = (String)tableComboBox.getSelectedItem();
157
        ProjectTable projectTable = getProjectTable(tableName);
158
        DataSource ds;
159
        ArrayList fieldName=new ArrayList();
160
        try {
161
            ds = projectTable.getModelo().getRecordset();
162
            for (int i = 0; i < ds.getFieldCount(); i++) {
163
                    // Podr?amos comprobar si es un campo num?rico pero entonces
164
                    // no va con el driver de csv => moraleja: cuando
165
                    // se cambia el driver de csv para que sepa de qu? campos
166
                    // hablamos, se habilita esta l?nea.
167
                    // if (ds.getFieldType(i)==Types.DOUBLE || ds.getFieldType(i)==Types.INTEGER || ds.getFieldType(i)==Types.VARCHAR)
168
                            fieldName.add(ds.getFieldName(i));
169
            }
170
        } catch (ReadDriverException e) {
171
            e.printStackTrace();
172
            NotificationManager.addError(e);
173
        }
174
        return (String[])fieldName.toArray(new String[0]);
175
    }
176

    
177
    private ProjectTable getProjectTable(String tableName) {
178
        ProjectTable projectTable = null;
179
        for (int i=0;i<tableList.size();i++) {
180
            if (((ProjectTable)tableList.get(i)).getName().equals(tableName)) {
181
                projectTable = (ProjectTable)tableList.get(i);
182
                break;
183
            }
184
        }
185
        return projectTable;
186
    }
187

    
188
    private void createNewLayerFromDataSource() {
189
        String tableName = (String)tableComboBox.getSelectedItem();
190
        ProjectTable projectTable = getProjectTable(tableName);
191
        try {
192
                DataSource ds = projectTable.getModelo().getRecordset();
193
                int xFieldIndex = 0;
194
                int yFieldIndex = 0;
195

    
196
            xFieldIndex = ds.getFieldIndexByName(getXFieldName());
197
            yFieldIndex = ds.getFieldIndexByName(getYFieldName());
198
                AddEventThemeDriver addEventThemeDriver = new AddEventThemeDriver();
199
                addEventThemeDriver.setData(ds, xFieldIndex, yFieldIndex);
200
                FLayer capa = null;
201
            capa = LayerFactory.createLayer(tableName, addEventThemeDriver, mapContext.getProjection());
202
                capa.addLayerListener(new AddEventThemListener());
203
                projectTable.setAssociatedTable((AlphanumericData)capa);
204
                mapContext.getLayers().addLayer(capa);
205
        } catch (ReadDriverException e1) {
206
            NotificationManager.addError(e1);
207
        }
208

    
209
    }
210

    
211
    private String getXFieldName() {
212
        return ((String)xComboBox.getSelectedItem());
213
    }
214

    
215
    private String getYFieldName() {
216
        return ((String)yComboBox.getSelectedItem());
217
    }
218

    
219
    /* (non-Javadoc)
220
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
221
     */
222
    public WindowInfo getWindowInfo() {
223
        // TODO Auto-generated method stub
224
        if (viewInfo == null) {
225
            viewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
226
            viewInfo.setTitle(PluginServices.getText(this,"Anadir_capa_de_eventos"));
227
        }
228
        return viewInfo;
229
    }
230

    
231
    /**
232
     * This method initializes tableComboBox
233
     *
234
     * @return javax.swing.JComboBox
235
     */
236
    private JComboBox getTableComboBox() {
237
            if (tableComboBox == null) {
238
                    tableComboBox = new JComboBox();
239
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getTableNames());
240
            tableComboBox.setModel(defaultModel);
241
                    tableComboBox.setBounds(92, 6, 290, 23);
242
                    tableComboBox.addItemListener(new java.awt.event.ItemListener() {
243
                            public void itemStateChanged(java.awt.event.ItemEvent e) {
244
                                    System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
245
                    // Cambiar el estado del xComboBox
246
                    DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
247
                    xComboBox.setModel(defaultModel);
248
                    // Cambiar el estado del yComboBox
249
                    defaultModel = new DefaultComboBoxModel(getFieldNames());
250
                    yComboBox.setModel(defaultModel);
251
                            }
252
                    });
253
            }
254
            return tableComboBox;
255
    }
256

    
257
    /**
258
     * This method initializes xComboBox
259
     *
260
     * @return javax.swing.JComboBox
261
     */
262
    private JComboBox getXComboBox() {
263
            if (xComboBox == null) {
264
                    xComboBox = new JComboBox();
265
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
266
            xComboBox.setModel(defaultModel);
267
                    xComboBox.setBounds(92, 37, 290, 23);
268
            }
269
            return xComboBox;
270
    }
271

    
272
    /**
273
     * This method initializes yComboBox
274
     *
275
     * @return javax.swing.JComboBox
276
     */
277
    private JComboBox getYComboBox() {
278
            if (yComboBox == null) {
279
                    yComboBox = new JComboBox();
280
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
281
            yComboBox.setModel(defaultModel);
282
                    yComboBox.setBounds(92, 68, 290, 23);
283
            }
284
            return yComboBox;
285
    }
286

    
287
    /**
288
     * This method initializes okButton
289
     *
290
     * @return javax.swing.JButton
291
     */
292
    private AcceptCancelPanel getAcceptPanel() {
293
            if (acceptPanel == null) {
294
                    acceptPanel = new AcceptCancelPanel(
295
                                    new java.awt.event.ActionListener() {
296
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
297
                                                System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
298
                                createNewLayerFromDataSource();
299
                                // y sale.
300
                                if (PluginServices.getMainFrame() == null)
301
                                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
302
                                else
303
                                    PluginServices.getMDIManager().closeWindow(AddEventThemePanel.this);
304
                                        }
305
                                },
306
                                    new java.awt.event.ActionListener() {
307
                                        public void actionPerformed(java.awt.event.ActionEvent e) {
308
                                if (PluginServices.getMainFrame() == null)
309
                                    ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
310
                                else
311
                                    PluginServices.getMDIManager().closeWindow(AddEventThemePanel.this);
312
                                        }
313
                                }
314
                                    );
315
                    acceptPanel.setBounds(0, 100, getWidth()-6, 30);
316
            }
317
            return acceptPanel;
318
    }
319
}  //  @jve:decl-index=0:visual-constraint="10,10"