KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > CloseOperationHandlerTest


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.multiview;
21
22 import java.awt.Image JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JToolBar JavaDoc;
26 import junit.framework.*;
27 import org.netbeans.core.api.multiview.MultiViewHandler;
28 import org.netbeans.core.api.multiview.MultiViews;
29 import org.netbeans.core.spi.multiview.CloseOperationHandler;
30 import org.netbeans.core.spi.multiview.CloseOperationState;
31 import org.netbeans.core.spi.multiview.MultiViewDescription;
32 import org.netbeans.core.spi.multiview.MultiViewFactory;
33 import org.netbeans.junit.*;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.lookup.Lookups;
36
37 import org.openide.windows.*;
38
39
40 /**
41  *
42  * @author Milos Kleint
43  */

44 public class CloseOperationHandlerTest extends NbTestCase {
45     
46     /** Creates a new instance of SFSTest */
47     public CloseOperationHandlerTest(String JavaDoc name) {
48         super (name);
49     }
50     
51     /**
52      * @param args the command line arguments
53      */

54     public static void main(java.lang.String JavaDoc[] args) {
55         junit.textui.TestRunner.run(suite());
56     }
57     
58     public static Test suite() {
59         TestSuite suite = new NbTestSuite(CloseOperationHandlerTest.class);
60         
61         return suite;
62     }
63
64     protected boolean runInEQ () {
65         return true;
66     }
67     
68     
69     public void testCreateMultiView2 () throws Exception JavaDoc {
70         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, new NonClosableElem());
71         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, new NonClosableElem());
72         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, new MVElem());
73         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
74         MyCloseHandler close = new MyCloseHandler();
75         TopComponent tc = MultiViewFactory.createMultiView(descs, desc1, close);
76         assertNotNull(tc);
77         
78          tc.open();
79         // just one element as shown..
80
close.supposed = 1;
81         tc.close();
82         assertTrue(close.wasUsed);
83         
84         tc.open();
85         MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
86         handler.requestActive(handler.getPerspectives()[2]);
87         // 1 shall be checked - elem3 can be closed.
88
close.supposed = 1;
89         // do not allow closing..
90
close.canClose = false;
91         
92         tc.close();
93         assertTrue(tc.isOpened());
94
95         
96         handler.requestActive(handler.getPerspectives()[1]);
97         // 2 shall be checked.
98
close.supposed = 2;
99         // allow closing..
100
close.canClose = true;
101         
102         tc.close();
103         assertTrue(!tc.isOpened());
104         
105     }
106
107     
108     private class MyCloseHandler implements CloseOperationHandler {
109         
110         public boolean wasUsed = false;
111         public int supposed = 0;
112         public boolean canClose = true;
113         
114         public boolean resolveCloseOperation(CloseOperationState[] elements) {
115             wasUsed = true;
116             if (supposed != elements.length) {
117                 throw new IllegalStateException JavaDoc("A different number of elements returned. Expected=" + supposed + " but was:" + elements.length);
118             }
119             return canClose;
120         }
121         
122         
123     }
124     
125     private class NonClosableElem extends MVElem {
126         
127         public CloseOperationState canCloseElement() {
128             return MultiViewFactory.createUnsafeCloseState("ID", MultiViewFactory.NOOP_CLOSE_ACTION, MultiViewFactory.NOOP_CLOSE_ACTION);
129         }
130         
131     }
132     
133 }
134
135
Popular Tags