Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / imp / DefaultAppgvSIGManager.java @ 40558

History | View | Annotate | Download (16.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
* MA  02110-1301, USA.
43
*
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2009 IVER T.I   {{Task}}
49
*/
50

    
51
/**
52
 *
53
 */
54
package org.gvsig.app.imp;
55

    
56
import java.awt.Component;
57
import java.io.File;
58
import java.util.ArrayList;
59
import java.util.Iterator;
60
import java.util.List;
61

    
62
import javax.swing.JFileChooser;
63
import javax.swing.filechooser.FileFilter;
64

    
65
import org.cresques.cts.IProjection;
66
import org.gvsig.about.AboutLocator;
67
import org.gvsig.about.AboutManager;
68
import org.gvsig.andami.Launcher;
69
import org.gvsig.andami.PluginServices;
70
import org.gvsig.andami.ui.mdiFrame.MainFrame;
71
import org.gvsig.andami.ui.mdiManager.IWindow;
72
import org.gvsig.andami.ui.mdiManager.MDIManager;
73
import org.gvsig.app.ApplicationManager;
74
import org.gvsig.app.PreferencesNode;
75
import org.gvsig.app.extension.Version;
76
import org.gvsig.app.gui.WizardPanel;
77
import org.gvsig.app.prepareAction.PrepareContext;
78
import org.gvsig.app.prepareAction.PrepareContextView;
79
import org.gvsig.app.prepareAction.PrepareDataStore;
80
import org.gvsig.app.prepareAction.PrepareDataStoreParameters;
81
import org.gvsig.app.prepareAction.PrepareLayer;
82
import org.gvsig.app.project.DefaultProject;
83
import org.gvsig.app.project.Project;
84
import org.gvsig.app.project.ProjectManager;
85
import org.gvsig.app.project.documents.Document;
86
import org.gvsig.app.project.documents.gui.IDocumentWindow;
87
import org.gvsig.fmap.crs.CRSFactory;
88
import org.gvsig.fmap.dal.DALLocator;
89
import org.gvsig.fmap.dal.DataManager;
90
import org.gvsig.fmap.dal.DataStore;
91
import org.gvsig.fmap.dal.DataStoreParameters;
92
import org.gvsig.fmap.geom.GeometryLocator;
93
import org.gvsig.fmap.geom.GeometryManager;
94
import org.gvsig.fmap.mapcontext.MapContextLocator;
95
import org.gvsig.fmap.mapcontext.MapContextManager;
96
import org.gvsig.fmap.mapcontext.layers.FLayer;
97
import org.gvsig.gui.ColorTablesFactory;
98
import org.gvsig.symbology.swing.SymbologySwingLocator;
99
import org.gvsig.tools.ToolsLocator;
100
import org.gvsig.tools.dataTypes.DataTypesManager;
101
import org.gvsig.tools.dispose.DisposableManager;
102
import org.gvsig.tools.dynobject.DynObjectManager;
103
import org.gvsig.tools.extensionpoint.ExtensionPoint;
104
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
105
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
106
import org.gvsig.tools.persistence.PersistenceManager;
107
import org.gvsig.tools.swing.api.ToolsSwingLocator;
108
import org.gvsig.tools.swing.icontheme.IconThemeManager;
109
import org.slf4j.Logger;
110
import org.slf4j.LoggerFactory;
111

    
112

    
113

    
114

    
115
/**
116
 * @author jmvivo
117
 *
118
 */
119
public class DefaultAppgvSIGManager implements ApplicationManager {
120

    
121
    @SuppressWarnings("unused")
122
        private static Logger logger = LoggerFactory.getLogger(DefaultAppgvSIGManager.class);
123
    
124
        private static final String EPNAME_PREPARE_OPEN_DATASTORE = "org.gvsig.datastore.open.prepare"; //"PrepareOpenDataStore";
125
        private static final String EPNAME_PREPARE_OPEN_DATASTOREPARAMETERS = "org.gvsig.datastoreparameters.open.prepare"; //"PrepareOpenDataStoreParameters";
126
        private static final String EPNAME_PREPARE_OPEN_LAYER = "org.gvsig.layer.open.prepare"; // "PrepareOpenLayer";
127

    
128
        private static final String EPNAME_ADD_TABLE_WIZARD = "AddLayerWizard";
129

    
130
        private ExtensionPointManager epManager;
131

    
132
    private Version version;
133

    
134
        public DefaultAppgvSIGManager() {
135
                epManager = ToolsLocator.getExtensionPointManager();
136
                epManager.add(EPNAME_PREPARE_OPEN_DATASTORE,
137
                                "Actions to do when open a DataStore");
138
                epManager.add(EPNAME_PREPARE_OPEN_DATASTOREPARAMETERS,
139
                                "Actions to do before open a DataStore with parameters");
140
                epManager.add(EPNAME_PREPARE_OPEN_LAYER,
141
                        "Actions to do after create a Layer");
142
                epManager.add(EPNAME_ADD_TABLE_WIZARD,
143
                                "Wizards to add new document table");
144

    
145
        version = new Version();
146
        }
147

    
148
        /* (non-Javadoc)
149
         * @see org.gvsig.appGvSigManager#pepareOpenDataSource(org.gvsig.fmap.dal.DataStore)
150
         */
151
        @SuppressWarnings("unchecked")
152
        public DataStore pepareOpenDataSource(DataStore store,
153
                        PrepareContext context) throws Exception {
154
                ExtensionPoint ep = epManager.get(EPNAME_PREPARE_OPEN_DATASTORE);
155
                if (ep.getCount() == 0) {
156
                        return store;
157
                }
158
                DataStore result = store;
159
                Iterator<ExtensionPoint.Extension> iter = ep.iterator();
160
                PrepareDataStore prepare;
161
                while (iter.hasNext()) {
162
                        prepare = (PrepareDataStore) iter.next().create();
163
                        result = prepare.prepare(store, context);
164
                }
165
                return result;
166
        }
167

    
168
        /* (non-Javadoc)
169
         * @see org.gvsig.appGvSigManager#prepareOpenDataStoreParameters(org.gvsig.fmap.dal.DataStoreParameters)
170
         */
171
        @SuppressWarnings("unchecked")
172
        public DataStoreParameters prepareOpenDataStoreParameters(
173
                        DataStoreParameters storeParameters, PrepareContext context)
174
                        throws Exception {
175

    
176
                ExtensionPoint ep = epManager
177
                                .get(EPNAME_PREPARE_OPEN_DATASTOREPARAMETERS);
178
                if (ep.getCount() == 0) {
179
                        return storeParameters;
180
                }
181
                DataStoreParameters result = storeParameters;
182
                Iterator<ExtensionPoint.Extension> iter = ep.iterator();
183
                PrepareDataStoreParameters prepare;
184
                while (iter.hasNext()) {
185
                        prepare = (PrepareDataStoreParameters) iter.next().create();
186
                        result = prepare.prepare(storeParameters, context);
187
                }
188

    
189
                return result;
190
        }
191
        
192
        /*
193
         * (non-Javadoc)
194
         * @see org.gvsig.app.ApplicationManager#prepareOpenDataStoreParameters(java.util.List, org.gvsig.app.prepareAction.PrepareContext)
195
         */
196
        @SuppressWarnings("unchecked")
197
        public List<DataStoreParameters> prepareOpenDataStoreParameters(
198
                        List<DataStoreParameters> storeParameters, PrepareContext context)
199
                        throws Exception {
200

    
201
                ExtensionPoint ep = epManager
202
                                .get(EPNAME_PREPARE_OPEN_DATASTOREPARAMETERS);
203
                if (ep.getCount() == 0) {
204
                        return storeParameters;
205
                }
206
                
207
                List<DataStoreParameters> result = new ArrayList<DataStoreParameters>();
208
                
209
                Iterator<ExtensionPoint.Extension> iter = ep.iterator();
210
                List<PrepareDataStoreParameters> prepareList = new ArrayList<PrepareDataStoreParameters>();
211
                while (iter.hasNext()) {
212
                        prepareList.add((PrepareDataStoreParameters) iter.next().create());
213
                }
214
                
215
                for (int i = 0; i < storeParameters.size(); i++) {
216
                        DataStoreParameters param = storeParameters.get(i);
217
                        for (int j = 0; j < prepareList.size(); j++) {
218
                                prepareList.get(j).pre(param, context);
219
                        }
220
                }
221
                
222
                for (int i = 0; i < storeParameters.size(); i++) {
223
                        DataStoreParameters param = storeParameters.get(i);
224
                        if(param != null) {
225
                                for (int j = 0; j < prepareList.size(); j++) {
226
                                        param = prepareList.get(j).prepare(param, context);
227
                                        if(param == null)
228
                                                break;
229
                                }
230
                                if(param != null)
231
                                        result.add(param);
232
                        }
233
                }
234
                
235
                for (int i = 0; i < storeParameters.size(); i++) {
236
                        DataStoreParameters param = storeParameters.get(i);
237
                        for (int j = 0; j < prepareList.size(); j++) {
238
                                prepareList.get(j).post(param, context);
239
                        }
240
                }
241

    
242
                return result;
243
        }
244

    
245
        public void registerPrepareOpenDataStore(PrepareDataStore action) {
246
                ExtensionPoint ep = epManager.get(EPNAME_PREPARE_OPEN_DATASTORE);
247
                ep.append(action.getName(), action.getDescription(), action);
248
        }
249

    
250
        public void registerPrepareOpenDataStoreParameters(
251
                        PrepareDataStoreParameters action) {
252
                ExtensionPoint ep = epManager
253
                                .get(EPNAME_PREPARE_OPEN_DATASTOREPARAMETERS);
254
                ep.append(action.getName(), action.getDescription(), action);
255

    
256

    
257
        }
258

    
259
        @SuppressWarnings("unchecked")
260
        public FLayer prepareOpenLayer(FLayer layer,
261
                        PrepareContextView context)
262
                        throws Exception {
263

    
264
                ExtensionPoint ep = epManager.get(EPNAME_PREPARE_OPEN_LAYER);
265

    
266
                if (ep.getCount() == 0) {
267
                        return layer;
268
                }
269
                FLayer result = layer;
270
                Iterator<ExtensionPoint.Extension> iter = ep.iterator();
271
                PrepareLayer prepare;
272
                while (iter.hasNext()) {
273
                        prepare = (PrepareLayer) iter.next().create();
274
                        result = prepare.prepare(result, context);
275
                }
276

    
277
                return result;
278

    
279
        }
280

    
281
        public void registerPrepareOpenLayer(PrepareLayer action) {
282
                ExtensionPoint ep = epManager.get(EPNAME_PREPARE_OPEN_LAYER);
283
                ep.append(action.getName(), action.getDescription(), action);
284
        }
285

    
286
        public void registerAddTableWizard(String name, String description,
287
                        Class<? extends WizardPanel> wpClass) {
288
                ExtensionPoint ep = epManager.get(EPNAME_ADD_TABLE_WIZARD);
289
                ep.append(name, description, wpClass);
290
        }
291

    
292
        @SuppressWarnings("unchecked")
293
        public List<WizardPanel> getWizardPanels() throws Exception {
294
                ExtensionPoint ep = epManager.get(EPNAME_ADD_TABLE_WIZARD);
295
                List<WizardPanel> result = new ArrayList<WizardPanel>();
296
                Iterator<Extension> iter = ep.iterator();
297
                while (iter.hasNext()) {
298
                        result.add((WizardPanel) iter.next().create());
299
                }
300
                return result;
301
        }
302

    
303
        public DataManager getDataManager() {
304
                return DALLocator.getDataManager();
305
        }
306

    
307
        public GeometryManager getGeometryManager() {
308
                return GeometryLocator.getGeometryManager();
309
        }
310

    
311
        public PersistenceManager getPersistenceManager() {
312
                return ToolsLocator.getPersistenceManager();
313
        }
314

    
315
        public DisposableManager getDisposableManager() {
316
                return ToolsLocator.getDisposableManager();
317
        }
318

    
319
        public DynObjectManager getDynObjectManager() {
320
                return ToolsLocator.getDynObjectManager();
321
        }
322

    
323
        public ExtensionPointManager getExtensionPointManager() {
324
                return ToolsLocator.getExtensionPointManager();
325
        }
326

    
327
        public ProjectManager getProjectManager() {
328
                return ProjectManager.getInstance();
329
        }
330

    
331
        public MDIManager getUIManager() {
332
                return PluginServices.getMDIManager();
333
        }
334

    
335
        public MapContextManager getMapContextManager() {
336
                return MapContextLocator.getMapContextManager();
337
        }
338

    
339
        public DataTypesManager getDataTypesManager() {
340
                return ToolsLocator.getDataTypesManager();
341
        }
342
        
343
        public IProjection getCRS(String code) {
344
                return CRSFactory.getCRS(code);
345
        }
346

    
347
        public IconThemeManager getIconThemeManager() {
348
                return ToolsSwingLocator.getIconThemeManager();
349
        }
350

    
351
        public String getArgument(String name) {
352
                return PluginServices.getArgumentByName(name);
353
        }
354

    
355
        public String[] getArguments() {
356
                return PluginServices.getArguments();
357
        }
358

    
359
        public String getFromClipboard() {
360
                return PluginServices.getFromClipboard();
361
        }
362

    
363
        public void putInClipboard(String data) {
364
                PluginServices.putInClipboard(data);
365
        }
366

    
367
        public Project getCurrentProject() {
368
                return ProjectManager.getInstance().getCurrentProject();
369
        }
370

    
371
        public PreferencesNode getPreferences(String node) {
372
                if( node.equalsIgnoreCase("project")) {
373
                        return DefaultProject.getPreferences();
374
                }
375
                return new DefaultPreferencesNode(node);
376
        }
377

    
378
        public PreferencesNode getPreferences() {
379
                return new DefaultPreferencesNode();
380
        }
381

    
382
        public Version getVersion() {
383
        return version;
384
        }
385
        
386
        public AboutManager getAbout() {
387
                AboutManager manager = AboutLocator.getManager();
388
                return manager;
389
        }
390

    
391
        public ColorTablesFactory getColorTablesFactory() {
392
                return SymbologySwingLocator.getSwingManager().getColorTablesFactory();
393
        }
394

    
395
        public void registerColorTablesFactory(ColorTablesFactory factory) {
396
                SymbologySwingLocator.getSwingManager().setColorTablesFactory(factory);
397
                
398
        }
399

    
400
    public IWindow getActiveWindow() {
401
        try {
402
            IWindow window = PluginServices.getMDIManager().getActiveWindow();
403
            return window;
404
        } catch (Exception ex) {
405
            return null;
406
        }
407
    }
408

    
409
    public Document getActiveDocument() {
410
            return this.getActiveDocument((Class<? extends Document>)null);
411
    }
412
    
413
    public Document getActiveDocument(String documentTypeName) {
414
        Document document = this.getActiveDocument();
415
        if( document!= null && document.getTypeName().equalsIgnoreCase(documentTypeName) ) {
416
            return document;
417
        }
418
        return null;
419
    }
420
    
421
    public Document getActiveDocument(Class<? extends Document> documentClass) {
422
        Project project = this.getCurrentProject();
423
        if( project == null ) {
424
            return null;
425
        }
426
        return project.getActiveDocument(documentClass);
427
    }
428
    
429
    public IDocumentWindow getDocumentWindow(Document document) {
430
        Class<? extends IDocumentWindow> defaultDocumentClass =
431
            document.getFactory().getMainWindowClass();
432
        return (IDocumentWindow) this.getUIManager().getSingletonWindow(defaultDocumentClass, document);
433
    }
434

    
435
        public String getLocaleLanguage() {
436
                return Launcher.getAndamiConfig().getLocaleLanguage();
437
        }
438

    
439
        public void message(String message, int message_type) {
440
                MainFrame main = PluginServices.getMainFrame();
441
                main.message(message, message_type);
442
        }
443

    
444
        public void messageDialog(String message, String title, int messageType) {
445
                MainFrame main = PluginServices.getMainFrame();
446
                main.messageDialog(message, title, messageType);
447
        }
448

    
449
        public void messageDialog(String message, String[] messageArgs,
450
                        String title, int messageType) {
451
                MainFrame main = PluginServices.getMainFrame();
452
                main.messageDialog(message, messageArgs, title, messageType);
453
        }
454

    
455
        public int confirmDialog(String message, String title, int optionType,
456
                        int messageType) {
457
                MainFrame main = PluginServices.getMainFrame();
458
                return main.confirmDialog(message, title, optionType, messageType);
459
        }
460

    
461
        public String inputDialog(String message, String title, int messageType,
462
                        String initialValue) {
463
                MainFrame main = PluginServices.getMainFrame();
464
                return main.inputDialog(message, title, messageType, initialValue);
465
        }
466

    
467
        public String inputDialog(String message, String title) {
468
                MainFrame main = PluginServices.getMainFrame();
469
                return main.inputDialog(message, title);
470
        }
471

    
472
        public void showDialog(Component contents, String title) {
473
                MainFrame main = PluginServices.getMainFrame();
474
                main.showDialog(contents, title);
475
        }
476

    
477
        public Component createComponent(Class<? extends Component> theClass,
478
                        Object... parameters) {
479
                MainFrame main = PluginServices.getMainFrame();
480
                return main.createComponentWithParams(theClass, parameters);
481
        }
482

    
483
        public Component createComponentWithParams(
484
                        Class<? extends Component> theClass, Object[] parameters) {
485
                MainFrame main = PluginServices.getMainFrame();
486
                return main.createComponentWithParams(theClass, parameters);
487
        }
488

    
489

    
490
        public File[] showChooserDialog(
491
                        final String title,
492
                        final int type, // SAVE_DIALOG / OPEN_DIALOG
493
                        final int selectionMode, //    JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES
494
                        final boolean multiselection, 
495
                        final File initialPath,
496
                        final FileFilter filter,
497
                        final boolean fileHidingEnabled
498
                        ) {
499
                MainFrame main = PluginServices.getMainFrame();
500
                return main.showChooserDialog(title, type, selectionMode, multiselection, initialPath, filter, fileHidingEnabled);
501
        }
502
        
503
        public File[] showOpenDirectoryDialog(String title, File initialPath) {
504
                return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.DIRECTORIES_ONLY, false, initialPath, null, false);
505
        }
506

    
507
        
508
        public File[] showOpenFileDialog(String title, File initialPath) {
509
                return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false);
510
        }
511

    
512
        
513
        public File[] showSaveFileDialog(String title, File initialPath) {
514
                return showChooserDialog(title, JFileChooser.SAVE_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false);
515
        }
516
        
517
        public String translate(String message, String... args) {
518
                return org.gvsig.i18n.Messages.translate(message, args);
519
        }
520
        
521
        public Component getRootComponent() {
522
                return (Component) PluginServices.getMainFrame();
523
        }
524

    
525
        public void refreshMenusAndToolBars() {
526
                PluginServices.getMainFrame().refreshControls();
527
        }
528

    
529
        
530
}