1 26 package org.nightlabs.editor2d.actions.order; 27 28 import java.util.Collections ; 29 import java.util.Comparator ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import org.eclipse.gef.commands.Command; 34 import org.eclipse.gef.commands.CompoundCommand; 35 import org.nightlabs.editor2d.AbstractEditor; 36 import org.nightlabs.editor2d.DrawComponent; 37 import org.nightlabs.editor2d.DrawComponentContainer; 38 import org.nightlabs.editor2d.Layer; 39 import org.nightlabs.editor2d.actions.AbstractEditorSelectionAction; 40 import org.nightlabs.editor2d.command.DrawComponentReorderCommand; 41 import org.nightlabs.editor2d.util.OrderUtil; 42 43 47 public abstract class AbstractChangeOrderSelectionAction 48 extends AbstractEditorSelectionAction 49 { 50 51 55 public AbstractChangeOrderSelectionAction(AbstractEditor editor, int style) { 56 super(editor, style); 57 } 58 59 62 public AbstractChangeOrderSelectionAction(AbstractEditor editor) { 63 super(editor); 64 } 65 66 73 76 protected boolean calculateEnabled() { 77 return !getDefaultSelection(false).isEmpty(); 78 } 79 80 84 public void run() 85 { 86 List dcs = getSelection(DrawComponent.class, true); 87 if (!dcs.isEmpty()) 88 { 89 if (getPrimarySelected().getModel() instanceof DrawComponent) { 90 primarySelected = (DrawComponent) getPrimarySelected().getModel(); 91 } 92 Collections.sort(dcs, indexComparator); 94 CompoundCommand compoundCmd = new CompoundCommand(); 95 for (Iterator it = dcs.iterator(); it.hasNext(); ) 96 { 97 DrawComponent dc = (DrawComponent) it.next(); 98 Command cmd = changeOrder(dc, getContainer(), getNewIndex()); 99 compoundCmd.add(cmd); 100 } 101 compoundCmd.setLabel(getText()); 102 execute(compoundCmd); 103 } 104 } 105 106 protected DrawComponent primarySelected = null; 107 108 113 public DrawComponent getPrimarySelectedDrawComponent() { 114 return primarySelected; 115 } 116 117 120 protected Comparator indexComparator = new Comparator () 121 { 122 public int compare(Object o1, Object o2) 123 { 124 if (o1 instanceof DrawComponent && o2 instanceof DrawComponent) 125 { 126 DrawComponent dc1 = (DrawComponent) o1; 127 DrawComponent dc2 = (DrawComponent) o2; 128 if (primarySelected != null) 129 { 130 DrawComponentContainer primaryContainer = primarySelected.getParent(); 131 if (!dc1.getParent().equals(primaryContainer) || !dc2.getParent().equals(primaryContainer)) 132 { 133 if (dc1.getParent().equals(primaryContainer) && !dc2.getParent().equals(primaryContainer)) 134 return 1; 135 if (!dc1.getParent().equals(primaryContainer) && dc2.getParent().equals(primaryContainer)) 136 return -1; 137 else { 138 return compareIndexInDifferentContainer(dc1, dc2); 139 } 140 } 141 else 142 return compareIndexInSameContainer(dc1, dc2); 143 } 144 else 145 return compareIndexInSameContainer(dc1, dc2); 146 } 147 return 0; 148 } 149 }; 150 151 154 protected int compareIndexInSameContainer(DrawComponent dc1, DrawComponent dc2) 155 { 156 int index1 = dc1.getParent().getDrawComponents().indexOf(dc1); 157 int index2 = dc2.getParent().getDrawComponents().indexOf(dc2); 158 if (index1 > index2) 159 return 1; 160 if (index1 < index2) 161 return -1; 162 else 163 return 0; 164 } 165 166 170 protected int compareIndexInDifferentContainer(DrawComponent dc1, DrawComponent dc2) 171 { 172 int parentIndex1 = dc1.getParent().getParent().getDrawComponents().indexOf(dc1.getParent()); 173 int parentIndex2 = dc2.getParent().getParent().getDrawComponents().indexOf(dc2.getParent()); 174 if (parentIndex1 > parentIndex2) 175 return 1; 176 if (parentIndex1 < parentIndex2) 177 return -1; 178 else 179 return 0; 180 } 181 182 189 public Command changeOrder(DrawComponent dc, DrawComponentContainer container, int newIndex) 190 { 191 return new DrawComponentReorderCommand(dc, container, newIndex); 192 } 193 194 public Layer getCurrentLayer() 195 { 196 if (getPrimarySelected() != null) 197 { 198 DrawComponent dc = (DrawComponent) getPrimarySelected().getModel(); 199 return dc.getRoot().getCurrentLayer(); 200 } 201 return null; 202 } 203 204 public int getLastIndex(DrawComponentContainer container) 205 { 206 return OrderUtil.getLastIndex(container); 207 } 208 209 public int indexOf(DrawComponent dc) 210 { 211 return OrderUtil.indexOf(dc); 212 } 213 214 220 public abstract int getNewIndex(); 221 222 227 public abstract DrawComponentContainer getContainer(); 228 } 229 | Popular Tags |