aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/com/sun/java/swing/ui/CommonUI.java
blob: a1c8127f475be65dbe7cd7f69dd9f0d9722cf1bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */


package com.sun.java.swing.ui;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.*;

public class CommonUI
{
    private static class NumberDocument extends PlainDocument
    {

        public void insertString(int offs, String str, AttributeSet atts)
            throws BadLocationException
        {
            if(!Character.isDigit(str.charAt(0)))
            {
                return;
            } else
            {
                super.insertString(offs, str, atts);
                return;
            }
        }

        private NumberDocument()
        {
        }

    }


    public CommonUI()
    {
    }

    public static JLabel createLabel(String text, int mnemonic, Component comp)
    {
        JLabel label = new JLabel("  " + text);
        label.setMinimumSize(labelPrefSize);
        if(mnemonic != -1)
            label.setDisplayedMnemonic(mnemonic);
        if(comp != null)
            label.setLabelFor(comp);
        if(text.length() == 0)
            label.setPreferredSize(labelPrefSize);
        return label;
    }

    public static JLabel createLabel(String text)
    {
        return createLabel(text, -1, null);
    }

    public static JTextField createTextField(String text, KeyListener listener, boolean numbers)
    {
        JTextField field = new JTextField(text);
        field.setMinimumSize(textPrefSize);
        if(text.length() == 0)
            field.setPreferredSize(textPrefSize);
        if(listener != null)
            field.addKeyListener(listener);
        if(numbers)
            field.setDocument(new NumberDocument());
        return field;
    }

    public static JTextField createTextField(String text, boolean numbers)
    {
        return createTextField(text, null, numbers);
    }

    public static JTextField createTextField(String text, KeyListener listener)
    {
        return createTextField(text, listener, false);
    }

    public static JTextField createTextField(String text)
    {
        return createTextField(text, null, false);
    }

    public static JRadioButton createRadioButton(String text, int mnemonic, ActionListener listener, boolean selected)
    {
        JRadioButton button = new JRadioButton(text);
        button.setMnemonic(mnemonic);
        button.setSelected(selected);
        button.setMinimumSize(labelPrefSize);
        if(listener != null)
            button.addActionListener(listener);
        if(text.length() == 0)
            button.setPreferredSize(labelPrefSize);
        return button;
    }

    public static JRadioButton createRadioButton(String text, int mnemonic, boolean selected)
    {
        return createRadioButton(text, mnemonic, null, selected);
    }

    public static JRadioButton createRadioButton(String text, int mnemonic, ActionListener listener)
    {
        return createRadioButton(text, mnemonic, listener, false);
    }

    public static JRadioButton createRadioButton(String text, int mnemonic)
    {
        return createRadioButton(text, mnemonic, null, false);
    }

    public static JRadioButton createRadioButton(String text)
    {
        return createRadioButton(text, -1, null, false);
    }

    public static JCheckBox createCheckBox(String text, int mnemonic, ActionListener listener, boolean selected)
    {
        JCheckBox checkbox = new JCheckBox(text);
        checkbox.setMinimumSize(labelPrefSize);
        if(mnemonic != -1)
            checkbox.setMnemonic(mnemonic);
        checkbox.setSelected(selected);
        if(text.length() == 0)
            checkbox.setPreferredSize(labelPrefSize);
        if(listener != null)
            checkbox.addActionListener(listener);
        return checkbox;
    }

    public static JCheckBox createCheckBox(String text, int mnemonic, ActionListener listener)
    {
        return createCheckBox(text, mnemonic, listener, false);
    }

    public static JCheckBox createCheckBox(String text, int mnemonic, boolean selected)
    {
        return createCheckBox(text, mnemonic, null, selected);
    }

    public static JCheckBox createCheckBox(String text, int mnemonic)
    {
        return createCheckBox(text, mnemonic, null, false);
    }

    public static JCheckBox createCheckBox(String text)
    {
        return createCheckBox(text, -1, null, false);
    }

    public static JComboBox createComboBox(Object items[], ActionListener listener, boolean editable)
    {
        JComboBox comboBox = new JComboBox(items);
        if(listener != null)
            comboBox.addActionListener(listener);
        comboBox.setEditable(editable);
        return comboBox;
    }

    public static JComboBox createComboBox(Object items[], boolean editable)
    {
        return createComboBox(items, null, editable);
    }

    public static JComboBox createComboBox(Vector items, ActionListener listener, boolean editable)
    {
        JComboBox comboBox = new JComboBox(items);
        if(listener != null)
            comboBox.addActionListener(listener);
        comboBox.setEditable(editable);
        return comboBox;
    }

    public static JComboBox createComboBox(Vector items, boolean editable)
    {
        return createComboBox(items, null, editable);
    }

    public static JButton createButton(Action action)
    {
        JButton button = new JButton(action);
        setButtonSize(button, buttonPrefSize);
        return button;
    }

