KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 08.06.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.actions;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.eclipse.draw2d.PositionConstants;
15 import org.eclipse.draw2d.geometry.PrecisionRectangle;
16 import org.eclipse.draw2d.geometry.Rectangle;
17 import org.eclipse.gef.EditPart;
18 import org.eclipse.gef.GraphicalEditPart;
19 import org.eclipse.gef.Request;
20 import org.eclipse.gef.RequestConstants;
21 import org.eclipse.gef.commands.Command;
22 import org.eclipse.gef.commands.CompoundCommand;
23 import org.eclipse.gef.internal.GEFMessages;
24 import org.eclipse.gef.internal.InternalImages;
25 import org.eclipse.gef.requests.AlignmentRequest;
26 import org.eclipse.gef.tools.ToolUtilities;
27 import org.eclipse.gef.ui.actions.GEFActionConstants;
28 import org.eclipse.gef.ui.actions.SelectionAction;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IWorkbenchPart;
31
32
33 public class EditorAlignmentAction
34 extends SelectionAction
35 {
36
37   /**
38    * Indicates that the bottom edges should be aligned.
39    */

40   public static final String JavaDoc ID_ALIGN_BOTTOM = GEFActionConstants.ALIGN_BOTTOM;
41
42   /**
43    * Indicates that the horizontal centers should be aligned.
44    */

45   public static final String JavaDoc ID_ALIGN_CENTER = GEFActionConstants.ALIGN_CENTER;
46
47   /**
48    * Indicates that the left edges should be aligned.
49    */

50   public static final String JavaDoc ID_ALIGN_LEFT = GEFActionConstants.ALIGN_LEFT;
51
52   /**
53    * Indicates that the vertical midpoints should be aligned.
54    */

55   public static final String JavaDoc ID_ALIGN_MIDDLE = GEFActionConstants.ALIGN_MIDDLE;
56
57   /**
58    * Indicates that the right edges should be aligned.
59    */

60   public static final String JavaDoc ID_ALIGN_RIGHT = GEFActionConstants.ALIGN_RIGHT;
61
62   /**
63    * Indicates that the top edges should be aligned.
64    */

65   public static final String JavaDoc ID_ALIGN_TOP = GEFActionConstants.ALIGN_TOP;
66   
67   protected int alignment;
68   protected List JavaDoc operationSet;
69
70   /**
71    * Constructs an AlignmentAction with the given part and alignment ID. The alignment ID
72    * must by one of:
73    * <UL>
74    * <LI>GEFActionConstants.ALIGN_LEFT
75    * <LI>GEFActionConstants.ALIGN_RIGHT
76    * <LI>GEFActionConstants.ALIGN_CENTER
77    * <LI>GEFActionConstants.ALIGN_TOP
78    * <LI>GEFActionConstants.ALIGN_BOTTOM
79    * <LI>GEFActionConstants.ALIGN_MIDDLE
80    * </UL>
81    * @param part the workbench part used to obtain context
82    * @param align the aligment ID.
83    */

84   public EditorAlignmentAction(IWorkbenchPart part, int align) {
85     super(part);
86     alignment = align;
87     initUI();
88   }
89
90   /**
91    * Returns the alignment rectangle to which all selected parts should be aligned.
92    * @param request the alignment Request
93    * @return the alignment rectangle
94    */

95   protected Rectangle calculateAlignmentRectangle(Request request) {
96     List JavaDoc editparts = getOperationSet(request);
97     if (editparts == null || editparts.isEmpty())
98         return null;
99     GraphicalEditPart part = (GraphicalEditPart)editparts.get(editparts.size() - 1);
100     Rectangle rect = new PrecisionRectangle(part.getFigure().getBounds());
101     part.getFigure().translateToAbsolute(rect);
102     return rect;
103   }
104
105   /**
106    * @see org.eclipse.gef.ui.actions.WorkbenchPartAction#calculateEnabled()
107    */

108   protected boolean calculateEnabled() {
109     operationSet = null;
110     Command cmd = createAlignmentCommand();
111     if (cmd == null)
112         return false;
113     return cmd.canExecute();
114   }
115
116   private Command createAlignmentCommand() {
117     AlignmentRequest request = new AlignmentRequest(RequestConstants.REQ_ALIGN);
118     request.setAlignmentRectangle(calculateAlignmentRectangle(request));
119     request.setAlignment(alignment);
120     List JavaDoc editparts = getOperationSet(request);
121     if (editparts.size() < 2)
122         return null;
123
124     CompoundCommand command = new CompoundCommand();
125     command.setDebugLabel(getText());
126     for (int i = 0; i < editparts.size(); i++) {
127         EditPart editpart = (EditPart)editparts.get(i);
128         command.add(editpart.getCommand(request));
129     }
130     return command;
131   }
132
133   /**
134    * @see org.eclipse.gef.Disposable#dispose()
135    */

136   public void dispose() {
137     operationSet = Collections.EMPTY_LIST;
138     super.dispose();
139   }
140
141   /**
142    * Returns the list of editparts which will participate in alignment.
143    * @param request the alignment request
144    * @return the list of parts which will be aligned
145    */

146   protected List JavaDoc getOperationSet(Request request) {
147     if (operationSet != null)
148         return operationSet;
149     List JavaDoc editparts = new ArrayList JavaDoc(getSelectedObjects());
150     if (editparts.isEmpty() || !(editparts.get(0) instanceof GraphicalEditPart))
151         return Collections.EMPTY_LIST;
152     Object JavaDoc primary = editparts.get(editparts.size() - 1);
153     editparts = ToolUtilities.getSelectionWithoutDependants(editparts);
154     ToolUtilities.filterEditPartsUnderstanding(editparts, request);
155     if (editparts.size() < 2 || !editparts.contains(primary))
156         return Collections.EMPTY_LIST;
157     EditPart parent = ((EditPart)editparts.get(0)).getParent();
158     for (int i = 1; i < editparts.size(); i++) {
159         EditPart part = (EditPart)editparts.get(i);
160         if (part.getParent() != parent)
161             return Collections.EMPTY_LIST;
162     }
163     return editparts;
164   }
165
166   /**
167    * Initializes the actions UI presentation.
168    */

169   protected void initUI() {
170     switch (alignment) {
171         case PositionConstants.LEFT:
172             setId(GEFActionConstants.ALIGN_LEFT);
173             setText(GEFMessages.AlignLeftAction_Label);
174             setToolTipText(GEFMessages.AlignLeftAction_Tooltip);
175             setImageDescriptor(InternalImages.DESC_HORZ_ALIGN_LEFT);
176             setDisabledImageDescriptor(InternalImages.DESC_HORZ_ALIGN_LEFT_DIS);
177             break;
178         
179         case PositionConstants.RIGHT:
180             setId(GEFActionConstants.ALIGN_RIGHT);
181             setText(GEFMessages.AlignRightAction_Label);
182             setToolTipText(GEFMessages.AlignRightAction_Tooltip);
183             setImageDescriptor(InternalImages.DESC_HORZ_ALIGN_RIGHT);
184             setDisabledImageDescriptor(InternalImages.DESC_HORZ_ALIGN_RIGHT_DIS);
185             break;
186         
187         case PositionConstants.TOP:
188             setId(GEFActionConstants.ALIGN_TOP);
189             setText(GEFMessages.AlignTopAction_Label);
190             setToolTipText(GEFMessages.AlignTopAction_Tooltip);
191             setImageDescriptor(InternalImages.DESC_VERT_ALIGN_TOP);
192             setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_TOP_DIS);
193             break;
194         
195         case PositionConstants.BOTTOM:
196             setId(GEFActionConstants.ALIGN_BOTTOM);
197             setText(GEFMessages.AlignBottomAction_Label);
198             setToolTipText(GEFMessages.AlignBottomAction_Tooltip);
199             setImageDescriptor(InternalImages.DESC_VERT_ALIGN_BOTTOM);
200             setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_BOTTOM_DIS);
201             break;
202         
203         case PositionConstants.CENTER:
204             setId(GEFActionConstants.ALIGN_CENTER);
205             setText(GEFMessages.AlignCenterAction_Label);
206             setToolTipText(GEFMessages.AlignCenterAction_Tooltip);
207             setImageDescriptor(InternalImages.DESC_HORZ_ALIGN_CENTER);
208             setDisabledImageDescriptor(InternalImages.DESC_HORZ_ALIGN_CENTER_DIS);
209             break;
210         
211         case PositionConstants.MIDDLE:
212             setId(GEFActionConstants.ALIGN_MIDDLE);
213             setText(GEFMessages.AlignMiddleAction_Label);
214             setToolTipText(GEFMessages.AlignMiddleAction_Tooltip);
215             setImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE);
216             setDisabledImageDescriptor(InternalImages.DESC_VERT_ALIGN_MIDDLE_DIS);
217             break;
218     }
219   }
220
221   /**
222    * @see org.eclipse.jface.action.IAction#run()
223    */

224   public void run() {
225     operationSet = null;
226     execute(createAlignmentCommand());
227   }
228   
229 }
Popular Tags