/*
* PreferencesDialog.java
*
*/
package samples;
import samples.accessory.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;
/**
* PreferencesDialog: Sample wordprocessor preferences dialog.
*/
public class PreferencesDialog extends JDialog {
/**
* Resource bundle with default locale
*/
private ResourceBundle resources = null;
/**
* map unit label abbreviations ("cm") -> unit name ("Centimeters")
*/
private HashMap unitsLabelToName = null;
/**
* names of all available fonts
*/
private String[] fontNames = null;
/**
* Command string for a "browse stationery" action (e.g., a button
* or menu item).
* This string is never presented to the user and should
* not be internationalized.
*/
private String CMD_BROWSE_STATIONERY = "cmd.browse.stationery"/*NOI18N*/;
/**
* Command string for a cancel action (e.g., a button or menu item).
* This string is never presented to the user and should
* not be internationalized.
*/
private String CMD_CANCEL = "cmd.cancel"/*NOI18N*/;
/**
* Command string for a help action (e.g., a button or menu item).
* This string is never presented to the user and should
* not be internationalized.
*/
private String CMD_HELP = "cmd.help"/*NOI18N*/;
/**
* Command string for an "OK" action (e.g., a button or menu item).
* This string is never presented to the user and should
* not be internationalized.
*/
private String CMD_OK = "cmd.ok"/*NOI18N*/;
/**
* Creates new form Preferences
*/
public PreferencesDialog(Frame parent,boolean modal) {
super (parent, modal);
initResources();
initValues();
initComponents();
pack();
}
/**
* Loads locale-specific resources: strings, images, et cetera
*/
private void initResources() {
Locale locale = Locale.getDefault();
resources = ResourceBundle.getBundle(
"samples.resources.bundles.PreferencesDialogResources", locale);
}
/**
* This method is called from within the constructor to
* initialize values needed by components.
*/
private void initValues() {
unitsLabelToName = new HashMap();
unitsLabelToName.put("cm","Centimeters");
unitsLabelToName.put("\"","Inches");
fontNames = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
} // initValues()
/**
*
* Called from the constructor to initialize this window
*
*
* We use dynamic layout managers, so that layout is dynamic and will
* adapt properly to user-customized fonts and localized text. The
* GridBagLayout makes it easy to line up components of varying
* sizes along invisible vertical and horizontal grid lines. It
* is important to sketch the layout of the interface and decide
* on the grid before writing the layout code.
*
*
* Here we actually use
* our own subclass of GridBagLayout called StringGridBagLayout,
* which allows us to use strings to specify constraints, rather
* than having to create GridBagConstraints objects manually.
*
*
* We use the JLabel.setLabelFor() method to connect
* labels to what they are labeling. This allows mnemonics to work
* and assistive to technologies used by persons with disabilities
* to provide much more useful information to the user.
*
*/
private void initComponents() {
// initialize the window
setTitle(resources.getString("window.title"));
Container contents = getContentPane();
StringGridBagLayout grid = new StringGridBagLayout();
// If all weights are 0, gridbag centers the components
grid.setDefaultConstraints("weightx=0,weighty=0");
contents.setLayout(grid);
addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent event) {
// user hit "close" button at top of window
windowAction(CMD_CANCEL);
}
});
Date currentDate = new Date(System.currentTimeMillis());
// accessibility - all frames, dialogs, and applets should
// have a description
getAccessibleContext().setAccessibleDescription(
resources.getString("window.description"));
JComboBox unitsComboBox = new JComboBox(); // needed below
// label for units combobox
JLabel unitsLabel = new JLabel();
unitsLabel.setDisplayedMnemonic(
resources.getString("unitsComboBox.mnemonic").charAt(0));
// setLabelFor() allows the label's mnemonic to work for the component
unitsLabel.setLabelFor(unitsComboBox);
unitsLabel.setText(resources.getString("unitsComboBox.label"));
contents.add(
"gridx=0,gridy=0,anchor=WEST,insets=[12,12,0,0]", unitsLabel);
// init combobox by Hashtable object values
Collection unitNames = unitsLabelToName.values();
Iterator iterator = unitNames.iterator();
while (iterator.hasNext()) {
unitsComboBox.addItem((String)iterator.next());
}
unitsComboBox.setToolTipText(
resources.getString("unitsComboBox.tooltip"));
contents.add(
"gridx=1,gridy=0,gridwidth=2,anchor=WEST,insets=[12,8,0,0]",
unitsComboBox);
JTextField leftMarginTextField = new JTextField(); // needed soon
// left margin label
JLabel leftMarginLabel = new JLabel();
leftMarginLabel.setDisplayedMnemonic(
resources.getString("leftMarginTextField.mnemonic").charAt(0));
// setLabelFor() allows the label's mnemonic to work for the component
leftMarginLabel.setLabelFor(leftMarginTextField);
leftMarginLabel.setText(
resources.getString("leftMarginTextField.label"));
contents.add(
"gridx=0,gridy=1,anchor=WEST,insets=[11,12,0,0]", leftMarginLabel);
// left margin text
leftMarginTextField.setToolTipText(
resources.getString("leftMarginTextField.tooltip"));
leftMarginTextField.setText(new Float(2.5f).toString());
contents.add(
"gridx=1,gridy=1,ipadx=27,anchor=WEST,insets=[11,8,0,0]",
leftMarginTextField);
JTextField rightMarginTextField = new JTextField(); // needed soon
// right margin label
JLabel rightMarginLabel = new JLabel();
rightMarginLabel.setDisplayedMnemonic(
resources.getString("rightMarginTextField.mnemonic").charAt(0));
// setLabelFor() allows the label's mnemonic to work for the component
rightMarginLabel.setLabelFor(rightMarginTextField);
rightMarginLabel.setText(
resources.getString("rightMarginTextField.label"));
contents.add(
"gridx=2,gridy=1,gridwidth=4,anchor=EAST,insets=[11,10,0,0]",
rightMarginLabel);
// right margin text
rightMarginTextField.setToolTipText(
resources.getString("rightMarginTextField.tooltip"));
rightMarginTextField.setText(new Float(2.5f).toString());
contents.add(
"gridx=6,gridy=1,ipadx=27,anchor=EAST,insets=[11,7,0,11]",
rightMarginTextField);
// "show" label (hidden text, margins)
JLabel showLabel = new JLabel();
showLabel.setText(resources.getString("show.label"));
contents.add(
"gridx=0,gridy=2,anchor=WEST,insets=[11,12,0,0]",
showLabel);
// show hidden text checkbox
JCheckBox hiddenTextCheckBox = new JCheckBox();
hiddenTextCheckBox.setMnemonic(
resources.getString("hiddenTextCheckBox.mnemonic").charAt(0));
hiddenTextCheckBox.setToolTipText(
resources.getString("hiddenTextCheckBox.tooltip"));
hiddenTextCheckBox.setText(
resources.getString("hiddenTextCheckBox.label"));
hiddenTextCheckBox.setMargin(new Insets(0, 2, 0, 2));
contents.add(
"gridx=1,gridy=2,gridwidth=6,anchor=WEST,insets=[11,7,0,0]",
hiddenTextCheckBox);
// show margins check box
JCheckBox marginsCheckBox = new JCheckBox();
marginsCheckBox.setMnemonic(
resources.getString("marginsCheckBox.mnemonic").charAt(0));
marginsCheckBox.setToolTipText(
resources.getString("marginsCheckBox.tooltip"));
marginsCheckBox.setText(
resources.getString("marginsCheckBox.label"));
marginsCheckBox.setMargin(new Insets(0, 2, 0, 2));
contents.add(
"gridx=1,gridy=3,gridwidth=6,anchor=WEST,insets=[0,7,0,0]",
marginsCheckBox);
JComboBox fontComboBox = new JComboBox(); // needed below
// font label
JLabel fontLabel = new JLabel();
fontLabel.setDisplayedMnemonic(
resources.getString("fontComboBox.mnemonic").charAt(0));
// setLabelFor() allows the label's mnemonic to work for the component
fontLabel.setLabelFor(fontComboBox);
fontLabel.setText(resources.getString("fontComboBox.label"));
contents.add(
"gridx=0,gridy=4,anchor=WEST,insets=[11,12,0,0]",
fontLabel);
// font combo box
for (int i = 0; i < fontNames.length; i++) {
if (fontNames[i] != null) {
fontComboBox.addItem(fontNames[i]);
}
}
fontComboBox.setToolTipText(
resources.getString("fontComboBox.tooltip"));
contents.add(
"gridx=1,gridy=4,gridwidth=2,anchor=WEST,insets=[11,7,0,0]",
fontComboBox);
JComboBox sizeComboBox = new JComboBox(); // used below
// size label
JLabel sizeLabel = new JLabel();
sizeLabel.setDisplayedMnemonic(
resources.getString("sizeComboBox.mnemonic").charAt(0));
sizeLabel.setText(resources.getString("sizeComboBox.label"));
// setLabelFor() allows the label's mnemonic to work for the component
sizeLabel.setLabelFor(sizeComboBox);
contents.add(
"gridx=3,gridy=4,gridwidth=3,anchor=EAST,insets=[11,11,0,0]",
sizeLabel);
// size combo box
sizeComboBox.setToolTipText(
resources.getString("sizeComboBox.tooltip"));
sizeComboBox.setEditable(true);
sizeComboBox.setEditor(new IntegerComboBoxEditor(3));
sizeComboBox.setRenderer(
new AlignListCellRenderer(
SwingConstants.RIGHT, SwingConstants.CENTER));
for (int i=0; i<35; i++) {
sizeComboBox.addItem(new Integer(i));
}
sizeComboBox.setSelectedIndex(0);
contents.add(
"gridx=6,gridy=4,anchor=EAST,insets=[11,7,0,11]",
sizeComboBox);
JRadioButton dateFormat1RadioButton = new JRadioButton();//needed below
// date format label
JLabel dateFormatLabel = new JLabel();
dateFormatLabel.setDisplayedMnemonic(
resources.getString("dateFormat.mnemonic").charAt(0));
dateFormatLabel.setText(resources.getString("dateFormat.label"));
// setLabelFor() allows the label's mnemonic to work for the component
dateFormatLabel.setLabelFor(dateFormat1RadioButton);
contents.add(
"gridx=0,gridy=5,anchor=WEST,insets=[11,12,0,0]",
dateFormatLabel);
// dat format 1
dateFormat1RadioButton.setToolTipText(
resources.getString("dateFormat1RadioButton.tooltip"));
dateFormat1RadioButton.setSelected(true);
dateFormat1RadioButton.setMargin(new Insets(0, 2, 0, 2));
dateFormat1RadioButton.setText(
DateFormat.getDateInstance(DateFormat.SHORT).format(currentDate));
contents.add(
"gridx=1,gridy=5,gridwidth=5,anchor=WEST,insets=[11,7,0,0]",
dateFormat1RadioButton);
// date format 2
JRadioButton dateFormat2RadioButton = new JRadioButton();
dateFormat2RadioButton.setToolTipText(
resources.getString("dateFormat2RadioButton.tooltip"));
dateFormat2RadioButton.setMargin(new Insets(0, 2, 0, 2));
dateFormat2RadioButton.setText(
DateFormat.getDateInstance(DateFormat.MEDIUM).format(currentDate));
contents.add(
"gridx=1,gridy=6,gridwidth=5,anchor=WEST,insets=[0,7,0,0]",
dateFormat2RadioButton);
// date format 3
JRadioButton dateFormat3RadioButton = new JRadioButton();
dateFormat3RadioButton.setToolTipText(
resources.getString("dateFormat3RadioButton.tooltip"));
dateFormat3RadioButton.setMargin(new Insets(0, 2, 0, 2));
dateFormat3RadioButton.setText(
DateFormat.getDateInstance(DateFormat.LONG).format(currentDate));
contents.add(
"gridx=1,gridy=7,gridwidth=5,anchor=WEST,insets=[0,7,0,0]",
dateFormat3RadioButton);
// group radio buttons
ButtonGroup dateFormatRadioGroup = new ButtonGroup();
dateFormatRadioGroup.add(dateFormat1RadioButton);
dateFormatRadioGroup.add(dateFormat2RadioButton);
dateFormatRadioGroup.add(dateFormat3RadioButton);
JTextField stationeryTextField = new JTextField(); //needed below
// stationery label
JLabel stationeryLabel = new JLabel();
stationeryLabel.setText(
resources.getString("stationeryTextField.label"));
stationeryLabel.setDisplayedMnemonic(
resources.getString("stationeryTextField.mnemonic").charAt(0));
stationeryLabel.setLabelFor(stationeryTextField);
contents.add(
"gridx=0,gridy=8,anchor=WEST,insets=[11,12,0,0]",
stationeryLabel);
// stationery text field
stationeryTextField.setToolTipText(
resources.getString("stationeryTextField.tooltip"));
stationeryTextField.setEditable(false);
contents.add(
"gridx=1,gridy=8,gridwidth=3,fill=HORIZONTAL,weightx=1.0"
+",insets=[11,7,0,0]",
stationeryTextField);
// browse button
JButton browseButton = new JButton();
browseButton.setMnemonic(
resources.getString("browseButton.mnemonic").charAt(0));
browseButton.setToolTipText(
resources.getString("browseButton.tooltip"));
browseButton.setText(resources.getString("browseButton.label"));
browseButton.setActionCommand(CMD_BROWSE_STATIONERY);
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
windowAction(event);
}
});
contents.add(
"gridx=4,gridy=8,gridwidth=3,anchor=EAST,insets=[11,11,0,11]",
browseButton);
// Button panel at the bottom of the window
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, 0));
// ok button
JButton okButton = new JButton();
okButton.setText(resources.getString("okButton.label"));
okButton.setActionCommand(CMD_OK);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
windowAction(event);
}
});
buttonPanel.add(okButton);
// space
buttonPanel.add(Box.createRigidArea(new Dimension(5,0)));
// cancel button
JButton cancelButton = new JButton();
cancelButton.setText(resources.getString("cancelButton.label"));
cancelButton.setActionCommand(CMD_CANCEL);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
windowAction(event);
}
});
buttonPanel.add(cancelButton);
// space
buttonPanel.add(Box.createRigidArea(new Dimension(5,0)));
// help button
JButton helpButton = new JButton();
helpButton.setText(resources.getString("helpButton.label"));
helpButton.setMnemonic(
resources.getString("helpButton.mnemonic").charAt(0));
helpButton.setActionCommand(CMD_HELP);
helpButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
windowAction(event);
}
});
buttonPanel.add(helpButton);
contents.add(
"gridx=0,gridy=9,gridwidth=7,insets=[17,12,11,11],anchor=EAST",
buttonPanel);
// Final touches on the buttons...
getRootPane().setDefaultButton(okButton);
Vector buttons = new Vector();
buttons.add(okButton);
buttons.add(helpButton);
buttons.add(cancelButton);
equalizeComponentSizes(buttons);
buttons.removeAllElements(); // simplify gc
} // initComponents()
/**
*
* Sets the JComponents in the list to be the same size.
* This is done dynamically by setting each button's
* preferred and maximum sizes after the components have
* been created. This way, the layout automatically adjusts
* to the locale-specific strings and customized fonts.
*
*
* The sizes of the JComponents are NOT modified here. That is
* done later by the layout manager.
*
* @param components must contain only instances of JComponent
*/
private void equalizeComponentSizes(java.util.List components) {
// Get the largest width and height
int i = 0;
Dimension maxPreferred = new Dimension(0,0);
JComponent oneComponent = null;
Dimension thisPreferred = null;
for (i = 0; i < components.size(); ++i) {
oneComponent = (JComponent)components.get(i);
thisPreferred = oneComponent.getPreferredSize();
maxPreferred.width =
Math.max(maxPreferred.width, (int)thisPreferred.getWidth());
maxPreferred.height =
Math.max(maxPreferred.height, (int)thisPreferred.getHeight());
}
// reset preferred and maximum size since BoxLayout takes both
// into account
for (i = 0; i < components.size(); ++i) {
oneComponent = (JComponent)components.get(i);
oneComponent.setPreferredSize((Dimension)maxPreferred.clone());
oneComponent.setMaximumSize((Dimension)maxPreferred.clone());
}
} // equalizeComponentSizes()
/**
* The user has selected an option.
* If actionCommand is an ActionEvent, getCommandString() is called,
* otherwise toString() is used to get the action command.
*
* @param actionCommand may be null
*/
private void windowAction(Object actionCommand) {
boolean closeWindow = false;
String cmd = null;
if (actionCommand != null) {
if (actionCommand instanceof ActionEvent) {
cmd = ((ActionEvent)actionCommand).getActionCommand();
} else {
cmd = actionCommand.toString();
}
}
if (cmd == null) {
// do nothing
} else if (cmd.equals(CMD_CANCEL)) {
System.out.println("your 'cancel' code here...");
closeWindow = true;
} else if (cmd.equals(CMD_HELP)) {
System.out.println("your 'help' code here...");
} else if (cmd.equals(CMD_OK)) {
System.out.println("your 'OK' code here...");
closeWindow = true;
} else {
System.out.println("Command invoked: " + cmd);
}
if (closeWindow) {
setVisible(false);
dispose();
}
} // windowAction()
/**
* This main() is provided for debugging purposes, to display a
* sample dialog.
*/
public static void main(String args[]) {
JFrame frame = new JFrame() {
public Dimension getPreferredSize() {
return new Dimension(200,100);
}
};
frame.setTitle("Debugging frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(false);
JDialog dialog = new PreferencesDialog(frame, true);
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
public void windowClosed(WindowEvent event) {
System.exit(0);
}
});
dialog.pack();
dialog.setVisible(true);
} // main()
} // class PreferencesDialog