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

History | View | Annotate | Download (12 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.Iterator;
28
import java.util.List;
29

    
30
import javax.swing.ImageIcon;
31

    
32
import org.gvsig.andami.IconThemeHelper;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.messages.NotificationManager;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.app.ApplicationLocator;
37
import org.gvsig.app.addlayer.AddLayerDialog;
38
import org.gvsig.app.gui.WizardPanel;
39
import org.gvsig.app.project.Project;
40
import org.gvsig.app.project.ProjectManager;
41
import org.gvsig.app.project.documents.AbstractDocument;
42
import org.gvsig.app.project.documents.AbstractDocumentManager;
43
import org.gvsig.app.project.documents.Document;
44
import org.gvsig.app.project.documents.actions.CopyDocumentAction;
45
import org.gvsig.app.project.documents.actions.CutDocumentAction;
46
import org.gvsig.app.project.documents.actions.PasteDocumentAction;
47
import org.gvsig.app.project.documents.gui.IDocumentWindow;
48
import org.gvsig.app.project.documents.gui.WindowLayout;
49
import org.gvsig.app.project.documents.table.TableDocument.TableLink;
50
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
51
import org.gvsig.app.project.documents.table.gui.TableProperties;
52
import org.gvsig.fmap.dal.feature.FeatureQuery;
53
import org.gvsig.fmap.dal.feature.FeatureQueryOrder;
54
import org.gvsig.fmap.dal.feature.FeatureStore;
55
import org.gvsig.fmap.dal.serverexplorer.filesystem.swing.FilesystemExplorerWizardPanel;
56
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
57
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
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
    public static final String PERSISTENCE_TABLELINK_DEFINITION_NAME =
76
        "TableLink";
77

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

    
80
    private DynStruct persistenceDefinition = null;
81

    
82
    public ImageIcon getIcon() {
83
            return IconThemeHelper.getImageIcon("document-table-icon");
84
    }
85

    
86
    public ImageIcon getIconSelected() {
87
        return IconThemeHelper.getImageIcon("document-table-icon-sel");
88
    }
89

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

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

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

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

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

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

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

    
152
                IconThemeHelper.registerIcon("document", "document-table-icon", TableManager.class);
153
        IconThemeHelper.registerIcon("document", "document-table-icon-sel", TableManager.class);
154
        IconThemeHelper.registerIcon("document", "document-table-icon-small", TableManager.class);
155

    
156
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
157
        manager.registerFactory(factory);
158

    
159
        if (factory.persistenceDefinition == null) {
160
            DynObjectManager dynman = ToolsLocator.getDynObjectManager();
161
            factory.persistenceDefinition =
162
                dynman.createDynClass(PersistenceManager.PERSISTENCE_NAMESPACE,
163
                    PERSISTENCE_TABLE_DOCUMENT_DEFINITION_NAME,
164
                    "Table document Persistence definition");
165
            factory.persistenceDefinition.extend(manager
166
                .getDefinition(AbstractDocument.PERSISTENCE_DEFINITION_NAME));
167

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

    
211
    }
212

    
213
    /**
214
     * Create a new table document.
215
     * 
216
     * @return TableDocument.
217
     */
218
    public AbstractDocument createDocument() {
219
        AbstractDocument doc = new TableDocument(this);
220
        if( this.notifyObservers(NOTIFY_AFTER_CREATEDOCUMENT,doc).isCanceled() ) {
221
            return null;
222
        }
223
            return doc;        
224
    }
225

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

    
230
    @Override
231
    public IWindow getMainWindow(Document doc, WindowLayout layout) {
232
       IDocumentWindow win = (IDocumentWindow) super.getMainWindow(doc, layout);
233
        if (win == null) {
234
            win = (IDocumentWindow) this.createDocumentWindow(doc);
235
            if (layout != null && win != null) {
236
                win.setWindowLayout(layout);
237
            }
238
        }
239
        if( this.notifyObservers(NOTIFY_AFTER_GETMAINWINDOW,win).isCanceled() ) {
240
            return null;
241
        }
242
        return win;
243
    }
244

    
245
    @Override
246
    public IWindow getPropertiesWindow(Document doc) {
247
        IWindow win = super.getPropertiesWindow(doc);
248
        if( win == null ) {
249
            win = new TableProperties((TableDocument) doc);
250
        }
251
        if( this.notifyObservers(NOTIFY_AFTER_GETPROPERTIESWINDOW,win).isCanceled() ) {
252
            return null;
253
        }
254
        return win;        
255
    }
256

    
257
    protected Class<? extends Document> getDocumentClass() {
258
        return TableDocument.class;
259
    }
260

    
261
    public DynStruct getDefinition(String className) {
262

    
263
        if (this.persistenceDefinition.getName().equalsIgnoreCase(className)) {
264
            return this.persistenceDefinition;
265
        }
266
        if (this.persistenceDefinition.getFullName()
267
            .equalsIgnoreCase(className)) {
268
            return this.persistenceDefinition;
269
        }
270
        if (this.getDocumentClass().getName().equals(className)) {
271
            return this.persistenceDefinition;
272
        }
273
        return null;
274
    }
275

    
276
    public boolean manages(Object object) {
277
        return object instanceof TableDocument;
278
    }
279

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

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

    
303
    private Project getProject() {
304
        return ProjectManager.getInstance().getCurrentProject();
305
    }
306
}