source: trunk/gli/src/org/greenstone/gatherer/gems/GEMSPreferences.java@ 12306

Last change on this file since 12306 was 12115, checked in by kjdon, 18 years ago

Changed text handling to use Dictionary.get rather than Dictionary.setText or Dictionary.registerBoth etc

  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Author: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gems;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.io.*;
32import java.net.*;
33import java.util.*;
34import javax.swing.*;
35import javax.swing.event.*;
36import javax.swing.border.*;
37import org.greenstone.gatherer.Configuration;
38import org.greenstone.gatherer.Dictionary;
39import org.greenstone.gatherer.cdm.LanguageManager;
40import org.greenstone.gatherer.gui.*;
41import org.greenstone.gatherer.util.StaticStrings;
42import org.w3c.dom.*;
43
44public class GEMSPreferences extends ModalDialog {
45
46 static final public String CONNECTION_PREFS = "connection";
47 static final public String GENERAL_PREFS = "general";
48 static final private Dimension LABEL_SIZE = new Dimension(280, 25);
49 static final private Dimension SIZE = new Dimension(640, 345);
50
51 private JButton apply_button;
52 private JButton cancel_button;
53 private JButton ok_button;
54
55 private JComboBox language_combobox;
56 private JList language_limited_jlist;
57 private JList language_code_jlist;
58 private JLabel interface_language_label;
59 private JLabel language_label;
60
61 private JTabbedPane tab_pane;
62 private GEMSPreferences self;
63
64 public GEMSPreferences() {
65 // Initialize
66 super((JFrame) null, true);
67 this.self = this;
68 setSize(SIZE);
69 setTitle(Dictionary.get("Preferences"));
70 setJMenuBar(new SimpleMenuBar("preferences"));
71
72 // Creation
73 JPanel content_pane = (JPanel) getContentPane();
74 tab_pane = new JTabbedPane();
75 JPanel general_preferences = createGeneralPreferences();
76 tab_pane.addTab(Dictionary.get("Preferences.General"), null, general_preferences, Dictionary.get("Preferences.General_Tooltip"));
77
78 JPanel button_pane = new JPanel();
79 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
80 apply_button = new GLIButton(Dictionary.get("General.Apply"), Dictionary.get("General.Apply_Tooltip"));
81 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
82
83 // Connection
84 ok_button.addActionListener(new OKButtonListener(true));
85 apply_button.addActionListener(new OKButtonListener(false));
86 cancel_button.addActionListener(new CancelButtonListener());
87
88 // Layout
89 button_pane.setBorder(BorderFactory.createEmptyBorder(5,2,2,2));
90 button_pane.setLayout(new GridLayout(1,3,0,5));
91 button_pane.add(ok_button);
92 button_pane.add(apply_button);
93 button_pane.add(cancel_button);
94
95 content_pane.setLayout(new BorderLayout());
96 content_pane.add(tab_pane, BorderLayout.CENTER);
97 content_pane.add(button_pane, BorderLayout.SOUTH);
98
99 setLocation(400, 300);
100 tab_pane.setSelectedComponent(general_preferences);
101
102 // Clean up
103 general_preferences = null;
104 cancel_button = null;
105 ok_button = null;
106 button_pane = null;
107 tab_pane = null;
108 content_pane = null;
109
110 setVisible(true);
111 }
112
113 private JPanel createGeneralPreferences() {
114 JPanel general_pane = new JPanel();
115
116 // Build the model of available languages
117 ArrayList dictionary_model = new ArrayList();
118
119 // The new method makes use of the successor to the languages.dat file, classes/xml/languages.xml
120 // Should update to also give the full name of the language --Matthew
121 NodeList language_elements = LanguageManager.LANGUAGES_DOCUMENT.getDocumentElement().getElementsByTagName(StaticStrings.LANGUAGE_ELEMENT);
122 for(int i = 0; i < language_elements.getLength(); i++) {
123 Element language_element = (Element) language_elements.item(i);
124 if((language_element.getAttribute(StaticStrings.GLI_ATTRIBUTE)).equalsIgnoreCase(StaticStrings.TRUE_STR) || (language_element.getAttribute(StaticStrings.MDS_ATTRIBUTE)).equalsIgnoreCase(StaticStrings.TRUE_STR)) {
125 Locale locale = new Locale(language_element.getAttribute(StaticStrings.CODE_ATTRIBUTE));
126 String description = language_element.getAttribute(StaticStrings.NAME_ATTRIBUTE);
127 DictionaryEntry entry = new DictionaryEntry(description, locale);
128 if(!dictionary_model.contains(entry)) {
129 dictionary_model.add(entry);
130 }
131 entry = null;
132 description = null;
133 locale = null;
134 }
135 language_element = null;
136 }
137 language_elements = null;
138
139 // Users email
140 JPanel lang_limited_pane = new JPanel();
141 interface_language_label = new JLabel(Dictionary.get("GEMS.Preferences.Selected_Languages_Tooltip"));
142 interface_language_label.setPreferredSize(LABEL_SIZE);
143
144 // Language
145 JPanel language_pane = new JPanel();
146 language_label = new JLabel(Dictionary.get("Preferences.General.Interface_Language"));
147 language_label.setPreferredSize(LABEL_SIZE);
148
149 language_combobox = new JComboBox(dictionary_model.toArray());
150 language_combobox.setToolTipText(Dictionary.get("Preferences.General.Interface_Language_Tooltip"));
151
152 // Try to locate and select the current language
153 String language_code = Configuration.getLanguage();
154 for(int b = 0; b < language_combobox.getItemCount(); b++) {
155 DictionaryEntry entry = (DictionaryEntry) language_combobox.getItemAt(b);
156 if(language_code.equalsIgnoreCase(entry.getLocale().getLanguage())) {
157 language_combobox.setSelectedIndex(b);
158 }
159 }
160
161 //create language selected box, and high light the apprpropate ones
162 int [] selectedLanguages = new int[300];
163 int selectedLanguagesLength = 0;
164
165 GEMSLanguageManager gemsLangManager = new GEMSLanguageManager(GEMS.getGLIDirectoryPath() + "classes" + File.separator + "xml" + File.separator + "languages.xml");
166
167 //Create the visible JList, and then another one with just the language codes.
168 language_code_jlist = new JList(gemsLangManager.getLanguageCodesToArray());
169 language_limited_jlist = new JList(gemsLangManager.getLanguageCodesAndNames());
170
171 //Set the list up to display tabs properly
172 TabListCellRenderer renderer = new TabListCellRenderer();
173 renderer.setTabs(new int[] {50, 200, 300});
174 language_limited_jlist.setCellRenderer(renderer);
175
176 String enabled_languages = Configuration.getString("GEMS.Preferences.Selected_Languages", true);
177 //we have a string: enabled_languages that contains an array of strings delimited by comma's
178 String []enabled_languages_split = enabled_languages.split(",");
179 Object []language_codes_array = gemsLangManager.getLanguageCodesToArray();
180
181 //for each enabled_languages_split, check which languages match, and store the index in selectedLanguages
182 for(int k = 0; k < enabled_languages_split.length; k++) {
183
184 for(int p =0; p < language_codes_array.length; p++){
185
186 //if the codes match, then include the index p into the vector selectedLanguages
187 if (language_codes_array[p].toString().toLowerCase().trim().compareTo(enabled_languages_split[k].toLowerCase().trim()) == 0)
188 {
189 selectedLanguages[selectedLanguagesLength] = p;
190 selectedLanguagesLength++;
191 language_limited_jlist.addSelectionInterval(p,p);
192 }
193 }
194 }
195
196 // Layout
197 JScrollPane langScrollPane = new JScrollPane(language_limited_jlist);
198 langScrollPane.setPreferredSize(new Dimension(100, 100));
199 lang_limited_pane.setLayout(new GridLayout(1,3));
200 lang_limited_pane.add(interface_language_label);
201 lang_limited_pane.add(langScrollPane);
202
203 language_pane.setLayout(new BorderLayout());
204 language_pane.add(language_label, BorderLayout.WEST);
205 language_pane.add(language_combobox, BorderLayout.CENTER);
206
207 general_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
208 general_pane.setLayout(null);
209 general_pane.add(lang_limited_pane);
210 general_pane.add(language_pane);
211 //general_pane.add(view_extracted_metadata_checkbox);
212 Insets general_pane_insets = general_pane.getInsets();
213
214 lang_limited_pane.setBounds(20+general_pane_insets.left, 20+general_pane_insets.top,
215 lang_limited_pane.getPreferredSize().width, lang_limited_pane.getPreferredSize().height);
216
217 language_pane.setBounds(20+general_pane_insets.left, 130+general_pane_insets.top,
218 language_pane.getPreferredSize().width, language_pane.getPreferredSize().height);
219
220 return general_pane;
221 }
222
223
224 private class OKButtonListener
225 implements ActionListener {
226 private boolean close;
227 public OKButtonListener(boolean close) {
228 this.close = close;
229 }
230 public void actionPerformed(ActionEvent event) {
231 // Submit the various changes
232
233 //save interface language
234 String current_lang = Configuration.getLanguage();
235 String new_lang = ((DictionaryEntry)language_combobox.getSelectedItem()).getLocale().getLanguage();
236 if (!current_lang.equals(new_lang)) {
237 Configuration.setLocale("general.locale", Configuration.GENERAL_SETTING, ((DictionaryEntry)language_combobox.getSelectedItem()).getLocale());
238 JOptionPane.showMessageDialog(null, Dictionary.get("Preferences.General.Restart_Required"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
239 }
240
241 //Set language_code_jlist to have the same selections as the language_limited_jlist.
242 //(before, there was only one JList, and it only contained the language codes).
243 int[] selection = language_limited_jlist.getSelectedIndices();
244 language_code_jlist.setSelectedIndices(selection);
245 Object[] selectedValues = language_code_jlist.getSelectedValues();
246 String concatString = new String();
247
248 for(int k = 0; k < selectedValues.length; k++) {
249 if(selectedValues.length-k > 1)
250 concatString = concatString.concat(selectedValues[k].toString() +",");
251 else
252 concatString = concatString.concat(selectedValues[k].toString());
253 }
254
255 Configuration.setString("GEMS.Preferences.Selected_Languages",true, concatString);
256
257 // Always save configuration changes immediately (in case the GLI crashes)
258 Configuration.save();
259
260 // Hide dialog
261 if(close) {
262 self.dispose();
263 }
264 }
265 }
266
267 private class CancelButtonListener
268 implements ActionListener {
269 public void actionPerformed(ActionEvent event) {
270 self.dispose();
271 }
272 }
273
274 private class DictionaryEntry
275 implements Comparable {
276 private Locale locale;
277 private String description;
278 public DictionaryEntry(Locale locale) {
279 this.description = null;
280 this.locale = locale;
281 }
282 public DictionaryEntry(String description, Locale locale) {
283 this.description = description;
284 this.locale = locale;
285 }
286 public int compareTo(Object object) {
287 return toString().compareTo(object.toString());
288 }
289 public boolean equals(Object object) {
290 return toString().equals(object.toString());
291 }
292 public Locale getLocale() {
293 return locale;
294 }
295 public String toString() {
296 if(description != null) {
297 return description;
298 }
299 else {
300 return locale.getDisplayName();
301 }
302 }
303 }
304}
305
306
307/**
308 A custom list renderer, to display tabs in the JList properly.
309
310 @see http://manning.com/sbe/files/uts2/Chapter10html/Chapter10.htm
311 Added by Matthew Whyte
312 Date last modified: 1/02/05
313 */
314class TabListCellRenderer extends JLabel implements ListCellRenderer
315{
316 protected static Border m_noFocusBorder;
317 protected FontMetrics m_fm = null;
318 protected Insets m_insets = new Insets(0, 0, 0, 0);
319
320 protected int m_defaultTab = 50;
321 protected int[] m_tabs = null;
322
323 public TabListCellRenderer()
324 {
325 super();
326 m_noFocusBorder = new EmptyBorder(1, 1, 1, 1);
327 setOpaque(true);
328 setBorder(m_noFocusBorder);
329 }
330
331 public Component getListCellRendererComponent(JList list,
332 Object value, int index, boolean isSelected, boolean cellHasFocus)
333 {
334 setText(value.toString());
335
336 setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
337 setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
338
339 setFont(list.getFont());
340 setBorder((cellHasFocus) ? UIManager.getBorder("List.focusCellHighlightBorder") : m_noFocusBorder);
341
342 return this;
343 }
344
345 public void setDefaultTab(int defaultTab) { m_defaultTab = defaultTab; }
346
347 public int getDefaultTab() { return m_defaultTab; }
348
349 public void setTabs(int[] tabs) { m_tabs = tabs; }
350
351 public int[] getTabs() { return m_tabs; }
352
353 public int getTab(int index)
354 {
355 if (m_tabs == null)
356 return m_defaultTab*index;
357
358 int len = m_tabs.length;
359 if (index >= 0 && index < len)
360 return m_tabs[index];
361
362 return m_tabs[len-1] + m_defaultTab*(index-len+1);
363 }
364
365
366 public void paint(Graphics g)
367 {
368 m_fm = g.getFontMetrics();
369
370 g.setColor(getBackground());
371 g.fillRect(0, 0, getWidth(), getHeight());
372 getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());
373
374 g.setColor(getForeground());
375 g.setFont(getFont());
376 m_insets = getInsets();
377 int x = m_insets.left;
378 int y = m_insets.top + m_fm.getAscent();
379
380 StringTokenizer st = new StringTokenizer(getText(), "\t");
381 while (st.hasMoreTokens())
382 {
383 String sNext = st.nextToken();
384 g.drawString(sNext, x, y);
385 x += m_fm.stringWidth(sNext);
386
387 if (!st.hasMoreTokens())
388 break;
389 int index = 0;
390 while (x >= getTab(index))
391 index++;
392 x = getTab(index);
393 }
394 }
395}
Note: See TracBrowser for help on using the repository browser.