KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.eclipse.gef.commands.CompoundCommand;
32 import org.eclipse.jface.util.IPropertyChangeListener;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.ui.actions.ActionFactory;
35 import org.nightlabs.editor2d.AbstractEditor;
36 import org.nightlabs.editor2d.DrawComponent;
37 import org.nightlabs.editor2d.EditorPlugin;
38 import org.nightlabs.editor2d.command.CloneDrawComponentCommand;
39
40 /**
41  * <p> Author: Daniel.Mazurek[AT]NightLabs[DOT]de </p>
42  */

43 public class PasteAction
44 extends AbstractEditorAction
45 {
46 // public static final String ID = PasteAction.class.getName();
47
public static final String JavaDoc ID = ActionFactory.PASTE.getId();
48     
49     /**
50      * @param part
51      */

52     public PasteAction(AbstractEditor part) {
53         super(part);
54     }
55
56     /**
57      * @param part
58      * @param style
59      */

60     public PasteAction(AbstractEditor part, int style) {
61         super(part, style);
62     }
63
64   protected void init()
65   {
66     super.init();
67     setText(EditorPlugin.getResourceString("action.paste.text"));
68     setToolTipText(EditorPlugin.getResourceString("action.paste.tooltip"));
69     setId(ID);
70     setActionDefinitionId(ID);
71     setAccelerator(SWT.CTRL | 'P');
72   }
73     
74     protected boolean enabled = false;
75     protected boolean calculateEnabled() {
76         return enabled;
77     }
78     
79     protected boolean copy = false;
80     protected boolean cut = false;
81     
82     protected List JavaDoc clipBoardContent = null;
83     public void run()
84     {
85         CompoundCommand cmd = new CompoundCommand();
86         if (clipBoardContent != null)
87         {
88             for (Iterator JavaDoc it = clipBoardContent.iterator(); it.hasNext(); )
89             {
90                 DrawComponent dc = (DrawComponent) it.next();
91                 CloneDrawComponentCommand cloneCmd = new CloneDrawComponentCommand(dc, getCurrentLayer());
92                 if (copy)
93                     cloneCmd.setCloneName(dc.getName() + getCopyString());
94                 if (cut)
95                     cloneCmd.setCloneName(dc.getName());
96                 cmd.add(cloneCmd);
97             }
98             execute(cmd);
99         }
100     }
101             
102     public IPropertyChangeListener copyListener = new IPropertyChangeListener()
103     {
104         public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event)
105         {
106             if (event.getProperty().equals(EditorActionConstants.PROP_COPY_TO_CLIPBOARD))
107             {
108                 copy = true;
109                 cut = false;
110                 Object JavaDoc content = event.getNewValue();
111                 if (!content.equals(EditorActionConstants.EMPTY_CLIPBOARD_CONTENT))
112                 {
113                     enabled = true;
114                     if (content instanceof List JavaDoc)
115                     {
116                         clipBoardContent = (List JavaDoc) content;
117                     }
118                 }
119             }
120         }
121     };
122                 
123     public IPropertyChangeListener cutListener = new IPropertyChangeListener()
124     {
125         public void propertyChange(org.eclipse.jface.util.PropertyChangeEvent event)
126         {
127             if (event.getProperty().equals(EditorActionConstants.PROP_COPY_TO_CLIPBOARD))
128             {
129                 cut = true;
130                 copy = false;
131                 Object JavaDoc content = event.getNewValue();
132                 if (!content.equals(EditorActionConstants.EMPTY_CLIPBOARD_CONTENT))
133                 {
134                     enabled = true;
135                     if (content instanceof List JavaDoc)
136                     {
137                         clipBoardContent = (List JavaDoc) content;
138                     }
139                 }
140             }
141         }
142     };
143     
144     protected String JavaDoc getCopyString()
145     {
146         return " ("+EditorPlugin.getResourceString("action.copy.text")+")";
147     }
148 }
149
Popular Tags