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 @ 44439

History | View | Annotate | Download (17.1 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

    
25
package org.gvsig.app.project.documents.table;
26

    
27
import java.util.Collections;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.function.Function;
31

    
32
import javax.swing.ImageIcon;
33
import javax.swing.JOptionPane;
34

    
35
import org.gvsig.andami.IconThemeHelper;
36
import org.gvsig.andami.PluginServices;
37
import org.gvsig.andami.messages.NotificationManager;
38
import org.gvsig.andami.ui.mdiManager.IWindow;
39
import org.gvsig.app.ApplicationLocator;
40
import org.gvsig.app.addlayer.AddLayerDialog;
41
import org.gvsig.app.gui.WizardPanel;
42
import org.gvsig.app.project.Project;
43
import org.gvsig.app.project.ProjectManager;
44
import org.gvsig.app.project.documents.AbstractDocument;
45
import org.gvsig.app.project.documents.AbstractDocumentManager;
46
import org.gvsig.app.project.documents.Document;
47
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
48
import org.gvsig.app.project.documents.actions.CutDocumentAction;
49
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
50
import org.gvsig.app.project.documents.gui.IDocumentWindow;
51
import org.gvsig.app.project.documents.gui.WindowLayout;
52
import org.gvsig.app.project.documents.table.TableDocument.TableLink;
53
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
54
import org.gvsig.app.project.documents.table.gui.TableProperties;
55
import org.gvsig.fmap.dal.AbstractStoresRepository;
56
import org.gvsig.fmap.dal.DALLocator;
57
import org.gvsig.fmap.dal.DataManager;
58
import org.gvsig.fmap.dal.DataStoreParameters;
59
import org.gvsig.fmap.dal.feature.FeatureQuery;
60
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
61
import org.gvsig.fmap.dal.feature.FeatureStore;
62
import org.gvsig.fmap.dal.feature.FeatureType;
63
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
64
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
65
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
66
import org.gvsig.tools.ToolsLocator;
67
import org.gvsig.tools.dynobject.DynObjectManager;
68
import org.gvsig.tools.dynobject.DynStruct;
69
import org.gvsig.tools.evaluator.Evaluator;
70
import org.gvsig.tools.i18n.I18nManager;
71
import org.gvsig.tools.persistence.PersistenceManager;
72
import org.gvsig.tools.swing.api.ToolsSwingLocator;
73
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
74
import org.gvsig.tools.util.MappedIterator;
75
import org.gvsig.tools.util.UnmodifiableBasicSet;
76

    
77
/**
78
 * Factory of Table.
79
 * 
80
 * @author 2005- Vicente Caballero
81
 * @author 2009- Joaquin del Cerro
82
 * 
83
 */
84
public class TableManager extends AbstractDocumentManager {
85
    
86
    private static class TableDocumentStoresRepository extends AbstractStoresRepository {
87

    
88
        public TableDocumentStoresRepository(String name, String label) {
89
            super(name, label);
90
        }
91

    
92
        @Override
93
        protected DataStoreParameters getMyParameters(String name) {
94
            Project project = ProjectManager.getInstance().getCurrentProject();
95
            TableDocument tableDoc = (TableDocument) project.getDocument(name, TYPENAME);
96
            if( tableDoc==null ) {
97
                return null;
98
            }
99
            return tableDoc.getFeatureStore().getParameters();
100
        }
101

    
102
        @Override
103
        protected boolean isEmptyMyRepository() {
104
            Project project = ProjectManager.getInstance().getCurrentProject();
105
            return project.getDocuments(TableManager.TYPENAME).isEmpty();
106
        }
107

    
108
        @Override
109
        protected int getMySize() {
110
            Project project = ProjectManager.getInstance().getCurrentProject();
111
            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
            if( docs==null ) {
120
                return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
121
            }
122
            UnmodifiableBasicSet<String> x = new UnmodifiableBasicSet<String>() {
123
                @Override
124
                public boolean isEmpty() {
125
                    return docs.isEmpty();
126
                }
127

    
128
                @Override
129
                public int size() {
130
                    return docs.size();
131
                }
132

    
133
                @Override
134
                public Iterator<String> iterator() {
135
                    return new MappedIterator<>(docs.iterator(), new Function<Document, String>() {
136
                        @Override
137
                        public String apply(Document t) {
138
                            return ((TableDocument)t).getName();
139
                        }
140
                    });
141
                }
142
            };
143
            return x;
144
        }
145

    
146
        
147
        @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
            return new MappedIterator<>(docs.iterator(), new Function<Document, DataStoreParameters>() {
155
                @Override
156
                public DataStoreParameters apply(Document t) {
157
                    return ((TableDocument)t).getFeatureStore().getParameters();
158
                }
159
            });
160
        }
161
        
162
        
163
    }
164
    
165
    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
    public ImageIcon getIcon() {
175
            return IconThemeHelper.getImageIcon("document-table-icon");
176
    }
177

    
178
    public ImageIcon getIconSelected() {
179
        return IconThemeHelper.getImageIcon("document-table-icon-sel");
180
    }
181

    
182
    public String getTitle() {
183
        return PluginServices.getText(this, "Tabla");
184
    }
185

    
186
    public String getTypeName() {
187
        return TYPENAME;
188
    }
189

    
190
    public int getPriority() {
191
        return 1;
192
    }
193

    
194
    public Iterator<? extends Document> createDocumentsByUser() {
195
        AddLayerDialog fopen = null;
196
        try {
197
            fopen =
198
                new AddLayerDialog(PluginServices.getText(this, "Nueva_tabla"));
199
            List<WizardPanel> wizards =
200
                ApplicationLocator.getManager().getWizardPanels();
201
            WizardPanel panel;
202
            Iterator<WizardPanel> iter = wizards.iterator();
203
            while (iter.hasNext()) {
204
                panel = iter.next();
205
                fopen.addWizardTab(panel.getTabName(), panel);
206
                panel.initWizard();
207
            }
208
            PluginServices.getMDIManager().addWindow(fopen);
209
            if (fopen.isAccepted()) {
210
                panel = (WizardPanel) fopen.getSelectedTab();
211
                @SuppressWarnings("unchecked")
212
                List<TableDocument> docs =
213
                    (List<TableDocument>) panel.executeWizard();
214
                for (TableDocument doc : docs) {
215
                    try {
216
                        FeatureStore store = ((TableDocument)doc).getDataStore();
217
                        FeatureType type = store.getDefaultFeatureType();
218
                        if( ! type.supportReferences() ) {
219
                            I18nManager i18n = ToolsLocator.getI18nManager();
220
                            ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
221
                            dialogs.messageDialog(
222
                                    "\""+ doc.getName() + "\"\n"+
223
                                    i18n.getTranslation("_The_table_has_no_primary_key_or_OID") +"\n" +
224
                                           i18n.getTranslation("_Many_features_selection_deletion_modification_will_not_be_available_as_they_require_it_for_proper_operation"),
225
                                    null, 
226
                                    i18n.getTranslation("_Warning"),
227
                                    JOptionPane.WARNING_MESSAGE, 
228
                                    "TableDoNotSupportReferences"
229
                           );
230
                        }
231
                    } catch (Exception ex) {
232

    
233
                    }
234
                }
235
                return docs.iterator();
236
            }
237
        } catch (Exception e) {
238
            NotificationManager.addError(e);
239
        } finally {
240
            if (fopen != null) {
241
                fopen.dispose();
242
                fopen = null;
243
            }
244
        }
245
        return null;
246
    }
247

    
248
    public AbstractDocument createDocumentByUser() {
249
        return (AbstractDocument) createDocumentsByUser().next();
250
    }
251

    
252
    /**
253
     * Registers in the points of extension the Factory with alias.
254
     * 
255
     */
256
    public static void register() {
257
        I18nManager i18n = ToolsLocator.getI18nManager();
258
        
259
        TableManager factory = new TableManager();
260
        // A?adimos nuestra extension para el tratamiento de la apertura de
261
        // ficheros
262
        // dentro de gvSIG
263
        ToolsLocator.getExtensionPointManager().add("FileTableOpenDialog", "")
264
            .append("FileOpenTable", "", FilesystemExplorerWizardPanel.class);
265

    
266
                IconThemeHelper.registerIcon("document", "document-table-icon", TableManager.class);
267
        IconThemeHelper.registerIcon("document", "document-table-icon-sel", TableManager.class);
268
        IconThemeHelper.registerIcon("document", "document-table-icon-small", TableManager.class);
269

    
270
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
271
        manager.registerFactory(factory);
272

    
273
        if (factory.persistenceDefinition == null) {
274
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
275
            factory.persistenceDefinition =
276
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
277
                    PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME,
278
                    "Table document Persistence definition");
279
            factory.persistenceDefinition.extend(manager
280
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
281

    
282
            factory.persistenceDefinition.addDynFieldObject("store")
283
                .setClassOfValue(FeatureStore.class).setMandatory(true);
284
            factory.persistenceDefinition.addDynFieldString("featureTypeId")
285
                .setMandatory(false);
286
            factory.persistenceDefinition.addDynFieldArray("attributeNames")
287
                .setClassOfItems(String.class).setMandatory(false);
288
            factory.persistenceDefinition.addDynFieldObject("associatedLayer")
289
                .setClassOfValue(FLyrVect.class).setMandatory(false);
290
            factory.persistenceDefinition.addDynFieldObject("query")
291
                .setClassOfValue(FeatureQuery.class).setMandatory(false);
292
            factory.persistenceDefinition.addDynFieldObject("baseFilter")
293
                .setClassOfValue(Evaluator.class).setMandatory(false);
294
            factory.persistenceDefinition.addDynFieldObject("baseOrder")
295
                .setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
296
            factory.persistenceDefinition.addDynFieldList("linkTable")
297
                .setClassOfItems(TableLink.class).setMandatory(false);          
298
            factory.persistenceDefinition.addDynFieldMap("patterns")
299
                .setClassOfItems(String.class).setMandatory(false);          
300
        }
301
        
302
        
303
        //Register also the TableLink
304
        if (manager.getDefinition(PERSISTENCE_TABLELINK_DEFINITION_NAME) == null){
305
            DynStruct tableLinkDefinition =
306
                manager.addDefinition(TableLink.class,
307
                    PERSISTENCE_TABLELINK_DEFINITION_NAME,
308
                    "TableLink Persistence definition", null, null);                 
309
            
310
            tableLinkDefinition.addDynFieldObject("source")
311
                .setClassOfValue(TableDocument.class).setMandatory(true);
312
            tableLinkDefinition.addDynFieldObject("target")
313
                .setClassOfValue(TableDocument.class).setMandatory(true);
314
            tableLinkDefinition.addDynFieldString("fieldSource").setMandatory(true);
315
            tableLinkDefinition.addDynFieldString("fieldTarget").setMandatory(true);
316
            tableLinkDefinition.addDynFieldBoolean("enabled").setMandatory(true);
317
        }  
318
    
319
        ProjectManager.getInstance().registerDocumentFactory(factory);
320
        
321
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CopyDocumentAction());
322
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new CutDocumentAction());
323
        ProjectManager.getInstance().registerDocumentAction(TYPENAME,new PasteDocumentAction());
