KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > JInlineMenu


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 package org.openide.awt;
20
21 import org.openide.util.Utilities;
22
23 import java.awt.*;
24 import java.awt.event.*;
25
26 import java.io.*;
27
28 import java.util.List JavaDoc;
29
30 import javax.swing.*;
31 import javax.swing.event.*;
32 import org.openide.awt.DynamicMenuContent;
33 import org.openide.util.actions.Presenter;
34
35
36 /**
37  * Menu element that can contain other menu items. These items are then
38  * displayed "inline". The JInlineMenu can be used to compose more menu items
39  * into one that can be added/removed at once.
40  *
41  * @deprecated since org.openide.awt 6.5 JInlineMenu is a simple implementation of {@link DynamicMenuContent}, it
42  * doesn't update when visible and doesn't handle the separators itself anymore.
43  *
44  * @author Jan Jancura
45  */

46 public class JInlineMenu extends JMenuItem implements DynamicMenuContent {
47     /** generated Serialized Version UID */
48     static final long serialVersionUID = -2310488127953523571L;
49     private static final Icon BLANK_ICON = new ImageIcon(
50             Utilities.loadImage("org/openide/resources/actions/empty.gif")
51         ); // NOI18N
52

53 // /** north separator */
54
// private JSeparator north = new JSeparator();
55
//
56
// /** south separator */
57
// private JSeparator south = new JSeparator();
58

59     /** Stores inner MenuItems added to outer menu. */
60     private JComponent[] items = new JComponent[0];
61
62     /** true iff items of this menu are up to date */
63     boolean upToDate;
64
65     /** private List of the items previously added to the parent menu */
66     private List JavaDoc addedItems;
67
68     /**
69     * Creates new JInlineMenu.
70     */

71     public JInlineMenu() {
72         setEnabled(false);
73         setVisible(false);
74         upToDate = true;
75     }
76
77     /** Overriden to eliminate big gap at top of JInline popup painting.
78      * @return cleared instets (0, 0, 0, 0) */

79     public Insets getInsets() {
80         return new Insets(0, 0, 0, 0);
81     }
82
83     /**
84      * Setter for array of items to display. Can be called only from event queue
85      * thread.
86      *
87      * @param newItems array of menu items to display
88      */

89     public void setMenuItems(final JMenuItem[] newItems) {
90         // if(!SwingUtilities.isEventDispatchThread()) {
91
//System.err.println("JInlineMenu.setMenuItems called outside of event queue !!!");
92
//Thread.dumpStack();
93
// }
94
// make a tuned private copy
95
JComponent[] local = new JComponent[newItems.length];
96
97         for (int i = 0; i < newItems.length; i++) {
98             local[i] = (newItems[i] != null) ? (JComponent) newItems[i] : new JSeparator();
99         }
100
101         items = local;
102         upToDate = false;
103
104         alignItems();
105
106     }
107
108
109     /** Overriden to return first non null icon of current items or null if
110      * all items has null icons.
111      */

112     private void alignItems() {
113         // hack - we use also getIcon() result of JInlineMenu as indicator if we
114
// should try to align items using empty icon or not
115
boolean shouldAlign = getIcon() != null;
116
117         if (!shouldAlign) {
118             for (int i = 0; i < items.length; i++) {
119                 if (items[i] instanceof JMenuItem) {
120                     if (((JMenuItem) items[i]).getIcon() != null) {
121                         shouldAlign = true;
122
123                         break;
124                     }
125                 }
126             }
127         }
128
129         if (!shouldAlign) {
130             return;
131         }
132
133         // align items using empty icon
134
JMenuItem curItem = null;
135
136         for (int i = 0; i < items.length; i++) {
137             if (items[i] instanceof JMenuItem) {
138                 curItem = (JMenuItem) items[i];
139
140                 if (curItem.getIcon() == null) {
141                     curItem.setIcon(BLANK_ICON);
142                 }
143             }
144         }
145     }
146
147
148
149     /** Finds the index of a component in array of components.
150      * @return index or -1
151      */

152     private static int findIndex(Object JavaDoc of, Object JavaDoc[] arr) {
153         int menuLength = arr.length;
154
155         for (int i = 0; i < menuLength; i++) {
156             if (of == arr[i]) {
157                 return i;
158             }
159         }
160
161         return -1;
162     }
163
164
165     public JComponent[] synchMenuPresenters(JComponent[] items) {
166         return this.items;
167     }
168
169     public JComponent[] getMenuPresenters() {
170         return items;
171     }
172
173 }
174
Popular Tags