svn-gvsig-desktop / branches / org.gvsig.desktop-cvsgis1 / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / clipboard / PasteAlphanumericValuesFromClipboardExtension.java @ 45288
History | View | Annotate | Download (9.45 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.List; |
28 |
import java.util.function.Predicate; |
29 |
import java.util.logging.Level; |
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.mapcontext.MapContext; |
48 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
49 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.gvsig.tools.i18n.I18nManager; |
52 |
import org.gvsig.tools.logger.FilteredLogger; |
53 |
|
54 |
import javax.json.JsonObject; |
55 |
import javax.json.Json; |
56 |
import javax.json.JsonArray; |
57 |
import javax.json.JsonReader; |
58 |
import org.gvsig.app.project.documents.Document; |
59 |
import org.gvsig.fmap.dal.exception.DataException; |
60 |
import org.gvsig.fmap.dal.feature.Feature; |
61 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
62 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
63 |
|
64 |
public class PasteAlphanumericValuesFromClipboardExtension extends Extension { |
65 |
|
66 |
private static final Logger LOGGER = LoggerFactory.getLogger(PasteAlphanumericValuesFromClipboardExtension.class); |
67 |
|
68 |
@Override
|
69 |
public void initialize() { |
70 |
|
71 |
IconThemeHelper.registerIcon("action", "layer-modify-clipboard-paste-alphanumeric", this); |
72 |
} |
73 |
|
74 |
@Override
|
75 |
public void execute(String actionCommand) { |
76 |
|
77 |
if (actionCommand.compareToIgnoreCase("layer-modify-clipboard-paste-alphanumeric") != 0) { |
78 |
return;
|
79 |
} |
80 |
ApplicationManager application = ApplicationLocator.getManager(); |
81 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
82 |
if (viewdoc == null) { |
83 |
return;
|
84 |
} |
85 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
86 |
String clipboardValue = application.getFromClipboard();
|
87 |
if (StringUtils.isBlank(clipboardValue)) {
|
88 |
application.messageDialog( |
89 |
i18n.getTranslation("_Clipboard_has_no_valid_info_or_is_empty"),
|
90 |
null,
|
91 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
92 |
JOptionPane.INFORMATION_MESSAGE,
|
93 |
"ClipboardEmptyWhenPasteFeatures"
|
94 |
); |
95 |
return;
|
96 |
} |
97 |
|
98 |
JsonArray jsonArray; |
99 |
try {
|
100 |
JsonReader jsonReader = Json.createReader(new StringReader(clipboardValue)); |
101 |
jsonArray = jsonReader.readArray(); |
102 |
jsonReader.close(); |
103 |
} catch (Exception ex) { |
104 |
LOGGER.warn("Can't get JSON from the clipboard.", ex);
|
105 |
application.messageDialog( |
106 |
i18n.getTranslation("_Clipboard_has_no_valid_info_or_is_empty"),
|
107 |
null,
|
108 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
109 |
JOptionPane.WARNING_MESSAGE,
|
110 |
"ClipboardNotValidWhenPasteFeatures"
|
111 |
); |
112 |
return;
|
113 |
} |
114 |
if (jsonArray.size() < 1) { |
115 |
application.messageDialog( |
116 |
i18n.getTranslation("_No_features_found_in_clipboard"),
|
117 |
null,
|
118 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
119 |
JOptionPane.WARNING_MESSAGE,
|
120 |
"ClipboardNotHasElementsWhenPasteFeatures"
|
121 |
); |
122 |
return;
|
123 |
} |
124 |
|
125 |
MapContext mapContext = viewdoc.getMapContext(); |
126 |
List<FLayer> layers = mapContext.getLayers().getLayers(new Predicate<FLayer>() { |
127 |
@Override
|
128 |
public boolean test(FLayer layer) { |
129 |
return layer instanceof FLyrVect |
130 |
&& layer.isActive() |
131 |
&& layer.isAvailable() |
132 |
&& layer.isEditing(); |
133 |
} |
134 |
}); |
135 |
if (layers.isEmpty()) {
|
136 |
return;
|
137 |
} |
138 |
FLyrVect layer = (FLyrVect) layers.get(0);
|
139 |
FeatureStore store = layer.getFeatureStore(); |
140 |
FeatureSelection selectionFeatures; |
141 |
try {
|
142 |
selectionFeatures = store.getFeatureSelection(); |
143 |
} catch (DataException ex) {
|
144 |
return;
|
145 |
} |
146 |
try {
|
147 |
if (selectionFeatures.isEmpty()) {
|
148 |
return;
|
149 |
} |
150 |
} catch (Exception ex) { |
151 |
return;
|
152 |
} |
153 |
FilteredLogger theLogger = new FilteredLogger(LOGGER, "PasteFeatures", 10); |
154 |
Object obj = jsonArray.get(0); |
155 |
if (!(obj instanceof JsonObject)) { |
156 |
return;
|
157 |
} |
158 |
JsonObject fjson = (JsonObject) obj; |
159 |
try {
|
160 |
EditableFeature f = store.createNewFeature(fjson); |
161 |
|
162 |
|
163 |
for (Feature selectionFeature : selectionFeatures) {
|
164 |
EditableFeature eds = selectionFeature.getEditable(); |
165 |
eds.copyFrom(f, new Predicate<FeatureAttributeDescriptor>() { |
166 |
public boolean test(FeatureAttributeDescriptor attr) { |
167 |
String nameGeom;
|
168 |
try {
|
169 |
nameGeom = store.getDefaultFeatureType().getDefaultGeometryAttributeName(); |
170 |
} catch (DataException ex) {
|
171 |
nameGeom = null;
|
172 |
} |
173 |
if (StringUtils.equals(attr.getName(), nameGeom)) {
|
174 |
return false; |
175 |
} |
176 |
return true; |
177 |
} |
178 |
}); |
179 |
selectionFeatures.update(eds); |
180 |
} |
181 |
} catch (Exception ex) { |
182 |
theLogger.warn("Can't create feature from JSON.", ex);
|
183 |
} |
184 |
mapContext.invalidate(); |
185 |
} |
186 |
|
187 |
//metodo static para escribir en el store
|
188 |
// pasteFromClipboard(FeatureStore store)
|
189 |
|
190 |
// isValidClipboard()
|
191 |
|
192 |
|
193 |
@Override
|
194 |
public boolean isEnabled() { |
195 |
ApplicationManager application = ApplicationLocator.getManager(); |
196 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
197 |
if (viewdoc == null) { |
198 |
return false; |
199 |
} |
200 |
String clipboardValue = application.getFromClipboard();
|
201 |
if (StringUtils.isBlank(clipboardValue)) {
|
202 |
return false; |
203 |
} |
204 |
JsonArray jsonArray; |
205 |
try {
|
206 |
JsonReader jsonReader = Json.createReader(new StringReader(clipboardValue)); |
207 |
jsonArray = jsonReader.readArray(); |
208 |
jsonReader.close(); |
209 |
|
210 |
} catch (Exception ex) { |
211 |
return false; |
212 |
} |
213 |
if (jsonArray.size() != 1) { |
214 |
return false; |
215 |
} |
216 |
MapContext mapContext = viewdoc.getMapContext(); |
217 |
MutableInt numActiveVectorial = new MutableInt(0); |
218 |
MutableInt numActiveVectorialEditable = new MutableInt(0); |
219 |
MutableInt hasSelection = new MutableInt(0); |
220 |
List<FLayer> layers = mapContext.getLayers().getLayers(new Predicate<FLayer>() { |
221 |
@Override
|
222 |
public boolean test(FLayer layer) { |
223 |
if (layer instanceof FLyrVect |
224 |
&& layer.isActive() |
225 |
&& layer.isAvailable()) { |
226 |
numActiveVectorial.increment(); |
227 |
if (layer.isEditing()) {
|
228 |
numActiveVectorialEditable.increment(); |
229 |
} |
230 |
try {
|
231 |
if (!((FLyrVect) layer).getFeatureStore().getFeatureSelection().isEmpty()) {
|
232 |
hasSelection.increment(); |
233 |
} |
234 |
} catch (DataException ex) {
|
235 |
|
236 |
} |
237 |
} |
238 |
// No recuperamos ninguna capa, solo contamos las activa en edicion
|
239 |
return false; |
240 |
} |
241 |
}); |
242 |
if (numActiveVectorialEditable.intValue() == 1 |
243 |
&& numActiveVectorial.intValue() == 1
|
244 |
&& hasSelection.intValue() > 0) {
|
245 |
return true; |
246 |
} |
247 |
return false; |
248 |
} |
249 |
|
250 |
@Override
|
251 |
public boolean isVisible() { |
252 |
ApplicationManager application = ApplicationLocator.getManager(); |
253 |
Document activeDocument = application.getActiveDocument(ViewManager.TYPENAME);
|
254 |
return activeDocument != null; |
255 |
} |
256 |
|
257 |
} |