324

    
325
        DataManager dataManager = DALLocator.getDataManager();
326
        dataManager.getStoresRepository().addRepository(
327
                new TableDocumentStoresRepository(
328
                    "PROJECT_TABLES",
329
                    i18n.getTranslation("_Project_tables")
330
            )
331
        );
332
    }
333

    
334
    /**
335
     * Create a new table document.
336
     * 
337
     * @return TableDocument.
338
     */
339
    @Override
340
    public AbstractDocument createDocument() {
341
        AbstractDocument doc = new TableDocument(this);
342
        if( this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT,doc).isCanceled() ) {
343
            return null;
344
        }
345
            return doc;        
346
    }
347

    
348
    public Class<? extends IDocumentWindow> getMainWindowClass() {
349
        return FeatureTableDocumentPanel.class;
350
    }
351

    
352
    @Override
353
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
354
       IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
355
        if (win == null) {
356
            win = (IDocumentWindow) this.createDocumentWindow(doc);
357
            if (layout != null && win != null) {
358
                win.setWindowLayout(layout);
359
            }
360
        }
361
        if( this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW,win).isCanceled() ) {
362
            return null;
363
        }
364
        return win;
365
    }
366

    
367
    @Override
368
    public IWindow getPropertiesWindow(Document doc) {
369
        IWindow win = super.getPropertiesWindow(doc);
370
        if( win == null ) {
371
            win = new TableProperties((TableDocument) doc);
372
        }
373
        if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) {
374
            return null;
375
        }
