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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 *
23 * ident	"%Z%%M%	%I%	%E% SMI"
24 *
25 * Copyright (c) 1999 by Sun Microsystems, Inc.
26 * All rights reserved.
27 *
28 * pmButton.java
29 *
30 */
31
32package com.sun.admin.pm.client;
33
34import java.util.*;
35import java.awt.*;
36import java.awt.event.*;
37import javax.swing.*;
38
39import com.sun.admin.pm.server.*;
40
41/*
42 * strategy:
43 * Keep a hashtable of root panes and their associated default buttons.
44 * Note that there is at present no way to remove a root pane entry
45 * from the table...
46 *
47 * Ideally there should be an interface to allow objects to
48 * remove themselves before disappearing.
49 */
50
51public class pmButton extends JButton {
52
53    // static JButton defaultButton = null;
54
55    // map root panes to their true default buttons
56    static Hashtable map = new Hashtable();
57
58    public static Hashtable getHashtable() {
59        return map;
60    }
61
62    /*
63     * make this button the default on this root pane
64     * retunrs true if success, false o/w
65     */
66    boolean makeDefaultButton() {
67        return makeDefaultButton(this);
68    }
69
70    /*
71     * make b the default on this root pane
72     * returns true if success, false otherwise
73     */
74    boolean makeDefaultButton(JButton b) {
75        JRootPane r = this.getRootPane();
76
77        if (r == null) {
78            Debug.info("BUTTON:  null root panel");
79            return false;
80        }
81
82        if (b == null) {
83            Debug.info("BUTTON:  makeDefaultButton null on " + r);
84        }
85
86        /*
87         * Debug.info("\nBUTTON:  makeDefaultButton " +
88         *	(b == null ? "null" : b.getText()) +
89         *		" on " + r + "\n");
90         */
91
92        if (b != null && b.isDefaultCapable() == false) {
93            Debug.info("BUTTON:  false isDefaultCapable on " + r);
94            return false;
95        }
96
97        // unfocus the old default, if it's different
98        JButton oldb;
99        if ((oldb = r.getDefaultButton()) != null && oldb != b) {
100            oldb.setFocusPainted(false);
101        }
102
103        /*
104         * Debug.info("\nBUTTON:  makeDefaultButton: old button was " +
105         *	(oldb == null ? "null" : oldb.getText()) + "\n");
106         */
107
108        r.setDefaultButton(b);
109
110        return true;
111    }
112
113
114    public pmButton(String s) {
115        super(s);
116
117        this.addFocusListener(new FocusAdapter() {
118
119            // upon gaining focus: make this the root pane's default
120            public void focusGained(FocusEvent e) {
121                if (e.isTemporary()) {
122                    /*
123                     * Debug.info("BUTTON:  " + getText() +
124                     *		" gained temp - ignoring");
125                     */
126                    return;
127                }
128
129                Debug.info("BUTTON:  " + getText() + " gained");
130
131                if (makeDefaultButton())
132                    setFocusPainted(true);
133
134            }
135
136            // upon losing focus: make 'true' default the default
137            public void focusLost(FocusEvent e) {
138                if (e.isTemporary()) {
139                    /*
140                     * Debug.info("BUTTON:  " + getText() +
141                     *		" lost temp - ignoring");
142                     */
143                    return;
144                }
145
146                Debug.info("BUTTON:  " + getText() + " lost");
147
148                /*
149                 * i thought it might make sense to test for the
150                 * next focusable comp, but what if focus is being
151                 * lost as the result of a mouse click??
152                 */
153
154                makeDefaultButton((JButton) map.get(getRootPane()));
155                // setFocusPainted(false);
156            }
157
158        });
159    }
160
161    // make this the true default for this root pane
162    void setAsDefaultButton() {
163        setAsDefaultButton(this);
164    }
165
166    // make b the true default for this root pane
167    void setAsDefaultButton(JButton b) {
168        JRootPane r = getRootPane();
169
170        /*
171         * Debug.message("BUTTON:  setAsDefaultButton " +
172         *	(b == null ? "null" : b.getText()) +
173         *			" root = " + r);
174         */
175
176        // setting default to null removes state
177        if (b == null)
178            map.remove(r);
179        else
180            map.put(r, b);	// creates a new entry if needed
181        makeDefaultButton(b);
182    }
183
184
185    // clean up component about to be removed
186    void unreference() {
187        JRootPane r = getRootPane();
188        map.remove(r);
189    }
190
191    public static void unreference(JComponent c) {
192        JRootPane r = c.getRootPane();
193        map.remove(r);
194    }
195
196    public static void unreference(JRootPane r) {
197        map.remove(r);
198    }
199
200
201    static boolean enableMnemonics = false;
202
203    static void setEnableMnemonics(boolean m) {
204        enableMnemonics = m;
205    }
206
207    public void setMnemonic(int mnemonic) {
208        setMnemonic((char)mnemonic);
209    }
210
211    public void setMnemonic(char mnemonic) {
212        if (enableMnemonics)
213            super.setMnemonic(mnemonic);
214    }
215
216}
217