KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > actions > AbstractEditorSelectionAction


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.actions;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.eclipse.gef.EditPart;
37 import org.eclipse.gef.GraphicalViewer;
38 import org.eclipse.gef.RootEditPart;
39 import org.eclipse.gef.ui.actions.SelectionAction;
40 import org.eclipse.jface.viewers.IStructuredSelection;
41 import org.eclipse.jface.viewers.StructuredSelection;
42 import org.eclipse.swt.widgets.Shell;
43
44 import org.nightlabs.editor2d.AbstractEditor;
45 import org.nightlabs.editor2d.DrawComponent;
46 import org.nightlabs.editor2d.Layer;
47 import org.nightlabs.editor2d.MultiLayerDrawComponent;
48 import org.nightlabs.editor2d.edit.LayerEditPart;
49
50 public abstract class AbstractEditorSelectionAction
51 extends SelectionAction
52 {
53
54     public AbstractEditorSelectionAction(AbstractEditor editor, int style) {
55         super(editor, style);
56     }
57
58     public AbstractEditorSelectionAction(AbstractEditor editor) {
59         super(editor);
60     }
61
62     protected abstract boolean calculateEnabled();
63
64     public boolean isActiveEditor()
65     {
66         if (getWorkbenchPart().getSite().getWorkbenchWindow().getActivePage().getActiveEditor().equals(getEditor())) {
67             return true;
68         }
69         return false;
70     }
71     
72     /**
73      *
74      * @return the AbstractEditor
75      * @see org.nightlabs.editor2d.AbstractEditor
76      */

77     public AbstractEditor getEditor() {
78         return (AbstractEditor) getWorkbenchPart();
79     }
80     
81     /**
82      *
83      * @return the MultiLayerDrawComponent of the AbstractEditor
84      * @see org.nightlabs.editor2d.MultiLayerDrawComponent
85      */

86     public MultiLayerDrawComponent getMultiLayerDrawComponent()
87     {
88         return getEditor().getMultiLayerDrawComponent();
89     }
90     
91     /**
92      *
93      * @param clazz the Class to search for
94      * @param amount the minimum amount of occurences of the given class
95      * in the selected objects #
96      * @param model determines if the Class-Check should be performed on the
97      * selected EditParts or the model (DrawComponent) of the EditParts
98      * @return true if the selected objects contain minimum so many EditPart
99      * as the given amount of the given class
100      */

101     public boolean selectionContains(Class JavaDoc clazz, int amount, boolean model)
102     {
103         if (!getSelectedObjects().isEmpty())
104         {
105             int counter = 0;
106             for (Iterator JavaDoc it = getSelectedObjects().iterator(); it.hasNext(); )
107             {
108                 EditPart editPart = (EditPart) it.next();
109                 Class JavaDoc c = null;
110                 if (!model)
111                     c = editPart.getClass();
112                 else
113                     c = editPart.getModel().getClass();
114                 
115                 if (clazz.isAssignableFrom(c)) {
116                     counter++;
117                     if (amount == counter)
118                         return true;
119                 }
120             }
121         }
122         return false;
123     }
124     
125     /**
126      * A Convenice Method which calls selectionContains with the amount 1
127      * @see selectionContains(Class clazz, int amount, boolean model)
128      */

129     public boolean selectionContains(Class JavaDoc clazz, boolean model) {
130         return selectionContains(clazz, 1, model);
131     }
132     
133     protected static List JavaDoc EMPTY_LIST = new LinkedList JavaDoc();
134     
135     /**
136      *
137      * @param clazz the Class to search for
138      * @param model determines if the Class-Check should be performed on the
139      * selected EditParts or the model (DrawComponent) of the EditParts
140      * @return a List of all objects from the selection which are assignable
141      * from the given class
142      */

143     public List JavaDoc getSelection(Class JavaDoc clazz, boolean model)
144     {
145         if (!getSelectedObjects().isEmpty())
146         {
147             List JavaDoc selection = new ArrayList JavaDoc();
148             for (Iterator JavaDoc it = getSelectedObjects().iterator(); it.hasNext(); )
149             {
150                 EditPart editPart = (EditPart) it.next();
151                 Class JavaDoc c = null;
152                 if (!model)
153                     c = editPart.getClass();
154                 else
155                     c = editPart.getModel().getClass();
156                 
157                 if (clazz.isAssignableFrom(c))
158                 {
159                     if (!model)
160                         selection.add(clazz.cast(editPart));
161                     else
162                         selection.add(clazz.cast(editPart.getModel()));
163                 }
164             }
165             return selection;
166         }
167         return EMPTY_LIST;
168     }
169     
170     /**
171      *
172      * @param excludeClasses a Collection of Class-Objects which should be excluded
173      * from the selection
174      * @param model determines if the Class-Check should be performed on the
175      * selected EditParts or the model (DrawComponent) of the EditParts
176      * @return a List of all selected objects (editParts or DrawComponents, determined by model),
177      * except those who are assignableFrom a Class included in the excludeList
178      * @see Class#isAssignableFrom(Class)
179      */

180     public List JavaDoc getSelection(Collection JavaDoc excludeClasses, boolean model)
181     {
182         if (!getSelectedObjects().isEmpty())
183         {
184             List JavaDoc selection = new ArrayList JavaDoc();
185             for (Iterator JavaDoc it = getSelectedObjects().iterator(); it.hasNext(); )
186             {
187                 EditPart editPart = (EditPart) it.next();
188                 Class JavaDoc c = null;
189                 if (!model)
190                     c = editPart.getClass();
191                 else
192                     c = editPart.getModel().getClass();
193                 
194                 for (Iterator JavaDoc itExclude = excludeClasses.iterator(); itExclude.hasNext(); )
195                 {
196                     Class JavaDoc clazz = (Class JavaDoc) itExclude.next();
197                         
198                     if (clazz.isAssignableFrom(c))
199                     {
200                         if (!model) {
201                             if (selection.contains(editPart))
202                                 selection.remove(editPart);
203                         }
204                         else {
205                             if (selection.contains(editPart.getModel()))
206                                 selection.remove(editPart.getModel());
207                         }
208                     }
209                     else {
210                         if (!model)
211                             selection.add(editPart);
212                         else
213                             selection.add(editPart.getModel());
214                     }
215                 }
216             }
217             return selection;
218         }
219         return EMPTY_LIST;
220     }
221     
222     protected Collection JavaDoc defaultEditPartExcludeList = null;
223     protected Collection JavaDoc defaultModelExcludeList = null;
224     public Collection JavaDoc getDefaultExcludeList(boolean model)
225     {
226         if (!model) {
227             if (defaultEditPartExcludeList == null) {
228                 defaultEditPartExcludeList = new LinkedList JavaDoc();
229                 defaultEditPartExcludeList.add(RootEditPart.class);
230                 defaultEditPartExcludeList.add(LayerEditPart.class);
231             }
232             return defaultEditPartExcludeList;
233         }
234         else {
235             if (defaultModelExcludeList == null) {
236                 defaultModelExcludeList = new LinkedList JavaDoc();
237                 defaultModelExcludeList.add(MultiLayerDrawComponent.class);
238                 defaultModelExcludeList.add(Layer.class);
239             }
240             return defaultModelExcludeList;
241         }
242     }
243     
244     public List JavaDoc getDefaultSelection(boolean model)
245     {
246         return getSelection(getDefaultExcludeList(model), model);
247     }
248     
249     public IStructuredSelection getStructuredSelection() {
250         return (IStructuredSelection) getSelection();
251     }
252     
253     public EditPart getPrimarySelected() {
254         return (EditPart) getStructuredSelection().getFirstElement();
255     }
256     
257     /**
258      *
259      * @return the GraphicalViewer of the AbstractEditor
260      * @see org.eclipse.gef.GraphicalViewer
261      */

262     public GraphicalViewer getGraphicalViewer() {
263         return getEditor().getOutlineGraphicalViewer();
264     }
265     
266     /**
267      *
268      * @param model the DrawComponent to find its EditPart
269      * @return the corresponding EditPart
270      *
271      */

272     public EditPart getEditPart(DrawComponent model) {
273         return (EditPart) getGraphicalViewer().getEditPartRegistry().get(model);
274     }
275     
276     /**
277      *
278      * @param drawComponents a List of DrawComponents to find a EditParts for
279      * @return a List of the corresponding EditParts
280      */

281     public List JavaDoc getEditParts(List JavaDoc drawComponents)
282     {
283         List JavaDoc editParts = new ArrayList JavaDoc();
284         for (Iterator JavaDoc it = drawComponents.iterator(); it.hasNext(); )
285         {
286             DrawComponent dc = (DrawComponent) it.next();
287             EditPart ep = getEditPart(dc);
288             editParts.add(ep);
289         }
290         return editParts;
291     }
292     
293     /**
294      * selects the EditParts in the GraphicalViewer for the the given List
295      * of drawComponents
296      * @param drawComponents
297      */

298     public void selectEditPart(List JavaDoc drawComponents)
299     {
300         List JavaDoc editParts = getEditParts(drawComponents);
301         getGraphicalViewer().setSelection(new StructuredSelection(editParts));
302     }
303     
304     /**
305      *
306      * @return the Shell of the Site for the AbstractEditor
307      */

308     public Shell getShell() {
309         return getEditor().getSite().getShell();
310     }
311
312     public void dispose()
313     {
314         super.dispose();
315     }
316         
317 }
318
Popular Tags