svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / clipboard / PasteFeaturesFromClipboardExtension.java @ 46277
History | View | Annotate | Download (11.3 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.app.extension.clipboard; |
25 |
|
26 |
import java.io.StringReader; |
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
import java.util.function.Predicate; |
30 |
|
31 |
import javax.swing.JOptionPane; |
32 |
import org.apache.commons.lang.mutable.MutableInt; |
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
|
35 |
import org.slf4j.Logger; |
36 |
import org.slf4j.LoggerFactory; |
37 |
|
38 |
import org.gvsig.andami.IconThemeHelper; |
39 |
import org.gvsig.andami.plugins.Extension; |
40 |
import org.gvsig.app.ApplicationLocator; |
41 |
import org.gvsig.app.ApplicationManager; |
42 |
import org.gvsig.app.project.documents.view.ViewDocument; |
43 |
import org.gvsig.app.project.documents.view.ViewManager; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.geom.Geometry; |
47 |
import org.gvsig.fmap.geom.GeometryUtils; |
48 |
import org.gvsig.fmap.geom.primitive.Envelope; |
49 |
import org.gvsig.fmap.mapcontext.MapContext; |
50 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
51 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
52 |
import org.gvsig.tools.ToolsLocator; |
53 |
import org.gvsig.tools.i18n.I18nManager; |
54 |
import org.gvsig.tools.logger.FilteredLogger; |
55 |
|
56 |
import javax.json.JsonObject; |
57 |
import javax.json.Json; |
58 |
import javax.json.JsonArray; |
59 |
import javax.json.JsonReader; |
60 |
import org.gvsig.fmap.dal.exception.DataException; |
61 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
62 |
import org.gvsig.fmap.dal.feature.FeatureType; |
63 |
|
64 |
|
65 |
public class PasteFeaturesFromClipboardExtension extends Extension { |
66 |
|
67 |
private static final Logger LOGGER = LoggerFactory.getLogger(PasteFeaturesFromClipboardExtension.class); |
68 |
|
69 |
@Override
|
70 |
public void initialize() { |
71 |
|
72 |
IconThemeHelper.registerIcon("action", "layer-modify-clipboard-paste", this); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void execute(String actionCommand) { |
77 |
|
78 |
if (actionCommand.compareToIgnoreCase("layer-modify-clipboard-paste") != 0) { |
79 |
return;
|
80 |
} |
81 |
ApplicationManager application = ApplicationLocator.getManager(); |
82 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
83 |
if( viewdoc == null ) { |
84 |
return;
|
85 |
} |
86 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
87 |
String clipboardValue = application.getFromClipboard();
|
88 |
if( StringUtils.isBlank(clipboardValue) ) {
|
89 |
application.messageDialog( |
90 |
i18n.getTranslation("_Clipboard_has_no_valid_info_or_is_empty"),
|
91 |
null,
|
92 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
93 |
JOptionPane.INFORMATION_MESSAGE,
|
94 |
"ClipboardEmptyWhenPasteFeatures"
|
95 |
); |
96 |
return;
|
97 |
} |
98 |
|
99 |
JsonArray jsonArray; |
100 |
try {
|
101 |
JsonReader jsonReader = Json.createReader(new StringReader(clipboardValue)); |
102 |
jsonArray = jsonReader.readArray(); |
103 |
jsonReader.close(); |
104 |
} catch(Exception ex) { |
105 |
LOGGER.warn("Can't get JSON from the clipboard.", ex);
|
106 |
application.messageDialog( |
107 |
i18n.getTranslation("_Clipboard_has_no_valid_info_or_is_empty"),
|
108 |
null,
|
109 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
110 |
JOptionPane.WARNING_MESSAGE,
|
111 |
"ClipboardNotValidWhenPasteFeatures"
|
112 |
); |
113 |
return;
|
114 |
} |
115 |
if( jsonArray.size()<1 ) { |
116 |
application.messageDialog( |
117 |
i18n.getTranslation("_No_features_found_in_clipboard"),
|
118 |
null,
|
119 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
120 |
JOptionPane.WARNING_MESSAGE,
|
121 |
"ClipboardNotHasElementsWhenPasteFeatures"
|
122 |
); |
123 |
return;
|
124 |
} |
125 |
|
126 |
|
127 |
MapContext mapContext = viewdoc.getMapContext(); |
128 |
List<FLayer> layers = mapContext.getLayers().getLayers(new Predicate<FLayer>() { |
129 |
@Override
|
130 |
public boolean test(FLayer layer) { |
131 |
return layer instanceof FLyrVect && |
132 |
layer.isActive() && |
133 |
layer.isAvailable() && |
134 |
layer.isEditing(); |
135 |
} |
136 |
}); |
137 |
if( layers.isEmpty() ) {
|
138 |
return;
|
139 |
} |
140 |
FLyrVect layer = (FLyrVect) layers.get(0);
|
141 |
FeatureStore store = layer.getFeatureStore(); |
142 |
FeatureType ft = store.getDefaultFeatureTypeQuietly(); |
143 |
boolean hasPk = false; |
144 |
|
145 |
if( !jsonArray.isEmpty() ) {
|
146 |
JsonObject fjson0 = (JsonObject) jsonArray.get(0);
|
147 |
for (String name : fjson0.keySet()) { |
148 |
FeatureAttributeDescriptor attr = ft.getAttributeDescriptor(name); |
149 |
if( attr!=null ) { |
150 |
if( attr.isPrimaryKey() ) {
|
151 |
hasPk = true;
|
152 |
break;
|
153 |
} |
154 |
} |
155 |
} |
156 |
} |
157 |
final boolean copyPk; |
158 |
if( hasPk ) {
|
159 |
int n = application.confirmDialog(
|
160 |
i18n.getTranslation("_Copy_primary_key_values_XQuestionX"),
|
161 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
162 |
JOptionPane.YES_NO_OPTION,
|
163 |
JOptionPane.QUESTION_MESSAGE,
|
164 |
"_CopyPrimaryKeyWhenPasteFeaturesFromClipboard"
|
165 |
); |
166 |
copyPk = ( n == JOptionPane.YES_OPTION );
|
167 |
} else {
|
168 |
copyPk = false;
|
169 |
} |
170 |
|
171 |
|
172 |
List<EditableFeature> features = new ArrayList<>(); |
173 |
FilteredLogger theLogger = new FilteredLogger(LOGGER, "PasteFeatures", 10); |
174 |
for (Object obj : jsonArray) { |
175 |
if( !(obj instanceof JsonObject ) ) { |
176 |
continue;
|
177 |
} |
178 |
JsonObject fjson = (JsonObject) obj; |
179 |
try {
|
180 |
EditableFeature f = store.createNewFeature(false);
|
181 |
f.copyFrom(fjson, new Predicate<FeatureAttributeDescriptor>() { |
182 |
@Override
|
183 |
public boolean test(FeatureAttributeDescriptor attr) { |
184 |
if( !copyPk && attr.isPrimaryKey() ) {
|
185 |
return false; |
186 |
} |
187 |
return true; |
188 |
} |
189 |
}); |
190 |
features.add(f); |
191 |
} catch (Exception ex) { |
192 |
theLogger.warn("Can't create feature from JSON.", ex);
|
193 |
} |
194 |
} |
195 |
if( features.isEmpty() ) {
|
196 |
application.messageDialog( |
197 |
i18n.getTranslation("_No_features_found_in_clipboard"),
|
198 |
null,
|
199 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
200 |
JOptionPane.WARNING_MESSAGE,
|
201 |
"ClipboardNotHasFeaturesWhenPasteFeatures"
|
202 |
); |
203 |
return;
|
204 |
} |
205 |
|
206 |
int inserteds = 0; |
207 |
theLogger = new FilteredLogger(LOGGER, "PasteFeatures", 10); |
208 |
Envelope envelope = GeometryUtils.createEnvelope(Geometry.SUBTYPES.GEOM2D); |
209 |
for (EditableFeature feature : features) {
|
210 |
try {
|
211 |
store.insert(feature); |
212 |
inserteds++; |
213 |
try {
|
214 |
envelope.add(feature.getDefaultGeometry()); |
215 |
} catch(Exception ex) { |
216 |
theLogger.warn("Can't calculate envelope.", ex);
|
217 |
} |
218 |
} catch(Exception ex) { |
219 |
theLogger.warn("Can't insert feature.", ex);
|
220 |
} |
221 |
} |
222 |
StringBuilder msg = new StringBuilder(); |
223 |
msg.append(i18n.getTranslation("_Number_of_features_pasted_from_clipboard"));
|
224 |
msg.append(": ");
|
225 |
msg.append(inserteds); |
226 |
msg.append("\n");
|
227 |
msg.append(i18n.getTranslation("_Number_of_features_from_clipboard_discarded_due_to_bad_format"));
|
228 |
msg.append(": ");
|
229 |
msg.append(jsonArray.size() - inserteds); |
230 |
if( inserteds<1 || envelope.isEmpty() ) { |
231 |
application.messageDialog( |
232 |
msg.toString(), |
233 |
null,
|
234 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
235 |
JOptionPane.WARNING_MESSAGE,
|
236 |
"ClipboardNotInsertFeaturesWhenPasteFeatures"
|
237 |
); |
238 |
return;
|
239 |
} |
240 |
msg.append("\n\n");
|
241 |
msg.append(i18n.getTranslation("_Zoom_to_added_features_question"));
|
242 |
int r = application.confirmDialog(
|
243 |
msg.toString(), |
244 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
245 |
JOptionPane.YES_NO_OPTION,
|
246 |
JOptionPane.QUESTION_MESSAGE,
|
247 |
"ZoomToInsertedFeaturesWhenPasteFeatures"
|
248 |
); |
249 |
if( r == JOptionPane.NO_OPTION ) { |
250 |
return;
|
251 |
} |
252 |
try {
|
253 |
viewdoc.getMapContext().getViewPort().setEnvelope(envelope); |
254 |
} catch(Exception ex) { |
255 |
LOGGER.warn("Can't zoom to envelope.", ex);
|
256 |
} |
257 |
} |
258 |
|
259 |
@Override
|
260 |
public boolean isEnabled() { |
261 |
ApplicationManager application = ApplicationLocator.getManager(); |
262 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
263 |
if( viewdoc == null ) { |
264 |
return false; |
265 |
} |
266 |
String clipboardValue = application.getFromClipboard();
|
267 |
if( StringUtils.isBlank(clipboardValue) ) {
|
268 |
return false; |
269 |
} |
270 |
MapContext mapContext = viewdoc.getMapContext(); |
271 |
MutableInt numActiveVectorial = new MutableInt(0); |
272 |
MutableInt numActiveVectorialEditable = new MutableInt(0); |
273 |
List<FLayer> layers = mapContext.getLayers().getLayers(new Predicate<FLayer>() { |
274 |
@Override
|
275 |
public boolean test(FLayer layer) { |
276 |
if (layer instanceof FLyrVect && |
277 |
layer.isActive() && |
278 |
layer.isAvailable() ) { |
279 |
numActiveVectorial.increment(); |
280 |
if (layer.isEditing()) {
|
281 |
numActiveVectorialEditable.increment(); |
282 |
} |
283 |
} |
284 |
// No recuperamos ninguna capa, solo contamos las activa en edicion
|
285 |
return false; |
286 |
} |
287 |
}); |
288 |
if (numActiveVectorialEditable.intValue() == 1 && |
289 |
numActiveVectorial.intValue() == 1) {
|
290 |
return true; |
291 |
} |
292 |
return false; |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public boolean isVisible() { |
297 |
ApplicationManager application = ApplicationLocator.getManager(); |
298 |
return application.getActiveDocument(ViewManager.TYPENAME)!=null; |
299 |
} |
300 |
|
301 |
} |