Statistics
| Revision:

svn-gvsig-desktop / 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 @ 37838

History | View | Annotate | Download (6.76 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
        String name = "unknow";
81
        try {
82
            tableDocument = getActiveTableDocument();
83
            if (tableDocument != null) {
84
                name = tableDocument.getName();
85
                    return (tableDocument != null && tableDocument.getStore().allowWrite());
86
            }
87
        } catch (Throwable e) {
88
            LOG.error("Error accesing to the table document ("+name+")", e);
89
        }
90
        return false;
91
    }
92

    
93
    public final boolean isVisible() {
94
        TableDocument tableDocument;
95
        String name = "unknow";
96
        try {
97
            tableDocument = getActiveTableDocument();
98
            if (tableDocument != null) {
99
                name = tableDocument.getName();
100
                return isVisibleForTable(tableDocument);
101
            }
102
        } catch (Throwable e) {
103
            LOG.error("Error accesing to the table document ("+name+")", e);
104
        }       
105
        return false;
106
    }
107

    
108
    /**
109
     * Returns if the current extension is visible for the provided 
110
     * table.
111
     * 
112
     * The provided table will be always non null.
113
     * 
114
     * @param layer
115
     *            the active table
116
     * @return if the extension is visible
117
     */
118
    protected abstract boolean isVisibleForTable(TableDocument tableDocument);
119

    
120
    /**
121
     * Returns the active table document. If the active document is not a table,
122
     * or if the table has not a geometry, return null.
123
     * 
124
     * @return the active table document
125
     * @throws DataException 
126
     */
127
    protected TableDocument getActiveTableDocument() throws DataException {
128
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
129

    
130
        if (window instanceof FeatureTableDocumentPanel) {
131
            FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) window;
132
            TableDocument tableDocument = (TableDocument)table.getDocument();
133
            if (tableDocument.getStore().getDefaultFeatureType().getDefaultGeometryAttribute() != null){
134
                return tableDocument;
135
            }
136
           
137
        }
138
        return null;
139
    }
140

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

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

    
179
    /**
180
     * Executes a command for the given {@link FeatureStore}.
181
     * 
182
     * @param actionCommand
183
     *            to execute
184
     * @param featureStore
185
     *            to use
186
     * @throws Exception
187
     *             if there is an error while executing
188
     */
189
    protected abstract void execute(String actionCommand,
190
        FeatureStore featureStore) throws Exception;
191

    
192
}