Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / TableManager.java @ 46050

History | View | Annotate | Download (17.9 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 40435 jjdelcerro
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8 40558 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * of the License, or (at your option) any later version.
10 40558 jjdelcerro
 *
11 40435 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 40435 jjdelcerro
 * 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 40558 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 40435 jjdelcerro
 * MA  02110-1301, USA.
20 40558 jjdelcerro
 *
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 40435 jjdelcerro
 */
24
25
package org.gvsig.app.project.documents.table;
26
27 44262 jjdelcerro
import java.util.Collections;
28 40435 jjdelcerro
import java.util.Iterator;
29
import java.util.List;
30
31
import javax.swing.ImageIcon;
32 44437 jjdelcerro
import javax.swing.JOptionPane;
33 40435 jjdelcerro
34
import org.gvsig.andami.IconThemeHelper;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.messages.NotificationManager;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.addlayer.AddLayerDialog;
40
import org.gvsig.app.gui.WizardPanel;
41
import org.gvsig.app.project.Project;
42
import org.gvsig.app.project.ProjectManager;
43
import org.gvsig.app.project.documents.AbstractDocument;
44
import org.gvsig.app.project.documents.AbstractDocumentManager;
45
import org.gvsig.app.project.documents.Document;
46 42096 cmartinez
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
47
import org.gvsig.app.project.documents.actions.CutDocumentAction;
48
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
49 40435 jjdelcerro
import org.gvsig.app.project.documents.gui.IDocumentWindow;
50
import org.gvsig.app.project.documents.gui.WindowLayout;
51
import org.gvsig.app.project.documents.table.TableDocument.TableLink;
52
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
53
import org.gvsig.app.project.documents.table.gui.TableProperties;
54 44304 jjdelcerro
import org.gvsig.fmap.dal.AbstractStoresRepository;
55 44253 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
56
import org.gvsig.fmap.dal.DataManager;
57
import org.gvsig.fmap.dal.DataStoreParameters;
58 40435 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
59
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
60
import org.gvsig.fmap.dal.feature.FeatureStore;
61 44437 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
62 40435 jjdelcerro
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
63
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
64
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
65
import org.gvsig.tools.ToolsLocator;
66
import org.gvsig.tools.dynobject.DynObjectManager;
67
import org.gvsig.tools.dynobject.DynStruct;
68
import org.gvsig.tools.evaluator.Evaluator;
69 44437 jjdelcerro
import org.gvsig.tools.i18n.I18nManager;
70 40435 jjdelcerro
import org.gvsig.tools.persistence.PersistenceManager;
71 44437 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
72
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
73 45931 jjdelcerro
import org.gvsig.tools.util.Invocable;
74 44262 jjdelcerro
import org.gvsig.tools.util.MappedIterator;
75
import org.gvsig.tools.util.UnmodifiableBasicSet;
76 40435 jjdelcerro
77
/**
78
 * Factory of Table.
79
 *
80 45543 jjdelcerro
 * @author gvSIG Tem
81 40435 jjdelcerro
 *
82
 */
83 45543 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
84 40435 jjdelcerro
public class TableManager extends AbstractDocumentManager {
85 44253 jjdelcerro
86 44304 jjdelcerro
    private static class TableDocumentStoresRepository extends AbstractStoresRepository {
87 40435 jjdelcerro
88 44439 jjdelcerro
        public TableDocumentStoresRepository(String name, String label) {
89
            super(name, label);
90 44304 jjdelcerro
        }
91
92 44253 jjdelcerro
        @Override
93 44304 jjdelcerro
        protected DataStoreParameters getMyParameters(String name) {
94 44253 jjdelcerro
            Project project = ProjectManager.getInstance().getCurrentProject();
95
            TableDocument tableDoc = (TableDocument) project.getDocument(name, TYPENAME);
96 45543 jjdelcerro
            if( tableDoc==null || tableDoc.getFeatureStore()==null) {
97 44253 jjdelcerro
                return null;
98
            }
99
            return tableDoc.getFeatureStore().getParameters();
100
        }
101 44262 jjdelcerro
102
        @Override
103 44304 jjdelcerro
        protected boolean isEmptyMyRepository() {
104 44262 jjdelcerro
            Project project = ProjectManager.getInstance().getCurrentProject();
105 44304 jjdelcerro
            return project.getDocuments(TableManager.TYPENAME).isEmpty();
106 44262 jjdelcerro
        }
107
108
        @Override
109 44304 jjdelcerro
        protected int getMySize() {
110 44262 jjdelcerro
            Project project = ProjectManager.getInstance().getCurrentProject();
111 44304 jjdelcerro
            int sz = project.getDocuments(TableManager.TYPENAME).size();
112
            return sz;
113
        }
114
115
        @Override
116
        protected UnmodifiableBasicSet<String> getMyKeySet() {
117
            Project project = ProjectManager.getInstance().getCurrentProject();
118
            final List<Document> docs = project.getDocuments(TableManager.TYPENAME);
119 44262 jjdelcerro
            if( docs==null ) {
120
                return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
121
            }
122 44304 jjdelcerro
            UnmodifiableBasicSet<String> x = new UnmodifiableBasicSet<String>() {
123 44262 jjdelcerro
                @Override
124 44304 jjdelcerro
                public boolean isEmpty() {
125
                    return docs.isEmpty();
126 44262 jjdelcerro
                }
127
128 44304 jjdelcerro
                @Override
129
                public int size() {
130
                    return docs.size();
131
                }
132 44262 jjdelcerro
133 44304 jjdelcerro
                @Override
134
                public Iterator<String> iterator() {
135 45543 jjdelcerro
                    return new MappedIterator<>(docs.iterator(), (Document t) -> {
136
                        if( t==null ) {
137
                            return null;
138 44304 jjdelcerro
                        }
139 45543 jjdelcerro
                        return ((TableDocument)t).getName();
140 44304 jjdelcerro
                    });
141
                }
142
            };
143
            return x;
144 44262 jjdelcerro
        }
145
146 44304 jjdelcerro
147 44262 jjdelcerro
        @Override
148
        public Iterator<DataStoreParameters> iterator() {
149
            Project project = ProjectManager.getInstance().getCurrentProject();
150
            List<Document> docs = project.getDocuments(TableManager.TYPENAME);
151
            if( docs==null ) {
152
                return Collections.EMPTY_LIST.iterator();
153
            }
154 45543 jjdelcerro
            return new MappedIterator<>(docs.iterator(), (Document t) -> {
155
                if( t==null || ((TableDocument)t).getFeatureStore()==null ) {
156
                    return null;
157 44262 jjdelcerro
                }
158 45543 jjdelcerro
                return ((TableDocument)t).getFeatureStore().getParameters();
159 44262 jjdelcerro
            });
160
        }
161 44253 jjdelcerro
162 44262 jjdelcerro
163 44253 jjdelcerro
    }
164
165 40435 jjdelcerro
    public static final String PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME =
166
        "TableDocument";
167
    public static final String PERSISTENCE_TABLELINK_DEFINITION_NAME =
168
        "TableLink";
169
170
    public static String TYPENAME = "project.document.table";
171
172
    private DynStruct persistenceDefinition = null;
173
174 45543 jjdelcerro
    @Override
175 40435 jjdelcerro
    public ImageIcon getIcon() {
176
            return IconThemeHelper.getImageIcon("document-table-icon");
177
    }
178
179 45543 jjdelcerro
    @Override
180 40435 jjdelcerro
    public ImageIcon getIconSelected() {
181
        return IconThemeHelper.getImageIcon("document-table-icon-sel");
182
    }
183
184 45543 jjdelcerro
    @Override
185 40435 jjdelcerro
    public String getTitle() {
186
        return PluginServices.getText(this, "Tabla");
187
    }
188
189 45543 jjdelcerro
    @Override
190 40435 jjdelcerro
    public String getTypeName() {
191
        return TYPENAME;
192
    }
193
194 45543 jjdelcerro
    @Override
195 40435 jjdelcerro
    public int getPriority() {
196
        return 1;
197
    }
198
199 45543 jjdelcerro
    @Override
200 40435 jjdelcerro
    public Iterator<? extends Document> createDocumentsByUser() {
201 45931 jjdelcerro
        return this.createDocumentsByUser(null);
202
    }
203
204
    @Override
205
    public Iterator<? extends Document> createDocumentsByUser(Invocable whenDocumentsLoaded) {
206 40435 jjdelcerro
        AddLayerDialog fopen = null;
207
        try {
208
            fopen =
209
                new AddLayerDialog(PluginServices.getText(this, "Nueva_tabla"));
210
            List<WizardPanel> wizards =
211
                ApplicationLocator.getManager().getWizardPanels();
212
            WizardPanel panel;
213
            Iterator<WizardPanel> iter = wizards.iterator();
214
            while (iter.hasNext()) {
215
                panel = iter.next();
216 44584 jjdelcerro
                panel.initWizard();
217 40435 jjdelcerro
                fopen.addWizardTab(panel.getTabName(), panel);
218
            }
219
            PluginServices.getMDIManager().addWindow(fopen);
220
            if (fopen.isAccepted()) {
221 45931 jjdelcerro
                WizardPanel selectedPanel = (WizardPanel) fopen.getSelectedTab();
222
                if( whenDocumentsLoaded==null ) {
223
                    List<TableDocument> docs = (List) selectedPanel.executeWizard();
224
                    this.checkDocuments(docs);
225
                    return docs.iterator();
226 44437 jjdelcerro
                }
227 45931 jjdelcerro
                Thread task = new Thread(() -> {
228
                    List<TableDocument> docs = (List) selectedPanel.executeWizard();
229
                    checkDocuments(docs);
230
                    whenDocumentsLoaded.call(docs.iterator());
231
                }, "DocumentTableLoader");
232
                task.start();
233
                return Collections.EMPTY_LIST.iterator();
234 40435 jjdelcerro
            }
235
        } catch (Exception e) {
236
            NotificationManager.addError(e);
237
        } finally {
238
            if (fopen != null) {
239
                fopen.dispose();
240
                fopen = null;
241
            }
242
        }
243
        return null;
244
    }
245 45931 jjdelcerro
246
    private void checkDocuments(List<TableDocument> docs) {
247
        if( docs==null ) {
248
            return;
249
        }
250
        for (TableDocument doc : docs) {
251
            try {
252
                FeatureStore store = ((TableDocument)doc).getDataStore();
253
                FeatureType type = store.getDefaultFeatureType();
254
                if( ! type.supportReferences() ) {
255
                    I18nManager i18n = ToolsLocator.getI18nManager();
256
                    ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
257
                    dialogs.messageDialog(
258
                            "\""+ doc.getName() + "\"\n"+
259
                            i18n.getTranslation("_The_table_has_no_primary_key_or_OID") +"\n" +
260
                                   i18n.getTranslation("_Many_features_selection_deletion_modification_will_not_be_available_as_they_require_it_for_proper_operation"),
261
                            null,
262
                            i18n.getTranslation("_Warning"),
263
                            JOptionPane.WARNING_MESSAGE,
264
                            "TableDoNotSupportReferences"
265
                   );
266
                }
267
            } catch (Exception ex) {
268 40435 jjdelcerro
269 45931 jjdelcerro
            }
270
        }
271
    }
272
273 45543 jjdelcerro
    @Override
274 40435 jjdelcerro
    public AbstractDocument createDocumentByUser() {
275
        return (AbstractDocument) createDocumentsByUser().next();
276
    }
277 46050 omartinez
278 40435 jjdelcerro
    /**
279
     * Registers in the points of extension the Factory with alias.
280
     *
281
     */
282
    public static void register() {
283 44439 jjdelcerro
        I18nManager i18n = ToolsLocator.getI18nManager();
284
285 40435 jjdelcerro
        TableManager factory = new TableManager();
286
        // A?adimos nuestra extension para el tratamiento de la apertura de
287
        // ficheros
288
        // dentro de gvSIG
289
        ToolsLocator.getExtensionPointManager().add("FileTableOpenDialog", "")
290
            .append("FileOpenTable", "", FilesystemExplorerWizardPanel.class);
291
292
                IconThemeHelper.registerIcon("document", "document-table-icon", TableManager.class);
293
        IconThemeHelper.registerIcon("document", "document-table-icon-sel", TableManager.class);
294
        IconThemeHelper.registerIcon("document", "document-table-icon-small", TableManager.class);
295
296
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
297
        manager.registerFactory(factory);
298
299
        if (factory.persistenceDefinition == null) {
300
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
301
            factory.persistenceDefinition =
302
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
303
                    PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME,
304
                    "Table document Persistence definition");
305
            factory.persistenceDefinition.extend(manager
306
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
307
308
            factory.persistenceDefinition.addDynFieldObject("store")
309
                .setClassOfValue(FeatureStore.class).setMandatory(true);
310
            factory.persistenceDefinition.addDynFieldString("featureTypeId")
311
                .setMandatory(false);
312
            factory.persistenceDefinition.addDynFieldArray("attributeNames")
313
                .setClassOfItems(String.class).setMandatory(false);
314
            factory.persistenceDefinition.addDynFieldObject("associatedLayer")
315
                .setClassOfValue(FLyrVect.class).setMandatory(false);
316
            factory.persistenceDefinition.addDynFieldObject("query")
317
                .setClassOfValue(FeatureQuery.class).setMandatory(false);
318
            factory.persistenceDefinition.addDynFieldObject("baseFilter")
319
                .setClassOfValue(Evaluator.class).setMandatory(false);
320
            factory.persistenceDefinition.addDynFieldObject("baseOrder")
321
                .setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
322
            factory.persistenceDefinition.addDynFieldList("linkTable")
323
                .setClassOfItems(TableLink.class).setMandatory(false);
324 43354 jjdelcerro
            factory.persistenceDefinition.addDynFieldMap("patterns")
325
                .setClassOfItems(String.class).setMandatory(false);
326 40435 jjdelcerro
        }
327
328
329
        //Register also the TableLink
330
        if (manager.getDefinition(PERSISTENCE_TABLELINK_DEFINITION_NAME) == null){
331
            DynStruct tableLinkDefinition =
332
                manager.addDefinition(TableLink.class,
333
                    PERSISTENCE_TABLELINK_DEFINITION_NAME,
334
                    "TableLink Persistence definition", null, null);
335
336
            tableLinkDefinition.addDynFieldObject("source")
337
                .setClassOfValue(TableDocument.class).setMandatory(true);
338
            tableLinkDefinition.addDynFieldObject("target")
339
                .setClassOfValue(TableDocument.class).setMandatory(true);
340
            tableLinkDefinition.addDynFieldString("fieldSource").setMandatory(true);
341
            tableLinkDefinition.addDynFieldString("fieldTarget").setMandatory(true);
342
            tableLinkDefinition.addDynFieldBoolean("enabled").setMandatory(true);
343
        }
344
345
        ProjectManager.getInstance().registerDocumentFactory(factory);
346 42096 cmartinez
347
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
348
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
349
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
350
351 44253 jjdelcerro
        DataManager dataManager = DALLocator.getDataManager();
352 44439 jjdelcerro
        dataManager.getStoresRepository().addRepository(
353
                new TableDocumentStoresRepository(
354
                    "PROJECT_TABLES",
355
                    i18n.getTranslation("_Project_tables")
356
            )
357
        );
358 40435 jjdelcerro
    }
359
360
    /**
361
     * Create a new table document.
362
     *
363
     * @return TableDocument.
364
     */
365 44304 jjdelcerro
    @Override
366 40435 jjdelcerro
    public AbstractDocument createDocument() {
367 41636 jjdelcerro
        AbstractDocument doc = new TableDocument(this);
368
        if( this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT,doc).isCanceled() ) {
369
            return null;
370
        }
371
            return doc;
372 40435 jjdelcerro
    }
373
374 45543 jjdelcerro
    @Override
375 40435 jjdelcerro
    public Class<? extends IDocumentWindow> getMainWindowClass() {
376
        return FeatureTableDocumentPanel.class;
377
    }
378
379 42873 jjdelcerro
    @Override
380 40435 jjdelcerro
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
381 42873 jjdelcerro
       IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
382
        if (win == null) {
383
            win = (IDocumentWindow) this.createDocumentWindow(doc);
384
            if (layout != null && win != null) {
385
                win.setWindowLayout(layout);
386
            }
387 40435 jjdelcerro
        }
388 41636 jjdelcerro
        if( this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW,win).isCanceled() ) {
389
            return null;
390
        }
