KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.eclipse.draw2d.PositionConstants;
35 import org.eclipse.draw2d.geometry.PrecisionRectangle;
36 import org.eclipse.draw2d.geometry.Rectangle;
37 import org.eclipse.gef.EditPart;
38 import org.eclipse.gef.GraphicalEditPart;
39 import org.eclipse.gef.Request;
40 import org.eclipse.gef.RequestConstants;
41 import org.eclipse.gef.commands.Command;
42 import org.eclipse.gef.commands.CompoundCommand;
43 import org.eclipse.gef.internal.GEFMessages;
44 import org.eclipse.gef.internal.InternalImages;
45 import org.eclipse.gef.requests.AlignmentRequest;
46 import org.eclipse.gef.tools.ToolUtilities;
47 import org.eclipse.gef.ui.actions.GEFActionConstants;
48 import org.eclipse.gef.ui.actions.SelectionAction;
49 import org.eclipse.ui.IWorkbenchPart;
50
51
52 public class EditorAlignmentAction
53 extends SelectionAction
54 {
55
56   /**
57    * Indicates that the bottom edges should be aligned.
58    */

59   public static final String JavaDoc ID_ALIGN_BOTTOM = GEFActionConstants.ALIGN_BOTTOM;
60
61   /**
62    * Indicates that the horizontal centers should be aligned.
63    */

64   public static final String JavaDoc ID_ALIGN_CENTER = GEFActionConstants.ALIGN_CENTER;
65
66   /**
67    * Indicates that the left edges should be aligned.
68    */

69   public static final String JavaDoc ID_ALIGN_LEFT = GEFActionConstants.ALIGN_LEFT;
70
71   /**
72    * Indicates that the vertical midpoints should be aligned.
73    */

74   public static final String JavaDoc ID_ALIGN_MIDDLE = GEFActionConstants.ALIGN_MIDDLE;
75
76   /**
77    * Indicates that the right edges should be aligned.
78    */

79   public static final String JavaDoc ID_ALIGN_RIGHT = GEFActionConstants.ALIGN_RIGHT;
80
81   /**
82    * Indicates that the top edges should be aligned.
83    */

84   public static final String JavaDoc ID_ALIGN_TOP = GEFActionConstants.ALIGN_TOP;
85   
86   protected int alignment;
87   protected List JavaDoc operationSet;
88
89   /**
90    * Constructs an AlignmentAction with the given part and alignment ID. The alignment ID
91    * must by one of:
92    * <UL>
93    * <LI>GEFActionConstants.ALIGN_LEFT
94    * <LI>GEFActionConstants.ALIGN_RIGHT
95    * <LI>GEFActionConstants.ALIGN_CENTER
96    * <LI>GEFActionConstants.ALIGN_TOP
97    * <LI>GEFActionConstants.ALIGN_BOTTOM
98    * <LI>GEFActionConstants.ALIGN_MIDDLE
99    * </UL>
100    * @param part the workbench part used to obtain context
101    * @param align the aligment ID.
102    */

103   public EditorAlignmentAction(IWorkbenchPart part, int align) {
104     super(part);
105     alignment = align;
106     initUI();
107   }
108
109   /**
110    * Returns the alignment rectangle to which all selected parts should be aligned.
111    * @param request the alignment Request
112    * @return the alignment rectangle
113    */

114   protected Rectangle calculateAlignmentRectangle(Request request) {
115     List JavaDoc editparts = getOperationSet(request);
116     if (editparts == null || editparts.isEmpty())
117         return null;
118     GraphicalEditPart part = (GraphicalEditPart)editparts.get(editparts.size() - 1);
119     Rectangle rect = new PrecisionRectangle(part.getFigure().getBounds());
120     part.getFigure().translateToAbsolute(rect);
121     return rect;
122   }
123
124   /**
125    * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
126    */

127   protected boolean calculateEnabled() {
128     operationSet = null;
129     Command cmd = createAlignmentCommand();
130     if (cmd == null)
131         return false;
132     return cmd.canExecute();
133   }
134
135   private Command createAlignmentCommand() {
136     AlignmentRequest request = new AlignmentRequest(RequestConstants.REQ_ALIGN);
137     request.setAlignmentRectangle(calculateAlignmentRectangle(request));
138     request.setAlignment(alignment);
139     List JavaDoc editparts = getOperationSet(request);
140     if (editparts.size() < 2)
141         return null;
142
143     CompoundCommand command = new CompoundCommand();
144     command.setDebugLabel(getText());
145     for (int i = 0; i < editparts.size(); i++) {
146         EditPart editpart = (EditPart)editparts.get(i);
147         command.add(editpart.getCommand(request));
148     }
149     return command;
150   }
151
152   /**
153    * @see org.eclipse.gef.Disposable#dispose()
154    */

155   public void dispose() {
156     operationSet = Collections.EMPTY_LIST;
157     super.dispose();
158   }
159
160   /**
161    * Returns the list of editparts which will participate in alignment.
162    * @param request the alignment request
163    * @return the list of parts which will be aligned
164    */

165   protected List JavaDoc getOperationSet(Request request) {
166     if (operationSet != null)
167         return operationSet;
168     List JavaDoc editparts = new ArrayList JavaDoc(getSelectedObjects());
169     if (editparts.isEmpty() || !(editparts.get(0) instanceof GraphicalEditPart))
170         return Collections.EMPTY_LIST;
171     Object JavaDoc primary = editparts.get(editparts.size() - 1);
172     editparts = ToolUtilities.getSelectionWithoutDependants(editparts);
173     ToolUtilities.filterEditPartsUnderstanding(editparts, request);
174     if (editparts.size() < 2 || !editparts.contains(primary))
175         return Collections.EMPTY_LIST;
176     EditPart parent = ((EditPart)editparts.get(0)).getParent();
177     for (int i = 1; i < editparts.size(); i++) {
178         EditPart part = (EditPart)editparts.get(i);
179         if (part.getParent() != parent)
180             return Collections.EMPTY_LIST;
181     }
182     return editparts;
183   }
184
185   /**
186    * Initializes the actions UI presentation.
187    */

188   protected void initUI() {
189     switch (alignment) {
190         case PositionConstants.LEFT:
191             setId(GEFActionConstants.ALIGN_LEFT);
192             setText(GEFMessages.AlignLeftAction_Label);
193             setToolTipText(GEFMessages.AlignLeftAction_Tooltip);
194             setImageDescriptor(InternalImages.DESC_HORZ_ALIGN_LEFT);
195             setDisabledImageDescriptor(InternalImages.DESC_HORZ_ALIGN_LEFT_DIS);
196             break;
197         
198         case PositionConstants.RIGHT:
199             setId(GEFActionConstants.ALIGN_RIGHT);
200             setText(GEFMessages.AlignRightAction_Label);
201             setToolTipText(GEFMessages.AlignRightAction_Tooltip);
202             setImageDescriptor(InternalImages.DESC_HORZ_ALIGN_RIGHT);
203             setDisabledImageDescriptor(InternalImages.DESC_HORZ_ALIGN_RIGHT_DIS);
204             break;
205         
206         case PositionConstants.TOP:
207             setId(GEFActionConstants.ALIGN_TOP);
208             setText(GEFMessages.AlignTopAction_Label);
209             setToolTipText(GEFMessages.AlignTopAction_Tooltip);
210             setImageDescriptor(InternalImages.DESC_VERT_ALIGN_TOP);
211             setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_TOP_DIS);
212             break;
213         
214         case PositionConstants.BOTTOM:
215             setId(GEFActionConstants.ALIGN_BOTTOM);
216             setText(GEFMessages.AlignBottomAction_Label);
217             setToolTipText(GEFMessages.AlignBottomAction_Tooltip);
218             setImageDescriptor(InternalImages.DESC_VERT_ALIGN_BOTTOM);
219             setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_BOTTOM_DIS);
220             break;
221         
222         case PositionConstants.CENTER:
223             setId(GEFActionConstants.ALIGN_CENTER);
224             setText(GEFMessages.AlignCenterAction_Label);
225             setToolTipText(GEFMessages.AlignCenterAction_Tooltip);
226             setImageDescriptor(InternalImages.DESC_HORZ_ALIGN_CENTER);
227             setDisabledImageDescriptor(InternalImages.DESC_HORZ_ALIGN_CENTER_DIS);
228             break;
229         
230         case PositionConstants.MIDDLE:
231             setId(GEFActionConstants.ALIGN_MIDDLE);
232             setText(GEFMessages.AlignMiddleAction_Label);
233             setToolTipText(GEFMessages.AlignMiddleAction_Tooltip);
234             setImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE);
235             setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE_DIS);
236             break;
237     }
238   }
239
240   /**
241    * @see org.eclipse.jface.action.IAction#run()
242    */

243   public void run() {
244     operationSet = null;
245     execute(createAlignmentCommand());
246   }
247   
248 }
249
Popular Tags