DurationHelper.java revision 4d0eb50e691de4c20b1dd9976ad6839fede8a42d
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1999-2000 by Sun Microsystems, Inc.
* All rights reserved.
*/
/**
* This creates a modal dialog box that lets the user enter a duration of
*/
public class DurationHelper extends Dialog {
private boolean save;
// For I18N
private static ResourceBundle rb =
private static ResourceBundle hrb =
getString("Minutes"),
getString("Hours"),
getString("Days"),
getString("Weeks"),
getString("Months"),
getString("Years") };
60*60*24*7, 60*60*24*30,
60*60*24*365 };
/**
* Constructor for DurationHelper.
* @param parent the parent Frame to whom input will be blocked
* while this dialog box is begin shown(modal behaviour).
*/
setLayout(new GridBagLayout());
addLabels();
addButtons();
setResizable(false);
addWindowListener(new DHWindowListener());
}
/**
* Adds all the labels.
*/
private void addLabels() {
}
/**
* Initializes the strings for the units.
*/
private void initUnits() {
public void itemStateChanged(ItemEvent e) {
DurationHelper.this.checkErrorAndSetTotal();
}
});
}
/**
* Adds all the fields
*/
initUnits();
// TBD: make total large enough to hold the largest int
public void actionPerformed(ActionEvent e) {
DurationHelper.this.durationHelperClose(true);
}
});
}
/**
* Adds all the buttons.
*/
private void addButtons() {
p.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
gbc = new GridBagConstraints();
}
/**
* Updates the label called total.
* @return false if the text entry in the value
* field is not parseable, true otherwise.
*/
private boolean checkErrorAndSetTotal() {
try {
} catch (NumberFormatException e) {
return false;
}
return true;
}
/**
* Hides the duration helper.
* @param save true if the user wants to save the current value in
* the dialog box, false if it is to be discarded. This is decided
* based on whether the user clicked on the "Ok" button or the
* "Cancel" button. Choosing the window close menu is equivalent to
* clicking on "Cancel."
*/
private void durationHelperClose(boolean save) {
if (save == true) {
if (!checkErrorAndSetTotal())
return;
}
setVisible(false);
}
/**
* Determine whether or not the user wanted to save the value in
* this Dialog box. The user indicates this by clicking on the Ok
* button to save it and on the Cancel button to discard it. Using the
* window close menu responds the same way as cancel.
* @return true if the user wanted to use this value,
* false if it is to be discarded.
*/
public boolean isSaved() {
return save;
}
/**
* The string representation of the contents of this dialog box.
* @return a String with the total number of seconds entered.
*/
}
// * **********************************************
// I N N E R C L A S S E S F O L L O W
// * **********************************************
/**
* Listener for closing the dialog box through the window close
* menu.
*/
private class DHWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
durationHelperClose(false);
}
}
/**
* Listener for all the buttons.
* The listener is shared for the sake
* of reducing the number of overall listeners.
*/
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
DurationHelper.this.durationHelperClose(true);
DurationHelper.this.durationHelperClose(false);
hd.setVisible(true);
else {
getString("Help for entering time duration"),
false, 5, 45);
hd.setVisible(true);
}
}
}
}
/**
* Call rb.getString(), but catch exception
* and return English
* key so that small spelling errors don't cripple the GUI
*
*/
}
try {
return res;
} catch (MissingResourceException e) {
return key;
}
}
/*
* A main method to test this class.
*/
/* BEGIN JSTYLED */
/*
public static void main(String args[]) {
Frame f = new Frame("Test DurationHelper");
f.setVisible(true); // for help dialog to use this as parent
DurationHelper dh = new DurationHelper(f, Color.white, Color.black);
dh.setVisible(true);
System.out.println("Save is " + dh.save);
}
*/
/* END JSTYLED */
}