Statistics
| Revision:

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

History | View | Annotate | Download (10.9 KB)

1
/*
2
 * Created on 09-nov-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica 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 Ib??ez, 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
import java.util.logging.ConsoleHandler;
48

    
49
import javax.swing.JPanel;
50

    
51
import com.hardcode.driverManager.DriverLoadException;
52
import com.hardcode.gdbms.engine.data.DataSource;
53
import com.iver.andami.PluginServices;
54
import com.iver.andami.messages.NotificationManager;
55
import com.iver.andami.ui.mdiManager.View;
56
import com.iver.andami.ui.mdiManager.ViewInfo;
57
import com.iver.cit.gvsig.fmap.DriverException;
58
import com.iver.cit.gvsig.fmap.FMap;
59
import com.iver.cit.gvsig.fmap.drivers.addevent.AddEventThemeDriver;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
62
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
63
import com.iver.cit.gvsig.project.ProjectTable;
64
import com.iver.gvsig.addeventtheme.AddEventThemListener;
65

    
66
import javax.swing.DefaultComboBoxModel;
67
import javax.swing.JDialog;
68
import javax.swing.JLabel;
69
import javax.swing.JComboBox;
70
import javax.swing.JButton;
71

    
72
import org.cresques.cts.IProjection;
73

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

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

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

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

    
149
    private String[] getTableNames() {
150
        String[] tableNames = new String[tableList.size()];
151
        for (int i=0;i<tableList.size();i++) {
152
            tableNames[i] = ((ProjectTable)tableList.get(i)).getName();
153
        }
154
        return tableNames;
155
    }
156
    private String[] getFieldNames() {
157
        String tableName = (String)tableComboBox.getSelectedItem();
158
        ProjectTable projectTable = getProjectTable(tableName);
159
        DataSource ds;
160
        String[] fieldNames = null;
161
        try {
162
            ds = projectTable.getModelo().getRecordset();
163
            fieldNames = new String[ds.getFieldCount()];
164
            for (int i = 0; i < ds.getFieldCount(); i++) {
165
                fieldNames[i] = ds.getFieldName(i);
166
            }
167
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
168
            // TODO Auto-generated catch block
169
            e.printStackTrace();
170
            NotificationManager.addError(e);
171
        } catch (DriverLoadException e) {
172
                        // TODO Auto-generated catch block
173
                        e.printStackTrace();
174
                }
175
        return fieldNames;
176
    }
177

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

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

    
197
            xFieldIndex = ds.getFieldIndexByName(getXFieldName());
198
            yFieldIndex = ds.getFieldIndexByName(getYFieldName());
199
                AddEventThemeDriver addEventThemeDriver = new AddEventThemeDriver();
200
                addEventThemeDriver.setData(ds, xFieldIndex, yFieldIndex);
201
                FLayer capa = null;
202
                try {
203
                    capa = LayerFactory.createLayer(tableName, addEventThemeDriver, mapContext.getProjection());
204

    
205
                } catch (DriverException e) {
206
                    // TODO Auto-generated catch block
207
                    e.printStackTrace();
208
                }
209
                capa.addLayerListener(new AddEventThemListener());
210
                projectTable.setAssociatedTable((AlphanumericData)capa);
211
                mapContext.getLayers().addLayer(capa);
212
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
213
            NotificationManager.addError(e1);
214
        } catch (DriverLoadException e) {
215
                NotificationManager.addError(e);
216
                }
217

    
218
    }
219

    
220
    private String getXFieldName() {
221
        return ((String)xComboBox.getSelectedItem());
222
    }
223

    
224
    private String getYFieldName() {
225
        return ((String)yComboBox.getSelectedItem());
226
    }
227

    
228
    /* (non-Javadoc)
229
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
230
     */
231
    public ViewInfo getViewInfo() {
232
        // TODO Auto-generated method stub
233
        if (viewInfo == null) {
234
            viewInfo=new ViewInfo(ViewInfo.MODALDIALOG);
235
            viewInfo.setTitle(PluginServices.getText(this,"Anadir_capa_de_eventos"));
236
        }
237
        return viewInfo;
238
    }
239

    
240
    /**
241
     * This method initializes tableComboBox
242
     *
243
     * @return javax.swing.JComboBox
244
     */
245
    private JComboBox getTableComboBox() {
246
            if (tableComboBox == null) {
247
                    tableComboBox = new JComboBox();
248
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getTableNames());
249
            tableComboBox.setModel(defaultModel);
250
                    tableComboBox.setBounds(43, 6, 161, 24);
251
                    tableComboBox.addItemListener(new java.awt.event.ItemListener() {
252
                            public void itemStateChanged(java.awt.event.ItemEvent e) {
253
                                    System.out.println("itemStateChanged()"); // TODO Auto-generated Event stub itemStateChanged()
254
                    // Cambiar el estado del xComboBox
255
                    DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
256
                    xComboBox.setModel(defaultModel);
257
                    // Cambiar el estado del yComboBox
258
                    defaultModel = new DefaultComboBoxModel(getFieldNames());
259
                    yComboBox.setModel(defaultModel);
260
                            }
261
                    });
262
            }
263
            return tableComboBox;
264
    }
265

    
266
    /**
267
     * This method initializes xComboBox
268
     *
269
     * @return javax.swing.JComboBox
270
     */
271
    private JComboBox getXComboBox() {
272
            if (xComboBox == null) {
273
                    xComboBox = new JComboBox();
274
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
275
            xComboBox.setModel(defaultModel);
276
                    xComboBox.setBounds(43, 37, 161, 24);
277
            }
278
            return xComboBox;
279
    }
280

    
281
    /**
282
     * This method initializes yComboBox
283
     *
284
     * @return javax.swing.JComboBox
285
     */
286
    private JComboBox getYComboBox() {
287
            if (yComboBox == null) {
288
                    yComboBox = new JComboBox();
289
            DefaultComboBoxModel defaultModel = new DefaultComboBoxModel(getFieldNames());
290
            yComboBox.setModel(defaultModel);
291
                    yComboBox.setBounds(43, 68, 161, 24);
292
            }
293
            return yComboBox;
294
    }
295

    
296
    /**
297
     * This method initializes okButton
298
     *
299
     * @return javax.swing.JButton
300
     */
301
    private JButton getOkButton() {
302
            if (okButton == null) {
303
                    okButton = new JButton();
304
                    okButton.setBounds(104, 100, 100, 24);
305
                    okButton.addActionListener(new java.awt.event.ActionListener() {
306
                            public void actionPerformed(java.awt.event.ActionEvent e) {
307
                                    System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
308
                    createNewLayerFromDataSource();
309
                    // y sale.
310
                    if (PluginServices.getMainFrame() == null)
311
                        ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
312
                    else
313
                        PluginServices.getMDIManager().closeView(AddEventThemePanel.this);
314
                            }
315
                    });
316
                    okButton.setText(PluginServices.getText(this,"Aceptar"));
317
            }
318
            return okButton;
319
    }
320
}  //  @jve:decl-index=0:visual-constraint="10,10"