    public static JButton createButton(String text, ActionListener listener, int mnemonic)
    {
        JButton button = new JButton(text);
        if(listener != null)
            button.addActionListener(listener);
        if(mnemonic != -1)
            button.setMnemonic(mnemonic);
        setButtonSize(button, buttonPrefSize);
        return button;
    }

    private static void setButtonSize(JButton button, Dimension size)
    {
        String text = button.getText();
        button.setMinimumSize(size);
        if(text.length() == 0)
        {
            button.setPreferredSize(size);
        } else
        {
            Dimension psize = button.getPreferredSize();
            if(psize.width < size.width)
                button.setPreferredSize(size);
        }
    }

    public static JButton createButton(String text, ActionListener listener)
    {
        return createButton(text, listener, -1);
    }

    public static JButton createSmallButton(String text, ActionListener listener, int mnemonic)
    {
        JButton button = createButton(text, listener, mnemonic);
        setButtonSize(button, smbuttonPrefSize);
        return button;
    }

    public static JButton createSmallButton(String text, ActionListener listener)
    {
        return createSmallButton(text, listener, -1);
    }

    public static Border createBorder(String text)
    {
        Border border = BorderFactory.createEtchedBorder();
        return BorderFactory.createTitledBorder(border, text, 0, 2);
    }

    public static Border createBorder()
    {
        return BorderFactory.createEmptyBorder(4, 4, 4, 4);
    }

    public static JScrollPane createListPane(JList list, String text)
    {
        JScrollPane pane = new JScrollPane(list);
        pane.setBorder(BorderFactory.createCompoundBorder(createBorder(text), BorderFactory.createLoweredBevelBorder()));
        return pane;
    }

    public static void centerComponent(Component source, Component parent)
    {
        Dimension dim = source.getSize();
        Rectangle rect;
        if(parent != null)
        {
            rect = parent.getBounds();
        } else
        {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
            rect = new Rectangle(0, 0, d.width, d.height);
        }
        int x = rect.x + (rect.width - dim.width) / 2;
        int y = rect.y + (rect.height - dim.height) / 2;
        source.setLocation(x, y);
    }

    public static void centerComponent(Component source)
    {
        centerComponent(source, null);
    }

    public static JFrame getParentFrame(Component source)
    {
        Container parent;
        for(parent = source.getParent(); parent != null; parent = parent.getParent())
            if(parent instanceof JFrame)
                break;

        if(parent == null)
            return null;
        else
            return (JFrame)parent;
    }

    public static Integer msToSec(Integer ms)
    {
        int value = ms.intValue();
        value /= 1000;
        return new Integer(value);
    }

    public static Integer secToMs(Integer sec)
    {
        int value = sec.intValue();
        value *= 1000;
        return new Integer(value);
    }

    public static String stringFromStringArray(String strings[], String delim)
    {
        String string = "";
        String separator;
        if(delim == null || delim.equals(""))
            separator = " ";
        else
            separator = delim;
        for(int i = 0; i < strings.length; i++)
        {
            string = string + strings[i];
            string = string + separator;
        }

        return string;
    }

    public static String stringFromStringArray(String strings[])
    {
        return stringFromStringArray(strings, "");
    }

    public static String[] stringArrayFromString(String string, String delim)
    {
        StringTokenizer st;
        if(delim == null || delim.equals(""))
            st = new StringTokenizer(string);
        else
            st = new StringTokenizer(string, delim);
        int numTokens = st.countTokens();
        String strings[] = new String[numTokens];
        int index = 0;
        while(st.hasMoreTokens())
            strings[index++] = st.nextToken();
        return strings;
    }

    public static String[] stringArrayFromString(String string)
    {
        return stringArrayFromString(string, "");
    }

    public static void setWaitCursor(Component comp)
    {
        comp.setCursor(Cursor.getPredefinedCursor(3));
    }

    public static void setDefaultCursor(Component comp)
    {
        comp.setCursor(Cursor.getPredefinedCursor(0));
    }

    public static Dimension getButtconPrefSize()
    {
        return buttconPrefSize;
    }

    private static final int BUTTON_WIDTH = 100;
    private static final int BUTTON_HEIGHT = 26;
    private static final int BUTTCON_WIDTH = 28;
    private static final int BUTTCON_HEIGHT = 28;
    private static final int SM_BUTTON_WIDTH = 72;
    private static final int SM_BUTTON_HEIGHT = 26;
    private static final int LABEL_WIDTH = 100;
    private static final int LABEL_HEIGHT = 20;
    private static final int TEXT_WIDTH = 150;
    private static final int TEXT_HEIGHT = 20;
    private static final Dimension buttonPrefSize = new Dimension(100, 26);
    private static final Dimension buttconPrefSize = new Dimension(28, 28);
    private static final Dimension smbuttonPrefSize = new Dimension(72, 26);
    private static final Dimension labelPrefSize = new Dimension(100, 20);
    private static final Dimension textPrefSize = new Dimension(150, 20);

}