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

History | View | Annotate | Download (7.01 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 org.gvsig.andami.IconThemeHelper;
26
import org.gvsig.andami.PluginServices;
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.mapcontext.MapContext;
38
import org.gvsig.fmap.mapcontext.layers.CancelationException;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.FLayers;
41
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
42
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
43
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
44
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
45
import org.gvsig.tools.util.ArrayUtils;
46

    
47
/**
48
 * Extensi?n que abre las tablas asociadas a las vistas.
49
 *
50
 */
51
public class ShowTable extends Extension implements LayerCollectionListener {
52

    
53
    @Override
54
    public boolean isEnabled() {
55
        ApplicationManager application = ApplicationLocator.getManager();
56
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
57
        if (doc == null) {
58
            return false;
59
        }
60
        MapContext mapContext = doc.getMapContext();
61
        return mapContext.hasActiveVectorLayers();
62
    }
63

    
64
    @Override
65
    public boolean isVisible() {
66
        ApplicationManager application = ApplicationLocator.getManager();
67
        ViewDocument doc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
68
        if (doc == null) {
69
            return false;
70
        }
71
        MapContext mapContext = doc.getMapContext();
72
        return mapContext.hasVectorLayers();
73
    }
74

    
75
    @Override
76
    public void execute(String command) {
77
        this.execute(command, null);
78
    }
79

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

    
96
            for (FLayer layer : layers) {
97
                if (layer instanceof FLyrVect) {
98
                    FLyrVect layerVect = (FLyrVect) layer;
99
                    TableDocument tableDoc = tableManager.getTableDocument(layerVect);
100
                    if (tableDoc == null) {
101
                        tableDoc = (TableDocument) tableManager.createDocument();
102
                        tableDoc.setName(PluginServices.getText(this,
103
                                "Tabla_de_Atributos")
104
                                + ": "
105
                                + layerVect.getName());
106
                        tableDoc.setStore(layerVect.getFeatureStore());
107
                        tableDoc.setAssociatedLayer(layerVect);
108
                        layerVect.getParentLayer().addLayerCollectionListener(this);
109
                        project.addDocument(tableDoc);
110
                    }
111
                    IWindow tablePanel = tableManager.getMainWindow(tableDoc);
112
                    tableDoc.setModified(true);
113
                    application.getUIManager().addWindow(tablePanel);
114
                }
115
            }
116
        }
117
    }
118

    
119
    private TableManager getTableManager() {
120
        ApplicationManager application = ApplicationLocator.getManager();
121
        DocumentManager tableManager = application.getProjectManager().getDocumentManager(
122
                TableManager.TYPENAME
123
        );
124
        return (TableManager) tableManager;
125
    }
126

    
127
    @Override
128
    public void initialize() {
129
        IconThemeHelper.registerIcon("action", "layer-show-attributes-table", this);
130
    }
131

    
132
    @Override
133
    public void layerAdded(LayerCollectionEvent e) {
134
        // Nothing to do
135
    }
136

    
137
    @Override
138
    public void layerMoved(LayerPositionEvent e) {
139
        // Nothing to do
140
    }
141

    
142
    @Override
143
    public void layerRemoved(LayerCollectionEvent e) {
144
        FLayer layer = e.getAffectedLayer();
145
        // Just in case we where listening to a group of layers being removed
146
        // remove us from there
147
        if (layer instanceof FLayers) {
148
            ((FLayers) layer).removeLayerCollectionListener(this);
149
        }
150
        // Remove the related table document, if any
151
        if (layer instanceof FLyrVect) {
152
            getTableManager().removeTableDocument((FLyrVect) layer);
153
            // If the parent layers has not other child layers, for sure there
154
            // are not related tables opened, don't need to listen
155
            // LayerCollectionEvents anymore.
156
            // TODO: remove us also when there are not any child layers with
157
            // related table documents
158
            FLayers layers = layer.getParentLayer();
159
            if (layers != null && layers.getLayersCount() == 0) {
160
                layers.removeLayerCollectionListener(this);
161
            }
162
        }
163
    }
164

    
165
    @Override
166
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
167
        // Nothing to do
168
    }
169

    
170
    @Override
171
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
172
        // Nothing to do
173
    }
174

    
175
    @Override
176
    public void layerRemoving(LayerCollectionEvent e)
177
            throws CancelationException {
178
        // Nothing to do
179
    }
180

    
181
    @Override
182
    public void visibilityChanged(LayerCollectionEvent e)
183
            throws CancelationException {
184
        // Nothing to do
185
    }
186
}