KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > actions > order > AbstractChangeOrderSelectionAction


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
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 *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26 package org.nightlabs.editor2d.actions.order;
27
28 import java.util.Collections JavaDoc;
29 import java.util.Comparator JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
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 /**
44  * An Abstract Base Implementation of a generic reorder action
45  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
46  */

47 public abstract class AbstractChangeOrderSelectionAction
48 extends AbstractEditorSelectionAction
49 {
50
51     /**
52      * @param editor
53      * @param style
54      */

55     public AbstractChangeOrderSelectionAction(AbstractEditor editor, int style) {
56         super(editor, style);
57     }
58
59     /**
60      * @param editor
61      */

62     public AbstractChangeOrderSelectionAction(AbstractEditor editor) {
63         super(editor);
64     }
65
66 // /**
67
// *@return true, if objects are selected
68
// */
69
// protected boolean calculateEnabled() {
70
// return !getSelectedObjects().isEmpty();
71
// }
72

73     /**
74      *@return true, if objects are selected, except the RootEditPart or LayerEditParts
75      */

76     protected boolean calculateEnabled() {
77         return !getDefaultSelection(false).isEmpty();
78     }
79     
80     /**
81      * executes a Command which changes the order, based on the newIndex and the
82      * DrawComponentContainer for all selected Objects
83      */

84     public void run()
85     {
86         List JavaDoc dcs = getSelection(DrawComponent.class, true);
87         if (!dcs.isEmpty())
88         {
89             if (getPrimarySelected().getModel() instanceof DrawComponent) {
90                 primarySelected = (DrawComponent) getPrimarySelected().getModel();
91             }
92             // sort the selection dependend on their index
93
Collections.sort(dcs, indexComparator);
94             CompoundCommand compoundCmd = new CompoundCommand();
95             for (Iterator JavaDoc 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     /**
109      *
110      * @return the primary selected DrawComponent
111      * @see
112      */

113     public DrawComponent getPrimarySelectedDrawComponent() {
114         return primarySelected;
115     }
116     
117     /**
118      * compares the index of 2 DrawComponents
119      */

120     protected Comparator JavaDoc indexComparator = new Comparator JavaDoc()
121     {
122         public int compare(Object JavaDoc o1, Object JavaDoc 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     /**
152      * compares the index of 2 DrawComponents if they are in the same container
153      */

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     /**
167      * compares the index of 2 DrawComponents if they are NOT in the same container,
168      * then the index of the parents are compared
169      */

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     /**
183      *
184      * @param dc the DrawComponent to reorder
185      * @param container the DrawComponentContainer the dc should be reordered to
186      * @param newIndex the newIndex in the List of the DrawComponentContainer
187      * @return a DrawComponentReorderCommand which executes the reordering
188      */

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     /**
215      *
216      * @return the newIndex of the List of DrawComponents in the
217      * given {@link DrawComponentContainer}
218      * @see AbstractChangeOrderSelection#getContainer()
219      */

220     public abstract int getNewIndex();
221     
222     /**
223      *
224      * @return the {@link DrawComponentContainer} where the selected {@link DrawComponent}
225      * should be reordered to
226      */

227     public abstract DrawComponentContainer getContainer();
228 }
229
Popular Tags