376
        return win;        
377
    }
378

    
379
    protected Class<? extends Document> getDocumentClass() {
380
        return TableDocument.class;
381
    }
382

    
383
    public DynStruct getDefinition(String className) {
384

    
385
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
386
            return this.persistenceDefinition;
387
        }
388
        if (this.persistenceDefinition.getFullName()
389
            .equalsIgnoreCase(className)) {
390
            return this.persistenceDefinition;
391
        }
392
        if (this.getDocumentClass().getName().equals(className)) {
393
            return this.persistenceDefinition;
394
        }
395
        return null;
396
    }
397

    
398
    public boolean manages(Object object) {
399
        return object instanceof TableDocument;
400
    }
401

    
402
    public TableDocument getTableDocument(VectorLayer layer) {
403
        if (layer == null) {
404
            return null;
405
        }
406
        List<Document> tableDocs =
407
            getProject().getDocuments(TableManager.TYPENAME);
408
        for (Document document : tableDocs) {
409
            if (layer == ((TableDocument) document).getAssociatedLayer()) {
410
                return (TableDocument) document;
411
            }
412
        }
413
        return null;
414
    }
415

    
416
    public void removeTableDocument(VectorLayer layer) {
417
        TableDocument doc = getTableDocument(layer);
418
        // Only remove it if it exists
419
        if (doc != null) {
420
            PluginServices.getMDIManager().closeSingletonWindow(doc);
421
            getProject().remove(doc);
422
        }
423
    }
424

    
425
    private Project getProject() {
426
        return ProjectManager.getInstance().getCurrentProject();
427
    }
428
}