2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.form.menu;
43
import java.awt.Color;
44
import java.awt.Component;
45
import java.awt.Point;
46
import java.util.HashMap;
48
import javax.swing.BorderFactory;
49
import javax.swing.BoxLayout;
50
import javax.swing.JComponent;
51
import javax.swing.JLayeredPane;
52
import javax.swing.JMenu;
53
import javax.swing.JPanel;
54
import javax.swing.Popup;
55
import javax.swing.PopupFactory;
56
import javax.swing.SwingUtilities;
57
import org.netbeans.modules.form.RADComponent;
58
import org.netbeans.modules.form.RADVisualContainer;
60
/** A PopupFactory which returns VisualDesignerJPanelPopup's
62
* @author joshua.marinacci@sun.com
64
class VisualDesignerPopupFactory extends PopupFactory {
65
private static final boolean DEBUG = false;
67
public Map<JMenu, JPanel> containerMap;
68
private Map<JMenu, VisualDesignerJPanelPopup> popupMap;
70
private MenuEditLayer canvas;
72
public VisualDesignerPopupFactory(MenuEditLayer canvas) {
73
containerMap = new HashMap<JMenu, JPanel>();
74
popupMap = new HashMap<JMenu, VisualDesignerJPanelPopup>();
78
public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
80
final JMenu menu = (JMenu) owner;
81
p("creating a popup for: " + menu.getText());
82
JComponent parent = canvas.getMenuParent(menu);
83
JPanel cont = containerMap.get(menu);
86
cont = new VisualDesignerJPanelContainer(menu,this);
87
cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
90
RADVisualContainer menuRAD = (RADVisualContainer) canvas.formDesigner.getMetaComponent(menu);
91
for(RADComponent c : menuRAD.getSubBeans()) {
92
JComponent comp = (JComponent) canvas.formDesigner.getComponent(c);
96
cont.setBorder(BorderFactory.createLineBorder(Color.BLACK));
97
containerMap.put(menu,cont);
98
canvas.layers.add(cont, JLayeredPane.DEFAULT_LAYER);
101
cont.setSize(cont.getLayout().preferredLayoutSize(cont));
103
canvas.setVisible(true);
104
final JPanel fcont = cont;
105
SwingUtilities.invokeLater(new Runnable() {
107
setLocationFromMenu(menu, fcont);
113
VisualDesignerJPanelPopup popup = new VisualDesignerJPanelPopup(cont, menu, this);
114
popupMap.put(menu,popup);
118
VisualDesignerJPanelPopup getPopup(JMenu menu) {
119
return popupMap.get(menu);
122
private void setLocationFromMenu(final JMenu menu, final JPanel cont) {
123
Point pt = SwingUtilities.convertPoint(menu, new Point(0,0), canvas);
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);
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());
135
cont.setLocation(pt);
138
private static void p(String string) {
140
System.out.println(string);
144
void hideOtherMenus(JMenu menu) {
145
for(JMenu m : containerMap.keySet()) {
147
// hide if not an ancestor of this menu
148
if(!isAncestor(m,menu)) {/* &&
149
(canvas.isTopLevelMenu(m) ||
150
canvas.hasSelectedDescendants(m))
152
JPanel popup = containerMap.get(m);
153
popup.setVisible(false);
159
private boolean isAncestor(JMenu m, JMenu menu) {
160
return canvas.isAncestor(menu, m);
163
private static class VisualDesignerJPanelContainer extends JPanel {
165
private VisualDesignerPopupFactory fact;
166
VisualDesignerJPanelContainer(JMenu menu, VisualDesignerPopupFactory fact) {
170
public void setVisible(boolean visible) {
173
// make sure the other menus are hidden
174
fact.hideOtherMenus(menu);
175
// make sure this menu popup is at the right place
176
fact.setLocationFromMenu(menu,this);
179
super.setVisible(visible);
b'\\ No newline at end of file'