KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > palette > PaletteSwitchTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.palette;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.core.windows.Constants;
24 import org.netbeans.core.windows.SplitConstraint;
25 import org.netbeans.core.windows.WindowManagerImpl;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileSystem;
28 import org.openide.filesystems.MIMEResolver;
29 import org.openide.filesystems.Repository;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataObjectNotFoundException;
32 import org.openide.nodes.Node;
33 import org.openide.util.lookup.AbstractLookup;
34 import org.openide.util.lookup.InstanceContent;
35 import org.openide.windows.Mode;
36 import org.openide.windows.TopComponent;
37 /**
38  *
39  * @author S. Aubrecht
40  */

41 public class PaletteSwitchTest extends AbstractPaletteTestHid {
42     
43     private String JavaDoc lookupPaletteRootName;
44     private FileObject lookupPaletteRootFolder;
45     
46     static String JavaDoc mimePaletteRootName;
47     private static FileObject mimePaletteRootFolder;
48     private static final String JavaDoc MIME_TYPE_NAME = "junittest/x-paletteswitchtest";
49     
50     private FileObject dummyDocumentFile;
51     private final static String JavaDoc DUMMY_DOCUMENT_FILE_EXTENSION = "junitPaletteSwitchTest";
52     
53     static {
54         String JavaDoc[] layers = new String JavaDoc[] {"org/netbeans/spi/palette/mf-layer.xml"};//NOI18N
55
Object JavaDoc[] instances = new Object JavaDoc[] { new MyMimeResolver() };
56         IDEInitializer.setup(layers,instances);
57     }
58     
59     public PaletteSwitchTest(String JavaDoc testName) {
60         super(testName);
61     }
62     
63     protected void setUp() throws Exception JavaDoc {
64         super.setUp();
65         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
66         
67         lookupPaletteRootName = "lookupPalette" + System.currentTimeMillis();
68         lookupPaletteRootFolder = fs.getRoot().createFolder( lookupPaletteRootName );
69         createDefaultPaletteContentInFolder( lookupPaletteRootFolder );
70         
71         if( null == mimePaletteRootName ) {
72             mimePaletteRootName = "mimePalette" + System.currentTimeMillis();
73             mimePaletteRootFolder = fs.getRoot().createFolder( mimePaletteRootName );
74             createDefaultPaletteContentInFolder( mimePaletteRootFolder );
75         }
76         
77         dummyDocumentFile = fs.getRoot().createData( "dummyDocumentFile" + System.currentTimeMillis(), DUMMY_DOCUMENT_FILE_EXTENSION );
78     }
79     
80     public void testNoLookupPalette() throws IOException JavaDoc {
81         TopComponent tc = createTopComponentWithPalette( null );
82         PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
83         
84         PaletteController foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false );
85         
86         assertNull( foundPalette );
87     }
88     
89     public void testNoMimePalette() throws IOException JavaDoc {
90         PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
91         
92         PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( "unknown/mimetype" );
93         
94         assertNull( foundPalette );
95     }
96     
97     public void testLookupPalette() throws IOException JavaDoc {
98         PaletteController pc = PaletteFactory.createPalette( lookupPaletteRootName, new DummyActions() );
99         
100         TopComponent tc = createTopComponentWithPalette( pc );
101         PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
102         
103         PaletteController foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false );
104         
105         assertNotNull( foundPalette );
106         assertEquals( pc.getModel().getName(), foundPalette.getModel().getName() );
107     }
108     
109     public void testMimePalette() throws IOException JavaDoc {
110         TopComponent tc = createTopComponentWithPalette( null );
111         tc.setActivatedNodes( new Node[] { DataObject.find( dummyDocumentFile ).getNodeDelegate() } );
112         
113         PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
114         
115         PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( MIME_TYPE_NAME );
116         assertNotNull( foundPalette );
117         assertEquals( mimePaletteRootName, foundPalette.getModel().getName() );
118         
119         foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false );
120         assertNotNull( foundPalette );
121         assertEquals( mimePaletteRootName, foundPalette.getModel().getName() );
122     }
123     
124     
125     public void testLookupPaletteTakePrecendsOverMimePalette() throws IOException JavaDoc {
126         PaletteController pc = PaletteFactory.createPalette( lookupPaletteRootName, new DummyActions() );
127         
128         TopComponent tc = createTopComponentWithPalette( pc );
129         tc.setActivatedNodes( new Node[] { DataObject.find( dummyDocumentFile ).getNodeDelegate() } );
130         
131         PaletteSwitch paletteSwitch = PaletteSwitch.getDefault();
132         
133         PaletteController foundPalette = paletteSwitch.getPaletteFromMimeType( MIME_TYPE_NAME );
134         assertNotNull( foundPalette );
135         assertEquals( mimePaletteRootName, foundPalette.getModel().getName() );
136         
137         foundPalette = paletteSwitch.getPaletteFromTopComponent( tc, false );
138         assertNotNull( foundPalette );
139         assertEquals( pc.getModel().getName(), foundPalette.getModel().getName() );
140     }
141     
142     private TopComponent createTopComponentWithPalette( PaletteController pc ) throws IOException JavaDoc {
143         TopComponent tc = new MyTopComponent( pc );
144         
145         Mode editorMode = WindowManagerImpl.getInstance().findMode( "unitTestEditorMode" );
146         if( null == editorMode ) {
147             editorMode = WindowManagerImpl.getInstance().createMode( "unitTestEditorMode", Constants.MODE_KIND_EDITOR, Constants.MODE_STATE_JOINED, false, new SplitConstraint[0] );
148         }
149         editorMode.dockInto(tc);
150         return tc;
151     }
152     
153     private static class MyTopComponent extends TopComponent {
154         public MyTopComponent( PaletteController palette ) throws DataObjectNotFoundException {
155             this( new InstanceContent(), palette );
156         }
157         
158         private MyTopComponent( InstanceContent ic, PaletteController palette ) throws DataObjectNotFoundException {
159             super( new AbstractLookup( ic ) );
160             if( null != palette )
161                 ic.add( palette );
162         }
163     }
164     
165     public static class MyMimeResolver extends MIMEResolver {
166         
167         public String JavaDoc findMIMEType(FileObject fo) {
168             if( DUMMY_DOCUMENT_FILE_EXTENSION.equals( fo.getExt() ) )
169                 return MIME_TYPE_NAME;
170             return null;
171         }
172     }
173     
174     
175 }
176
Popular Tags