hotspot/agent/src/share/classes/com/sun/java/swing/ui/CommonUI.java
changeset 966 9373f9953a5e
child 5547 f4b087cbb361
equal deleted inserted replaced
817:cd8b8f500fac 966:9373f9953a5e
       
     1 /*
       
     2  * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 package com.sun.java.swing.ui;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.ActionListener;
       
    30 import java.awt.event.KeyListener;
       
    31 import java.util.StringTokenizer;
       
    32 import java.util.Vector;
       
    33 import javax.swing.*;
       
    34 import javax.swing.border.Border;
       
    35 import javax.swing.text.*;
       
    36 
       
    37 public class CommonUI
       
    38 {
       
    39     private static class NumberDocument extends PlainDocument
       
    40     {
       
    41 
       
    42         public void insertString(int offs, String str, AttributeSet atts)
       
    43             throws BadLocationException
       
    44         {
       
    45             if(!Character.isDigit(str.charAt(0)))
       
    46             {
       
    47                 return;
       
    48             } else
       
    49             {
       
    50                 super.insertString(offs, str, atts);
       
    51                 return;
       
    52             }
       
    53         }
       
    54 
       
    55         private NumberDocument()
       
    56         {
       
    57         }
       
    58 
       
    59     }
       
    60 
       
    61 
       
    62     public CommonUI()
       
    63     {
       
    64     }
       
    65 
       
    66     public static JLabel createLabel(String text, int mnemonic, Component comp)
       
    67     {
       
    68         JLabel label = new JLabel("  " + text);
       
    69         label.setMinimumSize(labelPrefSize);
       
    70         if(mnemonic != -1)
       
    71             label.setDisplayedMnemonic(mnemonic);
       
    72         if(comp != null)
       
    73             label.setLabelFor(comp);
       
    74         if(text.length() == 0)
       
    75             label.setPreferredSize(labelPrefSize);
       
    76         return label;
       
    77     }
       
    78 
       
    79     public static JLabel createLabel(String text)
       
    80     {
       
    81         return createLabel(text, -1, null);
       
    82     }
       
    83 
       
    84     public static JTextField createTextField(String text, KeyListener listener, boolean numbers)
       
    85     {
       
    86         JTextField field = new JTextField(text);
       
    87         field.setMinimumSize(textPrefSize);
       
    88         if(text.length() == 0)
       
    89             field.setPreferredSize(textPrefSize);
       
    90         if(listener != null)
       
    91             field.addKeyListener(listener);
       
    92         if(numbers)
       
    93             field.setDocument(new NumberDocument());
       
    94         return field;
       
    95     }
       
    96 
       
    97     public static JTextField createTextField(String text, boolean numbers)
       
    98     {
       
    99         return createTextField(text, null, numbers);
       
   100     }
       
   101 
       
   102     public static JTextField createTextField(String text, KeyListener listener)
       
   103     {
       
   104         return createTextField(text, listener, false);
       
   105     }
       
   106 
       
   107     public static JTextField createTextField(String text)
       
   108     {
       
   109         return createTextField(text, null, false);
       
   110     }
       
   111 
       
   112     public static JRadioButton createRadioButton(String text, int mnemonic, ActionListener listener, boolean selected)
       
   113     {
       
   114         JRadioButton button = new JRadioButton(text);
       
   115         button.setMnemonic(mnemonic);
       
   116         button.setSelected(selected);
       
   117         button.setMinimumSize(labelPrefSize);
       
   118         if(listener != null)
       
   119             button.addActionListener(listener);
       
   120         if(text.length() == 0)
       
   121             button.setPreferredSize(labelPrefSize);
       
   122         return button;
       
   123     }
       
   124 
       
   125     public static JRadioButton createRadioButton(String text, int mnemonic, boolean selected)
       
   126     {
       
   127         return createRadioButton(text, mnemonic, null, selected);
       
   128     }
       
   129 
       
   130     public static JRadioButton createRadioButton(String text, int mnemonic, ActionListener listener)
       
   131     {
       
   132         return createRadioButton(text, mnemonic, listener, false);
       
   133     }
       
   134 
       
   135     public static JRadioButton createRadioButton(String text, int mnemonic)
       
   136     {
       
   137         return createRadioButton(text, mnemonic, null, false);
       
   138     }
       
   139 
       
   140     public static JRadioButton createRadioButton(String text)
       
   141     {
       
   142         return createRadioButton(text, -1, null, false);
       
   143     }
       
   144 
       
   145     public static JCheckBox createCheckBox(String text, int mnemonic, ActionListener listener, boolean selected)
       
   146     {
       
   147         JCheckBox checkbox = new JCheckBox(text);
       
   148         checkbox.setMinimumSize(labelPrefSize);
       
   149         if(mnemonic != -1)
       
   150             checkbox.setMnemonic(mnemonic);
       
   151         checkbox.setSelected(selected);
       
   152         if(text.length() == 0)
       
   153             checkbox.setPreferredSize(labelPrefSize);
       
   154         if(listener != null)
       
   155             checkbox.addActionListener(listener);
       
   156         return checkbox;
       
   157     }
       
   158 
       
   159     public static JCheckBox createCheckBox(String text, int mnemonic, ActionListener listener)
       
   160     {
       
   161         return createCheckBox(text, mnemonic, listener, false);
       
   162     }
       
   163 
       
   164     public static JCheckBox createCheckBox(String text, int mnemonic, boolean selected)
       
   165     {
       
   166         return createCheckBox(text, mnemonic, null, selected);
       
   167     }
       
   168 
       
   169     public static JCheckBox createCheckBox(String text, int mnemonic)
       
   170     {
       
   171         return createCheckBox(text, mnemonic, null, false);
       
   172     }
       
   173 
       
   174     public static JCheckBox createCheckBox(String text)
       
   175     {
       
   176         return createCheckBox(text, -1, null, false);
       
   177     }
       
   178 
       
   179     public static JComboBox createComboBox(Object items[], ActionListener listener, boolean editable)
       
   180     {
       
   181         JComboBox comboBox = new JComboBox(items);
       
   182         if(listener != null)
       
   183             comboBox.addActionListener(listener);
       
   184         comboBox.setEditable(editable);
       
   185         return comboBox;
       
   186     }
       
   187 
       
   188     public static JComboBox createComboBox(Object items[], boolean editable)
       
   189     {
       
   190         return createComboBox(items, null, editable);
       
   191     }
       
   192 
       
   193     public static JComboBox createComboBox(Vector items, ActionListener listener, boolean editable)
       
   194     {
       
   195         JComboBox comboBox = new JComboBox(items);
       
   196         if(listener != null)
       
   197             comboBox.addActionListener(listener);
       
   198         comboBox.setEditable(editable);
       
   199         return comboBox;
       
   200     }
       
   201 
       
   202     public static JComboBox createComboBox(Vector items, boolean editable)
       
   203     {
       
   204         return createComboBox(items, null, editable);
       
   205     }
       
   206 
       
   207     public static JButton createButton(Action action)
       
   208     {
       
   209         JButton button = new JButton(action);
       
   210         setButtonSize(button, buttonPrefSize);
       
   211         return button;
       
   212     }
       
   213 
       
   214     public static JButton createButton(String text, ActionListener listener, int mnemonic)
       
   215     {
       
   216         JButton button = new JButton(text);
       
   217         if(listener != null)
       
   218             button.addActionListener(listener);
       
   219         if(mnemonic != -1)
       
   220             button.setMnemonic(mnemonic);
       
   221         setButtonSize(button, buttonPrefSize);
       
   222         return button;
       
   223     }
       
   224 
       
   225     private static void setButtonSize(JButton button, Dimension size)
       
   226     {
       
   227         String text = button.getText();
       
   228         button.setMinimumSize(size);
       
   229         if(text.length() == 0)
       
   230         {
       
   231             button.setPreferredSize(size);
       
   232         } else
       
   233         {
       
   234             Dimension psize = button.getPreferredSize();
       
   235             if(psize.width < size.width)
       
   236                 button.setPreferredSize(size);
       
   237         }
       
   238     }
       
   239 
       
   240     public static JButton createButton(String text, ActionListener listener)
       
   241     {
       
   242         return createButton(text, listener, -1);
       
   243     }
       
   244 
       
   245     public static JButton createSmallButton(String text, ActionListener listener, int mnemonic)
       
   246     {
       
   247         JButton button = createButton(text, listener, mnemonic);
       
   248         setButtonSize(button, smbuttonPrefSize);
       
   249         return button;
       
   250     }
       
   251 
       
   252     public static JButton createSmallButton(String text, ActionListener listener)
       
   253     {
       
   254         return createSmallButton(text, listener, -1);
       
   255     }
       
   256 
       
   257     public static Border createBorder(String text)
       
   258     {
       
   259         Border border = BorderFactory.createEtchedBorder();
       
   260         return BorderFactory.createTitledBorder(border, text, 0, 2);
       
   261     }
       
   262 
       
   263     public static Border createBorder()
       
   264     {
       
   265         return BorderFactory.createEmptyBorder(4, 4, 4, 4);
       
   266     }
       
   267 
       
   268     public static JScrollPane createListPane(JList list, String text)
       
   269     {
       
   270         JScrollPane pane = new JScrollPane(list);
       
   271         pane.setBorder(BorderFactory.createCompoundBorder(createBorder(text), BorderFactory.createLoweredBevelBorder()));
       
   272         return pane;
       
   273     }
       
   274 
       
   275     public static void centerComponent(Component source, Component parent)
       
   276     {
       
   277         Dimension dim = source.getSize();
       
   278         Rectangle rect;
       
   279         if(parent != null)
       
   280         {
       
   281             rect = parent.getBounds();
       
   282         } else
       
   283         {
       
   284             Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
       
   285             rect = new Rectangle(0, 0, d.width, d.height);
       
   286         }
       
   287         int x = rect.x + (rect.width - dim.width) / 2;
       
   288         int y = rect.y + (rect.height - dim.height) / 2;
       
   289         source.setLocation(x, y);
       
   290     }
       
   291 
       
   292     public static void centerComponent(Component source)
       
   293     {
       
   294         centerComponent(source, null);
       
   295     }
       
   296 
       
   297     public static JFrame getParentFrame(Component source)
       
   298     {
       
   299         Container parent;
       
   300         for(parent = source.getParent(); parent != null; parent = parent.getParent())
       
   301             if(parent instanceof JFrame)
       
   302                 break;
       
   303 
       
   304         if(parent == null)
       
   305             return null;
       
   306         else
       
   307             return (JFrame)parent;
       
   308     }
       
   309 
       
   310     public static Integer msToSec(Integer ms)
       
   311     {
       
   312         int value = ms.intValue();
       
   313         value /= 1000;
       
   314         return new Integer(value);
       
   315     }
       
   316 
       
   317     public static Integer secToMs(Integer sec)
       
   318     {
       
   319         int value = sec.intValue();
       
   320         value *= 1000;
       
   321         return new Integer(value);
       
   322     }
       
   323 
       
   324     public static String stringFromStringArray(String strings[], String delim)
       
   325     {
       
   326         String string = "";
       
   327         String separator;
       
   328         if(delim == null || delim.equals(""))
       
   329             separator = " ";
       
   330         else
       
   331             separator = delim;
       
   332         for(int i = 0; i < strings.length; i++)
       
   333         {
       
   334             string = string + strings[i];
       
   335             string = string + separator;
       
   336         }
       
   337 
       
   338         return string;
       
   339     }
       
   340 
       
   341     public static String stringFromStringArray(String strings[])
       
   342     {
       
   343         return stringFromStringArray(strings, "");
       
   344     }
       
   345 
       
   346     public static String[] stringArrayFromString(String string, String delim)
       
   347     {
       
   348         StringTokenizer st;
       
   349         if(delim == null || delim.equals(""))
       
   350             st = new StringTokenizer(string);
       
   351         else
       
   352             st = new StringTokenizer(string, delim);
       
   353         int numTokens = st.countTokens();
       
   354         String strings[] = new String[numTokens];
       
   355         int index = 0;
       
   356         while(st.hasMoreTokens())
       
   357             strings[index++] = st.nextToken();
       
   358         return strings;
       
   359     }
       
   360 
       
   361     public static String[] stringArrayFromString(String string)
       
   362     {
       
   363         return stringArrayFromString(string, "");
       
   364     }
       
   365 
       
   366     public static void setWaitCursor(Component comp)
       
   367     {
       
   368         comp.setCursor(Cursor.getPredefinedCursor(3));
       
   369     }
       
   370 
       
   371     public static void setDefaultCursor(Component comp)
       
   372     {
       
   373         comp.setCursor(Cursor.getPredefinedCursor(0));
       
   374     }
       
   375 
       
   376     public static final int BUTTON_WIDTH = 100;
       
   377     public static final int BUTTON_HEIGHT = 26;
       
   378     public static final int BUTTCON_WIDTH = 28;
       
   379     public static final int BUTTCON_HEIGHT = 28;
       
   380     public static final int SM_BUTTON_WIDTH = 72;
       
   381     public static final int SM_BUTTON_HEIGHT = 26;
       
   382     public static final int LABEL_WIDTH = 100;
       
   383     public static final int LABEL_HEIGHT = 20;
       
   384     public static final int TEXT_WIDTH = 150;
       
   385     public static final int TEXT_HEIGHT = 20;
       
   386     public static Dimension buttonPrefSize = new Dimension(100, 26);
       
   387     public static Dimension buttconPrefSize = new Dimension(28, 28);
       
   388     public static Dimension smbuttonPrefSize = new Dimension(72, 26);
       
   389     public static Dimension labelPrefSize = new Dimension(100, 20);
       
   390     public static Dimension textPrefSize = new Dimension(150, 20);
       
   391 
       
   392 }