1 23 24 package org.objectweb.fractal.gui.clipboard.model; 25 26 import org.objectweb.fractal.gui.model.Factory; 27 import org.objectweb.fractal.gui.model.Component; 28 import org.objectweb.fractal.gui.model.Interface; 29 import org.objectweb.fractal.gui.model.ClientInterface; 30 import org.objectweb.fractal.gui.model.ServerInterface; 31 import org.objectweb.fractal.gui.graph.model.GraphModel; 32 import org.objectweb.fractal.gui.graph.model.BasicGraphModel; 33 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.HashMap ; 37 38 41 42 public class BasicClipboard implements Clipboard { 43 44 47 48 private Component original; 49 50 54 55 private Component copy; 56 57 60 61 private GraphModel graph; 62 63 66 67 public BasicClipboard () { 68 graph = new BasicGraphModel(); 69 } 70 71 75 public boolean canCut (final Component srcComponent) { 76 return srcComponent != null && srcComponent.getParent() != null; 77 } 78 79 public void cut ( 80 final Component srcComponent, 81 final GraphModel srcGraph, 82 final Factory srcFactory) 83 { 84 original = srcComponent; 85 if (original.getMasterComponent() == null) { 86 copy = clone(srcComponent, srcGraph, graph, srcFactory); 87 } 88 Component parent = srcComponent.getParent(); 89 parent.removeSubComponent(srcComponent); 90 } 91 92 public boolean canCopy (final Component srcComponent) { 93 return srcComponent != null; 94 } 95 96 public void copy ( 97 final Component srcComponent, 98 final GraphModel srcGraph, 99 final Factory srcFactory) 100 { 101 original = srcComponent; 102 if (original.getMasterComponent() == null) { 103 copy = clone(srcComponent, srcGraph, graph, srcFactory); 104 } 105 } 106 107 public boolean canPaste (final Component dstComponent) { 108 if (original != null && original.getMasterComponent() != null) { 109 return canPasteAsShared(dstComponent); 110 } else if (dstComponent != null && copy != null) { 111 if (copy.containsSlaveOfExternalComponent(copy)) { 112 return dstComponent.getRootComponent() == original.getRootComponent(); 113 } else { 114 return true; 115 } 116 } 117 return false; 118 } 119 120 public void paste ( 121 final Component dstComponent, 122 final GraphModel dstGraph, 123 final Factory dstFactory) 124 { 125 if (original != null && original.getMasterComponent() != null) { 126 pasteAsShared(dstComponent, dstGraph, dstFactory); 127 } else { 128 Component child = clone(copy, graph, dstGraph, dstFactory); 129 dstComponent.addSubComponent(child); 130 } 131 } 132 133 public boolean canPasteAsShared (final Component dstComponent) { 134 if (dstComponent != null && original != null) { 135 return dstComponent.getRootComponent() == original.getRootComponent(); 137 } 138 return false; 139 } 140 141 public void pasteAsShared ( 142 final Component dstComponent, 143 final GraphModel dstGraph, 144 final Factory dstFactory) 145 { 146 Component child = dstFactory.createComponent(original); 147 dstComponent.addSubComponent(child); 148 } 149 150 154 163 164 private Component clone ( 165 final Component c, 166 final GraphModel srcGraph, 167 final GraphModel dstGraph, 168 final Factory f) 169 { 170 if (c.getMasterComponent() != null) { 171 throw new RuntimeException ("Internal error"); 172 } 173 Map clones = new HashMap (); 174 cloneMasterComponents(c, f, clones); 175 cloneSlaveComponents(c, f, clones); 176 return clone(c, srcGraph, dstGraph, clones); 177 } 178 179 190 191 private void cloneMasterComponents ( 192 final Component c, 193 final Factory f, 194 final Map clones) 195 { 196 if (c.getMasterComponent() == null) { 197 Component d = f.createComponent(); 198 Clone clone = new Clone(d); 199 200 List itfs = c.getServerInterfaces(); 202 for (int i = 0; i < itfs.size(); ++i) { 203 Interface itf = (Interface)itfs.get(i); 204 if (itf.isCollection() && itf.getMasterCollectionInterface() != null) { 205 continue; 207 } 208 Interface citf = f.createServerInterface(d); 209 citf.setName(itf.getName()); 210 citf.setSignature(itf.getSignature()); 211 citf.setIsOptional(itf.isOptional()); 212 citf.setIsCollection(itf.isCollection()); 213 d.addServerInterface((ServerInterface)citf); 214 clone.interfaceClones.put(itf, citf); 215 clone.interfaceClones.put( 216 itf.getComplementaryInterface(), citf.getComplementaryInterface()); 217 } 218 219 itfs = c.getClientInterfaces(); 221 for (int i = 0; i < itfs.size(); ++i) { 222 Interface itf = (Interface)itfs.get(i); 223 if (itf.isCollection() && itf.getMasterCollectionInterface() != null) { 224 continue; 226 } 227 Interface citf = f.createClientInterface(d); 228 citf.setName(itf.getName()); 229 citf.setSignature(itf.getSignature()); 230 citf.setIsOptional(itf.isOptional()); 231 citf.setIsCollection(itf.isCollection()); 232 d.addClientInterface((ClientInterface)citf); 233 clone.interfaceClones.put(itf, citf); 234 clone.interfaceClones.put( 235 itf.getComplementaryInterface(), citf.getComplementaryInterface()); 236 } 237 238 clones.put(c, clone); 239 } 240 List subComponents = c.getSubComponents(); 241 for (int i = 0; i < subComponents.size(); ++i) { 242 cloneMasterComponents((Component)subComponents.get(i), f, clones); 243 } 244 } 245 246 258 259 private void cloneSlaveComponents ( 260 final Component c, 261 final Factory f, 262 final Map clones) 263 { 264 Component master = c.getMasterComponent(); 265 if (master != null) { 266 if (clones.get(master) != null) { 267 master = ((Clone)clones.get(master)).clone; 268 } 269 clones.put(c, new Clone(f.createComponent(master))); 270 } 271 List subComponents = c.getSubComponents(); 272 for (int i = 0; i < subComponents.size(); ++i) { 273 cloneSlaveComponents((Component)subComponents.get(i), f, clones); 274 } 275 } 276 277 293 294 private Component clone ( 295 final Component c, 296 final GraphModel srcGraph, 297 final GraphModel dstGraph, 298 final Map clones) 299 { 300 Clone clone = (Clone)clones.get(c); 301 Component d = clone.clone; 302 d.setName(c.getName()); 303 d.setType(c.getType()); 304 d.setImplementation(c.getImplementation()); 305 306 d.setAttributeController(c.getAttributeController()); 308 List attrNames = c.getAttributeNames(); 309 for (int i = 0; i < attrNames.size(); ++i) { 310 String attrName = (String )attrNames.get(i); 311 d.setAttribute(attrName, c.getAttribute(attrName)); 312 } 313 d.setTemplateControllerDescriptor(c.getTemplateControllerDescriptor()); 314 d.setComponentControllerDescriptor(c.getComponentControllerDescriptor()); 315 316 List subComponents = c.getSubComponents(); 318 for (int i = 0; i < subComponents.size(); ++i) { 319 d.addSubComponent( 320 clone((Component)subComponents.get(i), srcGraph, dstGraph, clones)); 321 } 322 323 List itfs = c.getServerInterfaces(); 326 for (int i = 0; i < itfs.size(); ++i) { 327 Interface itf = (Interface)itfs.get(i); 328 ClientInterface citf = (ClientInterface)itf.getComplementaryInterface(); 329 if (citf.getBinding() != null) { 330 ServerInterface sitf = citf.getBinding().getServerInterface(); 331 Clone sclone = (Clone)clones.get(sitf.getOwner()); 332 if (sclone != null) { 333 citf = (ClientInterface)clone.interfaceClones.get(citf); 334 sitf = (ServerInterface)sclone.interfaceClones.get(sitf); 335 if (citf != null && sitf != null) { 336 citf.getOwner().bind(citf, citf.getName(), sitf); 337 } 338 } 339 } 340 } 341 for (int i = 0; i < subComponents.size(); ++i) { 342 Component subComponent = (Component)subComponents.get(i); 343 itfs = subComponent.getClientInterfaces(); 344 for (int j = 0; j < itfs.size(); ++j) { 345 ClientInterface citf = (ClientInterface)itfs.get(j); 346 if (citf.getBinding() != null) { 347 ServerInterface sitf = citf.getBinding().getServerInterface(); 348 Clone cclone = (Clone)clones.get(citf.getOwner()); 349 Clone sclone = (Clone)clones.get(sitf.getOwner()); 350 if (cclone != null && sclone != null) { 351 if (citf.getMasterCollectionInterface() != null) { 352 citf = (ClientInterface)citf.getMasterCollectionInterface(); 353 } 354 citf = (ClientInterface)cclone.interfaceClones.get(citf); 355 sitf = (ServerInterface)sclone.interfaceClones.get(sitf); 356 if (citf != null && sitf != null) { 357 citf.getOwner().bind(citf, citf.getName(), sitf); 358 } 359 } 360 } 361 } 362 } 363 364 if (srcGraph != null && dstGraph != null) { 366 dstGraph.setComponentPosition(d, srcGraph.getComponentPosition(c)); 367 dstGraph.setComponentColor(d, srcGraph.getComponentColor(c)); 368 } 369 370 return d; 371 } 372 373 376 377 static class Clone { 378 379 382 383 public Component clone; 384 385 389 390 public Map interfaceClones; 391 392 397 398 public Clone (final Component clone) { 399 this.clone = clone; 400 this.interfaceClones = new HashMap (); 401 } 402 } 403 } 404 | Popular Tags |