KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > gui > action > PasteAction


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
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 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 Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23 package org.objectweb.clif.scenario.util.isac.gui.action;
24
25 import java.net.URL JavaDoc;
26
27 import org.apache.log4j.Category;
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.jface.window.ApplicationWindow;
32 import org.eclipse.swt.dnd.Clipboard;
33 import org.objectweb.clif.scenario.util.isac.FileName;
34 import org.objectweb.clif.scenario.util.isac.gui.ScenarioGUIEditor;
35 import org.objectweb.clif.scenario.util.isac.gui.tree.ScenarioTreeViewer;
36 import org.objectweb.clif.scenario.util.isac.gui.tree.dnd.ScenarioTreeTransfer;
37 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
38 import org.objectweb.clif.scenario.util.isac.util.tree.TreeManager;
39 /**
40  * Action which paste a behaviors tree node
41  *
42  * @author JC Meillaud
43  * @author A Peyrard
44  */

45 public class PasteAction extends Action {
46     /**
47      * The application window, which launch this action
48      */

49     ScenarioGUIEditor window;
50     ScenarioTreeViewer viewer;
51     static Category cat = Category.getInstance(PasteAction.class.getName());
52     /**
53      * Build a new paste node action
54      *
55      * @param w
56      * The application window, in wich we will use this action
57      */

58     public PasteAction(ApplicationWindow w) {
59         cat.debug("-> constructor");
60         this.window = (ScenarioGUIEditor) w;
61         this.setText("Paste@Ctrl+V");
62         this.setToolTipText("Paste as child of the selected node or the selected text");
63         try {
64             this.setImageDescriptor(ImageDescriptor.createFromURL(new URL JavaDoc(
65                     "file:" + FileName.PASTE_ICON)));
66         } catch (Exception JavaDoc e) {
67             cat.warn("Unable to find paste icon file");
68         }
69     }
70
71     public void run() {
72         cat.debug("-> run");
73         switch (this.window.getEditorShowed()) {
74             case ScenarioGUIEditor.GUI_EDITOR :
75                 this.viewer = this.window.getTreeViewer();
76             Clipboard clipboard = this.viewer.getClipboard();
77                 ScenarioNode node = this.viewer.getSelectedNode();
78                 if (node == null)
79                     node = (ScenarioNode) this.viewer.getInput();
80                 // if the node is steal null exit
81
if (node == null)
82                     return;
83                 ScenarioNode child = (ScenarioNode) clipboard
84                         .getContents(ScenarioTreeTransfer.getInstance());
85                 if (child == null) {
86                     cat.warn("CHILD IS NULL !!!");
87                     return;
88                 }
89                 // Create a copy of the node which is in the clipboard
90
TreeManager treeManager = TreeManager.getTreeManager(null);
91                 ScenarioNode copy = treeManager.copyScenarioNode(child);
92                 if (treeManager.childrenAllowed(node).contains(
93                         treeManager.getNodeType(child))) {
94                     cat.warn("ADDING A " + treeManager.getNodeType(child)
95                             + " ON A " + treeManager.getNodeType(node));
96                     node.addChild(copy);
97                     this.viewer.expand(copy, true);
98                 } else {
99                     MessageDialog.openError(this.window.getShell(),
100                             "Clipboard error", "Can not copy a \""
101                                     + treeManager.getNodeType(child)
102                                     + "\" node into a \""
103                                     + treeManager.getNodeType(node)
104                                     + "\" node !");
105                     // delete the copy node
106
treeManager.deleteBehaviorsTreeNode(copy);
107                 }
108                 this.viewer.refresh();
109                 break;
110             case ScenarioGUIEditor.XML_EDITOR :
111                 cat.warn("XML EDITOR PASTE") ;
112                 this.window.getStyledText().paste() ;
113                 break;
114             default :
115         // there is none other choice
116
}
117         // change the scenario saved state
118
this.window.setScenarioSavedState(false) ;
119     }
120 }
Popular Tags