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

History | View | Annotate | Download (11.5 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.IconThemeHelper;
37
import org.gvsig.andami.PluginServices;
38
import org.gvsig.andami.messages.NotificationManager;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.app.ApplicationLocator;
41
import org.gvsig.app.addlayer.AddLayerDialog;
42
import org.gvsig.app.extension.AddLayer;
43
import org.gvsig.app.gui.WizardPanel;
44
import org.gvsig.app.project.Project;
45
import org.gvsig.app.project.ProjectManager;
46
import org.gvsig.app.project.documents.AbstractDocument;
47
import org.gvsig.app.project.documents.AbstractDocumentManager;
48
import org.gvsig.app.project.documents.Document;
49
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
import org.gvsig.fmap.dal.feature.FeatureQuery;
55
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
56
import org.gvsig.fmap.dal.feature.FeatureStore;
57
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
59
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
60
import org.gvsig.tools.ToolsLocator;
61
import org.gvsig.tools.dynobject.DynObjectManager;
62
import org.gvsig.tools.dynobject.DynStruct;
63
import org.gvsig.tools.evaluator.Evaluator;
64
import org.gvsig.tools.persistence.PersistenceManager;
65

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

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

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

    
82
    private DynStruct persistenceDefinition = null;
83

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

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

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

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

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

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

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

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

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

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

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

    
163
        IconThemeHelper.registerIcon("document", "document-table-icon", TableManager.class);
164
        IconThemeHelper.registerIcon("document", "document-table-icon-sel", TableManager.class);
165

    
166
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
167
        manager.registerFactory(factory);
168

    
169
        if (factory.persistenceDefinition == null) {
170
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
171
            factory.persistenceDefinition =
172
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
173
                    PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME,
174
                    "Table document Persistence definition");
175
            factory.persistenceDefinition.extend(manager
176
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
177

    
178
            factory.persistenceDefinition.addDynFieldObject("store")
179
                .setClassOfValue(FeatureStore.class).setMandatory(true);
180
            factory.persistenceDefinition.addDynFieldString("featureTypeId")
181
                .setMandatory(false);
182
            factory.persistenceDefinition.addDynFieldArray("attributeNames")
183
                .setClassOfItems(String.class).setMandatory(false);
184
            factory.persistenceDefinition.addDynFieldObject("associatedLayer")
185
                .setClassOfValue(FLyrVect.class).setMandatory(false);
186
            factory.persistenceDefinition.addDynFieldObject("query")
187
                .setClassOfValue(FeatureQuery.class).setMandatory(false);
188
            factory.persistenceDefinition.addDynFieldObject("baseFilter")
189
                .setClassOfValue(Evaluator.class).setMandatory(false);
190
            factory.persistenceDefinition.addDynFieldObject("baseOrder")
191
                .setClassOfValue(FeatureQueryOrder.class).setMandatory(false);
192
            factory.persistenceDefinition.addDynFieldList("linkTable")
193
                .setClassOfItems(TableLink.class).setMandatory(false);          
194
        }
195
        
196
        
197
        //Register also the TableLink
198
        if (manager.getDefinition(PERSISTENCE_TABLELINK_DEFINITION_NAME) == null){
199
            DynStruct tableLinkDefinition =
200
                manager.addDefinition(TableLink.class,
201
                    PERSISTENCE_TABLELINK_DEFINITION_NAME,
202
                    "TableLink Persistence definition", null, null);                 
203
            
204
            tableLinkDefinition.addDynFieldObject("source")
205
                .setClassOfValue(TableDocument.class).setMandatory(true);
206
            tableLinkDefinition.addDynFieldObject("target")
207
                .setClassOfValue(TableDocument.class).setMandatory(true);
208
            tableLinkDefinition.addDynFieldString("fieldSource").setMandatory(true);
209
            tableLinkDefinition.addDynFieldString("fieldTarget").setMandatory(true);
210
            tableLinkDefinition.addDynFieldBoolean("enabled").setMandatory(true);
211
        }  
212
    }
213

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

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

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

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

    
244
    public IWindow getPropertiesWindow(Document doc) {
245

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

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

    
256
    public DynStruct getDefinition(String className) {
257

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

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

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

    
289
    public void removeTableDocument(VectorLayer layer) {
290
        TableDocument doc = getTableDocument(layer);
291
        // Only remove it if it exists
292
        if (doc != null) {
293
            PluginServices.getMDIManager().closeSingletonWindow(doc);
294
            getProject().remove(doc);
295
        }
296
    }
297

    
298
    private Project getProject() {
299
        return ProjectManager.getInstance().getCurrentProject();
300
    }
301
}