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 / extension / ShowTable.java @ 44437

History | View | Annotate | Download (8.35 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.extension;
24

    
25
import javax.swing.JOptionPane;
26
import org.gvsig.andami.IconThemeHelper;
27
import org.gvsig.andami.plugins.Extension;
28
import org.gvsig.andami.ui.mdiManager.IWindow;
29
import org.gvsig.app.ApplicationLocator;
30
import org.gvsig.app.ApplicationManager;
31
import org.gvsig.app.project.Project;
32
import org.gvsig.app.project.documents.DocumentManager;
33
import org.gvsig.app.project.documents.table.TableDocument;
34
import org.gvsig.app.project.documents.table.TableManager;
35
import org.gvsig.app.project.documents.view.ViewDocument;
36
import org.gvsig.app.project.documents.view.ViewManager;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.mapcontext.MapContext;
40
import org.gvsig.fmap.mapcontext.layers.CancelationException;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.FLayers;
43
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
44
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
45
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.i18n.I18nManager;
49
import org.gvsig.tools.swing.api.ToolsSwingLocator;
50
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
51
import org.gvsig.tools.util.ArrayUtils;
52

    
53
/**
54
 * Extensi?n que abre las tablas asociadas a las vistas.
55
 *
56
 */
57
public class ShowTable extends Extension implements LayerCollectionListener {
58

    
59
    @Override
60
    public boolean isEnabled() {
61
        ApplicationManager application = ApplicationLocator.getManager();
62
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
63
        if (doc == null) {
64
            return false;
65
        }
66
        MapContext mapContext = doc.getMapContext();
67
        return mapContext.hasActiveVectorLayers();
68
    }
69

    
70
    @Override
71
    public boolean isVisible() {
72
        ApplicationManager application = ApplicationLocator.getManager();
73
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
74
        if (doc == null) {
75
            return false;
76
        }
77
        MapContext mapContext = doc.getMapContext();
78
        return mapContext.hasVectorLayers();
79
    }
80

    
81
    @Override
82
    public void execute(String command) {
83
        this.execute(command, null);
84
    }
85

    
86
    @Override
87
    public void execute(String command, Object[] args) {
88
        if ("layer-show-attributes-table".equalsIgnoreCase(command)) {
89
            ApplicationManager application = ApplicationLocator.getManager();
90
            FLayer[] layers = (FLayer[]) ArrayUtils.get(args, 0);
91
            if( layers == null ) {
92
                ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
93
                if (doc == null) {
94
                    return;
95
                }
96
                MapContext mapContext = doc.getMapContext();
97
                layers = mapContext.getLayers().getActives();
98
            }
99
            Project project = application.getCurrentProject();
100
            TableManager tableManager = getTableManager();
101

    
102
            for (FLayer layer : layers) {
103
                if (layer instanceof FLyrVect) {
104
                    FLyrVect layerVect = (FLyrVect) layer;                    
105
                    TableDocument tableDoc = tableManager.getTableDocument(layerVect);
106
                    if (tableDoc == null) {
107
                        tableDoc = (TableDocument) tableManager.createDocument();
108
                        tableDoc.setName(layerVect.getName());
109
                        tableDoc.setStore(layerVect.getFeatureStore());
110
                        tableDoc.setAssociatedLayer(layerVect);
111
                        layerVect.getParentLayer().addLayerCollectionListener(this);
112
                        project.addDocument(tableDoc);
113
                    }
114
                    try {
115
                        FeatureStore store = tableDoc.getDataStore();
116
                        FeatureType type = store.getDefaultFeatureType();
117
                        if( ! type.supportReferences() ) {
118
                            I18nManager i18n = ToolsLocator.getI18nManager();
119
                            ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
120
                            dialogs.messageDialog(
121
                                    "\""+ store.getName() + "\"\n"+
122
                                    i18n.getTranslation("_The_table_has_no_primary_key_or_OID") +"\n" +
123
                                           i18n.getTranslation("_Many_features_selection_deletion_modification_will_not_be_available_as_they_require_it_for_proper_operation"),
124
                                    null, 
125
                                    i18n.getTranslation("_Warning"),
126
                                    JOptionPane.WARNING_MESSAGE, 
127
                                    "TableDoNotSupportReferences"
128
                           );
129
                        }
130
                    } catch (Exception ex) {
131

    
132
                    }
133
                    
134
                    IWindow tablePanel = tableManager.getMainWindow(tableDoc);
135
                    tableDoc.setModified(true);
136
                    application.getUIManager().addWindow(tablePanel);
137
                }
138
            }
139
        }
140
    }
141

    
142
    private TableManager getTableManager() {
143
        ApplicationManager application = ApplicationLocator.getManager();
144
        DocumentManager tableManager = application.getProjectManager().getDocumentManager(
145
                TableManager.TYPENAME
146
        );
147
        return (TableManager) tableManager;
148
    }
149

    
150
    @Override
151
    public void initialize() {
152
        IconThemeHelper.registerIcon("action", "layer-show-attributes-table", this);
153
    }
154

    
155
    @Override
156
    public void layerAdded(LayerCollectionEvent e) {
157
        // Nothing to do
158
    }
159

    
160
    @Override
161
    public void layerMoved(LayerPositionEvent e) {
162
        // Nothing to do
163
    }
164

    
165
    @Override
166
    public void layerRemoved(LayerCollectionEvent e) {
167
        FLayer layer = e.getAffectedLayer();
168
        // Just in case we where listening to a group of layers being removed
169
        // remove us from there
170
        if (layer instanceof FLayers) {
171
            ((FLayers) layer).removeLayerCollectionListener(this);
172
        }
173
        // Remove the related table document, if any
174
        if (layer instanceof FLyrVect) {
175
            getTableManager().removeTableDocument((FLyrVect) layer);
176
            // If the parent layers has not other child layers, for sure there
177
            // are not related tables opened, don't need to listen
178
            // LayerCollectionEvents anymore.
179
            // TODO: remove us also when there are not any child layers with
180
            // related table documents
181
            FLayers layers = layer.getParentLayer();
182
            if (layers != null && layers.getLayersCount() == 0) {
183
                layers.removeLayerCollectionListener(this);
184
            }
185
        }
186
    }
187

    
188
    @Override
189
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
190
        // Nothing to do
191
    }
192

    
193
    @Override
194
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
195
        // Nothing to do
196
    }
197

    
198
    @Override
199
    public void layerRemoving(LayerCollectionEvent e)
200
            throws CancelationException {
201
        // Nothing to do
202
    }
203

    
204
    @Override
205
    public void visibilityChanged(LayerCollectionEvent e)
206
            throws CancelationException {
207
        // Nothing to do
208
    }
209
}