KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > ActionCopyPasteTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.services;
21
22 import java.awt.datatransfer.Transferable JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import javax.swing.AbstractAction JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.Repository;
28 import org.openide.loaders.DataFolder;
29 import org.openide.loaders.DataObject;
30 import org.openide.util.datatransfer.PasteType;
31
32 /**
33  * @author Jaroslav Tulach, Jiri Rechtacek
34  */

35 public class ActionCopyPasteTest extends NbTestCase {
36
37     ToolbarFolderNode toolbar1;
38     ToolbarFolderNode toolbar2;
39     MenuFolderNode menu1;
40     MenuFolderNode menu2;
41     DataObject actionToPaste;
42     
43     public ActionCopyPasteTest(String JavaDoc testName) {
44         super (testName);
45     }
46     
47     protected void setUp() throws Exception JavaDoc {
48         toolbar1 = new ToolbarFolderNode( createFolder( "Toolbars", "tb1" ) );
49         toolbar2 = new ToolbarFolderNode( createFolder( "Toolbars", "tb2" ) );
50
51         menu1 = new MenuFolderNode( createFolder( "Menu", "menu1" ) );
52         menu2 = new MenuFolderNode( createFolder( "Menu", "menu2" ) );
53         
54         createChildren( toolbar1.getDataObject().getPrimaryFile(), new Class JavaDoc[] { ActionA1.class, ActionA2.class } );
55         createChildren( menu1.getDataObject().getPrimaryFile(), new Class JavaDoc[] { ActionA1.class, ActionA2.class } );
56
57         createChildren( toolbar2.getDataObject().getPrimaryFile(), new Class JavaDoc[] { ActionB1.class, ActionB2.class } );
58         createChildren( menu2.getDataObject().getPrimaryFile(), new Class JavaDoc[] { ActionB1.class, ActionB2.class } );
59         
60     }
61
62     protected boolean runInEQ () {
63         return true;
64     }
65     
66     public void testDoNotPasteDuplicateActions() throws Exception JavaDoc {
67         //check copy & paste for toolbar folders
68
DataObject[] folderChildren = ((DataFolder)toolbar1.getDataObject()).getChildren();
69         DataObject child1 = folderChildren[0];
70         Transferable JavaDoc t = child1.getNodeDelegate().clipboardCopy();
71         PasteType[] types;
72         
73         types = toolbar1.getPasteTypes( t );
74         assertEquals( "Cannot paste an action if the toolbar already contains it.", 0, types.length );
75         
76         types = toolbar2.getPasteTypes( t );
77         assertTrue( "Pasting to a different folder is ok.", types.length > 0 );
78         
79         types = menu1.getPasteTypes( t );
80         assertEquals( "Cannot paste an action if the menu already contains it.", 0, types.length );
81         
82         types = menu2.getPasteTypes( t );
83         assertTrue( "Pasting to a different menu is ok.", types.length > 0 );
84
85         //check copy & paste for menu folders
86
folderChildren = ((DataFolder)menu1.getDataObject()).getChildren();
87         child1 = folderChildren[0];
88         t = child1.getNodeDelegate().clipboardCopy();
89
90         types = toolbar1.getPasteTypes( t );
91         assertEquals( "Cannot paste an action if the toolbar already contains it.", 0, types.length );
92         
93         types = toolbar2.getPasteTypes( t );
94         assertTrue( "Pasting to a different folder is ok.", types.length > 0 );
95         
96         types = menu1.getPasteTypes( t );
97         assertEquals( "Cannot paste an action if the menu already contains it.", 0, types.length );
98         
99         types = menu2.getPasteTypes( t );
100         assertTrue( "Pasting to a different menu is ok.", types.length > 0 );
101     }
102     
103     DataFolder createFolder( String JavaDoc parent, String JavaDoc folderName ) throws Exception JavaDoc {
104         FileObject folderObj = Repository.getDefault().getDefaultFileSystem().findResource( parent+"/"+folderName );
105         if( null != folderObj )
106             folderObj.delete();
107
108         FileObject parentFolder = Repository.getDefault().getDefaultFileSystem().findResource( parent );
109         assertNotNull( parentFolder );
110         parentFolder.createFolder( folderName );
111         
112         DataFolder res = DataFolder.findFolder( Repository.getDefault().getDefaultFileSystem().findResource( parent+"/"+folderName ) );
113         assertNotNull( res );
114         return res;
115     }
116     
117     void createChildren( FileObject folder, Class JavaDoc[] actions ) throws Exception JavaDoc {
118         for( int i=0; i<actions.length; i++ ) {
119             folder.createData( actions[i].getName()+".instance" );
120         }
121     }
122     
123     public static class ActionA1 extends AbstractAction JavaDoc {
124         public ActionA1() {
125             super( "actiona1" );
126         }
127         
128         public void actionPerformed(ActionEvent JavaDoc e) {}
129     }
130     
131     public static class ActionA2 extends AbstractAction JavaDoc {
132         public ActionA2() {
133             super( "actiona2" );
134         }
135         
136         public void actionPerformed(ActionEvent JavaDoc e) {}
137     }
138
139     public static class ActionB1 extends AbstractAction JavaDoc {
140         public ActionB1() {
141             super( "actionb1" );
142         }
143         
144         public void actionPerformed(ActionEvent JavaDoc e) {}
145     }
146     
147     public static class ActionB2 extends AbstractAction JavaDoc {
148         public ActionB2() {
149             super( "actionb2" );
150         }
151         
152         public void actionPerformed(ActionEvent JavaDoc e) {}
153     }
154 }
155
Popular Tags