KickJava   Java API By Example, From Geeks To Geeks.

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


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: 11.11.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.actions;
9
10 import org.eclipse.draw2d.geometry.Point;
11 import org.eclipse.gef.EditPart;
12 import org.eclipse.gef.commands.Command;
13 import org.eclipse.gef.requests.CreateRequest;
14 import org.eclipse.gef.requests.CreationFactory;
15 import org.eclipse.gef.ui.actions.Clipboard;
16 import org.eclipse.gef.ui.actions.SelectionAction;
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.eclipse.ui.actions.ActionFactory;
19
20 import com.nightlabs.editor2d.EditorPlugin;
21
22 /**
23 * If the current object on the default GEF {@link org.eclipse.gef.ui.actions.Clipboard}
24 * is a template and the current viewer is a graphical viewer, this action will paste the
25 * template to the viewer.
26 * @author Eric Bordeau
27 */

28
29
30 public abstract class PasteTemplateAction
31 extends SelectionAction
32 {
33
34  /**
35   * Constructor for PasteTemplateAction.
36   * @param editor
37   */

38  public PasteTemplateAction(IWorkbenchPart editor) {
39     super(editor);
40  }
41
42  /**
43   * Returns <code>true</code> if the {@link Clipboard clipboard's} contents are not
44   * <code>null</code> and the command returned by {@link #createPasteCommand()} can
45   * execute.
46   */

47  protected boolean calculateEnabled() {
48     Command command = null;
49     if (getClipboardContents() != null)
50         command = createPasteCommand();
51     return command != null && command.canExecute();
52  }
53
54  /**
55   * Creates and returns a command (which may be <code>null</code>) to create a new EditPart
56   * based on the template on the clipboard.
57   * @return the paste command
58   */

59  protected Command createPasteCommand() {
60     if (getSelectedObjects() == null || getSelectedObjects().isEmpty())
61         return null;
62     CreateRequest request = new CreateRequest();
63     request.setFactory(getFactory(getClipboardContents()));
64     request.setLocation(getPasteLocation());
65     Object JavaDoc obj = getSelectedObjects().get(0);
66     if (obj instanceof EditPart)
67         return ((EditPart)obj).getCommand(request);
68     return null;
69  }
70
71  /**
72   * Returns the contents of the default GEF {@link Clipboard}.
73   * @return the clipboard's contents
74   */

75  protected Object JavaDoc getClipboardContents() {
76     return Clipboard.getDefault().getContents();
77  }
78
79  /**
80   * Returns the appropriate Factory object to be used for the specified template. This
81   * Factory is used on the CreateRequest that is sent to the target EditPart.
82   * @param template the template Object
83   * @return a Factory
84   */

85  protected abstract CreationFactory getFactory(Object JavaDoc template);
86
87  protected abstract Point getPasteLocation();
88
89  /**
90   * @see org.eclipse.gef.ui.actions.EditorPartAction#init()
91   */

92  protected void init() {
93     setId(ActionFactory.PASTE.getId());
94     setText(EditorPlugin.getResourceString("action_paste_label"));
95  }
96
97  /**
98   * Executes the command returned by {@link #createPasteCommand()}.
99   */

100  public void run() {
101     execute(createPasteCommand());
102  }
103 }
104
Popular Tags