Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / extension / ShowGraphicsLayerTable.java @ 38415

History | View | Annotate | Download (5.72 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.app.extension;
23

    
24
import java.beans.PropertyChangeEvent;
25
import java.beans.PropertyChangeListener;
26

    
27
import org.gvsig.andami.PluginServices;
28
import org.gvsig.andami.plugins.Extension;
29
import org.gvsig.app.ApplicationLocator;
30
import org.gvsig.app.ApplicationManager;
31
import org.gvsig.app.project.ProjectManager;
32
import org.gvsig.app.project.documents.table.TableDocument;
33
import org.gvsig.app.project.documents.table.TableManager;
34
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
35
import org.gvsig.app.project.documents.view.ViewDocument;
36
import org.gvsig.app.project.documents.view.ViewManager;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.fmap.mapcontext.layers.CancelationException;
40
import org.gvsig.fmap.mapcontext.layers.FLayer;
41
import org.gvsig.fmap.mapcontext.layers.FLayers;
42
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
43
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
44
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
45
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
46
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
/**
51
 * Opens a table document of the current view's graphic layer.
52
 * 
53
 * @author gvSIG team
54
 */
55
public class ShowGraphicsLayerTable extends Extension implements
56
                PropertyChangeListener {
57

    
58
        private static final Logger LOG = LoggerFactory
59
                        .getLogger(ShowGraphicsLayerTable.class);
60

    
61
        private ApplicationManager appManager = ApplicationLocator.getManager();
62

    
63
        /**
64
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
65
         */
66
        public boolean isEnabled() {
67
                return getCurrentViewDocument() != null;
68
        }
69

    
70
        private ViewDocument getCurrentViewDocument() {
71
                return (ViewDocument) appManager
72
                                .getActiveDocument(ViewManager.TYPENAME);
73
        }
74

    
75
        /**
76
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
77
         */
78
        public boolean isVisible() {
79
                try {
80
                        ViewDocument document = getCurrentViewDocument();
81
                        if (document == null) {
82
                                return false;
83
                        }
84
                        GraphicLayer graphicsLayer = document.getMapContext()
85
                                        .getGraphicsLayer();
86
                        TableManager tableManager = getTableManager();
87
                        TableDocument tableDocument = tableManager
88
                                        .getTableDocument(graphicsLayer);
89
                        return tableDocument != null
90
                                        || document.getMapContext().getGraphicsLayer()
91
                                                        .getFeatureStore().getFeatureCount() > 0;
92
                } catch (DataException e) {
93
                        LOG.error("Error checking if the current document is a "
94
                                        + "view and has data in the graphics layer", e);
95
                        return false;
96
                }
97
        }
98

    
99
        /**
100
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
101
         */
102
        public void execute(String action) {
103

    
104
                ViewDocument document = getCurrentViewDocument();
105
                GraphicLayer graphicsLayer = document.getMapContext()
106
                                .getGraphicsLayer();
107

    
108
                ProjectManager projectManager = getProjectManager();
109
                TableManager tableManager = getTableManager();
110

    
111
                TableDocument tableDocument = tableManager
112
                                .getTableDocument(graphicsLayer);
113
                FeatureStore fs = graphicsLayer.getFeatureStore();
114

    
115
                if (tableDocument == null) {
116
                        tableDocument = (TableDocument) projectManager.createDocument(
117
                                        TableManager.TYPENAME,
118
                                        PluginServices.getText(this, "Tabla_de_Atributos") + ": "
119
                                                        + graphicsLayer.getName());
120
                        tableDocument.setStore(fs);
121
                        tableDocument.setAssociatedLayer(graphicsLayer);
122
                        projectManager.getCurrentProject().add(tableDocument);
123
                }
124

    
125
                tableDocument.setModified(true);
126
                FeatureTableDocumentPanel featureTableDocumentPanel = (FeatureTableDocumentPanel) tableManager
127
                                .getMainWindow(tableDocument);
128

    
129
                appManager.getUIManager().addWindow(featureTableDocumentPanel);
130

    
131
        }
132

    
133
        private ProjectManager getProjectManager() {
134
                return ProjectManager.getInstance();
135
        }
136

    
137
        private TableManager getTableManager() {
138
                TableManager tableManager = (TableManager) getProjectManager()
139
                                .getDocumentManager(TableManager.TYPENAME);
140
                return tableManager;
141
        }
142

    
143
        /**
144
         * @see org.gvsig.andami.plugins.IExtension#initialize()
145
         */
146
        public void initialize() {
147
                registerIcons();
148
        }
149

    
150
        public void postInitialize() {
151
                // Listen just in case a view document is removed, to remove also
152
                // the related graphics layer table document, if any
153
                getProjectManager().getCurrentProject().addPropertyChangeListener(this);
154
        }
155

    
156
        private void registerIcons() {
157
                PluginServices.getIconTheme().registerDefault(
158
                                "layer-show-attribute-table",
159
                                this.getClass().getClassLoader()
160
                                                .getResource("images/ResultConsulta.png"));
161
        }
162

    
163
        public void propertyChange(PropertyChangeEvent evt) {
164
                if ("delDocument".equals(evt.getPropertyName())) {
165
                        if (evt.getOldValue() != null
166
                                        && evt.getOldValue() instanceof ViewDocument) {
167
                                ViewDocument viewDocument = (ViewDocument) evt.getOldValue();
168
                                getTableManager().removeTableDocument(
169
                                                viewDocument.getMapContext().getGraphicsLayer());
170
                        }
171
                }
172
        }
173
}