KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > awt > DefaultAWTBridge


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.modules.openide.awt;
21
22 import java.awt.Component JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JMenuItem JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import javax.swing.JSeparator JavaDoc;
30 import org.openide.awt.Actions;
31 import org.openide.awt.DynamicMenuContent;
32 import org.openide.util.actions.BooleanStateAction;
33 import org.openide.util.actions.SystemAction;
34
35 /** Default implementaiton of presenters for various action types.
36  */

37 public final class DefaultAWTBridge extends org.netbeans.modules.openide.util.AWTBridge {
38     public JMenuItem JavaDoc createMenuPresenter (Action JavaDoc action) {
39         if (action instanceof BooleanStateAction) {
40             BooleanStateAction b = (BooleanStateAction)action;
41             return new Actions.CheckboxMenuItem (b, true);
42         }
43         if (action instanceof SystemAction) {
44             SystemAction s = (SystemAction)action;
45             return new Actions.MenuItem (s, true);
46         }
47             
48         return new Actions.MenuItem (action, true);
49     }
50     
51     public JMenuItem JavaDoc createPopupPresenter(Action JavaDoc action) {
52         if (action instanceof BooleanStateAction) {
53             BooleanStateAction b = (BooleanStateAction)action;
54             return new Actions.CheckboxMenuItem (b, false);
55         }
56         if (action instanceof SystemAction) {
57             SystemAction s = (SystemAction)action;
58             return new Actions.MenuItem (s, false);
59         }
60             
61         return new Actions.MenuItem (action, false);
62     }
63     
64     public Component JavaDoc createToolbarPresenter(Action JavaDoc action) {
65         if (action instanceof BooleanStateAction) {
66             BooleanStateAction b = (BooleanStateAction)action;
67             return new Actions.ToolbarToggleButton (b);
68         }
69         if (action instanceof SystemAction) {
70             SystemAction s = (SystemAction)action;
71             return new Actions.ToolbarButton (s);
72         }
73             
74         return new Actions.ToolbarButton (action);
75     }
76     
77     public JPopupMenu JavaDoc createEmptyPopup() {
78         return new JPopupMenu JavaDoc();
79     }
80     
81     public Component JavaDoc[] convertComponents(Component JavaDoc comp) {
82          if (comp instanceof DynamicMenuContent) {
83             Component JavaDoc[] toRet = ((DynamicMenuContent)comp).getMenuPresenters();
84             boolean atLeastOne = false;
85             Collection JavaDoc<Component JavaDoc> col = new ArrayList JavaDoc<Component JavaDoc>();
86             for (int i = 0; i < toRet.length; i++) {
87                 if (toRet[i] instanceof DynamicMenuContent && toRet[i] != comp) {
88                     col.addAll(Arrays.asList(convertComponents(toRet[i])));
89                     atLeastOne = true;
90                 } else {
91                     if (toRet[i] == null) {
92                         toRet[i] = new JSeparator JavaDoc();
93                     }
94                     col.add(toRet[i]);
95                 }
96             }
97             if (atLeastOne) {
98                 return col.toArray(new Component JavaDoc[col.size()]);
99             } else {
100                 return toRet;
101             }
102          }
103          return new Component JavaDoc[] {comp};
104     }
105     
106 }
107
Popular Tags