VisClient/org/hfbk/vis/PrefsDialog.java

Go to the documentation of this file.
00001 package org.hfbk.vis;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Button;
00005 import java.awt.Checkbox;
00006 import java.awt.Color;
00007 import java.awt.Component;
00008 import java.awt.Container;
00009 import java.awt.Dimension;
00010 import java.awt.FlowLayout;
00011 import java.awt.Frame;
00012 import java.awt.Label;
00013 import java.awt.Panel;
00014 import java.awt.ScrollPane;
00015 import java.awt.TextField;
00016 import java.awt.event.ActionEvent;
00017 import java.awt.event.ActionListener;
00018 import java.awt.event.ItemEvent;
00019 import java.awt.event.ItemListener;
00020 import java.awt.event.TextEvent;
00021 import java.awt.event.TextListener;
00022 import java.awt.event.WindowAdapter;
00023 import java.awt.event.WindowEvent;
00024 import java.io.File;
00025 import java.lang.reflect.Field;
00026 
00027 import org.hfbk.util.DirectoryChooser;
00028 import org.hfbk.util.FontDialog;
00029 import org.hfbk.util.Tooltip;
00030 import org.hfbk.util.UIUtils;
00031 
00046 public class PrefsDialog {
00047         
00048         Panel prefsPanel;
00049 
00050         Prefs lastPrefs;
00051 
00052         private Frame frame;
00053 
00054         static void setTT(Field f, Component owner){
00055                 String tt=Prefs.help.get(f.getName());
00056                 if(tt!=null) new Tooltip(tt, owner);
00057         }
00058         
00059         public static Checkbox getPrefsCheckbox(final Field f)
00060                         throws IllegalAccessException {
00061                 final Checkbox c = new Checkbox("", f.getBoolean(Prefs.current));
00062                 setTT(f, c);
00063         c.addItemListener(new ItemListener() {
00064                         public void itemStateChanged(ItemEvent e) {
00065                                 try {
00066                                         f.setBoolean(Prefs.current, c.getState());
00067                                 } catch (Exception ex) {
00068                                         throw new RuntimeException(ex);
00069                                 }
00070                         }
00071                 });
00072                 return c;
00073         }
00074 
00075         public static TextField getPrefsStringInput(final Field f, int cols)
00076                         throws IllegalAccessException {
00077                         
00078                 final TextField t = new TextField((String) f.get(Prefs.current), cols);
00079                 setTT(f, t);
00080         
00081                 t.addTextListener(new TextListener() {
00082                         public void textValueChanged(TextEvent e) {
00083                                 try {
00084                                         f.set(Prefs.current, t.getText());
00085                                 } catch (Exception ex) {
00086                                         throw new RuntimeException(ex);
00087                                 }
00088                         }
00089                 });
00090                 return t;
00091         }
00092 
00093         public static Panel getPrefsIntInput(final Field f)
00094                         throws IllegalAccessException {
00095                 final TextField t = new TextField(String.valueOf(f.getInt(Prefs.current)), 8);
00096                 setTT(f, t);
00097         
00098                 t.addTextListener(new TextListener() {
00099                         public void textValueChanged(TextEvent e) {
00100                                 try {
00101                                         f.set(Prefs.current, Integer.parseInt(t.getText()));
00102                                 } catch (Exception ex) {
00103                                         throw new RuntimeException(ex);
00104                                 }
00105                         }
00106                 });
00107 
00108                 Panel p = new Panel();
00109                 p.add(new Label(f.getName()));
00110                 p.add(t);
00111                 
00112 
00113                 return p;
00114         }
00115 
00124         Panel getFieldsPanel(String filter) {
00125                 Panel panel = new Panel();
00126                 
00127                 // we modify the FlowLayout to not spread infinitely
00128                 // but keep an constant width while breaking items to
00129                 // rows like text would do.
00130                 panel.setLayout(new FlowLayout() {
00131                         public Dimension preferredLayoutSize(Container target) {
00132                                 Dimension d = super.minimumLayoutSize(target);
00133 
00134                                 int area = d.width * d.height;
00135                                 d.width = 500;
00136                                 d.height = area / d.width;
00137                                 return d;
00138                         }
00139                 });
00140 
00141                 try {
00142                         // for all public fields of this class
00143                         for (Field f : Prefs.current.getClass().getFields()) {
00144                                 if (!f.getName().matches(filter))
00145                                         continue;
00146                                 Class type = f.getType();
00147                                 
00148                                 if (type == Boolean.TYPE) {
00149                                         Panel p = new Panel();
00150                                         
00151                                         p.setBackground(new Color(100,100,100));
00152                                         p.add(getPrefsCheckbox(f));
00153                                         p.add(new Label(f.getName()));
00154                                         panel.add(p);
00155                                 } else if (type == Integer.TYPE)
00156                                         panel.add(getPrefsIntInput(f));
00157                                 else if (type == String.class) {
00158                                         Panel p = new Panel();
00159                                         Label l = new Label(f.getName(), Label.LEFT);
00160                                         l.setPreferredSize(new Dimension(100, 20));
00161                                         p.add(l);
00162                                         final TextField textField = getPrefsStringInput(f, 50);
00163                                         p.add(textField);
00164                                         if (f.getName().endsWith("dir"))
00165                                                 p.add(createFileChooserButton(f.getName(), textField));
00166                                         if (f.getName().endsWith("font"))
00167                                                 p.add(createFontChooserButton(textField));
00168 
00169                                         panel.add(p);
00170                                 }
00171                         }
00172                 } catch (Exception e) {
00173                         throw new RuntimeException(e);
00174                 }
00175 
00176                 UIUtils.blackify(panel); // apply the classic vis look!
00177 
00178                 return panel;
00179         }
00180 
00181         SimpleButton createFontChooserButton(final TextField textField) {
00182                 return new SimpleButton("...") {
00183                         void action() {
00184                                 
00185                                 String family;
00186                                 
00187                                 if (Prefs.current.font.contains("."))
00188                                 {                               
00189                                         String[] parts=Prefs.current.font.split("\\.");
00190                                         family=parts[0];
00191                                 }
00192                                 else family=Prefs.current.font;
00193                                 
00194                                 FontDialog fc = new FontDialog(frame, family);
00195                                 
00196                                 //try {  //what could happen here? what kind of ex?
00197                                 textField.setText(fc.chooseFont().getFamily());
00198                                 /*}
00199                                 catch (Exception e)
00200                                 {
00201                                 }*/
00202                         }
00203                 };
00204         }
00205         
00206         SimpleButton createFileChooserButton(final String label, final TextField textField) {
00207                 return new SimpleButton("...") {
00208                         void action() {
00209                                 DirectoryChooser dc= new DirectoryChooser("Choose "+label+"...", new File(textField.getText()));
00210                                 textField.setText(dc.chooseDir());      
00211                         }
00212                 };
00213         }
00214 
00216         public PrefsDialog() {
00217                 this(".*");
00218         }
00219 
00226         public PrefsDialog(final String filter) {
00227                 lastPrefs = Prefs.current.clone();
00228 
00229                 frame = new Frame("vis/space Preferences");
00230 
00231                 prefsPanel = getFieldsPanel(filter);
00232 
00233                 final ScrollPane pane = new ScrollPane(); // wrap the UI by a scroll
00234                 // pane to allow future grow
00235                 pane.add(prefsPanel);
00236 
00237                 Panel buttonBar = new Panel(); // add some buttons to the bottom
00238 
00239                 if (filter.equals(".*"))
00240                         buttonBar.add(new SimpleButton("DEFAULT") {
00241                                 void action() {
00242                                         Prefs.current = Prefs.defaults.clone();
00243                                         pane.remove(prefsPanel);
00244                                         prefsPanel = getFieldsPanel(filter);
00245                                         pane.add(prefsPanel);
00246                                         pane.validate();
00247                                 }
00248                         });
00249 
00250                 buttonBar.add(new SimpleButton("RESET") {
00251                         void action() {
00252                                 Prefs.current = lastPrefs.clone();
00253                                 pane.remove(prefsPanel);
00254                                 prefsPanel = getFieldsPanel(filter);
00255                                 pane.add(prefsPanel);
00256                                 pane.validate();
00257                         }
00258                 });
00259 
00260                 buttonBar.add(new SimpleButton("OK") {
00261                         void action() {
00262                                 frame.dispose();
00263                         }
00264                 });
00265 
00266                 buttonBar.add(new SimpleButton("CANCEL") {
00267                         void action() {
00268                                 Prefs.current = lastPrefs;
00269                                 frame.dispose();
00270                         }
00271                 });
00272 
00273                 frame.addWindowListener(new WindowAdapter() {
00274                         public void windowClosing(WindowEvent e) {
00275                                 frame.dispose();
00276                         }
00277                 });
00278                 
00279                 frame.add(pane, BorderLayout.CENTER);
00280                 frame.add(buttonBar, BorderLayout.SOUTH);
00281                 frame.setSize(650, 600);
00282                 frame.setVisible(true);
00283                 frame.setSize(650, 500);
00284                 
00285                 UIUtils.blackify(frame);
00286                 pane.setWheelScrollingEnabled(true);
00287                 
00288         }
00289 
00290         class SimpleButton extends Button {
00291                 public SimpleButton(String text) {
00292                         super(text);
00293                         addActionListener(new ActionListener() {
00294                                 public void actionPerformed(ActionEvent e) {
00295                                         action();
00296                                 }
00297                         });
00298                 }
00299 
00300                 void action() {
00301                 };
00302         }
00303 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1