KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > EditorViewerKeyHandler


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;
27
28 import java.awt.Rectangle JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.eclipse.draw2d.PositionConstants;
35 import org.eclipse.gef.EditPart;
36 import org.eclipse.gef.GraphicalViewer;
37 import org.eclipse.gef.commands.CommandStack;
38 import org.eclipse.gef.commands.CompoundCommand;
39 import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
40 import org.eclipse.jface.viewers.ISelection;
41 import org.eclipse.jface.viewers.ISelectionChangedListener;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.viewers.SelectionChangedEvent;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.events.DisposeEvent;
46 import org.eclipse.swt.events.DisposeListener;
47 import org.eclipse.swt.events.KeyEvent;
48 import org.nightlabs.config.Config;
49 import org.nightlabs.config.ConfigException;
50 import org.nightlabs.editor2d.command.SetConstraintCommand;
51 import org.nightlabs.editor2d.config.QuickOptionsConfigModule;
52 import org.nightlabs.editor2d.edit.AbstractDrawComponentEditPart;
53
54 /**
55  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
56  */

57 public class EditorViewerKeyHandler
58 extends GraphicalViewerKeyHandler
59 {
60     public static final Logger LOGGER = Logger.getLogger(EditorViewerKeyHandler.class);
61     
62     /**
63      * @param viewer
64      */

65     public EditorViewerKeyHandler(GraphicalViewer viewer)
66     {
67         super(viewer);
68         init();
69     }
70
71     protected void init()
72     {
73         initConfigModule();
74         getViewer().addSelectionChangedListener(selectionListener);
75         getViewer().getControl().addDisposeListener(disposeListener);
76     }
77     
78     protected QuickOptionsConfigModule confMod = null;
79     protected QuickOptionsConfigModule getConfigModule()
80     {
81         if (confMod == null)
82             initConfigModule();
83         
84         return confMod;
85     }
86     
87     protected void initConfigModule()
88     {
89         try {
90             confMod = (QuickOptionsConfigModule)
91                 Config.sharedInstance().createConfigModule(QuickOptionsConfigModule.class);
92         } catch (ConfigException e) {
93             throw new RuntimeException JavaDoc(e);
94         }
95     }
96     
97     protected CommandStack getCommandStack() {
98         return getViewer().getEditDomain().getCommandStack();
99     }
100     
101     public static final List JavaDoc EMPTY_LIST = new ArrayList JavaDoc(0);
102     
103     protected List JavaDoc selectedObjects = null;
104     protected List JavaDoc getSelectedObjects()
105     {
106         if (selectedObjects == null)
107             selectedObjects = EMPTY_LIST;
108         
109         return selectedObjects;
110     }
111     
112     protected ISelectionChangedListener selectionListener = new ISelectionChangedListener()
113     {
114         public void selectionChanged(SelectionChangedEvent event)
115         {
116             ISelection selection = event.getSelection();
117             if (!selection.isEmpty())
118             {
119                 if (selection instanceof IStructuredSelection)
120                 {
121 // LOGGER.debug("Selection changed");
122
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
123                     selectedObjects = structuredSelection.toList();
124                     return;
125                 }
126             }
127             selectedObjects = EMPTY_LIST;
128         }
129     };
130     
131     protected DisposeListener disposeListener = new DisposeListener()
132     {
133         public void widgetDisposed(DisposeEvent e)
134         {
135             getViewer().removeSelectionChangedListener(selectionListener);
136         }
137     };
138     
139     public boolean keyPressed(KeyEvent event)
140     {
141 // LOGGER.debug("Key pressed");
142
if (!getSelectedObjects().isEmpty())
143         {
144             if (acceptLeft(event)) {
145 // LOGGER.debug("Left pressed");
146
translate(getSelectedObjects(), LEFT);
147                 return true;
148             }
149             if (acceptRight(event)) {
150 // LOGGER.debug("Right pressed");
151
translate(getSelectedObjects(), RIGHT);
152                 return true;
153             }
154             if (acceptUp(event)) {
155 // LOGGER.debug("Up pressed");
156
translate(getSelectedObjects(), UP);
157                 return true;
158             }
159             if (acceptDown(event)) {
160 // LOGGER.debug("Down pressed");
161
translate(getSelectedObjects(), DOWN);
162                 return true;
163             }
164         }
165 // return false;
166
return super.keyPressed(event);
167     }
168     
169     public static final int LEFT = PositionConstants.LEFT;
170     public static final int RIGHT = PositionConstants.RIGHT;
171     public static final int UP = PositionConstants.TOP;
172     public static final int DOWN = PositionConstants.BOTTOM;
173         
174 // protected int translationX = 25;
175
// public void setTranslationX(int translation) {
176
// this.translationX = translation;
177
// }
178
public int getTranslationX() {
179         return getConfigModule().getMoveTranslationX();
180     }
181     
182 // protected int translationY = 25;
183
// public void setTranslationY(int translation) {
184
// this.translationY = translation;
185
// }
186
public int getTranslationY() {
187         return getConfigModule().getMoveTranslationY();
188     }
189                     
190     /**
191      *
192      * @param editParts a List of EditParts to translate
193      * @param direction the translation direction
194      */

195     protected void translate(List JavaDoc editParts, int direction)
196     {
197         CompoundCommand compoundCmd = new CompoundCommand();
198         for (Iterator JavaDoc it = editParts.iterator(); it.hasNext(); )
199         {
200             EditPart ep = (EditPart) it.next();
201             if (ep instanceof AbstractDrawComponentEditPart)
202             {
203                 AbstractDrawComponentEditPart dcep = (AbstractDrawComponentEditPart) ep;
204                 DrawComponent dc = dcep.getDrawComponent();
205                 SetConstraintCommand cmd = new SetConstraintCommand();
206                 cmd.setPart(dc);
207                 Rectangle JavaDoc dcBounds = new Rectangle JavaDoc(dc.getBounds());
208                 switch (direction)
209                 {
210                     case(DOWN):
211                         dcBounds.y += getTranslationY();
212                         break;
213                     case(UP):
214                         dcBounds.y -= getTranslationY();
215                         break;
216                     case(LEFT):
217                         dcBounds.x -= getTranslationX();
218                         break;
219                     case(RIGHT):
220                         dcBounds.x += getTranslationX();
221                         break;
222                 }
223                 cmd.setBounds(dcBounds);
224                 compoundCmd.add(cmd);
225             }
226         }
227         if (!compoundCmd.getCommands().isEmpty()) {
228             getCommandStack().execute(compoundCmd);
229         }
230         
231         LOGGER.debug("Translate Command executed");
232     }
233     
234     /**
235      * checks if the LEFT ARROW has been pressed
236      * @return <code>true</code> if the keys pressed indicate translate selection left
237      */

238     protected boolean acceptLeft(KeyEvent event) {
239 // return ((event.stateMask & SWT.ALT) != 0) && (event.keyCode == SWT.ARROW_LEFT);
240
return (event.keyCode == SWT.ARROW_LEFT);
241     }
242     
243     /**
244      * checks if the RIGHT ARROW has been pressed
245      * @return <code>true</code> if the keys pressed indicate translate selection left
246      */

247     protected boolean acceptRight(KeyEvent event) {
248 // return ((event.stateMask & SWT.ALT) != 0) && (event.keyCode == SWT.ARROW_RIGHT);
249
return (event.keyCode == SWT.ARROW_RIGHT);
250     }
251
252     /**
253      * checks if the DOWN ARROW has been pressed
254      * @return <code>true</code> if the keys pressed indicate translate selection left
255      */

256     protected boolean acceptDown(KeyEvent event) {
257 // return ((event.stateMask & SWT.ALT) != 0) && (event.keyCode == SWT.ARROW_DOWN);
258
return (event.keyCode == SWT.ARROW_DOWN);
259     }
260
261     /**
262      * checks if the UP ARROW has been pressed
263      * @return <code>true</code> if the keys pressed indicate translate selection left
264      */

265     protected boolean acceptUp(KeyEvent event) {
266 // return ((event.stateMask & SWT.ALT) != 0) && (event.keyCode == SWT.ARROW_UP);
267
return (event.keyCode == SWT.ARROW_UP);
268     }
269     
270 }
271
Popular Tags