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

History | View | Annotate | Download (6.86 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(layerVect.getName());
103
                        tableDoc.setStore(layerVect.getFeatureStore());
104
                        tableDoc.setAssociatedLayer(layerVect);
105
                        layerVect.getParentLayer().addLayerCollectionListener(this);
106
                        project.addDocument(tableDoc);
107
                    }
108
                    IWindow tablePanel = tableManager.getMainWindow(tableDoc);
109
                    tableDoc.setModified(true);
110
                    application.getUIManager().addWindow(tablePanel);
111
                }
112
            }
113
        }
114
    }
115

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

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

    
129
    @Override
130
    public void layerAdded(LayerCollectionEvent e) {
131
        // Nothing to do
132
    }
133

    
134
    @Override
135
    public void layerMoved(LayerPositionEvent e) {
136
        // Nothing to do
137
    }
138

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

    
162
    @Override
163
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
164
        // Nothing to do
165
    }
166

    
167
    @Override
168
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
169
        // Nothing to do
170
    }
171

    
172
    @Override
173
    public void layerRemoving(LayerCollectionEvent e)
174
            throws CancelationException {
175
        // Nothing to do
176
    }
177

    
178
    @Override
179
    public void visibilityChanged(LayerCollectionEvent e)
180
            throws CancelationException {
181
        // Nothing to do
182
    }
183
}