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

History | View | Annotate | Download (11.6 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
 * AUTHORS (In addition to CIT):
26
 * 2004-2009 IVER TI
27
 *   
28
 */
29

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

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

    
35
import javax.swing.ImageIcon;
36

    
37
import org.gvsig.andami.IconThemeHelper;
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.messages.NotificationManager;
40
import org.gvsig.andami.ui.mdiManager.IWindow;
41
import org.gvsig.app.ApplicationLocator;
42
import org.gvsig.app.addlayer.AddLayerDialog;
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 IconThemeHelper.getImageIcon("document-table-icon");
86
    }
87

    
88
    public ImageIcon getIconSelected() {
89
        return IconThemeHelper.getImageIcon("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
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"copy",new
155
        // CopyDocumentContextMenuAction());
156
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"cut",new
157
        // CutDocumentContextMenuAction());
158
        // ProjectFactory.getInstance().registerDocumentAction(TYPENAME,"paste",new
159
        // PasteDocumentContextMenuAction());
160

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

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

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

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

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

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

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

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

    
245
    public IWindow getPropertiesWindow(Document doc) {
246

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

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

    
257
    public DynStruct getDefinition(String className) {
258

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

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

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

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

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