Statistics
| Revision:

root / tags / v2_0_0_Build_2049 / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / TableManager.java @ 38497

History | View | Annotate | Download (12.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2004-2009 IVER TI
26
 *   
27
 */
28

    
29
package org.gvsig.app.project.documents.table;
30

    
31
import java.util.Iterator;
32
import java.util.List;
33

    
34
import javax.swing.ImageIcon;
35

    
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.extension.AddLayer;
42
import org.gvsig.app.gui.WizardPanel;
43
import org.gvsig.app.project.Project;
44
import org.gvsig.app.project.ProjectManager;
45
import org.gvsig.app.project.documents.AbstractDocument;
46
import org.gvsig.app.project.documents.AbstractDocumentManager;
47
import org.gvsig.app.project.documents.Document;
48
import org.gvsig.app.project.documents.gui.IDocumentWindow;
49
import org.gvsig.app.project.documents.gui.WindowLayout;
50
import org.gvsig.app.project.documents.table.TableDocument.TableLink;
51
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
52
import org.gvsig.app.project.documents.table.gui.TableProperties;
53
import org.gvsig.fmap.dal.feature.FeatureQuery;
54
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
57
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
59
import org.gvsig.tools.ToolsLocator;
60
import org.gvsig.tools.dynobject.DynObjectManager;
61
import org.gvsig.tools.dynobject.DynStruct;
62
import org.gvsig.tools.evaluator.Evaluator;
63
import org.gvsig.tools.persistence.PersistenceManager;
64

    
65
/**
66
 * Factory of Table.
67
 * 
68
 * @author 2005- Vicente Caballero
69
 * @author 2009- Joaquin del Cerro
70
 * 
71
 */
72
public class TableManager extends AbstractDocumentManager {
73

    
74
    public static final String PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME =
75
        "TableDocument";
76
    public static final String PERSISTENCE_TABLELINK_DEFINITION_NAME =
77
        "TableLink";
78

    
79
    public static String TYPENAME = "project.document.table";
80

    
81
    private DynStruct persistenceDefinition = null;
82

    
83
    public ImageIcon getIcon() {
84
        return PluginServices.getIconTheme().get("document-table-icon");
85
    }
86

    
87
    public ImageIcon getIconSelected() {
88
        return PluginServices.getIconTheme().get("document-table-icon-sel");
89
    }
90

    
91
    public String getTitle() {
92
        return PluginServices.getText(this, "Tabla");
93
    }
94

    
95
    public String getTypeName() {
96
        return TYPENAME;
97
    }
98

    
99
    public int getPriority() {
100
        return 1;
101
    }
102

    
103
    public Iterator<? extends Document> createDocumentsByUser() {
104
        AddLayerDialog fopen = null;
105
        try {
106
            fopen =
107
                new AddLayerDialog(PluginServices.getText(this, "Nueva_tabla"));
108
            List<WizardPanel> wizards =
109
                ApplicationLocator.getManager().getWizardPanels();
110
            WizardPanel panel;
111
            Iterator<WizardPanel> iter = wizards.iterator();
112
            while (iter.hasNext()) {
113
                panel = iter.next();
114
                fopen.addWizardTab(panel.getTabName(), panel);
115
                panel.initWizard();
116
            }
117
            PluginServices.getMDIManager().addWindow(fopen);
118
            if (fopen.isAccepted()) {
119
                panel = (WizardPanel) fopen.getSelectedTab();
120
                @SuppressWarnings("unchecked")
121
                List<TableDocument> docs =
122
                    (List<TableDocument>) panel.executeWizard();
123
                return docs.iterator();
124
            }
125
        } catch (Exception e) {
126
            NotificationManager.addError(e);
127
        } finally {
128
            if (fopen != null) {
129
                fopen.dispose();
130
                fopen = null;
131
            }
132
        }
133
        return null;
134
    }
135

    
136
    public AbstractDocument createDocumentByUser() {
137
        return (AbstractDocument) createDocumentsByUser().next();
138
    }
139

    
140
    /**
141
     * Registers in the points of extension the Factory with alias.
142
     * 
143
     */
144
    public static void register() {
145

    
146
        TableManager factory = new TableManager();
147
        // A?adimos nuestra extension para el tratamiento de la apertura de
148
        // ficheros
149
        // dentro de gvSIG
150
        ToolsLocator.getExtensionPointManager().add("FileTableOpenDialog", "")
151
            .append("FileOpenTable", "", FilesystemExplorerWizardPanel.class);
152

    
153
        ProjectManager.getInstance().registerDocumentFactory(factory);
154

    
155
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"copy",new
156
        // CopyDocumentContextMenuAction());
157
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"cut",new
158
        // CutDocumentContextMenuAction());
159
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"paste",new
160
        // PasteDocumentContextMenuAction());
161

    
162
        PluginServices.getIconTheme().registerDefault("document-table-icon",
163
            AddLayer.class.getClassLoader().getResource("images/tablas.png"));//
164

    
165
        PluginServices.getIconTheme().registerDefault(
166
            "document-table-icon-sel",
167
            AddLayer.class.getClassLoader()
168
                .getResource("images/tablas_sel.png"));
169

    
170
        PluginServices.getIconTheme().registerDefault("edit-copy",
171
            AddLayer.class.getClassLoader().getResource("images/editcopy.png"));//
172
        PluginServices.getIconTheme().registerDefault("edit-cut",
173
            AddLayer.class.getClassLoader().getResource("images/editcut.png"));
174
        PluginServices.getIconTheme()
175
            .registerDefault(
176
                "edit-paste",
177
                AddLayer.class.getClassLoader().getResource(
178
                    "images/editpaste.png"));
179
        PluginServices.getIconTheme().registerDefault(
180
            "edit-delete",
181
            AddLayer.class.getClassLoader()
182
                .getResource("images/editdelete.png"));
183

    
184
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
185
        manager.registerFactory(factory);
186

    
187
        if (factory.persistenceDefinition == null) {
188
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
189
            factory.persistenceDefinition =
190
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
191
                    PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME,
192
                    "Table document Persistence definition");
193
            factory.persistenceDefinition.extend(manager
194
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
195

    
196
            factory.persistenceDefinition.addDynFieldObject("store")
197
                .setClassOfValue(FeatureStore.class).setMandatory(true);
198
            factory.persistenceDefinition.addDynFieldString("featureTypeId")
199
                .setMandatory(false);
200
            factory.persistenceDefinition.addDynFieldArray("attributeNames")
201
                .setClassOfItems(String.class).setMandatory(false);
202
            factory.persistenceDefinition.addDynFieldObject("associatedLayer")
203
                .setClassOfValue(FLyrVect.class).setMandatory(false);
204
            factory.persistenceDefinition.addDynFieldObject("query")
205
                .setClassOfValue(FeatureQuery.class).setMandatory(false);
206
            factory.persistenceDefinition.addDynFieldObject("baseFilter")
207
                .setClassOfValue(Evaluator.class).setMandatory(false);
208
            factory.persistenceDefinition.addDynFieldObject("baseOrder")
209
                .setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
210
            factory.persistenceDefinition.addDynFieldList("linkTable")
211
                .setClassOfItems(TableLink.class).setMandatory(false);          
212
        }
213
        
214
        
215
        //Register also the TableLink
216
        if (manager.getDefinition(PERSISTENCE_TABLELINK_DEFINITION_NAME) == null){
217
            DynStruct tableLinkDefinition =
218
                manager.addDefinition(TableLink.class,
219
                    PERSISTENCE_TABLELINK_DEFINITION_NAME,
220
                    "TableLink Persistence definition", null, null);                 
221
            
222
            tableLinkDefinition.addDynFieldObject("source")
223
                .setClassOfValue(TableDocument.class).setMandatory(true);
224
            tableLinkDefinition.addDynFieldObject("target")
225
                .setClassOfValue(TableDocument.class).setMandatory(true);
226
            tableLinkDefinition.addDynFieldString("fieldSource").setMandatory(true);
227
            tableLinkDefinition.addDynFieldString("fieldTarget").setMandatory(true);
228
            tableLinkDefinition.addDynFieldBoolean("enabled").setMandatory(true);
229
        }  
230
    }
231

    
232
    /**
233
     * Create a new table document.
234
     * 
235
     * @return TableDocument.
236
     */
237
    public AbstractDocument createDocument() {
238
        return new TableDocument(this);
239
    }
240

    
241
    public Class<? extends IDocumentWindow> getMainWindowClass() {
242
        return FeatureTableDocumentPanel.class;
243
    }
244

    
245
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
246
        IDocumentWindow tablePanel;
247
        tablePanel =
248
            (IDocumentWindow) PluginServices.getMDIManager()
249
                .getSingletonWindow(getMainWindowClass(), doc);
250
        if (tablePanel != null) {
251
            // The table window document is already created, return it.
252
            return tablePanel;
253
        }
254

    
255
        tablePanel = (IDocumentWindow) this.createDocumentWindow(doc);
256
        if (layout != null && tablePanel != null) {
257
            tablePanel.setWindowLayout(layout);
258
        }
259
        return tablePanel;
260
    }
261

    
262
    public IWindow getPropertiesWindow(Document doc) {
263

    
264
        // IWindow mainwin =
265
        // PluginServices.getMDIManager().getSingletonWindow(FeatureTableDocumentPanel.class,
266
        // doc);
267
        return new TableProperties((TableDocument) doc);
268
    }
269

    
270
    protected Class<? extends Document> getDocumentClass() {
271
        return TableDocument.class;
272
    }
273

    
274
    public DynStruct getDefinition(String className) {
275

    
276
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
277
            return this.persistenceDefinition;
278
        }
279
        if (this.persistenceDefinition.getFullName()
280
            .equalsIgnoreCase(className)) {
281
            return this.persistenceDefinition;
282
        }
283
        if (this.getDocumentClass().getName().equals(className)) {
284
            return this.persistenceDefinition;
285
        }
286
        return null;
287
    }
288

    
289
    public boolean manages(Object object) {
290
        return object instanceof TableDocument;
291
    }
292

    
293
    public TableDocument getTableDocument(VectorLayer layer) {
294
        if (layer == null) {
295
            return null;
296
        }
297
        List<Document> tableDocs =
298
            getProject().getDocuments(TableManager.TYPENAME);
299
        for (Document document : tableDocs) {
300
            if (layer == ((TableDocument) document).getAssociatedLayer()) {
301
                return (TableDocument) document;
302
            }
303
        }
304
        return null;
305
    }
306

    
307
    public void removeTableDocument(VectorLayer layer) {
308
        TableDocument doc = getTableDocument(layer);
309
        // Only remove it if it exists
310
        if (doc != null) {
311
            PluginServices.getMDIManager().closeSingletonWindow(doc);
312
            getProject().remove(doc);
313
        }
314
    }
315

    
316
    private Project getProject() {
317
        return ProjectManager.getInstance().getCurrentProject();
318
    }
319
}