1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * ident "%Z%%M% %I% %E% SMI" 24*0Sstevel@tonic-gate * 25*0Sstevel@tonic-gate * Copyright (c) 1999-2000 by Sun Microsystems, Inc. 26*0Sstevel@tonic-gate * All rights reserved. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate import java.awt.*; 30*0Sstevel@tonic-gate import java.awt.event.*; 31*0Sstevel@tonic-gate import java.text.NumberFormat; 32*0Sstevel@tonic-gate import java.util.ResourceBundle; 33*0Sstevel@tonic-gate import java.util.MissingResourceException; 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate /** 36*0Sstevel@tonic-gate * This creates a modal dialog box that lets the user enter a duration of 37*0Sstevel@tonic-gate * time in seconds/minutes/hours/days/weeks/months/years. 38*0Sstevel@tonic-gate */ 39*0Sstevel@tonic-gate public class DurationHelper extends Dialog { 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate private boolean save; 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate private Frame parent; 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate private Choice unit; 46*0Sstevel@tonic-gate private TextField value; 47*0Sstevel@tonic-gate private Label total; 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate private Button ok; 50*0Sstevel@tonic-gate private Button cancel; 51*0Sstevel@tonic-gate private Button help; 52*0Sstevel@tonic-gate private Button compute; 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate private HelpDialog hd = null; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate // For I18N 57*0Sstevel@tonic-gate private static ResourceBundle rb = 58*0Sstevel@tonic-gate ResourceBundle.getBundle("GuiResource" /* NOI18N */); 59*0Sstevel@tonic-gate private static ResourceBundle hrb = 60*0Sstevel@tonic-gate ResourceBundle.getBundle("HelpData" /* NOI18N */); 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate private static String[] units = { getString("Seconds"), 63*0Sstevel@tonic-gate getString("Minutes"), 64*0Sstevel@tonic-gate getString("Hours"), 65*0Sstevel@tonic-gate getString("Days"), 66*0Sstevel@tonic-gate getString("Weeks"), 67*0Sstevel@tonic-gate getString("Months"), 68*0Sstevel@tonic-gate getString("Years") }; 69*0Sstevel@tonic-gate private static int[] unitMultipliers = {1, 60, 60*60, 60*60*24, 70*0Sstevel@tonic-gate 60*60*24*7, 60*60*24*30, 71*0Sstevel@tonic-gate 60*60*24*365 }; 72*0Sstevel@tonic-gate private static NumberFormat nf = NumberFormat.getInstance(); 73*0Sstevel@tonic-gate private static Toolkit toolkit = Toolkit.getDefaultToolkit(); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate /** 76*0Sstevel@tonic-gate * Constructor for DurationHelper. 77*0Sstevel@tonic-gate * @param parent the parent Frame to whom input will be blocked 78*0Sstevel@tonic-gate * while this dialog box is begin shown(modal behaviour). 79*0Sstevel@tonic-gate */ DurationHelper(Frame parent, Color background, Color foreground)80*0Sstevel@tonic-gate public DurationHelper(Frame parent, Color background, Color foreground) { 81*0Sstevel@tonic-gate super(parent, getString("SEAM Duration Helper"), true); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate this.parent = parent; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate setLayout(new GridBagLayout()); 86*0Sstevel@tonic-gate addLabels(); 87*0Sstevel@tonic-gate addFields(background, foreground); 88*0Sstevel@tonic-gate addButtons(); 89*0Sstevel@tonic-gate setSize(350, 150); 90*0Sstevel@tonic-gate setResizable(false); 91*0Sstevel@tonic-gate addWindowListener(new DHWindowListener()); 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate /** 95*0Sstevel@tonic-gate * Adds all the labels. 96*0Sstevel@tonic-gate */ addLabels()97*0Sstevel@tonic-gate private void addLabels() { 98*0Sstevel@tonic-gate GridBagConstraints gbc = new GridBagConstraints(); 99*0Sstevel@tonic-gate gbc.weightx = gbc.weighty = 1; 100*0Sstevel@tonic-gate add(new Label(getString("Unit")), gbc); 101*0Sstevel@tonic-gate add(new Label(getString("Value")), gbc); 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate gbc.gridx = 3; 104*0Sstevel@tonic-gate gbc.gridy = 0; 105*0Sstevel@tonic-gate add(new Label(getString("Seconds")), gbc); 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /** 109*0Sstevel@tonic-gate * Initializes the strings for the units. 110*0Sstevel@tonic-gate */ initUnits()111*0Sstevel@tonic-gate private void initUnits() { 112*0Sstevel@tonic-gate unit = new Choice(); 113*0Sstevel@tonic-gate for (int i = 0; i < units.length; i++) 114*0Sstevel@tonic-gate unit.add(units[i]); 115*0Sstevel@tonic-gate unit.select(getString("Hours")); 116*0Sstevel@tonic-gate unit.addItemListener(new ItemListener() { 117*0Sstevel@tonic-gate public void itemStateChanged(ItemEvent e) { 118*0Sstevel@tonic-gate DurationHelper.this.checkErrorAndSetTotal(); 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate }); 121*0Sstevel@tonic-gate } 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate /** 124*0Sstevel@tonic-gate * Adds all the fields 125*0Sstevel@tonic-gate */ addFields(Color background, Color foreground)126*0Sstevel@tonic-gate private void addFields(Color background, Color foreground) { 127*0Sstevel@tonic-gate GridBagConstraints gbc = new GridBagConstraints(); 128*0Sstevel@tonic-gate gbc.weightx = gbc.weighty = 1; 129*0Sstevel@tonic-gate initUnits(); 130*0Sstevel@tonic-gate value = new TextField(); 131*0Sstevel@tonic-gate value.setBackground(background); 132*0Sstevel@tonic-gate value.setForeground(foreground); 133*0Sstevel@tonic-gate value.setColumns(10); 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate // TBD: make total large enough to hold the largest int 136*0Sstevel@tonic-gate total = new Label(" " /* NO18N */, 137*0Sstevel@tonic-gate Label.RIGHT); 138*0Sstevel@tonic-gate gbc.gridx = 0; 139*0Sstevel@tonic-gate gbc.gridy = 1; 140*0Sstevel@tonic-gate add(unit, gbc); 141*0Sstevel@tonic-gate gbc.gridx = 1; 142*0Sstevel@tonic-gate add(value, gbc); 143*0Sstevel@tonic-gate gbc.gridx = 3; 144*0Sstevel@tonic-gate add(total, gbc); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate value.addActionListener(new ActionListener() { 147*0Sstevel@tonic-gate public void actionPerformed(ActionEvent e) { 148*0Sstevel@tonic-gate DurationHelper.this.durationHelperClose(true); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate }); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate /** 154*0Sstevel@tonic-gate * Adds all the buttons. 155*0Sstevel@tonic-gate */ addButtons()156*0Sstevel@tonic-gate private void addButtons() { 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate GridBagConstraints gbc = new GridBagConstraints(); 159*0Sstevel@tonic-gate gbc.weightx = gbc.weighty = 1; 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate gbc.gridwidth = GridBagConstraints.REMAINDER; 162*0Sstevel@tonic-gate gbc.fill = GridBagConstraints.BOTH; 163*0Sstevel@tonic-gate gbc.gridx = 0; 164*0Sstevel@tonic-gate gbc.gridy = 2; 165*0Sstevel@tonic-gate gbc.insets = new Insets(0, 10, 0, 10); 166*0Sstevel@tonic-gate add(new LineSeparator(), gbc); 167*0Sstevel@tonic-gate gbc.insets = new Insets(0, 0, 0, 0); 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate Panel p = new Panel(); 170*0Sstevel@tonic-gate p.setLayout(new GridBagLayout()); 171*0Sstevel@tonic-gate ok = new Button(getString("OK")); 172*0Sstevel@tonic-gate cancel = new Button(getString("Cancel")); 173*0Sstevel@tonic-gate help = new Button(getString("Help")); 174*0Sstevel@tonic-gate gbc = new GridBagConstraints(); 175*0Sstevel@tonic-gate gbc.weightx = gbc.weighty = 1; 176*0Sstevel@tonic-gate p.add(ok, gbc); 177*0Sstevel@tonic-gate p.add(cancel, gbc); 178*0Sstevel@tonic-gate p.add(help, gbc); 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate ActionListener bl = new ButtonListener(); 181*0Sstevel@tonic-gate ok.addActionListener(bl); 182*0Sstevel@tonic-gate cancel.addActionListener(bl); 183*0Sstevel@tonic-gate help.addActionListener(bl); 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate gbc.gridy = 3; 186*0Sstevel@tonic-gate gbc.gridwidth = GridBagConstraints.REMAINDER; 187*0Sstevel@tonic-gate gbc.fill = GridBagConstraints.HORIZONTAL; 188*0Sstevel@tonic-gate add(p, gbc); 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate gbc = new GridBagConstraints(); 191*0Sstevel@tonic-gate gbc.gridx = 2; 192*0Sstevel@tonic-gate gbc.gridy = 1; 193*0Sstevel@tonic-gate compute = new Button(getString("=")); 194*0Sstevel@tonic-gate add(compute, gbc); 195*0Sstevel@tonic-gate compute.addActionListener(bl); 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate /** 200*0Sstevel@tonic-gate * Updates the label called total. 201*0Sstevel@tonic-gate * @return false if the text entry in the value 202*0Sstevel@tonic-gate * field is not parseable, true otherwise. 203*0Sstevel@tonic-gate */ checkErrorAndSetTotal()204*0Sstevel@tonic-gate private boolean checkErrorAndSetTotal() { 205*0Sstevel@tonic-gate try { 206*0Sstevel@tonic-gate String noSpaces = value.getText().trim(); 207*0Sstevel@tonic-gate value.setText(noSpaces); 208*0Sstevel@tonic-gate Long l = Long.valueOf(noSpaces); 209*0Sstevel@tonic-gate total.setText(nf.format(l.longValue() * 210*0Sstevel@tonic-gate unitMultipliers[unit.getSelectedIndex()])); 211*0Sstevel@tonic-gate } catch (NumberFormatException e) { 212*0Sstevel@tonic-gate value.requestFocus(); 213*0Sstevel@tonic-gate value.selectAll(); 214*0Sstevel@tonic-gate toolkit.beep(); 215*0Sstevel@tonic-gate return false; 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate return true; 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /** 222*0Sstevel@tonic-gate * Hides the duration helper. 223*0Sstevel@tonic-gate * @param save true if the user wants to save the current value in 224*0Sstevel@tonic-gate * the dialog box, false if it is to be discarded. This is decided 225*0Sstevel@tonic-gate * based on whether the user clicked on the "Ok" button or the 226*0Sstevel@tonic-gate * "Cancel" button. Choosing the window close menu is equivalent to 227*0Sstevel@tonic-gate * clicking on "Cancel." 228*0Sstevel@tonic-gate */ durationHelperClose(boolean save)229*0Sstevel@tonic-gate private void durationHelperClose(boolean save) { 230*0Sstevel@tonic-gate if (save == true) { 231*0Sstevel@tonic-gate if (!checkErrorAndSetTotal()) 232*0Sstevel@tonic-gate return; 233*0Sstevel@tonic-gate } 234*0Sstevel@tonic-gate this.save = save; 235*0Sstevel@tonic-gate setVisible(false); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate /** 239*0Sstevel@tonic-gate * Determine whether or not the user wanted to save the value in 240*0Sstevel@tonic-gate * this Dialog box. The user indicates this by clicking on the Ok 241*0Sstevel@tonic-gate * button to save it and on the Cancel button to discard it. Using the 242*0Sstevel@tonic-gate * window close menu responds the same way as cancel. 243*0Sstevel@tonic-gate * @return true if the user wanted to use this value, 244*0Sstevel@tonic-gate * false if it is to be discarded. 245*0Sstevel@tonic-gate */ isSaved()246*0Sstevel@tonic-gate public boolean isSaved() { 247*0Sstevel@tonic-gate return save; 248*0Sstevel@tonic-gate } 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate /** 251*0Sstevel@tonic-gate * The string representation of the contents of this dialog box. 252*0Sstevel@tonic-gate * @return a String with the total number of seconds entered. 253*0Sstevel@tonic-gate */ toString()254*0Sstevel@tonic-gate public String toString() { 255*0Sstevel@tonic-gate return total.getText(); 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate // * ********************************************** 259*0Sstevel@tonic-gate // I N N E R C L A S S E S F O L L O W 260*0Sstevel@tonic-gate // * ********************************************** 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate /** 263*0Sstevel@tonic-gate * Listener for closing the dialog box through the window close 264*0Sstevel@tonic-gate * menu. 265*0Sstevel@tonic-gate */ 266*0Sstevel@tonic-gate private class DHWindowListener extends WindowAdapter { windowClosing(WindowEvent e)267*0Sstevel@tonic-gate public void windowClosing(WindowEvent e) { 268*0Sstevel@tonic-gate durationHelperClose(false); 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate /** 273*0Sstevel@tonic-gate * Listener for all the buttons. 274*0Sstevel@tonic-gate * The listener is shared for the sake 275*0Sstevel@tonic-gate * of reducing the number of overall listeners. 276*0Sstevel@tonic-gate */ 277*0Sstevel@tonic-gate private class ButtonListener implements ActionListener { actionPerformed(ActionEvent e)278*0Sstevel@tonic-gate public void actionPerformed(ActionEvent e) { 279*0Sstevel@tonic-gate if (e.getSource() == ok) { 280*0Sstevel@tonic-gate DurationHelper.this.durationHelperClose(true); 281*0Sstevel@tonic-gate } else if (e.getSource() == cancel) { 282*0Sstevel@tonic-gate DurationHelper.this.durationHelperClose(false); 283*0Sstevel@tonic-gate } else if (e.getSource() == help) { 284*0Sstevel@tonic-gate if (hd != null) 285*0Sstevel@tonic-gate hd.show(); 286*0Sstevel@tonic-gate else { 287*0Sstevel@tonic-gate hd = new HelpDialog(DurationHelper. this.parent, 288*0Sstevel@tonic-gate getString("Help for entering time duration"), 289*0Sstevel@tonic-gate false, 5, 45); 290*0Sstevel@tonic-gate hd.setVisible(true); 291*0Sstevel@tonic-gate hd.setText(getString(hrb, "DurationHelperHelp")); 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate } else if (e.getSource() == compute) { 294*0Sstevel@tonic-gate checkErrorAndSetTotal(); 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate } 297*0Sstevel@tonic-gate } 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate /** 300*0Sstevel@tonic-gate * Call rb.getString(), but catch exception 301*0Sstevel@tonic-gate * and return English 302*0Sstevel@tonic-gate * key so that small spelling errors don't cripple the GUI 303*0Sstevel@tonic-gate * 304*0Sstevel@tonic-gate */ getString(String key)305*0Sstevel@tonic-gate private static final String getString(String key) { 306*0Sstevel@tonic-gate return (getString(rb, key)); 307*0Sstevel@tonic-gate } 308*0Sstevel@tonic-gate getString(ResourceBundle rb, String key)309*0Sstevel@tonic-gate private static final String getString(ResourceBundle rb, String key) { 310*0Sstevel@tonic-gate try { 311*0Sstevel@tonic-gate String res = rb.getString(key); 312*0Sstevel@tonic-gate return res; 313*0Sstevel@tonic-gate } catch (MissingResourceException e) { 314*0Sstevel@tonic-gate System.out.println("Missing resource "+key+", using English."); 315*0Sstevel@tonic-gate return key; 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate /* 320*0Sstevel@tonic-gate * A main method to test this class. 321*0Sstevel@tonic-gate */ 322*0Sstevel@tonic-gate /* 323*0Sstevel@tonic-gate public static void main(String args[]) { 324*0Sstevel@tonic-gate Frame f = new Frame("Test DurationHelper"); 325*0Sstevel@tonic-gate f.setVisible(true); // for help dialog to use this as parent 326*0Sstevel@tonic-gate DurationHelper dh = new DurationHelper(f, Color.white, Color.black); 327*0Sstevel@tonic-gate dh.setVisible(true); 328*0Sstevel@tonic-gate System.out.println("Save is " + dh.save); 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate */ 331*0Sstevel@tonic-gate } 332