001: /*
002: *****************************************************************************
003: * Copyright (C) 2000-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *****************************************************************************
006: */
007: package com.ibm.rbm.gui;
008:
009: import java.awt.*;
010: import java.awt.event.*;
011: import java.io.IOException;
012: import java.util.Locale;
013:
014: import javax.swing.*;
015:
016: import com.ibm.rbm.*;
017:
018: /**
019: * Dialog to display to a user about their preferences.
020: */
021: class PreferencesDialog extends JDialog {
022: String userName;
023: Locale locale;
024: LookAndFeel laf;
025: RBManagerGUI gui;
026:
027: // ** COMPONENTS **
028: JTextField nameField;
029: JRadioButton machineRadio;
030: JRadioButton definedRadio;
031: JRadioButton isoRadio;
032: JComboBox machineCombo;
033: JComboBox definedCombo;
034: JComboBox isoLangCombo;
035: JComboBox isoCounCombo;
036: JComboBox lafCombo;
037: JButton okButton;
038: JButton cancelButton;
039:
040: public PreferencesDialog(RBManagerGUI gui) {
041: super (gui,
042: Resources.getTranslation("dialog_title_preferences"),
043: true);
044: this .gui = gui;
045: userName = gui.getUser();
046: locale = Resources.getLocale();
047: laf = UIManager.getLookAndFeel();
048:
049: initComponents();
050: enableEvents(AWTEvent.KEY_EVENT_MASK);
051: }
052:
053: protected void processKeyEvent(KeyEvent ev) {
054: if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
055: updatePreferences();
056: } else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
057: this WindowClosing();
058: }
059: }
060:
061: private void initComponents() {
062: UIManager.LookAndFeelInfo lafi[] = UIManager
063: .getInstalledLookAndFeels();
064: String lafn[] = new String[lafi.length];
065: for (int i = 0; i < lafi.length; i++) {
066: lafn[i] = lafi[i].getName();
067: }
068:
069: // COMPONENTS
070:
071: JPanel panel1 = new JPanel();
072: JPanel panel2 = new JPanel();
073: JPanel panel3 = new JPanel();
074: JPanel panel4 = new JPanel();
075: Box mainBox = new Box(BoxLayout.Y_AXIS);
076: Box localeBox1 = new Box(BoxLayout.Y_AXIS);
077: Box localeBox2 = new Box(BoxLayout.Y_AXIS);
078: JPanel localePanel = new JPanel();
079:
080: Dimension localeDim1 = new Dimension(200, 25);
081: Dimension localeDim2 = new Dimension(150, 25);
082: Dimension localeDim3 = new Dimension(50, 25);
083:
084: JLabel nameLabel = new JLabel(Resources
085: .getTranslation("dialog_preferences_username"));
086: JLabel lafLabel = new JLabel(Resources
087: .getTranslation("dialog_preferences_lookandfeel"));
088: JLabel warnLabel = new JLabel(Resources
089: .getTranslation("dialog_preferences_locale_warning"));
090: JLabel underscoreLabel = new JLabel("_");
091:
092: nameField = new JTextField(userName);
093: machineRadio = new JRadioButton(Resources
094: .getTranslation("dialog_preferences_locale_machine"),
095: false);
096: definedRadio = new JRadioButton(Resources
097: .getTranslation("dialog_preferences_locale_defined"),
098: true);
099: isoRadio = new JRadioButton(Resources
100: .getTranslation("dialog_preferences_locale_iso"), false);
101: machineCombo = new JComboBox(Locale.getAvailableLocales());
102: definedCombo = new JComboBox(Resources.getAvailableLocales());
103: isoLangCombo = new JComboBox(Locale.getISOLanguages());
104: isoCounCombo = new JComboBox(Locale.getISOCountries());
105: lafCombo = new JComboBox(lafn);
106: okButton = new JButton(Resources
107: .getTranslation("button_update"));
108: cancelButton = new JButton(Resources
109: .getTranslation("button_cancel"));
110:
111: machineRadio.setPreferredSize(localeDim1);
112: definedRadio.setPreferredSize(localeDim1);
113: isoRadio.setPreferredSize(localeDim1);
114:
115: nameLabel.setPreferredSize(localeDim1);
116: lafLabel.setPreferredSize(localeDim1);
117:
118: //localePanel.setPreferredSize(localeDim2);
119: machineCombo.setPreferredSize(localeDim2);
120: definedCombo.setPreferredSize(localeDim2);
121:
122: nameField.setPreferredSize(localeDim2);
123: lafCombo.setPreferredSize(localeDim2);
124:
125: isoLangCombo.setPreferredSize(localeDim3);
126: isoCounCombo.setPreferredSize(localeDim3);
127:
128: // Select the appropriate entries in the combo boxes
129: String lafname = UIManager.getLookAndFeel().getName();
130: for (int i = 0; i < lafCombo.getItemCount(); i++) {
131: if (lafCombo.getItemAt(i).toString().equals(lafname)) {
132: lafCombo.setSelectedIndex(i);
133: break;
134: }
135: }
136: String locname = Resources.getLocale().toString();
137: String loclang = Resources.getLocale().getLanguage();
138: String loccoun = Resources.getLocale().getCountry();
139: for (int i = 0; i < machineCombo.getItemCount(); i++) {
140: if (machineCombo.getItemAt(i).toString().equalsIgnoreCase(
141: locname)) {
142: machineCombo.setSelectedIndex(i);
143: break;
144: }
145: }
146: for (int i = 0; i < definedCombo.getItemCount(); i++) {
147: if (definedCombo.getItemAt(i).toString().equalsIgnoreCase(
148: locname)) {
149: definedCombo.setSelectedIndex(i);
150: break;
151: }
152: }
153: for (int i = 0; i < isoLangCombo.getItemCount(); i++) {
154: if (isoLangCombo.getItemAt(i).toString().equalsIgnoreCase(
155: loclang)) {
156: isoLangCombo.setSelectedIndex(i);
157: break;
158: }
159: }
160: for (int i = 0; i < isoCounCombo.getItemCount(); i++) {
161: if (isoCounCombo.getItemAt(i).toString().equalsIgnoreCase(
162: loccoun)) {
163: isoCounCombo.setSelectedIndex(i);
164: break;
165: }
166: }
167:
168: // Set the radio button group
169: ButtonGroup group = new ButtonGroup();
170: group.add(machineRadio);
171: group.add(definedRadio);
172: group.add(isoRadio);
173:
174: nameField.setColumns(15);
175:
176: // Add action listeners
177: cancelButton.addActionListener(new ActionListener() {
178: public void actionPerformed(ActionEvent ev) {
179: this WindowClosing();
180: }
181: });
182:
183: okButton.addActionListener(new ActionListener() {
184: public void actionPerformed(ActionEvent ev) {
185: updatePreferences();
186: }
187: });
188: getRootPane().setDefaultButton(okButton);
189:
190: panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory
191: .createEtchedBorder(), Resources
192: .getTranslation("dialog_preferences_locale")));
193: panel3.setLayout(new BorderLayout());
194:
195: localePanel.add(isoLangCombo);
196: localePanel.add(underscoreLabel);
197: localePanel.add(isoCounCombo);
198:
199: localeBox1.add(machineRadio);
200: localeBox1.add(definedRadio);
201: localeBox1.add(isoRadio);
202: localeBox2.add(machineCombo);
203: localeBox2.add(definedCombo);
204: localeBox2.add(localePanel);
205: localeBox1.add(Box.createVerticalStrut(5));
206: localeBox2.add(Box.createVerticalStrut(5));
207:
208: panel1.add(nameLabel);
209: panel1.add(nameField);
210: panel2.add(lafLabel);
211: panel2.add(lafCombo);
212: panel3.add(localeBox1, BorderLayout.WEST);
213: panel3.add(localeBox2, BorderLayout.EAST);
214: panel3.add(warnLabel, BorderLayout.SOUTH);
215: panel4.add(okButton);
216: panel4.add(cancelButton);
217:
218: mainBox.add(panel1);
219: mainBox.add(panel2);
220: mainBox.add(panel3);
221: mainBox.add(panel4);
222:
223: getContentPane().add(mainBox);
224: //validate();
225: pack();
226: setVisible(true);
227: }
228:
229: private void this WindowClosing() {
230: setVisible(false);
231: dispose();
232: }
233:
234: void updatePreferences() {
235: // Set the user name
236: gui.setUser(nameField.getText().trim());
237: // Set the look and feel
238: try {
239: UIManager.LookAndFeelInfo lafi[] = UIManager
240: .getInstalledLookAndFeels();
241: for (int i = 0; i < lafi.length; i++) {
242: if (lafi[i].getName().equals(
243: lafCombo.getSelectedItem().toString())) {
244: UIManager.setLookAndFeel(lafi[i].getClassName());
245: gui.updateUI();
246: break;
247: }
248: }
249: } catch (Exception e) {
250: System.err.println("Could not change the look and feel");
251: e.printStackTrace(System.err);
252: }
253: // Set the locale
254: String language = null;
255: String country = null;
256: String variant = null;
257: if (definedRadio.isSelected()) {
258: String encoding = "";
259: if (definedCombo.getSelectedItem() != null) {
260: encoding = definedCombo.getSelectedItem().toString();
261: }
262: language = Resources.getLanguage(encoding);
263: country = Resources.getCountry(encoding);
264: variant = Resources.getVariant(encoding);
265: RBManagerGUI.debugMsg("Before: " + language + "_" + country
266: + "_" + variant);
267: if (country == null)
268: country = new String();
269: if (variant == null)
270: locale = new Locale(language, country);
271: else
272: locale = new Locale(language, country, variant);
273: RBManagerGUI.debugMsg("After: " + locale.toString());
274: } else if (machineRadio.isSelected()) {
275: String encoding = machineCombo.getSelectedItem().toString();
276: language = Resources.getLanguage(encoding);
277: country = Resources.getCountry(encoding);
278: variant = Resources.getVariant(encoding);
279: if (country == null)
280: country = new String();
281: if (variant == null)
282: locale = new Locale(language, country);
283: else
284: locale = new Locale(language, country, variant);
285: } else if (isoRadio.isSelected()) {
286: language = isoLangCombo.getSelectedItem().toString();
287: country = isoCounCombo.getSelectedItem().toString();
288: if (variant == null)
289: locale = new Locale(language, country);
290: else
291: locale = new Locale(language, country, variant);
292: }
293: Resources.setLocale(locale);
294: gui.updateLocale(locale);
295:
296: // Write the preferences
297: Preferences.setPreference("username", gui.getUser());
298: Preferences.setPreference("lookandfeel", UIManager
299: .getLookAndFeel().getClass().getName());
300: Preferences.setPreference("locale", locale.toString());
301: try {
302: Preferences.savePreferences();
303: } catch (IOException ioe) {
304: JOptionPane.showMessageDialog(this , Resources
305: .getTranslation("error_preferences_save"),
306: Resources.getTranslation("error"),
307: JOptionPane.ERROR_MESSAGE);
308: ioe.printStackTrace(System.err);
309: }
310:
311: // Close the window
312: thisWindowClosing();
313: }
314: }
|