| 1 |
/* |
|---|
| 2 |
* Moonlight|3D |
|---|
| 3 |
* Copyright (C) 2009 The Moonlight|3D team |
|---|
| 4 |
* |
|---|
| 5 |
* This library is free software; you can redistribute it and/or |
|---|
| 6 |
* modify it under the terms of the GNU Lesser General Public |
|---|
| 7 |
* License as published by the Free Software Foundation; either |
|---|
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
|---|
| 9 |
* |
|---|
| 10 |
* This library is distributed in the hope that it will be useful, |
|---|
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 |
* Lesser General Public License for more details. |
|---|
| 14 |
* |
|---|
| 15 |
* You should have received a copy of the GNU Lesser General Public |
|---|
| 16 |
* License along with this library; if not, write to the Free Software |
|---|
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 |
* |
|---|
| 19 |
* Created on Nov 29, 2009 |
|---|
| 20 |
*/ |
|---|
| 21 |
|
|---|
| 22 |
package eu.moonlight3d.ml3d.modelling.objects; |
|---|
| 23 |
|
|---|
| 24 |
import java.util.ArrayList; |
|---|
| 25 |
|
|---|
| 26 |
import eu.moonlight3d.common.ui.command.Command; |
|---|
| 27 |
import eu.moonlight3d.framework.State; |
|---|
| 28 |
import eu.moonlight3d.framework.localization.L10N; |
|---|
| 29 |
import eu.moonlight3d.framework.properties.BooleanValue; |
|---|
| 30 |
import eu.moonlight3d.framework.properties.IntegerValue; |
|---|
| 31 |
import eu.moonlight3d.framework.properties.StringArrayValue; |
|---|
| 32 |
import eu.moonlight3d.framework.ui.ExceptionDialog; |
|---|
| 33 |
import eu.moonlight3d.ml3d.backend.selection.Selection; |
|---|
| 34 |
import eu.moonlight3d.ml3d.backend.threedim.document.Document; |
|---|
| 35 |
import eu.moonlight3d.ml3d.backend.threedim.helper.OperatorGraphHelper; |
|---|
| 36 |
import eu.moonlight3d.ml3d.backend.threedim.og.Manager; |
|---|
| 37 |
import eu.moonlight3d.ml3d.backend.threedim.og.Node; |
|---|
| 38 |
import eu.moonlight3d.ml3d.backend.threedim.og.Slot; |
|---|
| 39 |
|
|---|
| 40 |
public class DuplicateObjectsCommand implements Command { |
|---|
| 41 |
private int numCopies; |
|---|
| 42 |
private boolean copyChildren; |
|---|
| 43 |
private String[] names; |
|---|
| 44 |
private String previousOGNodeName; |
|---|
| 45 |
private String previousSlotName; |
|---|
| 46 |
private String duplicateObjectsNodeName; |
|---|
| 47 |
|
|---|
| 48 |
public DuplicateObjectsCommand(int numCopies, boolean copyChildren) { |
|---|
| 49 |
this.numCopies=numCopies; |
|---|
| 50 |
this.copyChildren=copyChildren; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
public boolean execute() { |
|---|
| 54 |
try { |
|---|
| 55 |
State state=State.getInstance(); |
|---|
| 56 |
|
|---|
| 57 |
// don't do anything if selected object is not a SG node |
|---|
| 58 |
Selection selection = ((Document) state.getDocumentManager().getMainDocument()).getSelectionManager().getSelection("viewSelection"); |
|---|
| 59 |
if(selection.getType()!=eu.moonlight3d.ml3d.backend.selection.Manager.getSelectionType("SGNode")) { |
|---|
| 60 |
state.logger().warning(L10N.translate("current selection must be a scene graph node")); |
|---|
| 61 |
return false; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
String[] path=selection.getActiveObject().split("/"); |
|---|
| 65 |
previousOGNodeName=path[1]; |
|---|
| 66 |
previousSlotName=path[2]; |
|---|
| 67 |
|
|---|
| 68 |
ArrayList<Object> objects = selection.getSelectionObjects(); |
|---|
| 69 |
names=new String[objects.size()]; |
|---|
| 70 |
for(int i=0;i<objects.size();i++) { |
|---|
| 71 |
names[i]=((eu.moonlight3d.ml3d.backend.threedim.sg.Node) objects.get(i)).getName(); |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
redo(); |
|---|
| 75 |
|
|---|
| 76 |
// clear selection to prevent rogue selection items |
|---|
| 77 |
selection.clearSelection(eu.moonlight3d.ml3d.backend.selection.Manager.getSelectionType("SGNode")); |
|---|
| 78 |
|
|---|
| 79 |
return true; |
|---|
| 80 |
} catch (Exception e) { |
|---|
| 81 |
ExceptionDialog.run(e); |
|---|
| 82 |
} |
|---|
| 83 |
return false; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
public String getBriefDescription() { |
|---|
| 87 |
return L10N.translate("Duplicate Objects"); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
public boolean isUndoable() { |
|---|
| 91 |
return true; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
public void redo() { |
|---|
| 95 |
try { |
|---|
| 96 |
State state=State.getInstance(); |
|---|
| 97 |
|
|---|
| 98 |
Manager ogManager=((Document) state.getDocumentManager().getMainDocument()).getOGManager(); |
|---|
| 99 |
Node ogNode=ogManager.getNodeByName(previousOGNodeName); |
|---|
| 100 |
Slot slot=ogNode.getSlot(previousSlotName); |
|---|
| 101 |
ogNode=ogManager.createNodeByName("DuplicateObjects"); |
|---|
| 102 |
duplicateObjectsNodeName=ogNode.getName(); |
|---|
| 103 |
|
|---|
| 104 |
ogNode.getProperty("name").setValue(new StringArrayValue(names)); |
|---|
| 105 |
ogNode.getProperty("copies").setValue(new IntegerValue(numCopies)); |
|---|
| 106 |
ogNode.getProperty("copyChildren").setValue(new BooleanValue(copyChildren)); |
|---|
| 107 |
|
|---|
| 108 |
// set links |
|---|
| 109 |
ogManager.insertNodeAfter(slot,ogNode.getSlot("input"),ogNode.getSlot("output")); |
|---|
| 110 |
|
|---|
| 111 |
ogManager.update(); |
|---|
| 112 |
} catch (Exception e) { |
|---|
| 113 |
ExceptionDialog.run(e); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
public void undo() { |
|---|
| 119 |
try { |
|---|
| 120 |
State state=State.getInstance(); |
|---|
| 121 |
Manager ogManager=((Document) state.getDocumentManager().getMainDocument()).getOGManager(); |
|---|
| 122 |
Node operation=ogManager.getNodeByName(duplicateObjectsNodeName); |
|---|
| 123 |
if(operation.isDrain()) { |
|---|
| 124 |
OperatorGraphHelper.remove(duplicateObjectsNodeName); |
|---|
| 125 |
} else { |
|---|
| 126 |
OperatorGraphHelper.removeAndBridge(duplicateObjectsNodeName); |
|---|
| 127 |
} |
|---|
| 128 |
ogManager.update(); |
|---|
| 129 |
} catch (Exception e) { |
|---|
| 130 |
ExceptionDialog.run(e); |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
} |
|---|