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

Last change on this file since 9078 was 9078, checked in by mdewsnip, 19 years ago

Removed unused BorderFactory class.

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