ThebaPrefs.java
Go to the documentation of this file.00001 package theba.core.gui;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.FlowLayout;
00005 import java.awt.event.ActionEvent;
00006 import java.awt.event.ActionListener;
00007 import java.util.StringTokenizer;
00008 import java.util.prefs.BackingStoreException;
00009 import java.util.prefs.Preferences;
00010
00011 import javax.swing.JButton;
00012 import javax.swing.JDialog;
00013 import javax.swing.JFrame;
00014 import javax.swing.JLabel;
00015 import javax.swing.JList;
00016 import javax.swing.JPanel;
00017 import javax.swing.JScrollPane;
00018 import javax.swing.JTextField;
00019 import javax.swing.event.ListSelectionEvent;
00020 import javax.swing.event.ListSelectionListener;
00021
00029 public class ThebaPrefs extends JDialog {
00033 private static final long serialVersionUID = 1L;
00034
00035 private JTextField nameField = new JTextField(8);
00036
00037 private JTextField valueField = new JTextField(8);
00038
00039 private JList prefList = new JList(new String[] {});
00040
00041 private Preferences prefs;
00042
00043 private static ThebaPrefs instance;
00044
00045 public static ThebaPrefs getInstance() {
00046 if (instance == null)
00047 return new ThebaPrefs(null);
00048 return instance;
00049 }
00050
00051
00052
00053
00054
00055 public double getDouble(String name, double defaultValue) {
00056 double val = prefs.getDouble(name, defaultValue);
00057 if (val == defaultValue) {
00058 prefs.putDouble(name, defaultValue);
00059 }
00060 return val;
00061 }
00062
00063 public void setInt(String name, int value) {
00064 prefs.putInt(name, value);
00065 }
00066
00067 public void setString(String name, String value) {
00068 if (value != null)
00069 prefs.put(name, value);
00070 }
00071
00076 public String getString(String name, String defaultValue) {
00077 String val = prefs.get(name, defaultValue);
00078 if (val == defaultValue && val != null) {
00079 prefs.put(name, defaultValue);
00080 }
00081 return val;
00082 }
00083
00088 public int getInt(String name, int defaultValue) {
00089 int val = prefs.getInt(name, defaultValue);
00090 if (val == defaultValue) {
00091 prefs.putInt(name, defaultValue);
00092 }
00093 return val;
00094 }
00095
00096 ThebaPrefs(JFrame f) {
00097 super(f);
00098 prefs = Preferences.userNodeForPackage(this.getClass());
00099 prefList.addListSelectionListener(new ListSelectionListener() {
00100 public void valueChanged(ListSelectionEvent e) {
00101 if (prefList.getSelectedValue() == null)
00102 return;
00103 String value = prefList.getSelectedValue().toString();
00104 StringTokenizer tk = new StringTokenizer(value, "=");
00105 if (tk.hasMoreTokens())
00106 nameField.setText(tk.nextToken());
00107 if (tk.hasMoreTokens())
00108 valueField.setText(tk.nextToken());
00109 }
00110 });
00111 setTitle("Preferences");
00112 setModal(true);
00113 setListFromPrefs();
00114
00115 JScrollPane scrollingList = new JScrollPane(prefList);
00116 JButton newKeyButton = new JButton("Set value");
00117 JButton clearButton = new JButton("Clear All");
00118 JButton delSelectedButton = new JButton("Remove Selected");
00119
00120 newKeyButton.addActionListener(new ActionListener() {
00121 public void actionPerformed(ActionEvent e) {
00122 if (nameField.getText().length() > 0) {
00123 prefs.put(nameField.getText(), valueField.getText());
00124 nameField.setText("");
00125 valueField.setText("");
00126 setListFromPrefs();
00127 } else {
00128 nameField.setText("Key?");
00129 }
00130 }
00131 });
00132 clearButton.addActionListener(new ActionListener() {
00133 public void actionPerformed(ActionEvent e) {
00134 try {
00135 prefs.clear();
00136 setListFromPrefs();
00137 } catch (BackingStoreException ex) {
00138 System.out.println(ex);
00139 }
00140 }
00141 });
00142 delSelectedButton.addActionListener(new ActionListener() {
00143 public void actionPerformed(ActionEvent e) {
00144 String selection = (String) prefList.getSelectedValue();
00145 if (selection != null) {
00146 String key = selection.substring(0, selection.indexOf("="));
00147 prefs.remove(key);
00148 setListFromPrefs();
00149 }
00150 }
00151 });
00152
00153 JPanel buttonPanel = new JPanel(new FlowLayout());
00154 buttonPanel.add(new JLabel("Key"));
00155 buttonPanel.add(nameField);
00156 buttonPanel.add(new JLabel("Value"));
00157 buttonPanel.add(valueField);
00158 buttonPanel.add(newKeyButton);
00159 buttonPanel.add(delSelectedButton);
00160 buttonPanel.add(clearButton);
00161 JPanel content = new JPanel(new BorderLayout());
00162 content.add(buttonPanel, BorderLayout.NORTH);
00163 content.add(scrollingList, BorderLayout.CENTER);
00164 this.setContentPane(content);
00165 this.pack();
00166 setLocationRelativeTo(f);
00167 }
00168
00169 private void setListFromPrefs() {
00170 try {
00171 String[] keys = prefs.keys();
00172 for (int i = 0; i < keys.length; i++) {
00173 keys[i] += "=" + prefs.get(keys[i], "ERROR");
00174 }
00175 prefList.setListData(keys);
00176 } catch (BackingStoreException ex) {
00177 System.out.println(ex);
00178 }
00179 }
00180 }