svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toc / actions / PasteLayersTocMenuEntry.java @ 41094
History | View | Annotate | Download (4.51 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.project.documents.view.toc.actions; |
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.app.ApplicationLocator; |
35 |
import org.gvsig.app.extension.ProjectExtension; |
36 |
import org.gvsig.app.project.Project; |
37 |
import org.gvsig.app.project.documents.view.toc.AbstractTocContextMenuAction; |
38 |
import org.gvsig.app.project.documents.view.toc.ITocItem; |
39 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
40 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
41 |
import org.gvsig.i18n.Messages; |
42 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
43 |
|
44 |
|
45 |
public class PasteLayersTocMenuEntry extends AbstractTocContextMenuAction { |
46 |
|
47 |
private static Logger logger = |
48 |
LoggerFactory.getLogger(PasteLayersTocMenuEntry.class); |
49 |
|
50 |
public String getGroup() { |
51 |
return "copyPasteLayer"; |
52 |
} |
53 |
|
54 |
public int getGroupOrder() { |
55 |
return 60; |
56 |
} |
57 |
|
58 |
public int getOrder() { |
59 |
return 2; |
60 |
} |
61 |
|
62 |
public String getText() { |
63 |
return PluginServices.getText(this, "pegar"); |
64 |
} |
65 |
|
66 |
public boolean isEnabled(ITocItem item, FLayer[] selectedItems) { |
67 |
if (isTocItemBranch(item)) {
|
68 |
FLayer lyr = getNodeLayer(item); |
69 |
if (lyr instanceof FLayers) { |
70 |
/*
|
71 |
* We cannot parse it here because it can
|
72 |
* lead to bad error dialog. We accept anything.
|
73 |
* When user tries to paste, the error
|
74 |
* message will show if there is a problem.
|
75 |
*/
|
76 |
return !CopyPasteLayersUtils.isClipboardEmpty();
|
77 |
} |
78 |
|
79 |
} else if (!isTocItemLeaf(item)) { |
80 |
if (getNodeLayer(item) == null) { |
81 |
/*
|
82 |
* We cannot parse it here because it can
|
83 |
* lead to bad error dialog. We accept anything.
|
84 |
* When user tries to paste, the error
|
85 |
* message will show if there is a problem.
|
86 |
*/
|
87 |
return !CopyPasteLayersUtils.isClipboardEmpty();
|
88 |
} |
89 |
} |
90 |
return false; |
91 |
} |
92 |
|
93 |
/**
|
94 |
* This method can return null (if content of clipboard not useful)
|
95 |
*
|
96 |
* @return
|
97 |
*/
|
98 |
private FLayers getFLayersFromClipboard() {
|
99 |
|
100 |
FLayers resp = null;
|
101 |
try {
|
102 |
resp = CopyPasteLayersUtils.getClipboardAsFLayers(); |
103 |
// resp can be null here if clipboard content not useful
|
104 |
} catch (PersistenceException e) {
|
105 |
ApplicationLocator.getManager().message( |
106 |
Messages.getText("_Clipboard_error"),
|
107 |
JOptionPane.ERROR_MESSAGE);
|
108 |
resp = null;
|
109 |
} |
110 |
return resp;
|
111 |
} |
112 |
|
113 |
public void execute(ITocItem item, FLayer[] selectedItems) { |
114 |
FLayers target_root; |
115 |
|
116 |
FLayers clipboard_root = this.getFLayersFromClipboard();
|
117 |
if (clipboard_root == null) { |
118 |
JOptionPane.showMessageDialog(
|
119 |
(Component)PluginServices.getMainFrame(),
|
120 |
Messages.getText("No_ha_sido_posible_realizar_la_operacion"),
|
121 |
Messages.getText("pegar"),
|
122 |
JOptionPane.ERROR_MESSAGE
|
123 |
); |
124 |
logger.info("Unable to parse clipboard as flayers");
|
125 |
return;
|
126 |
} |
127 |
|
128 |
if (isTocItemBranch(item)) {
|
129 |
target_root = (FLayers)getNodeLayer(item); |
130 |
} else if (getNodeLayer(item) == null){ |
131 |
target_root = getMapContext().getLayers(); |
132 |
} else {
|
133 |
return;
|
134 |
} |
135 |
|
136 |
getMapContext().beginAtomicEvent(); |
137 |
|
138 |
boolean isOK = CopyPasteLayersUtils.addLayers(clipboard_root, target_root);
|
139 |
|
140 |
getMapContext().endAtomicEvent(); |
141 |
|
142 |
if (isOK) {
|
143 |
getMapContext().invalidate(); |
144 |
Project project=((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject(); |
145 |
project.setModified(true);
|
146 |
} |
147 |
} |
148 |
|
149 |
|
150 |
} |