391
        return win;
392 40435 jjdelcerro
    }
393
394 42873 jjdelcerro
    @Override
395 40435 jjdelcerro
    public IWindow getPropertiesWindow(Document doc) {
396 42873 jjdelcerro
        IWindow win = super.getPropertiesWindow(doc);
397
        if( win == null ) {
398
            win = new TableProperties((TableDocument) doc);
399
        }
400 41636 jjdelcerro
        if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) {
401
            return null;
402
        }
403
        return win;
404 40435 jjdelcerro
    }
405
406 45543 jjdelcerro
    @Override
407 40435 jjdelcerro
    protected Class<? extends Document> getDocumentClass() {
408
        return TableDocument.class;
409
    }
410
411 45543 jjdelcerro
    @Override
412 40435 jjdelcerro
    public DynStruct getDefinition(String className) {
413
414
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
415
            return this.persistenceDefinition;
416
        }
417
        if (this.persistenceDefinition.getFullName()
418
            .equalsIgnoreCase(className)) {
419
            return this.persistenceDefinition;
420
        }
421
        if (this.getDocumentClass().getName().equals(className)) {
422
            return this.persistenceDefinition;
423
        }
424
        return null;
425
    }
426
427 45543 jjdelcerro
    @Override
428 40435 jjdelcerro
    public boolean manages(Object object) {
429
        return object instanceof TableDocument;
430
    }
431
432
    public TableDocument getTableDocument(VectorLayer layer) {
433
        if (layer == null) {
434
            return null;
435
        }
436
        List<Document> tableDocs =
437
            getProject().getDocuments(TableManager.TYPENAME);
438
        for (Document document : tableDocs) {
439
            if (layer == ((TableDocument) document).getAssociatedLayer()) {
440
                return (TableDocument) document;
441
            }
442
        }
443
        return null;
444
    }
445
446
    public void removeTableDocument(VectorLayer layer) {
447
        TableDocument doc = getTableDocument(layer);
448
        // Only remove it if it exists
449
        if (doc != null) {
450
            PluginServices.getMDIManager().closeSingletonWindow(doc);
451
            getProject().remove(doc);
452
        }
453
    }
454
455
    private Project getProject() {
456
        return ProjectManager.getInstance().getCurrentProject();
457
    }
458
}