Statistics
| Revision:

root / 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 @ 38564

History | View | Annotate | Download (5.32 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.vectorial.GraphicLayer;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
/**
44
 * Opens a table document of the current view's graphic layer.
45
 * 
46
 * @author gvSIG team
47
 */
48
public class ShowGraphicsLayerTable extends Extension implements
49
                PropertyChangeListener {
50

    
51
        private static final Logger LOG = LoggerFactory
52
                        .getLogger(ShowGraphicsLayerTable.class);
53

    
54
        private ApplicationManager appManager = ApplicationLocator.getManager();
55

    
56
        /**
57
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
58
         */
59
        public boolean isEnabled() {
60
                return getCurrentViewDocument() != null;
61
        }
62

    
63
        private ViewDocument getCurrentViewDocument() {
64
                return (ViewDocument) appManager
65
                                .getActiveDocument(ViewManager.TYPENAME);
66
        }
67

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

    
92
        /**
93
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
94
         */
95
        public void execute(String action) {
96

    
97
                ViewDocument document = getCurrentViewDocument();
98
                GraphicLayer graphicsLayer = document.getMapContext()
99
                                .getGraphicsLayer();
100

    
101
                ProjectManager projectManager = getProjectManager();
102
                TableManager tableManager = getTableManager();
103

    
104
                TableDocument tableDocument = tableManager
105
                                .getTableDocument(graphicsLayer);
106
                FeatureStore fs = graphicsLayer.getFeatureStore();
107

    
108
                if (tableDocument == null) {
109
                        tableDocument = (TableDocument) projectManager.createDocument(
110
                                        TableManager.TYPENAME,
111
                                        PluginServices.getText(this, "Tabla_de_Atributos") + ": "
112
                                                        + graphicsLayer.getName());
113
                        tableDocument.setStore(fs);
114
                        tableDocument.setAssociatedLayer(graphicsLayer);
115
                        projectManager.getCurrentProject().add(tableDocument);
116
                }
117

    
118
                tableDocument.setModified(true);
119
                FeatureTableDocumentPanel featureTableDocumentPanel = (FeatureTableDocumentPanel) tableManager
120
                                .getMainWindow(tableDocument);
121

    
122
                appManager.getUIManager().addWindow(featureTableDocumentPanel);
123

    
124
        }
125

    
126
        private ProjectManager getProjectManager() {
127
                return ProjectManager.getInstance();
128
        }
129

    
130
        private TableManager getTableManager() {
131
                TableManager tableManager = (TableManager) getProjectManager()
132
                                .getDocumentManager(TableManager.TYPENAME);
133
                return tableManager;
134
        }
135

    
136
        /**
137
         * @see org.gvsig.andami.plugins.IExtension#initialize()
138
         */
139
        public void initialize() {
140
                registerIcons();
141
        }
142

    
143
        public void postInitialize() {
144
                // Listen just in case a view document is removed, to remove also
145
                // the related graphics layer table document, if any
146
                getProjectManager().getCurrentProject().addPropertyChangeListener(this);
147
        }
148

    
149
        private void registerIcons() {
150
                PluginServices.getIconTheme().registerDefault(
151
                                "layer-show-attribute-table",
152
                                this.getClass().getClassLoader()
153
                                                .getResource("images/ResultConsulta.png"));
154
        }
155

    
156
        public void propertyChange(PropertyChangeEvent evt) {
157
                if ("delDocument".equals(evt.getPropertyName())) {
158
                        if (evt.getOldValue() != null
159
                                        && evt.getOldValue() instanceof ViewDocument) {
160
                                ViewDocument viewDocument = (ViewDocument) evt.getOldValue();
161
                                getTableManager().removeTableDocument(
162
                                                viewDocument.getMapContext().getGraphicsLayer());
163
                        }
164
                }
165
        }
166
}