Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / 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 @ 36630

History | View | Annotate | Download (11.1 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.tools.ToolsLocator;
59
import org.gvsig.tools.dynobject.DynObjectManager;
60
import org.gvsig.tools.dynobject.DynStruct;
61
import org.gvsig.tools.evaluator.Evaluator;
62
import org.gvsig.tools.persistence.PersistenceManager;
63

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

    
73
    public static final String PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME =
74
        "TableDocument";
75

    
76
    public static String TYPENAME = "project.document.table";
77

    
78
    private DynStruct persistenceDefinition = null;
79

    
80
    public ImageIcon getIcon() {
81
        return PluginServices.getIconTheme().get("document-table-icon");
82
    }
83

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

    
88
    public String getTitle() {
89
        return PluginServices.getText(this, "Tabla");
90
    }
91

    
92
    public String getTypeName() {
93
        return TYPENAME;
94
    }
95

    
96
    public int getPriority() {
97
        return 1;
98
    }
99

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

    
133
    public AbstractDocument createDocumentByUser() {
134
        return (AbstractDocument) createDocumentsByUser().next();
135
    }
136

    
137
    /**
138
     * Registers in the points of extension the Factory with alias.
139
     * 
140
     */
141
    public static void register() {
142

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

    
150
        ProjectManager.getInstance().registerDocumentFactory(factory);
151

    
152
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"copy",new
153
        // CopyDocumentContextMenuAction());
154
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"cut",new
155
        // CutDocumentContextMenuAction());
156
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"paste",new
157
        // PasteDocumentContextMenuAction());
158

    
159
        PluginServices.getIconTheme().registerDefault("document-table-icon",
160
            AddLayer.class.getClassLoader().getResource("images/tablas.png"));//
161

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

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

    
181
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
182
        manager.registerFactory(factory);
183

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

    
193
            factory.persistenceDefinition.addDynFieldObject("store")
194
                .setClassOfValue(FeatureStore.class).setMandatory(true);
195
            factory.persistenceDefinition.addDynFieldString("featureTypeId")
196
                .setMandatory(false);
197
            factory.persistenceDefinition.addDynFieldArray("attributeNames")
198
                .setClassOfItems(String.class).setMandatory(false);
199
            factory.persistenceDefinition.addDynFieldObject("associatedLayer")
200
                .setClassOfValue(FLyrVect.class).setMandatory(false);
201
            factory.persistenceDefinition.addDynFieldObject("query")
202
                .setClassOfValue(FeatureQuery.class).setMandatory(false);
203
            factory.persistenceDefinition.addDynFieldObject("baseFilter")
204
                .setClassOfValue(Evaluator.class).setMandatory(false);
205
            factory.persistenceDefinition.addDynFieldObject("baseOrder")
206
                .setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
207
            factory.persistenceDefinition.addDynFieldList("linkTable")
208
                .setClassOfItems(TableLink.class).setMandatory(true);
209
        }
210

    
211
    }
212

    
213
    /**
214
     * Create a new table document.
215
     * 
216
     * @return TableDocument.
217
     */
218
    public AbstractDocument createDocument() {
219
        return new TableDocument(this);
220
    }
221

    
222
    public Class<? extends IDocumentWindow> getMainWindowClass() {
223
        return FeatureTableDocumentPanel.class;
224
    }
225

    
226
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
227
        IDocumentWindow tablePanel;
228
        tablePanel =
229
            (IDocumentWindow) PluginServices.getMDIManager()
230
                .getSingletonWindow(getMainWindowClass(), doc);
231
        if (tablePanel != null) {
232
            // The table window document is already created, return it.
233
            return tablePanel;
234
        }
235

    
236
        tablePanel = (IDocumentWindow) this.createDocumentWindow(doc);
237
        if (layout != null && tablePanel != null) {
238
            tablePanel.setWindowLayout(layout);
239
        }
240
        return tablePanel;
241
    }
242

    
243
    public IWindow getPropertiesWindow(Document doc) {
244

    
245
        // IWindow mainwin =
246
        // PluginServices.getMDIManager().getSingletonWindow(FeatureTableDocumentPanel.class,
247
        // doc);
248
        return new TableProperties((TableDocument) doc);
249
    }
250

    
251
    protected Class<? extends Document> getDocumentClass() {
252
        return TableDocument.class;
253
    }
254

    
255
    public DynStruct getDefinition(String className) {
256

    
257
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
258
            return this.persistenceDefinition;
259
        }
260
        if (this.persistenceDefinition.getFullName()
261
            .equalsIgnoreCase(className)) {
262
            return this.persistenceDefinition;
263
        }
264
        if (this.getDocumentClass().getName().equals(className)) {
265
            return this.persistenceDefinition;
266
        }
267
        return null;
268
    }
269

    
270
    public boolean manages(Object object) {
271
        return object instanceof TableDocument;
272
    }
273

    
274
    public TableDocument getTableDocument(FLyrVect layer) {
275
        if (layer == null) {
276
            return null;
277
        }
278
        List<Document> tableDocs =
279
            getProject().getDocuments(TableManager.TYPENAME);
280
        for (Document document : tableDocs) {
281
            if (layer == ((TableDocument) document).getAssociatedLayer()) {
282
                return (TableDocument) document;
283
            }
284
        }
285
        return null;
286
    }
287

    
288
    public void removeTableDocument(FLyrVect layer) {
289
        TableDocument doc = getTableDocument(layer);
290
        PluginServices.getMDIManager().closeSingletonWindow(doc);
291
        getProject().remove(doc);
292
    }
293

    
294
    private Project getProject() {
295
        return ProjectManager.getInstance().getCurrentProject();
296
    }
297
}