Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / oracle / extension / ExportOracleExtension.java @ 29865

History | View | Annotate | Download (7.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop 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
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package org.gvsig.oracle.extension;
44

    
45
import javax.swing.ImageIcon;
46

    
47
import org.gvsig.andami.PluginServices;
48
import org.gvsig.andami.plugins.Extension;
49
import org.gvsig.andami.ui.mdiManager.IWindow;
50
import org.gvsig.andami.ui.wizard.WizardAndami;
51
import org.gvsig.app.extension.About;
52
import org.gvsig.app.project.documents.view.gui.View;
53
import org.gvsig.fmap.dal.store.oracle.OracleLibrary;
54
import org.gvsig.fmap.mapcontext.MapContext;
55
import org.gvsig.fmap.mapcontext.layers.FLayer;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
57
import org.gvsig.fmap.mapcontrol.MapControl;
58
import org.gvsig.oracle.gui.NewOracleTableWizard;
59
import org.gvsig.oracle.gui.RepeatedChooseGeometryTypePanel;
60
import org.gvsig.oracle.gui.RepeatedFieldDefinitionPanel;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

    
64

    
65

    
66
/**
67
 * This extension adds the export-to-oracle button.
68
 * 
69
 * @author jldominguez, vsanjaime
70
 * 
71
 */
72
public class ExportOracleExtension extends Extension {
73

    
74
        private static Logger logger = LoggerFactory
75
                        .getLogger(ExportOracleExtension.class);
76

    
77
        public static boolean ORACLE_JAR_PRESENT = false;
78
        private OracleLibrary oracleSpatialLibrary = null;
79

    
80
        /**
81
         * initialize
82
         */
83
        public void initialize() {
84

    
85
                ORACLE_JAR_PRESENT = isOracleJarPresent();
86

    
87
                if (!ORACLE_JAR_PRESENT) {
88
                        logger
89
                                        .info("*-----------------------------------------------------------------------*");
90
                        logger
91
                                        .info("* Oracle library not found!                                             *");
92
                        logger
93
                                        .info("* You need to place the Oracle JDBC library in gvSIG's main LIB folder. *");
94
                        logger
95
                                        .info("* Read the Oracle driver user manual for details.                       *");
96
                        logger
97
                                        .info("*-----------------------------------------------------------------------*");
98
                } else {
99
                        logger
100
                                        .info("*-----------------------------------------------------------------------*");
101
                        logger
102
                                        .info("* Found the Oracle JDBC library! :)                                     *");
103
                        logger
104
                                        .info("*-----------------------------------------------------------------------*");
105
                }
106

    
107
                // about
108
                java.net.URL newurl = createResourceUrl("about/jdbc-os-about.html");
109
                About claseAbout = (About) PluginServices
110
                                .getExtension(About.class);
111
                claseAbout.getAboutPanel().addAboutUrl("JDBC Oracle Spatial", newurl);
112

    
113
                oracleSpatialLibrary = new OracleLibrary();
114
                oracleSpatialLibrary.initialize();
115
        }
116

    
117
        /**
118
         * Post initialize extension
119
         */
120
        public void postInitialize() {
121

    
122
                // postinitialize library
123
                oracleSpatialLibrary.postInitialize();
124
        }
125

    
126
        /**
127
         * execute extension
128
         */
129
        public void execute(String actionCommand) {
130

    
131
                if (actionCommand.compareToIgnoreCase("EXPORT_TO_ORACLE_SPATIAL") == 0) {
132
                        FLyrVect lyrv = null;
133
                        MapContext mx = null;
134

    
135
                        try {
136
                                IWindow w = PluginServices.getMDIManager().getActiveWindow();
137

    
138
                                if (w instanceof View) {
139
                                        View v = (View) w;
140
                                        MapControl mc = v.getMapControl();
141
                                        mx = mc.getMapContext();
142

    
143
                                        FLayer[] lyrs = mx.getLayers().getActives();
144

    
145
                                        if (lyrs.length == 1) {
146
                                                FLayer lyr = lyrs[0];
147

    
148
                                                if (lyr instanceof FLyrVect) {
149
                                                        lyrv = (FLyrVect) lyr;
150

    
151
                                                        ExportToOracle export = new ExportToOracle();
152
                                                        export.toOracle(mx, lyrv);
153
                                                }
154
                                        }
155
                                }
156
                        } catch (Exception ex) {
157
                                logger
158
                                                .error("Unexpected error while getting active vect layer: "
159
                                                                + ex.getMessage());
160
                                logger.error("Nothing done.");
161
                        }
162
                }
163

    
164
                if (actionCommand.compareToIgnoreCase("NEW_ORACLE_SPATIAL") == 0) {
165

    
166
                        IWindow w = PluginServices.getMDIManager().getActiveWindow();
167
                        if (w instanceof View) {
168
                                try {
169
                                        String _file = createResourceUrl(
170
                                                        "images/new_geodb_table.png").getFile();
171
                                        ImageIcon iicon = new ImageIcon(_file);
172

    
173
                                        WizardAndami wizard = new WizardAndami(iicon);
174
                                        RepeatedChooseGeometryTypePanel panelChoose = new RepeatedChooseGeometryTypePanel(
175
                                                        wizard.getWizardComponents());
176
                                        RepeatedFieldDefinitionPanel panelFields = new RepeatedFieldDefinitionPanel(
177
                                                        wizard.getWizardComponents());
178
                                        NewOracleTableWizard connPanel = new NewOracleTableWizard(
179
                                                        wizard.getWizardComponents());
180

    
181
                                        wizard.getWizardComponents().addWizardPanel(panelChoose);
182
                                        wizard.getWizardComponents().addWizardPanel(panelFields);
183
                                        wizard.getWizardComponents().addWizardPanel(connPanel);
184

    
185
                                        View theView = (View) w;
186
                                        MapContext mc = theView.getMapControl().getMapContext();
187

    
188
                                        NewOracleTableFinishAction action = new NewOracleTableFinishAction(
189
                                                        wizard.getWizardComponents(), wizard, connPanel, mc);
190

    
191
                                        wizard.getWizardComponents().setFinishAction(action);
192
                                        wizard.getWizardComponents().getFinishButton().setEnabled(
193
                                                        false);
194
                                        wizard.getWindowInfo().setWidth(640);
195
                                        wizard.getWindowInfo().setHeight(350);
196
                                        wizard.getWindowInfo().setTitle(
197
                                                        PluginServices.getText(this, "new_layer"));
198
                                        PluginServices.getMDIManager().addWindow(wizard);
199

    
200
                                } catch (Exception ex) {
201
                                        logger
202
                                                        .error("While showing new oracle spatial table wizard: "
203
                                                                        + ex.getMessage());
204
                                }
205
                        }
206
                }
207
        }
208

    
209
        /**
210
         * 
211
         */
212
        public boolean isEnabled() {
213
                return isVisible();
214
        }
215

    
216
        /**
217
         * Is visible when there is one vector layer selected
218
         */
219
        public boolean isVisible() {
220

    
221
                if (!ORACLE_JAR_PRESENT) {
222
                        return false;
223
                }
224

    
225
                try {
226
                        IWindow w = PluginServices.getMDIManager().getActiveWindow();
227

    
228
                        if (w instanceof View) {
229
                                View v = (View) w;
230
                                MapControl mc = v.getMapControl();
231
                                MapContext mx = mc.getMapContext();
232
                                FLayer[] lyrs = mx.getLayers().getActives();
233

    
234
                                if (lyrs.length == 1) {
235
                                        FLayer lyr = lyrs[0];
236

    
237
                                        if ((lyr instanceof FLyrVect) && (true)) {
238
                                                return true;
239
                                        }
240
                                }
241
                        }
242
                } catch (Exception ex) {
243
                        return false;
244
                }
245

    
246
                return false;
247
        }
248

    
249
        /**
250
         * Create resource url
251
         * 
252
         * @param path
253
         * @return
254
         */
255
        private java.net.URL createResourceUrl(String path) {
256
                return getClass().getClassLoader().getResource(path);
257
        }
258

    
259
        /**
260
         * Is there a Oracle jar present
261
         * 
262
         * @return
263
         */
264
        private boolean isOracleJarPresent() {
265

    
266
                try {
267
                        Class rowid_class = Class.forName("oracle.sql.ROWID");
268
                } catch (Exception ex) {
269
                        logger.error("Unable to instantiate ROWID (oracle jar missing?) : "
270
                                        + ex.getMessage());
271
                        return false;
272
                }
273
                return true;
274
        }
275
}