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