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 / PasteAlphanumericValuesFromClipboardExtension.java @ 46277
History | View | Annotate | Download (10.2 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 |
|
30 |
import javax.swing.JOptionPane; |
31 |
import org.apache.commons.lang.mutable.MutableInt; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
|
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
import org.gvsig.andami.IconThemeHelper; |
38 |
import org.gvsig.andami.plugins.Extension; |
39 |
import org.gvsig.app.ApplicationLocator; |
40 |
import org.gvsig.app.ApplicationManager; |
41 |
import org.gvsig.app.project.documents.view.ViewDocument; |
42 |
import org.gvsig.app.project.documents.view.ViewManager; |
43 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.mapcontext.MapContext; |
46 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
47 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
48 |
import org.gvsig.tools.ToolsLocator; |
49 |
import org.gvsig.tools.i18n.I18nManager; |
50 |
import org.gvsig.tools.logger.FilteredLogger; |
51 |
|
52 |
import javax.json.JsonObject; |
53 |
import javax.json.Json; |
54 |
import javax.json.JsonArray; |
55 |
import javax.json.JsonReader; |
56 |
import org.gvsig.app.project.documents.Document; |
57 |
import org.gvsig.fmap.dal.exception.DataException; |
58 |
import org.gvsig.fmap.dal.feature.Feature; |
59 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
60 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
61 |
import org.gvsig.fmap.dal.feature.FeatureType; |
62 |
|
63 |
@SuppressWarnings({"UseSpecificCatch", "Convert2Lambda", "ConvertToTryWithResources"}) |
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((FLayer layer) -> layer instanceof FLyrVect |
127 |
&& layer.isActive() |
128 |
&& layer.isAvailable() |
129 |
&& layer.isEditing()); |
130 |
if (layers.isEmpty()) {
|
131 |
return;
|
132 |
} |
133 |
FLyrVect layer = (FLyrVect) layers.get(0);
|
134 |
FeatureStore store = layer.getFeatureStore(); |
135 |
FeatureSelection selectionFeatures; |
136 |
try {
|
137 |
selectionFeatures = store.getFeatureSelection(); |
138 |
} catch (DataException ex) {
|
139 |
return;
|
140 |
} |
141 |
try {
|
142 |
if (selectionFeatures.isEmpty()) {
|
143 |
return;
|
144 |
} |
145 |
} catch (Exception ex) { |
146 |
return;
|
147 |
} |
148 |
FilteredLogger theLogger = new FilteredLogger(LOGGER, "PasteFeatures", 10); |
149 |
Object obj = jsonArray.get(0); |
150 |
if (!(obj instanceof JsonObject)) { |
151 |
return;
|
152 |
} |
153 |
JsonObject fjson = (JsonObject) obj; |
154 |
try {
|
155 |
boolean hasPk = false; |
156 |
FeatureType ft = store.getDefaultFeatureTypeQuietly(); |
157 |
for (String name : fjson.keySet()) { |
158 |
FeatureAttributeDescriptor attr = ft.getAttributeDescriptor(name); |
159 |
if( attr!=null ) { |
160 |
if( attr.isPrimaryKey() ) {
|
161 |
hasPk = true;
|
162 |
break;
|
163 |
} |
164 |
} |
165 |
} |
166 |
final boolean copyPk = hasPk && (application.confirmDialog( |
167 |
i18n.getTranslation("_Copy_primary_key_values_XQuestionX"),
|
168 |
i18n.getTranslation("_Pasting_features_from_clipboard"),
|
169 |
JOptionPane.YES_NO_OPTION,
|
170 |
JOptionPane.QUESTION_MESSAGE,
|
171 |
"_CopyPrimaryKeyWhenPasteAlphanumericValuesFromClipboard"
|
172 |
) == JOptionPane.YES_OPTION);
|
173 |
EditableFeature f = store.createNewFeature(fjson); |
174 |
|
175 |
for (Feature selectionFeature : selectionFeatures) {
|
176 |
EditableFeature eds = selectionFeature.getEditable(); |
177 |
eds.copyFrom(f, new Predicate<FeatureAttributeDescriptor>() { |
178 |
@Override
|
179 |
public boolean test(FeatureAttributeDescriptor attr) { |
180 |
String nameGeom;
|
181 |
try {
|
182 |
nameGeom = store.getDefaultFeatureType().getDefaultGeometryAttributeName(); |
183 |
} catch (DataException ex) {
|
184 |
nameGeom = null;
|
185 |
} |
186 |
if (StringUtils.equals(attr.getName(), nameGeom)) {
|
187 |
return false; |
188 |
} |
189 |
if( !copyPk && attr.isPrimaryKey() ) {
|
190 |
return false; |
191 |
} |
192 |
return true; |
193 |
} |
194 |
}); |
195 |
selectionFeatures.update(eds); |
196 |
} |
197 |
} catch (Exception ex) { |
198 |
theLogger.warn("Can't create feature from JSON.", ex);
|
199 |
} |
200 |
mapContext.invalidate(); |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public boolean isEnabled() { |
205 |
ApplicationManager application = ApplicationLocator.getManager(); |
206 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
207 |
if (viewdoc == null) { |
208 |
return false; |
209 |
} |
210 |
String clipboardValue = application.getFromClipboard();
|
211 |
if (StringUtils.isBlank(clipboardValue)) {
|
212 |
return false; |
213 |
} |
214 |
JsonArray jsonArray; |
215 |
try {
|
216 |
JsonReader jsonReader = Json.createReader(new StringReader(clipboardValue)); |
217 |
jsonArray = jsonReader.readArray(); |
218 |
jsonReader.close(); |
219 |
|
220 |
} catch (Exception ex) { |
221 |
return false; |
222 |
} |
223 |
if (jsonArray.size() != 1) { |
224 |
return false; |
225 |
} |
226 |
MapContext mapContext = viewdoc.getMapContext(); |
227 |
MutableInt numActiveVectorial = new MutableInt(0); |
228 |
MutableInt numActiveVectorialEditable = new MutableInt(0); |
229 |
MutableInt hasSelection = new MutableInt(0); |
230 |
List<FLayer> layers = mapContext.getLayers().getLayers(new Predicate<FLayer>() { |
231 |
@Override
|
232 |
public boolean test(FLayer layer) { |
233 |
if (layer instanceof FLyrVect |
234 |
&& layer.isActive() |
235 |
&& layer.isAvailable()) { |
236 |
numActiveVectorial.increment(); |
237 |
if (layer.isEditing()) {
|
238 |
numActiveVectorialEditable.increment(); |
239 |
} |
240 |
if (!((FLyrVect) layer).getFeatureStore().isFeatureSelectionEmpty()) {
|
241 |
hasSelection.increment(); |
242 |
} |
243 |
} |
244 |
// No recuperamos ninguna capa, solo contamos las activa en edicion
|
245 |
return false; |
246 |
} |
247 |
}); |
248 |
if (numActiveVectorialEditable.intValue() == 1 |
249 |
&& numActiveVectorial.intValue() == 1
|
250 |
&& hasSelection.intValue() > 0) {
|
251 |
return true; |
252 |
} |
253 |
return false; |
254 |
} |
255 |
|
256 |
@Override
|
257 |
public boolean isVisible() { |
258 |
ApplicationManager application = ApplicationLocator.getManager(); |
259 |
Document activeDocument = application.getActiveDocument(ViewManager.TYPENAME);
|
260 |
return activeDocument != null; |
261 |
} |
262 |
|
263 |
} |