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 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 42838 jjdelcerro
 * 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 40558 jjdelcerro
 *
11 42838 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 42838 jjdelcerro
 * 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 40558 jjdelcerro
 *
20 42838 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.app.extension;
24
25 44437 jjdelcerro
import javax.swing.JOptionPane;
26 40435 jjdelcerro
import org.gvsig.andami.IconThemeHelper;
27
import org.gvsig.andami.plugins.Extension;
28 42838 jjdelcerro
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 40435 jjdelcerro
import org.gvsig.app.project.documents.table.TableDocument;
34
import org.gvsig.app.project.documents.table.TableManager;
35 42838 jjdelcerro
import org.gvsig.app.project.documents.view.ViewDocument;
36
import org.gvsig.app.project.documents.view.ViewManager;
37 44437 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39 42838 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContext;
40 40435 jjdelcerro
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 44437 jjdelcerro
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 42838 jjdelcerro
import org.gvsig.tools.util.ArrayUtils;
52 40435 jjdelcerro
53
/**
54
 * Extensi?n que abre las tablas asociadas a las vistas.
55 42835 dmartinezizquierdo
 *
56 40435 jjdelcerro
 */
57
public class ShowTable extends Extension implements LayerCollectionListener {
58
59 42838 jjdelcerro
    @Override
60 40435 jjdelcerro
    public boolean isEnabled() {
61 42838 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
62
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
63
        if (doc == null) {
64 40435 jjdelcerro
            return false;
65
        }
66 42838 jjdelcerro
        MapContext mapContext = doc.getMapContext();
67
        return mapContext.hasActiveVectorLayers();
68 40435 jjdelcerro
    }
69
70 42838 jjdelcerro
    @Override
71 40435 jjdelcerro
    public boolean isVisible() {
72 42838 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
73
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
74
        if (doc == null) {
75 40435 jjdelcerro
            return false;
76
        }
77 42838 jjdelcerro
        MapContext mapContext = doc.getMapContext();
78
        return mapContext.hasVectorLayers();
79
    }
80 40435 jjdelcerro
81 42838 jjdelcerro
    @Override
82
    public void execute(String command) {
83
        this.execute(command, null);
84
    }
85 42835 dmartinezizquierdo
86 42838 jjdelcerro
    @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 42835 dmartinezizquierdo
102 42838 jjdelcerro
            for (FLayer layer : layers) {
103
                if (layer instanceof FLyrVect) {
104 44437 jjdelcerro
                    FLyrVect layerVect = (FLyrVect) layer;
105 42838 jjdelcerro
                    TableDocument tableDoc = tableManager.getTableDocument(layerVect);
106
                    if (tableDoc == null) {
107
                        tableDoc = (TableDocument) tableManager.createDocument();
108 44262 jjdelcerro
                        tableDoc.setName(layerVect.getName());
109 42838 jjdelcerro
                        tableDoc.setStore(layerVect.getFeatureStore());
110
                        tableDoc.setAssociatedLayer(layerVect);
111
                        layerVect.getParentLayer().addLayerCollectionListener(this);
112
                        project.addDocument(tableDoc);
113 42835 dmartinezizquierdo
                    }
114 44437 jjdelcerro
                    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 42838 jjdelcerro
                    IWindow tablePanel = tableManager.getMainWindow(tableDoc);
135
                    tableDoc.setModified(true);
136
                    application.getUIManager().addWindow(tablePanel);
137 42835 dmartinezizquierdo
                }
138
            }
139
        }
140 40435 jjdelcerro
    }
141
142
    private TableManager getTableManager() {
143 42838 jjdelcerro
        ApplicationManager application = ApplicationLocator.getManager();
144
        DocumentManager tableManager = application.getProjectManager().getDocumentManager(
145
                TableManager.TYPENAME
146
        );
147
        return (TableManager) tableManager;
148 40435 jjdelcerro
    }
149
150 42838 jjdelcerro
    @Override
151 40435 jjdelcerro
    public void initialize() {
152 42838 jjdelcerro
        IconThemeHelper.registerIcon("action", "layer-show-attributes-table", this);
153 40435 jjdelcerro
    }
154
155 42838 jjdelcerro
    @Override
156 40435 jjdelcerro
    public void layerAdded(LayerCollectionEvent e) {
157
        // Nothing to do
158
    }
159
160 42838 jjdelcerro
    @Override
161 40435 jjdelcerro
    public void layerMoved(LayerPositionEvent e) {
162
        // Nothing to do
163
    }
164
165 42838 jjdelcerro
    @Override
166 40435 jjdelcerro
    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 42838 jjdelcerro
    @Override
189 40435 jjdelcerro
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
190
        // Nothing to do
191
    }
192
193 42838 jjdelcerro
    @Override
194 40435 jjdelcerro
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
195
        // Nothing to do
196
    }
197
198 42838 jjdelcerro
    @Override
199 40435 jjdelcerro
    public void layerRemoving(LayerCollectionEvent e)
200 42838 jjdelcerro
            throws CancelationException {
201 40435 jjdelcerro
        // Nothing to do
202
    }
203
204 42838 jjdelcerro
    @Override
205 40435 jjdelcerro
    public void visibilityChanged(LayerCollectionEvent e)
206 42838 jjdelcerro
            throws CancelationException {
207 40435 jjdelcerro
        // Nothing to do
208
    }
209
}