svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geometrymeasurement.app / org.gvsig.geometrymeasurement.app.mainplugin / src / main / java / org / gvsig / geometrymeasurement / app / extension / AbstractGeometryMeasurementExtension.java @ 44137
History | View | Annotate | Download (6.31 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40557 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40557 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40557 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.geometrymeasurement.app.extension; |
||
25 | |||
26 | import java.awt.Component; |
||
27 | import java.util.Locale; |
||
28 | |||
29 | import javax.swing.JOptionPane; |
||
30 | |||
31 | import org.slf4j.Logger; |
||
32 | import org.slf4j.LoggerFactory; |
||
33 | |||
34 | import org.gvsig.andami.PluginServices; |
||
35 | import org.gvsig.andami.messages.NotificationManager; |
||
36 | import org.gvsig.andami.plugins.Extension; |
||
37 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
38 | import org.gvsig.app.ApplicationLocator; |
||
39 | import org.gvsig.app.project.documents.table.TableDocument; |
||
40 | import org.gvsig.app.project.documents.table.gui.FeatureTableDocumentPanel; |
||
41 | import org.gvsig.fmap.dal.exception.DataException; |
||
42 | import org.gvsig.fmap.dal.exception.ReadException; |
||
43 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
44 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
45 | import org.gvsig.fmap.geom.type.GeometryType; |
||
46 | import org.gvsig.i18n.Messages; |
||
47 | |||
48 | /**
|
||
49 | * Abstract class for Geometry measurement operations extensions.
|
||
50 | *
|
||
51 | * @author gvSIG Team
|
||
52 | * @version $Id$
|
||
53 | */
|
||
54 | public abstract class AbstractGeometryMeasurementExtension extends Extension { |
||
55 | |||
56 | private static final Logger LOG = LoggerFactory |
||
57 | .getLogger(AbstractGeometryMeasurementExtension.class); |
||
58 | |||
59 | //Just to know if the resources has been initialized
|
||
60 | private static boolean isInitialized = false; |
||
61 | |||
62 | public void initialize() { |
||
63 | if (!isInitialized){
|
||
64 | isInitialized = true;
|
||
65 | } |
||
66 | } |
||
67 | |||
68 | public boolean isEnabled() { |
||
69 | TableDocument tableDocument; |
||
70 | String name = "unknow"; |
||
71 | try {
|
||
72 | tableDocument = getActiveTableDocument(); |
||
73 | if (tableDocument != null) { |
||
74 | name = tableDocument.getName(); |
||
75 | return (tableDocument != null && tableDocument.getStore().allowWrite()); |
||
76 | } |
||
77 | } catch (Throwable e) { |
||
78 | LOG.info("Error accesing to the table document ("+name+")", e); |
||
79 | } |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | public final boolean isVisible() { |
||
84 | TableDocument tableDocument; |
||
85 | String name = "unknow"; |
||
86 | try {
|
||
87 | tableDocument = getActiveTableDocument(); |
||
88 | if (tableDocument != null) { |
||
89 | name = tableDocument.getName(); |
||
90 | return isVisibleForTable(tableDocument);
|
||
91 | } |
||
92 | } catch (Throwable e) { |
||
93 | LOG.info("Error accesing to the table document ("+name+")", e); |
||
94 | } |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | /**
|
||
99 | * Returns if the current extension is visible for the provided
|
||
100 | * table.
|
||
101 | *
|
||
102 | * The provided table will be always non null.
|
||
103 | *
|
||
104 | * @param layer
|
||
105 | * the active table
|
||
106 | * @return if the extension is visible
|
||
107 | */
|
||
108 | protected abstract boolean isVisibleForTable(TableDocument tableDocument); |
||
109 | |||
110 | /**
|
||
111 | * Returns the active table document. If the active document is not a table,
|
||
112 | * or if the table has not a geometry, return null.
|
||
113 | *
|
||
114 | * @return the active table document
|
||
115 | * @throws DataException
|
||
116 | */
|
||
117 | protected TableDocument getActiveTableDocument() throws DataException { |
||
118 | IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
||
119 | |||
120 | if (window instanceof FeatureTableDocumentPanel) { |
||
121 | FeatureTableDocumentPanel table = (FeatureTableDocumentPanel) window; |
||
122 | TableDocument tableDocument = (TableDocument)table.getDocument(); |
||
123 | if (tableDocument.getStore().getDefaultFeatureType().getDefaultGeometryAttribute() != null){ |
||
124 | return tableDocument;
|
||
125 | } |
||
126 | |||
127 | } |
||
128 | return null; |
||
129 | } |
||
130 | |||
131 | protected boolean isStoreOfAnyType(TableDocument tableDocument, int[] geomTypes) { |
||
132 | try {
|
||
133 | FeatureType featureType = tableDocument.getStore().getDefaultFeatureType(); |
||
134 | GeometryType geometryType = featureType.getDefaultGeometryAttribute().getGeomType(); |
||
135 | for (int i = 0; i < geomTypes.length; i++) { |
||
136 | if (geometryType.isTypeOf(geomTypes[i])) {
|
||
137 | return true; |
||
138 | } |
||
139 | } |
||
140 | } catch (ReadException e) {
|
||
141 | LOG.error("Error reading the geometry type", e);
|
||
142 | } catch (DataException e) {
|
||
143 | LOG.error("Error reading the geometry type", e);
|
||
144 | } |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | public void execute(String actionCommand) { |
||
149 | try {
|
||
150 | TableDocument tableDocument = getActiveTableDocument(); |
||
151 | if (tableDocument == null) { |
||
152 | return;
|
||
153 | } |
||
154 | Component parentWindow =
|
||
155 | (Component)ApplicationLocator.getManager().getUIManager().getActiveWindow();
|
||
156 | int addField = JOptionPane.showConfirmDialog(parentWindow, Messages.getText("new_field_query"), |
||
157 | Messages.getText("info"), JOptionPane.YES_NO_OPTION); |
||
158 | |||
159 | if (addField == JOptionPane.YES_OPTION){ |
||
160 | FeatureStore featureStore = tableDocument.getStore(); |
||
161 | execute(actionCommand, featureStore); |
||
162 | } |
||
163 | } catch (Exception e) { |
||
164 | LOG.error("Not possible to add the new field", e);
|
||
165 | NotificationManager.addError(e); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /**
|
||
170 | * Executes a command for the given {@link FeatureStore}.
|
||
171 | *
|
||
172 | * @param actionCommand
|
||
173 | * to execute
|
||
174 | * @param featureStore
|
||
175 | * to use
|
||
176 | * @throws Exception
|
||
177 | * if there is an error while executing
|
||
178 | */
|
||
179 | protected abstract void execute(String actionCommand, |
||
180 | FeatureStore featureStore) throws Exception; |
||
181 | |||
182 | } |