1 19 20 package org.netbeans.modules.xml.xam.ui; 21 22 import java.awt.datatransfer.Transferable ; 23 import java.io.IOException ; 24 import java.util.Arrays ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Set ; 28 import org.netbeans.modules.xml.xam.Component; 29 import org.netbeans.modules.xml.xam.Model; 30 import org.netbeans.modules.xml.xam.Nameable; 31 import org.netbeans.modules.xml.xam.Named; 32 import org.netbeans.modules.xml.xam.ui.cookies.GetComponentCookie; 33 import org.openide.nodes.Node; 34 import org.openide.nodes.NodeTransfer; 35 import org.openide.util.datatransfer.PasteType; 36 37 44 public class ComponentPasteType { 45 46 private static final int[] STANDARD_OPERATIONS = new int[] { 47 NodeTransfer.COPY, 48 NodeTransfer.MOVE 49 }; 50 51 60 public static PasteType getPasteType(Component target, 61 Transferable transfer, Class <? extends Component> type) { 62 return getPasteType(target, transfer, type, STANDARD_OPERATIONS, -1); 63 } 64 65 76 public static PasteType getDropType(Component target, 77 Transferable transfer, Class <? extends Component> type, 78 int action, int index) { 79 return getPasteType(target, transfer, type, STANDARD_OPERATIONS, index); 83 } 84 85 96 private static PasteType getPasteType(Component target, 97 Transferable transfer, Class <? extends Component> type, 98 int[] operations, int index) { 99 PasteType retVal = null; 100 for (int oper : operations) { 102 Node[] nodes = NodeTransfer.nodes(transfer, oper); 104 if (nodes != null) { 105 if (canPaste(nodes, target, oper, type)) { 107 retVal = new PasteTypeImpl(Arrays.asList(nodes), target, 108 oper, index); 109 break; 110 } 111 } 112 } 113 return retVal; 114 } 115 116 126 private static boolean canPaste(Node[] nodes, Component target, 127 int operation, Class <? extends Component> type) { 128 Set <Node> pasteableNodes = new HashSet <Node>(); 129 for (Node pasteableNode : nodes) { 130 GetComponentCookie gcc = (GetComponentCookie) pasteableNode. 132 getLookup().lookup(GetComponentCookie.class); 133 if (gcc != null) { 134 Component pasteableComponent = gcc.getComponent(); 135 if ((type == null || 139 type.isAssignableFrom(gcc.getComponentType())) && 140 pasteableComponent.getModel() != null && 141 target.canPaste(pasteableComponent)) { 142 boolean isCopyPaste = (operation & NodeTransfer.COPY) != 0; 143 boolean isCutPaste = (operation & NodeTransfer.MOVE) != 0 && 145 !(pasteableComponent.getParent().equals(target)) && 146 pasteableNode.canDestroy(); 147 if (isCopyPaste || isCutPaste) { 148 if (isCutPaste) { 149 Component parent = target; 151 while (parent != null) { 152 if (parent.equals(pasteableComponent)) { 153 return false; 154 } 155 parent = parent.getParent(); 156 } 157 } 158 pasteableNodes.add(pasteableNode); 161 } 162 } 163 } 164 } 165 return pasteableNodes.size() == nodes.length; 166 } 167 168 171 private static class PasteTypeImpl extends PasteType { 172 173 private Component target; 174 175 private List <Node> nodes; 176 177 private int operation; 178 179 private int index; 180 181 189 private PasteTypeImpl(List <Node> nodes, 190 Component target, int operation, int index) { 191 this.target = target; 192 this.nodes = nodes; 193 this.operation = operation; 194 if (index < 0) { 195 this.index = 0; 199 } else { 200 this.index = index; 201 } 202 } 203 204 @SuppressWarnings ("unchecked") 205 public Transferable paste() throws IOException { 206 if (target != null && nodes.size() > 0) { 208 Model model = target.getModel(); 209 GetComponentCookie gcc = (GetComponentCookie) nodes.get(0). 210 getCookie(GetComponentCookie.class); 211 if (gcc == null) { 212 return null; 214 } 215 Model srcModel = gcc.getComponent().getModel(); 216 model.startTransaction(); 221 try { 222 for (Node node : nodes) { 223 gcc = (GetComponentCookie) node.getCookie( 224 GetComponentCookie.class); 225 Component child = gcc.getComponent(); 226 Component copy = child.copy(target); 231 if (copy != null) { 235 if ((operation & NodeTransfer.MOVE) != 0) { 236 boolean srcInTransaction = srcModel.isIntransaction(); 239 try { 240 if (!srcInTransaction) { 241 srcModel.startTransaction(); 244 } 245 srcModel.removeChildComponent(child); 246 } finally { 247 if (!srcInTransaction) { 248 srcModel.endTransaction(); 249 } 250 } 251 } 252 if (copy instanceof Nameable) { 255 String name = ((Nameable) copy).getName(); 256 String preferredName = name; 257 HashSet <String > nameSet = new HashSet <String >(); 258 for (Object sibling : target.getChildren()) { 259 if (sibling instanceof Named) { 260 nameSet.add(((Named) sibling).getName()); 261 } 262 } 263 int unique = 1; 264 while (nameSet.contains(name)) { 265 name = preferredName + unique; 266 unique++; 267 } 268 ((Nameable) copy).setName(name); 269 } 270 model.addChildComponent(target, copy, index); 272 } 273 } 274 } finally { 275 model.endTransaction(); 276 } 277 } 278 return null; 279 } 280 281 public String toString() { 282 return "PasteTypeImpl=[operation=" + operation + ",index=" + index + "]"; 283 } 284 } 285 } 286 | Popular Tags |