001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.form.menu;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Point;
046: import java.util.HashMap;
047: import java.util.Map;
048: import javax.swing.BorderFactory;
049: import javax.swing.BoxLayout;
050: import javax.swing.JComponent;
051: import javax.swing.JLayeredPane;
052: import javax.swing.JMenu;
053: import javax.swing.JPanel;
054: import javax.swing.Popup;
055: import javax.swing.PopupFactory;
056: import javax.swing.SwingUtilities;
057: import org.netbeans.modules.form.RADComponent;
058: import org.netbeans.modules.form.RADVisualContainer;
059:
060: /** A PopupFactory which returns VisualDesignerJPanelPopup's
061: *
062: * @author joshua.marinacci@sun.com
063: */
064: class VisualDesignerPopupFactory extends PopupFactory {
065: public Map<JMenu, JPanel> containerMap;
066: private Map<JMenu, VisualDesignerJPanelPopup> popupMap;
067:
068: private MenuEditLayer canvas;
069:
070: public VisualDesignerPopupFactory(MenuEditLayer canvas) {
071: containerMap = new HashMap<JMenu, JPanel>();
072: popupMap = new HashMap<JMenu, VisualDesignerJPanelPopup>();
073: this .canvas = canvas;
074: }
075:
076: @Override
077: public Popup getPopup(Component owner, Component contents, int x,
078: int y) throws IllegalArgumentException {
079: final JMenu menu = (JMenu) owner;
080: JPanel cont = containerMap.get(menu);
081:
082: if (cont == null) {
083: cont = new VisualDesignerJPanelContainer(menu, this );
084: cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
085:
086: RADVisualContainer menuRAD = (RADVisualContainer) canvas.formDesigner
087: .getMetaComponent(menu);
088: for (RADComponent c : menuRAD.getSubBeans()) {
089: JComponent comp = (JComponent) canvas.formDesigner
090: .getComponent(c);
091: cont.add(comp);
092: }
093:
094: cont.setBorder(BorderFactory.createLineBorder(Color.BLACK));
095: containerMap.put(menu, cont);
096: canvas.layers.add(cont, JLayeredPane.DEFAULT_LAYER);
097: }
098:
099: cont.setSize(cont.getLayout().preferredLayoutSize(cont));
100: canvas.validate();
101: canvas.setVisible(true);
102: final JPanel fcont = cont;
103: SwingUtilities.invokeLater(new Runnable() {
104: public void run() {
105: setLocationFromMenu(menu, fcont);
106: }
107: });
108:
109: canvas.validate();
110: canvas.repaint();
111: VisualDesignerJPanelPopup popup = new VisualDesignerJPanelPopup(
112: cont, menu, this );
113: popupMap.put(menu, popup);
114: return popup;
115: }
116:
117: VisualDesignerJPanelPopup getPopup(JMenu menu) {
118: return popupMap.get(menu);
119: }
120:
121: private void setLocationFromMenu(final JMenu menu, final JPanel cont) {
122: Point pt = SwingUtilities.convertPoint(menu, new Point(0, 0),
123: canvas);
124:
125: JComponent parent = canvas.getMenuParent(menu);
126: if (parent instanceof JMenu) {
127: // get this menu's location in local coords
128: // move to the right edge of the menu
129: pt = new Point(pt.x + menu.getWidth(), pt.y);
130: } else {
131: // if parent isn't a jmenu the this must be a toplevel,
132: // so we must position below the menu instead of next to it
133: pt = new Point(pt.x, pt.y + menu.getHeight());
134: }
135: cont.setLocation(pt);
136: }
137:
138: void hideOtherMenus(JMenu menu) {
139: for (JMenu m : containerMap.keySet()) {
140: if (m != menu) {
141: // hide if not an ancestor of this menu
142: if (!isAncestor(m, menu)) {/* &&
143: (canvas.isTopLevelMenu(m) ||
144: canvas.hasSelectedDescendants(m))
145: ) {*/
146: JPanel popup = containerMap.get(m);
147: popup.setVisible(false);
148: }
149: }
150: }
151: }
152:
153: private boolean isAncestor(JMenu m, JMenu menu) {
154: return canvas.isAncestor(menu, m);
155: }
156:
157: private static class VisualDesignerJPanelContainer extends JPanel {
158: private JMenu menu;
159: private VisualDesignerPopupFactory fact;
160:
161: VisualDesignerJPanelContainer(JMenu menu,
162: VisualDesignerPopupFactory fact) {
163: this .menu = menu;
164: this .fact = fact;
165: }
166:
167: @Override
168: public void setVisible(boolean visible) {
169: // if making visible
170: if (visible) {
171: // make sure the other menus are hidden
172: fact.hideOtherMenus(menu);
173: // make sure this menu popup is at the right place
174: fact.setLocationFromMenu(menu, this );
175: // repack?
176: }
177: super.setVisible(visible);
178: }
179: }
180:
181: }
|