Revision 44655 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/clipboard/CopyFeaturesToClipboardExtension.java
CopyFeaturesToClipboardExtension.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.app.extension.clipboard; |
25 | 25 |
|
26 |
import javax.json.JsonArray; |
|
26 | 27 |
import javax.swing.JOptionPane; |
27 | 28 |
|
28 | 29 |
import org.slf4j.Logger; |
29 | 30 |
import org.slf4j.LoggerFactory; |
30 | 31 |
|
31 | 32 |
import org.gvsig.andami.IconThemeHelper; |
32 |
import org.gvsig.andami.PluginServices; |
|
33 | 33 |
import org.gvsig.andami.plugins.Extension; |
34 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
|
35 | 34 |
import org.gvsig.app.ApplicationLocator; |
36 |
import org.gvsig.app.extension.clipboard.util.FeatureTextUtils;
|
|
37 |
import org.gvsig.app.project.documents.view.gui.IView;
|
|
38 |
import org.gvsig.fmap.dal.exception.DataException;
|
|
35 |
import org.gvsig.app.ApplicationManager;
|
|
36 |
import org.gvsig.app.project.documents.view.ViewDocument;
|
|
37 |
import org.gvsig.app.project.documents.view.ViewManager;
|
|
39 | 38 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
41 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
|
39 |
import org.gvsig.fmap.mapcontext.MapContext; |
|
42 | 40 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
43 |
import org.gvsig.i18n.Messages; |
|
41 |
import org.gvsig.tools.ToolsLocator; |
|
42 |
import org.gvsig.tools.i18n.I18nManager; |
|
44 | 43 |
|
45 |
|
|
46 | 44 |
public class CopyFeaturesToClipboardExtension extends Extension { |
47 | 45 |
|
48 |
private static Logger logger = LoggerFactory.getLogger( |
|
49 |
CopyFeaturesToClipboardExtension.class); |
|
46 |
private static final Logger LOGGER = LoggerFactory.getLogger(CopyFeaturesToClipboardExtension.class); |
|
50 | 47 |
|
51 |
public void initialize() {
|
|
48 |
public void initialize() {
|
|
52 | 49 |
|
53 |
IconThemeHelper.registerIcon("action", "layer-modify-clipboard-copy", this);
|
|
54 |
}
|
|
50 |
IconThemeHelper.registerIcon("action", "layer-modify-clipboard-copy", this);
|
|
51 |
}
|
|
55 | 52 |
|
56 |
public void execute(String actionCommand) {
|
|
53 |
public void execute(String actionCommand) {
|
|
57 | 54 |
|
58 |
if (actionCommand.compareToIgnoreCase("layer-modify-clipboard-copy") != 0) { |
|
59 |
return; |
|
60 |
} |
|
55 |
if (actionCommand.compareToIgnoreCase("layer-modify-clipboard-copy") != 0) { |
|
56 |
return; |
|
57 |
} |
|
58 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
59 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
|
60 |
if( viewdoc == null ) { |
|
61 |
return; |
|
62 |
} |
|
63 |
MapContext mapContext = viewdoc.getMapContext(); |
|
64 |
FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer(); |
|
65 |
if( layer==null ) { |
|
66 |
return; |
|
67 |
} |
|
68 |
try { |
|
69 |
FeatureSelection selection = layer.getFeatureStore().getFeatureSelection(); |
|
61 | 70 |
|
62 |
IWindow actw = actWin(); |
|
71 |
JsonArray json = selection.toJSON(); |
|
72 |
application.putInClipboard(json.toString()); |
|
63 | 73 |
|
64 |
if (actw instanceof IView) { |
|
74 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
75 |
application.messageDialog( |
|
76 |
i18n.getTranslation("_Number_of_features_copied_to_clipboard") |
|
77 |
+ ": " + selection.getSize(), |
|
78 |
null, |
|
79 |
i18n.getTranslation("_Copy_selected_features_to_clipboard"), |
|
80 |
JOptionPane.INFORMATION_MESSAGE, |
|
81 |
"CopyFeaturesToClipboard" |
|
82 |
); |
|
65 | 83 |
|
66 |
IView vw = (IView) actw;
|
|
67 |
FLayer[] act_lyr = vw.getMapControl().getMapContext().getLayers().getActives();
|
|
68 |
if (act_lyr == null || act_lyr.length != 1
|
|
69 |
|| !(act_lyr[0] instanceof FLyrVect)) {
|
|
84 |
} catch (Exception e) {
|
|
85 |
LOGGER.warn("Can't get the selection as JSON.", e);
|
|
86 |
}
|
|
87 |
}
|
|
70 | 88 |
|
71 |
} else { |
|
89 |
public boolean isEnabled() { |
|
90 |
// it's enabled if there is exactly one vector layer in the active view |
|
91 |
// and it has a selection |
|
72 | 92 |
|
73 |
int usr_opt = JOptionPane.showConfirmDialog( |
|
74 |
ApplicationLocator.getManager().getRootComponent(), |
|
75 |
Messages.getText("_Include_alphanumeric_attributes_Question"), |
|
76 |
Messages.getText("_Copy_selected_features_to_clipboard"), |
|
77 |
JOptionPane.YES_NO_CANCEL_OPTION, |
|
78 |
JOptionPane.QUESTION_MESSAGE); |
|
93 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
94 |
ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
|
95 |
if( viewdoc == null ) { |
|
96 |
return false; |
|
97 |
} |
|
98 |
MapContext mapContext = viewdoc.getMapContext(); |
|
99 |
FLyrVect layer = mapContext.getLayers().getFirstActiveVectorLayer(); |
|
100 |
if( layer==null ) { |
|
101 |
return false; |
|
102 |
} |
|
103 |
if (!layer.isAvailable()) { |
|
104 |
// This can happen when opening a persisted project |
|
105 |
// and there is a "slow" layer (GeoDB) |
|
106 |
return false; |
|
107 |
} |
|
108 |
try { |
|
109 |
FeatureSelection selection = layer.getFeatureStore().getFeatureSelection(); |
|
110 |
return !selection.isEmpty(); |
|
111 |
} catch (Exception ex) { |
|
112 |
LOGGER.warn("Can't get selection from layer '"+layer.getName()+"'.", ex); |
|
113 |
return false; |
|
114 |
} |
|
115 |
} |
|
79 | 116 |
|
117 |
public boolean isVisible() { |
|
118 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
119 |
return application.getActiveDocument(ViewManager.TYPENAME)!=null; |
|
120 |
} |
|
80 | 121 |
|
81 |
if (usr_opt == JOptionPane.CANCEL_OPTION) { |
|
82 |
return; |
|
83 |
} |
|
84 |
|
|
85 |
FLyrVect vect = (FLyrVect) act_lyr[0]; |
|
86 |
FeatureSelection sele = null; |
|
87 |
FeatureStore fsto = null; |
|
88 |
try { |
|
89 |
fsto = vect.getFeatureStore(); |
|
90 |
sele = (FeatureSelection) fsto.getSelection(); |
|
91 |
|
|
92 |
StringBuilder strb = FeatureTextUtils.toString( |
|
93 |
sele, fsto.getDefaultFeatureType(), |
|
94 |
usr_opt == JOptionPane.YES_OPTION); |
|
95 |
/* |
|
96 |
* Put selected features in clipboard. |
|
97 |
*/ |
|
98 |
PluginServices.putInClipboard(strb.toString()); |
|
99 |
|
|
100 |
JOptionPane.showMessageDialog( |
|
101 |
ApplicationLocator.getManager().getRootComponent(), |
|
102 |
Messages.getText("_Number_of_features_copied_to_clipboard") |
|
103 |
+ ": " + sele.getSize() + " ", |
|
104 |
Messages.getText("_Copy_selected_features_to_clipboard"), |
|
105 |
JOptionPane.INFORMATION_MESSAGE); |
|
106 |
|
|
107 |
} catch (DataException e) { |
|
108 |
logger.error("While getting store and selection. ", e); |
|
109 |
} |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
} |
|
114 |
|
|
115 |
public boolean isEnabled() { |
|
116 |
|
|
117 |
/* |
|
118 |
* It's enabled if there is exactly one vector layer in the active view |
|
119 |
* and it has a selection |
|
120 |
*/ |
|
121 |
|
|
122 |
IWindow actw = actWin(); |
|
123 |
|
|
124 |
if (actw instanceof IView) { |
|
125 |
|
|
126 |
IView vw = (IView) actw; |
|
127 |
FLayer[] act_lyr = vw.getMapControl().getMapContext().getLayers().getActives(); |
|
128 |
if (act_lyr == null || act_lyr.length != 1 |
|
129 |
|| !(act_lyr[0] instanceof FLyrVect)) { |
|
130 |
return false; |
|
131 |
|
|
132 |
} else { |
|
133 |
FLyrVect vect = (FLyrVect) act_lyr[0]; |
|
134 |
if (!vect.isAvailable()) { |
|
135 |
/* |
|
136 |
* This can happen when opening a persisted project |
|
137 |
* and there is a "slow" layer (GeoDB) |
|
138 |
*/ |
|
139 |
return false; |
|
140 |
} |
|
141 |
FeatureSelection sele = null; |
|
142 |
long sele_count = 0; |
|
143 |
try { |
|
144 |
sele = (FeatureSelection) vect.getFeatureStore().getSelection(); |
|
145 |
sele_count = sele.getSize(); |
|
146 |
} catch (DataException e) { |
|
147 |
logger.error("While getting store. ", e); |
|
148 |
return false; |
|
149 |
} |
|
150 |
return sele_count > 0; |
|
151 |
} |
|
152 |
|
|
153 |
} else { |
|
154 |
return false; |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
public boolean isVisible() { |
|
159 |
|
|
160 |
return actWin() instanceof IView; |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* Gets active window |
|
165 |
* @return |
|
166 |
*/ |
|
167 |
private IWindow actWin() { |
|
168 |
return ApplicationLocator.getManager().getActiveWindow(); |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 | 122 |
} |
Also available in: Unified diff