1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * ident	"%Z%%M%	%I%	%E% SMI"
24 *
25 * Copyright (c) 2000 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28
29/*
30 *        Copyright (C) 1996  Active Software, Inc.
31 *                  All rights reserved.
32 *
33 * @(#) MenuBarShadow.java 1.43 - last change made 07/28/97
34 */
35
36package sunsoft.jws.visual.rt.shadow.java.awt;
37
38import sunsoft.jws.visual.rt.base.*;
39import sunsoft.jws.visual.rt.type.AMRef;
40import sunsoft.jws.visual.rt.base.Global;
41
42import java.util.Enumeration;
43import java.awt.MenuBar;
44import java.awt.MenuComponent;
45import java.awt.Menu;
46
47/**
48 * Wraps an AWT widget.  The attributes available for this
49 * class are listed below.  In the type column, type names beginning
50 * with "sunsoft.jws.visual.rt" have been abbreviated to begin with "rt".
51 *
52 * <pre>
53name            type                      default value
54-----------------------------------------------------------------------
55helpMenu        rt.type.AMRef             null
56*  < /pre>
57*
58* Check the super class for additional attributes.
59*
60* @see MenuBar
61*/
62public class MenuBarShadow extends MenuComponentShadow implements AMContainer {
63    private AMContainerHelper helper = new AMContainerHelper(this);
64    private boolean fHelpMenu = false;
65    private boolean fCreate = false;
66    private Menu helpMenu = null;
67
68    public MenuBarShadow() {
69        attributes.add(/* NOI18N */"helpMenu",
70		       /* NOI18N */"sunsoft.jws.visual.rt.type.AMRef",
71		       null, 0);
72    }
73
74    protected Object getOnBody(String key) {
75        if (key.equals(/* NOI18N */"helpMenu"))
76	    // a menu shadow ref is returned
77	    return (getFromTable(/* NOI18N */"helpMenu"));
78        else
79            return (super.getOnBody(key));
80    }
81
82    public void create()
83    {
84        fCreate = true;
85        super.create();
86        fCreate = false;
87    }
88    protected void setOnBody(String key, Object value) {
89        if (key.equals(/* NOI18N */"helpMenu")) {
90            // Check to make sure value is set yet
91            if (value != null) {
92                // a reference to the help menu shadow is what is
93                // stored as attribute
94                MenuShadow ms = (MenuShadow)((AMRef)value).getRef(this);
95                // Create the body if it does not yet exist
96                if (ms != null && ms.getBody() == null)
97		    ms.createBody();
98
99                if (ms != null && ms.getBody() != null)
100		    {
101
102
103			if (Global.isWindows())
104			    {
105				if (fCreate) // from create Menubar
106				    {
107					fHelpMenu = true;
108					helpMenu = (Menu) ms.getBody();
109				// no create just setting the help menu...
110				    } else
111					    /* JSTYLED */
112					((MenuBar) body).setHelpMenu((Menu) ms.getBody());
113			    }
114			else
115			    {
116				    /* JSTYLED */
117				((MenuBar) body).setHelpMenu((Menu) ms.getBody());
118			    }
119		    }
120                else
121			/* JSTYLED */
122                    System.out.println(Global.fmtMsg("sunsoft.jws.visual.rt.awt.java.awt.MenuBarShadow.CantResolveHelpMenu",
123						    getName(),
124						    ((AMRef)value).getName()));
125            }
126        }
127        else
128            super.setOnBody(key, value);
129    }
130
131    public void createBody() {
132        body = new MenuBar();
133    }
134
135
136
137    protected void postCreate() {
138
139        /*
140         * note that the sethelpMenu call in the jdk creates and adds
141         * it if the menu does not exists.
142         * so during the setOnBody the help menu is created first.
143         * and since on windows the menu appear as they are added we
144         * see the help menu first. I believe it is
145         * a bug in the windows peer. This is a workaround for that..
146         * we remove and add the help menu again..
147         * after all the menus are created.. bug id 4033014....-kp
148         */
149
150        Menu m;
151        MenuBar menubar;
152        menubar = (MenuBar)body;
153        int i;
154
155        if (Global.isWindows())
156	    {
157		if (fHelpMenu)  // help menu present so add it..
158		    {
159			if (helpMenu != null)
160			    menubar.setHelpMenu(helpMenu);
161		    }
162	    }
163        super.postCreate();
164    }
165
166    // AMContainer interfaces
167
168    public void add(AttributeManager child) {
169        helper.add(child);
170    }
171
172    public void remove(AttributeManager child) {
173        helper.remove(child);
174    }
175
176    public void addChildBody(Shadow child) {
177        if (body != null) {
178            MenuBar menubar = (MenuBar)body;
179            Menu menu = (Menu)child.getBody();
180
181            if (menu.getParent() != menubar) {
182                menubar.add(menu);
183                updateContainerAttributes((AMContainer)this, child);
184            }
185        }
186    }
187
188    public void updateContainerAttribute(AttributeManager child,
189					 String key, Object value) {
190        // Menus could have a layout constraint specifying their position
191        // in the menubar.  This is not yet implemented.
192    }
193
194    public void removeChildBody(Shadow child) {
195        if (body != null) {
196            ((MenuBar) body).remove((MenuComponent) child.getBody());
197        }
198    }
199
200    public void createChildren() {
201        helper.createChildren();
202    }
203
204    public void reparentChildren() {
205        helper.reparentChildren();
206    }
207
208    public void destroyChildren() {
209        helper.destroyChildren();
210    }
211
212    public AttributeManager getChild(String name) {
213        return (helper.getChild(name));
214    }
215
216    public Enumeration getChildList() {
217        return (helper.getChildList());
218    }
219
220    public int getChildCount() {
221        return (helper.getChildCount());
222    }
223
224    /**
225     * replicate is used by Visual Java for cut and paste.
226     */
227    //
228    // Normally this method resides in AttributeManager (super-duper class).
229    // We override it here to put in special handling for the helpMenu --
230    // fix for Sun Bug # 4043169: help menu getting duplicated on
231    // copy/paste frame with help menu.  -- Simran 5/16/97
232    //
233    public AttributeManager replicate() {
234
235        // System.out.println("MenuBarShadow.replicate: ");
236        // System.out.println("       this: "+this);
237
238        // Create a new instance of the AttributeManager
239        AttributeManager newMgr = null;
240        try {
241            newMgr = (AttributeManager)getClass().newInstance();
242        }
243        catch (InstantiationException ex) {
244            System.out.println(ex.getMessage() + /* NOI18N */" " + this);
245        }
246        catch (IllegalAccessException ex) {
247            System.out.println(ex.getMessage() + /* NOI18N */" " + this);
248        }
249        if (newMgr == null)
250            return null;
251
252        // Copy the attribute list
253        AttributeList list = getAttributeList();
254        // System.out.println(" ----- attribute list: ");
255        // printAttList(list);
256        Enumeration e = list.elements();
257        String helpMenuName = null;
258        AttributeManager helpMenuMgr = null;
259        while (e.hasMoreElements()) {
260            Attribute attr = (Attribute)e.nextElement();
261            String aName = attr.getName();
262
263            //
264            // Find the name of the helpMenu, don't set it until
265            // we replicate the help menu. Then set it to the name of the
266            // newly replicated helpMenu.
267            // Fix for Sun Bug # 4043169: help menu getting duplicated on
268            // copy/paste frame with help menu.  -- Simran 5/16/97
269            //
270            if (aName.equals(/* NOI18N */"helpMenu")) {
271                Object gv = attr.getValue();
272                AMRef helpMenuRef = (AMRef)gv;
273                if (helpMenuRef != null) {
274                    helpMenuMgr =  helpMenuRef.getRef(this);
275                    helpMenuName = helpMenuRef.getName();
276                }
277
278                // System.out.println("FOund help menu
279                // attribute. *NOT* setting it in new mgr. (yet)");
280
281            } else  if (!attr.flagged(TRANSIENT | READONLY)) {
282                newMgr.set(aName, attr.getValue());
283            }
284        }
285
286        // Replicate the children
287        if (this instanceof AMContainer) {
288            AMContainer newCntr = (AMContainer)newMgr;
289            e = ((AMContainer)this).getChildList();
290
291            // System.out.print("       childList: (isMenuBar: "+isMenuBar+")");
292            // printChildList(this);
293            while (e.hasMoreElements()) {
294                AttributeManager child = (AttributeManager)e.nextElement();
295
296                // Check for helpmenu
297                //
298
299                // System.out.println(); System.out.println("IS
300                // MENUBAR: child name="+child.getName());
301                // System.out.println(" looking for: "+helpMenuName);
302
303                // Is the name test going to be sufficient for finding
304                // the help menu?  It seems that if we're restricted
305                // to doing the test only if isMenuBar, then we're
306                // OK. How unique do names have to be?
307                if (child.getName().equals(helpMenuName)) {
308                    // System.out.println(" replicating
309                    // (helpMenu)child: "+child);
310                    AttributeManager replicantChild = child.replicate();
311                    newMgr.set(/* NOI18N */"helpMenu",
312			       new AMRef(replicantChild));
313                    newCntr.add(replicantChild);
314                } else {
315                    // System.out.println(" replicating (normal) child
316                    // of MenuBar: "+child);
317                    newCntr.add(child.replicate());
318                }
319            }
320        }
321
322        return newMgr;
323    }
324
325    void printAttList(AttributeList list) {
326        Enumeration e = list.elements();
327        while (e.hasMoreElements()) {
328            Attribute attr = (Attribute)e.nextElement();
329            // System.out.println(" name: "+attr.getName()+"; value:
330            // "+(String)attr.getValue());
331            System.out.print(/* NOI18N */" name: "
332			     + attr.getName()+ /* NOI18N */"; ");
333            Object gv = attr.getValue();
334            System.out.println(/* NOI18N */"  value.toString(): "
335			       + ((gv != null) ? gv.toString()
336				  : /* NOI18N */"<null value>"));
337            if (attr.getName().equals(/* NOI18N */"helpMenu")) {
338                System.out.println();
339                System.out.println(/* NOI18N */"  found help menu att: "
340				   + ((gv != null) ? gv.toString()
341				      : /* NOI18N */"<null value>"));
342                System.out.println();
343            }
344        }
345    }
346
347    void printChildList(AttributeManager mgr) {
348        int i = 0;
349        for (Enumeration e = ((AMContainer) this).getChildList();
350	     /* JSTYLED */
351	     e.hasMoreElements();) {
352
353            System.out.print(/* NOI18N */"     child["+i+ /* NOI18N */"] ");
354            System.out.println(((AttributeManager)e.nextElement())
355			       + /* NOI18N */"");
356            i++;
357        }
358        System.out.println(/* NOI18N */"==============");
359        System.out.println();
360    }
361}
362