Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.geometrymeasurement.app / org.gvsig.geometrymeasurement.app.extension / src / main / java / org / gvsig / geometrymeasurement / app / extension / AbstractGeometryMeasurementExtension.java @ 37536

History | View | Annotate | Download (6.53 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.geometrymeasurement.app.extension;
23

    
24
import java.awt.Component;
25
import java.util.Locale;
26

    
27
import javax.swing.JOptionPane;
28

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.andami.messages.NotificationManager;
34
import org.gvsig.andami.plugins.Extension;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.app.ApplicationLocator;
37
import org.gvsig.app.project.documents.table.TableDocument;
38
import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.geom.type.GeometryType;
44
import org.gvsig.i18n.Messages;
45

    
46
/**
47
 * Abstract class for Geometry measurement operations extensions.
48
 * 
49
 * @author gvSIG Team
50
 * @version $Id$
51
 */
52
public abstract class AbstractGeometryMeasurementExtension extends Extension {
53

    
54
    private static final Logger LOG = LoggerFactory
55
        .getLogger(AbstractGeometryMeasurementExtension.class);
56
    
57
    //Just to know if the resources has been initialized
58
    private static boolean isInitialized = false;
59
       
60
    public void initialize() {
61
        if (!isInitialized){
62
            //Register the strings
63
            if (!Messages.hasLocales()) {
64
                Messages.addLocale(Locale.getDefault());
65
            }
66
            
67
            Messages.addResourceFamily(
68
                    "org.gvsig.geometrymeasurement.app.extension.i18n.text",
69
                    AbstractGeometryMeasurementExtension.class.getClassLoader(),
70
                    AbstractGeometryMeasurementExtension.class.getClass().getName());
71
            
72
            //Register the icons
73
            
74
            isInitialized = true;
75
        }        
76
    }
77

    
78
    public boolean isEnabled() {
79
        TableDocument tableDocument;
80
        try {
81
            tableDocument = getActiveTableDocument();
82
            return (tableDocument != null && tableDocument.getStore().allowWrite());
83
        } catch (DataException e) {
84
            LOG.error("Error getting the table document", e);
85
        }
86
        return false;
87
    }
88

    
89
    public final boolean isVisible() {
90
        TableDocument tableDocument;
91
        try {
92
            tableDocument = getActiveTableDocument();
93
            if (tableDocument != null) {
94
                return isVisibleForTable(tableDocument);
95
            }
96
        } catch (DataException e) {
97
            LOG.error("Error getting the table document", e);
98
        }       
99
        return false;
100
    }
101

    
102
    /**
103
     * Returns if the current extension is visible for the provided 
104
     * table.
105
     * 
106
     * The provided table will be always non null.
107
     * 
108
     * @param layer
109
     *            the active table
110
     * @return if the extension is visible
111
     */
112
    protected abstract boolean isVisibleForTable(TableDocument tableDocument);
113

    
114
    /**
115
     * Returns the active table document. If the active document is not a table,
116
     * or if the table has not a geometry, return null.
117
     * 
118
     * @return the active table document
119
     * @throws DataException 
120
     */
121
    protected TableDocument getActiveTableDocument() throws DataException {
122
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
123

    
124
        if (window instanceof FeatureTableDocumentPanel) {
125
            FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) window;
126
            TableDocument tableDocument = (TableDocument)table.getDocument();
127
            if (tableDocument.getStore().getDefaultFeatureType().getDefaultGeometryAttribute() != null){
128
                return tableDocument;
129
            }
130
           
131
        }
132
        return null;
133
    }
134

    
135
    protected boolean isStoreOfAnyType(TableDocument tableDocument, int[] geomTypes) {
136
        try {
137
            FeatureType featureType = tableDocument.getStore().getDefaultFeatureType();
138
            GeometryType geometryType = featureType.getDefaultGeometryAttribute().getGeomType();
139
            for (int i = 0; i < geomTypes.length; i++) {
140
                if (geometryType.isTypeOf(geomTypes[i])) {
141
                    return true;
142
                }
143
            }
144
        } catch (ReadException e) {
145
            LOG.error("Error reading the geometry type", e);
146
        } catch (DataException e) {
147
            LOG.error("Error reading the geometry type", e);
148
        }
149
        return false;
150
    }
151

    
152
    public void execute(String actionCommand) {
153
        try {
154
            TableDocument tableDocument = getActiveTableDocument();
155
            if (tableDocument == null) {
156
                return;
157
            }
158
            Component parentWindow = 
159
                (Component)ApplicationLocator.getManager().getUIManager().getActiveWindow();
160
            int addField = JOptionPane.showConfirmDialog(parentWindow, Messages.getText("new_field_query"),
161
                Messages.getText("info"), JOptionPane.YES_NO_OPTION);
162
            
163
            if (addField == JOptionPane.YES_OPTION){
164
                FeatureStore featureStore = tableDocument.getStore();
165
                execute(actionCommand, featureStore);
166
            }
167
        } catch (Exception e) {
168
            LOG.error("Not possible to add the new field", e);
169
            NotificationManager.addError(e);
170
        }
171
    }
172

    
173
    /**
174
     * Executes a command for the given {@link FeatureStore}.
175
     * 
176
     * @param actionCommand
177
     *            to execute
178
     * @param featureStore
179
     *            to use
180
     * @throws Exception
181
     *             if there is an error while executing
182
     */
183
    protected abstract void execute(String actionCommand,
184
        FeatureStore featureStore) throws Exception;
185

    
186
}