KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.eclipse.gef.EditPart;
35 import org.eclipse.gef.GraphicalViewer;
36 import org.eclipse.gef.ui.actions.EditorPartAction;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.swt.widgets.Shell;
39
40 import org.nightlabs.editor2d.AbstractEditor;
41 import org.nightlabs.editor2d.DrawComponent;
42 import org.nightlabs.editor2d.Layer;
43 import org.nightlabs.editor2d.MultiLayerDrawComponent;
44
45 public abstract class AbstractEditorAction
46 extends EditorPartAction
47 {
48     public AbstractEditorAction(AbstractEditor editor, int style) {
49         super(editor, style);
50     }
51
52     public AbstractEditorAction(AbstractEditor editor) {
53         super(editor);
54     }
55
56     protected abstract boolean calculateEnabled();
57     
58     /**
59      *
60      * @return the AbstractEditor
61      * @see org.nightlabs.editor2d.AbstractEditor
62      */

63     public AbstractEditor getEditor() {
64         return (AbstractEditor) getWorkbenchPart();
65     }
66     
67     /**
68      *
69      * @return the MultiLayerDrawComponent of the AbstractEditor
70      * @see org.nightlabs.editor2d.MultiLayerDrawComponent
71      */

72     public MultiLayerDrawComponent getMultiLayerDrawComponent() {
73         return getEditor().getMultiLayerDrawComponent();
74     }
75     
76     /**
77      *
78      * @return the currentLayer of the MultiLayerDrawComponent
79      */

80     public Layer getCurrentLayer() {
81         return getMultiLayerDrawComponent().getCurrentLayer();
82     }
83     
84     /**
85      *
86      * @return the GraphicalViewer of the AbstractEditor
87      * @see org.eclipse.gef.GraphicalViewer
88      */

89     public GraphicalViewer getGraphicalViewer() {
90         return getEditor().getOutlineGraphicalViewer();
91     }
92     
93     /**
94      *
95      * @param model the DrawComponent to find its EditPart
96      * @return the corresponding EditPart
97      *
98      */

99     public EditPart getEditPart(DrawComponent model) {
100         return (EditPart) getGraphicalViewer().getEditPartRegistry().get(model);
101     }
102     
103     /**
104      *
105      * @param drawComponents a List of DrawComponents to find a EditParts for
106      * @return a List of the corresponding EditParts
107      */

108     public List JavaDoc getEditParts(List JavaDoc drawComponents)
109     {
110         List JavaDoc editParts = new ArrayList JavaDoc();
111         for (Iterator JavaDoc it = drawComponents.iterator(); it.hasNext(); )
112         {
113             DrawComponent dc = (DrawComponent) it.next();
114             EditPart ep = getEditPart(dc);
115             editParts.add(ep);
116         }
117         return editParts;
118     }
119     
120     /**
121      * selects the EditParts in the GraphicalViewer for the the given List
122      * of drawComponents
123      * @param drawComponents
124      */

125     public void selectEditPart(List JavaDoc drawComponents)
126     {
127         List JavaDoc editParts = getEditParts(drawComponents);
128         getGraphicalViewer().setSelection(new StructuredSelection(editParts));
129     }
130     
131     /**
132      *
133      * @return the Shell of the Site for the AbstractEditor
134      */

135     public Shell getShell() {
136         return getEditor().getSite().getShell();
137     }
138     
139     /**
140      * @return true if the AbstractEditor is the active Editor of the Workbench
141      */

142     public boolean isActiveEditor()
143     {
144         // TODO: find out why getActivePage() always return null
145
if (getWorkbenchPart().getSite().getWorkbenchWindow().getActivePage() != null)
146         {
147             if (getWorkbenchPart().getSite().getWorkbenchWindow().getActivePage().getActiveEditor().equals(getEditor())) {
148                 return true;
149             }
150         }
151         return false;
152     }
153 }
154
Popular Tags