~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to form/src/org/netbeans/modules/form/menu/VisualDesignerPopupFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.form.menu;
 
42
 
 
43
import java.awt.Color;
 
44
import java.awt.Component;
 
45
import java.awt.Point;
 
46
import java.util.HashMap;
 
47
import java.util.Map;
 
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;
 
59
 
 
60
/** A PopupFactory which returns VisualDesignerJPanelPopup's
 
61
 *
 
62
 * @author joshua.marinacci@sun.com
 
63
 */
 
64
class VisualDesignerPopupFactory extends PopupFactory {
 
65
    private static final boolean DEBUG = false;
 
66
    
 
67
    public Map<JMenu, JPanel> containerMap;
 
68
    private Map<JMenu, VisualDesignerJPanelPopup> popupMap;
 
69
    
 
70
    private MenuEditLayer canvas;
 
71
    
 
72
    public VisualDesignerPopupFactory(MenuEditLayer canvas) {
 
73
        containerMap = new HashMap<JMenu, JPanel>();
 
74
        popupMap = new HashMap<JMenu, VisualDesignerJPanelPopup>();
 
75
        this.canvas = canvas;
 
76
    }
 
77
    
 
78
    public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
 
79
        
 
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);
 
84
        
 
85
        if (cont == null) {
 
86
            cont = new VisualDesignerJPanelContainer(menu,this);
 
87
            cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));
 
88
            
 
89
            
 
90
            RADVisualContainer menuRAD = (RADVisualContainer) canvas.formDesigner.getMetaComponent(menu);
 
91
            for(RADComponent c : menuRAD.getSubBeans()) {
 
92
                JComponent comp = (JComponent) canvas.formDesigner.getComponent(c);
 
93
                cont.add(comp);
 
94
            }
 
95
            
 
96
            cont.setBorder(BorderFactory.createLineBorder(Color.BLACK));
 
97
            containerMap.put(menu,cont);
 
98
            canvas.layers.add(cont, JLayeredPane.DEFAULT_LAYER);
 
99
        }
 
100
        
 
101
        cont.setSize(cont.getLayout().preferredLayoutSize(cont));
 
102
        canvas.validate();
 
103
        canvas.setVisible(true);
 
104
        final JPanel fcont = cont;
 
105
        SwingUtilities.invokeLater(new Runnable() {
 
106
            public void run() {
 
107
                setLocationFromMenu(menu, fcont);
 
108
            }
 
109
        });
 
110
        
 
111
        canvas.validate();
 
112
        canvas.repaint();
 
113
        VisualDesignerJPanelPopup popup = new VisualDesignerJPanelPopup(cont, menu, this);
 
114
        popupMap.put(menu,popup);
 
115
        return popup;
 
116
    }
 
117
    
 
118
    VisualDesignerJPanelPopup getPopup(JMenu menu) {
 
119
        return popupMap.get(menu);
 
120
    }
 
121
    
 
122
    private void setLocationFromMenu(final JMenu menu, final JPanel cont) {
 
123
        Point pt = SwingUtilities.convertPoint(menu, new Point(0,0), 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
    private static void p(String string) {
 
139
        if(DEBUG) {
 
140
            System.out.println(string);
 
141
        }
 
142
    }
 
143
    
 
144
    void hideOtherMenus(JMenu menu) {
 
145
        for(JMenu m : containerMap.keySet()) {
 
146
            if(m != menu) {
 
147
                // hide if not an ancestor of this menu
 
148
                if(!isAncestor(m,menu)) {/* && 
 
149
                        (canvas.isTopLevelMenu(m) ||
 
150
                         canvas.hasSelectedDescendants(m))
 
151
                        ) {*/
 
152
                    JPanel popup = containerMap.get(m);
 
153
                    popup.setVisible(false);
 
154
                }
 
155
            }
 
156
        }
 
157
    }
 
158
    
 
159
    private boolean isAncestor(JMenu m, JMenu menu) {
 
160
        return canvas.isAncestor(menu, m);
 
161
    }
 
162
    
 
163
    private static class VisualDesignerJPanelContainer extends JPanel {
 
164
        private JMenu menu;
 
165
        private VisualDesignerPopupFactory fact;
 
166
        VisualDesignerJPanelContainer(JMenu menu, VisualDesignerPopupFactory fact) {
 
167
            this.menu = menu;
 
168
            this.fact = fact;
 
169
        }
 
170
        public void setVisible(boolean visible) {
 
171
            // if making visible
 
172
            if(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);
 
177
                // repack?
 
178
            }
 
179
            super.setVisible(visible);
 
180
        }
 
181
    }
 
182
    
 
183
}
 
 
b'\\ No newline at end of file'