• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *****************************************************************************
3  * Copyright (C) 2000-2004, International Business Machines Corporation and  *
4  * others. All Rights Reserved.                                              *
5  *****************************************************************************
6  */
7 package com.ibm.rbm.gui;
8 
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.io.IOException;
12 import java.util.Locale;
13 
14 import javax.swing.*;
15 
16 import com.ibm.rbm.*;
17 
18 /**
19  * Dialog to display to a user about their preferences.
20  */
21 class PreferencesDialog extends JDialog {
22 	String userName;
23 	Locale locale;
24 	LookAndFeel laf;
25 	RBManagerGUI gui;
26 
27 	// ** COMPONENTS **
28 	JTextField   nameField;
29 	JRadioButton machineRadio;
30 	JRadioButton definedRadio;
31 	JRadioButton isoRadio;
32 	JComboBox    machineCombo;
33 	JComboBox    definedCombo;
34 	JComboBox    isoLangCombo;
35 	JComboBox    isoCounCombo;
36 	JComboBox    lafCombo;
37 	JButton      okButton;
38 	JButton      cancelButton;
39 
PreferencesDialog(RBManagerGUI gui)40 	public PreferencesDialog(RBManagerGUI gui) {
41 		super(gui, Resources.getTranslation("dialog_title_preferences"), true);
42 		this.gui = gui;
43 		userName = gui.getUser();
44 		locale = Resources.getLocale();
45 		laf = UIManager.getLookAndFeel();
46 
47 		initComponents();
48 		enableEvents(AWTEvent.KEY_EVENT_MASK);
49 	}
50 
processKeyEvent(KeyEvent ev)51 	protected void processKeyEvent(KeyEvent ev) {
52 		if (ev.getKeyCode() == KeyEvent.VK_ENTER) {
53 			updatePreferences();
54 		} else if (ev.getKeyCode() == KeyEvent.VK_CANCEL) {
55 			thisWindowClosing();
56 		}
57 	}
58 
initComponents()59 	private void initComponents() {
60 		UIManager.LookAndFeelInfo lafi[] = UIManager.getInstalledLookAndFeels();
61 		String lafn[] = new String[lafi.length];
62 		for (int i=0; i < lafi.length; i++) {
63 			lafn[i] = lafi[i].getName();
64 		}
65 
66 		// COMPONENTS
67 
68 		JPanel panel1 = new JPanel();
69 		JPanel panel2 = new JPanel();
70 		JPanel panel3 = new JPanel();
71 		JPanel panel4 = new JPanel();
72 		Box    mainBox = new Box(BoxLayout.Y_AXIS);
73 		Box    localeBox1 = new Box(BoxLayout.Y_AXIS);
74 		Box    localeBox2 = new Box(BoxLayout.Y_AXIS);
75 		JPanel localePanel = new JPanel();
76 
77 		Dimension localeDim1 = new Dimension(200,25);
78 		Dimension localeDim2 = new Dimension(150,25);
79 		Dimension localeDim3 = new Dimension(50,25);
80 
81 		JLabel nameLabel = new JLabel(Resources.getTranslation("dialog_preferences_username"));
82 		JLabel lafLabel = new JLabel(Resources.getTranslation("dialog_preferences_lookandfeel"));
83 		JLabel warnLabel = new JLabel(Resources.getTranslation("dialog_preferences_locale_warning"));
84 		JLabel underscoreLabel = new JLabel("_");
85 
86 		nameField = new JTextField(userName);
87 		machineRadio = new JRadioButton(Resources.getTranslation("dialog_preferences_locale_machine"), false);
88 		definedRadio = new JRadioButton(Resources.getTranslation("dialog_preferences_locale_defined"), true);
89 		isoRadio = new JRadioButton(Resources.getTranslation("dialog_preferences_locale_iso"), false);
90 		machineCombo = new JComboBox(Locale.getAvailableLocales());
91 		definedCombo = new JComboBox(Resources.getAvailableLocales());
92 		isoLangCombo = new JComboBox(Locale.getISOLanguages());
93 		isoCounCombo = new JComboBox(Locale.getISOCountries());
94 		lafCombo = new JComboBox(lafn);
95 		okButton = new JButton(Resources.getTranslation("button_update"));
96 		cancelButton = new JButton(Resources.getTranslation("button_cancel"));
97 
98 		machineRadio.setPreferredSize(localeDim1);
99 		definedRadio.setPreferredSize(localeDim1);
100 		isoRadio.setPreferredSize(localeDim1);
101 
102 		nameLabel.setPreferredSize(localeDim1);
103 		lafLabel.setPreferredSize(localeDim1);
104 
105 		//localePanel.setPreferredSize(localeDim2);
106 		machineCombo.setPreferredSize(localeDim2);
107 		definedCombo.setPreferredSize(localeDim2);
108 
109 		nameField.setPreferredSize(localeDim2);
110 		lafCombo.setPreferredSize(localeDim2);
111 
112 		isoLangCombo.setPreferredSize(localeDim3);
113 		isoCounCombo.setPreferredSize(localeDim3);
114 
115 		// Select the appropriate entries in the combo boxes
116 		String lafname = UIManager.getLookAndFeel().getName();
117 		for (int i = 0; i < lafCombo.getItemCount(); i++) {
118 			if (lafCombo.getItemAt(i).toString().equals(lafname)) {
119 				lafCombo.setSelectedIndex(i);
120 				break;
121 			}
122 		}
123 		String locname = Resources.getLocale().toString();
124 		String loclang = Resources.getLocale().getLanguage();
125 		String loccoun = Resources.getLocale().getCountry();
126 		for (int i = 0; i < machineCombo.getItemCount(); i++) {
127 			if (machineCombo.getItemAt(i).toString().equalsIgnoreCase(locname)) {
128 				machineCombo.setSelectedIndex(i);
129 				break;
130 			}
131 		}
132 		for (int i = 0; i < definedCombo.getItemCount(); i++) {
133 			if (definedCombo.getItemAt(i).toString().equalsIgnoreCase(locname)) {
134 				definedCombo.setSelectedIndex(i);
135 				break;
136 			}
137 		}
138 		for (int i = 0; i < isoLangCombo.getItemCount(); i++) {
139 			if (isoLangCombo.getItemAt(i).toString().equalsIgnoreCase(loclang)) {
140 				isoLangCombo.setSelectedIndex(i);
141 				break;
142 			}
143 		}
144 		for (int i = 0; i < isoCounCombo.getItemCount(); i++) {
145 			if (isoCounCombo.getItemAt(i).toString().equalsIgnoreCase(loccoun)) {
146 				isoCounCombo.setSelectedIndex(i);
147 				break;
148 			}
149 		}
150 
151 		// Set the radio button group
152 		ButtonGroup group = new ButtonGroup();
153 		group.add(machineRadio);
154 		group.add(definedRadio);
155 		group.add(isoRadio);
156 
157 		nameField.setColumns(15);
158 
159 		// Add action listeners
160 		cancelButton.addActionListener(new ActionListener() {
161 			public void actionPerformed(ActionEvent ev) {
162 				thisWindowClosing();
163 			}
164 		});
165 
166 		okButton.addActionListener(new ActionListener() {
167 			public void actionPerformed(ActionEvent ev) {
168 				updatePreferences();
169 			}
170 		});
171 		getRootPane().setDefaultButton(okButton);
172 
173 		panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
174 														  Resources.getTranslation("dialog_preferences_locale")));
175 		panel3.setLayout(new BorderLayout());
176 
177 		localePanel.add(isoLangCombo);
178 		localePanel.add(underscoreLabel);
179 		localePanel.add(isoCounCombo);
180 
181 		localeBox1.add(machineRadio);
182 		localeBox1.add(definedRadio);
183 		localeBox1.add(isoRadio);
184 		localeBox2.add(machineCombo);
185 		localeBox2.add(definedCombo);
186 		localeBox2.add(localePanel);
187 		localeBox1.add(Box.createVerticalStrut(5));
188 		localeBox2.add(Box.createVerticalStrut(5));
189 
190 		panel1.add(nameLabel);
191 		panel1.add(nameField);
192 		panel2.add(lafLabel);
193 		panel2.add(lafCombo);
194 		panel3.add(localeBox1, BorderLayout.WEST);
195 		panel3.add(localeBox2, BorderLayout.EAST);
196 		panel3.add(warnLabel, BorderLayout.SOUTH);
197 		panel4.add(okButton);
198 		panel4.add(cancelButton);
199 
200 		mainBox.add(panel1);
201 		mainBox.add(panel2);
202 		mainBox.add(panel3);
203 		mainBox.add(panel4);
204 
205 		getContentPane().add(mainBox);
206 		//validate();
207 		pack();
208 		setVisible(true);
209 	}
210 
thisWindowClosing()211 	private void thisWindowClosing() {
212 		setVisible(false);
213 		dispose();
214 	}
215 
updatePreferences()216 	void updatePreferences() {
217 		// Set the user name
218 		gui.setUser(nameField.getText().trim());
219 		// Set the look and feel
220 		try {
221 			UIManager.LookAndFeelInfo lafi[] = UIManager.getInstalledLookAndFeels();
222 			for (int i=0; i < lafi.length; i++) {
223 				if (lafi[i].getName().equals(lafCombo.getSelectedItem().toString())) {
224 					UIManager.setLookAndFeel(lafi[i].getClassName());
225 					gui.updateUI();
226 					break;
227 				}
228 			}
229 		} catch (Exception e) {
230 			System.err.println("Could not change the look and feel");
231 			e.printStackTrace(System.err);
232 		}
233 		// Set the locale
234 		String language = null;
235 		String country = null;
236 		String variant = null;
237 		if (definedRadio.isSelected()) {
238 			String encoding = "";
239 			if (definedCombo.getSelectedItem() != null) {
240 				encoding = definedCombo.getSelectedItem().toString();
241 			}
242 			language = Resources.getLanguage(encoding);
243 			country = Resources.getCountry(encoding);
244 			variant = Resources.getVariant(encoding);
245 			RBManagerGUI.debugMsg("Before: " + language + "_" + country + "_" + variant);
246 			if (country == null) country = new String();
247 			if (variant == null) locale = new Locale(language, country);
248 			else locale = new Locale(language, country, variant);
249 			RBManagerGUI.debugMsg("After: " + locale.toString());
250 		} else if (machineRadio.isSelected()) {
251 			String encoding = machineCombo.getSelectedItem().toString();
252 			language = Resources.getLanguage(encoding);
253 			country = Resources.getCountry(encoding);
254 			variant = Resources.getVariant(encoding);
255 			if (country == null) country = new String();
256 			if (variant == null) locale = new Locale(language, country);
257 			else locale = new Locale(language, country, variant);
258 		} else if (isoRadio.isSelected()) {
259 			language = isoLangCombo.getSelectedItem().toString();
260 			country = isoCounCombo.getSelectedItem().toString();
261 			if (variant == null) locale = new Locale(language, country);
262 			else locale = new Locale(language, country, variant);
263 		}
264 		Resources.setLocale(locale);
265 		gui.updateLocale(locale);
266 
267 		// Write the preferences
268 		Preferences.setPreference("username", gui.getUser());
269 		Preferences.setPreference("lookandfeel", UIManager.getLookAndFeel().getClass().getName());
270 		Preferences.setPreference("locale", locale.toString());
271 		try {
272 			Preferences.savePreferences();
273 		} catch (IOException ioe) {
274 			JOptionPane.showMessageDialog(this, Resources.getTranslation("error_preferences_save"),
275 										  Resources.getTranslation("error"), JOptionPane.ERROR_MESSAGE);
276 			ioe.printStackTrace(System.err);
277 		}
278 
279 		// Close the window
280 		thisWindowClosing();
281 	}
282 }
283