001    // COpyright 2004, David Cok
002    
003    package escjava.gui;
004    
005    import java.lang.reflect.Field;
006    import javax.swing.*;
007    import java.awt.*;
008    import java.awt.event.ActionEvent;
009    import java.awt.event.ActionListener;
010    
011    public class GuiOptionsPanel extends JPanel implements ActionListener {
012    
013        static public class Settings {
014            public boolean autoExpand = true;
015            public boolean autoScroll = true;
016            public boolean breadthFirst = false;
017            public boolean autoShowErrors = true;
018        }
019    
020        public Settings settings = new Settings();
021    
022            /**  The array contains the text string, field name and the tooltip. */
023        static final public String[][] info = {
024            { "Auto Expand the nodes", "autoExpand", "Automatically expands the nodes as processing progresses"},
025            { "Auto scroll", "autoScroll", "Automatically scroll the window to keep processing point in view (will also expand nodes)"},
026            { "Breadth first checking", "breadthFirst", "Check all nodes at a given level (parsing, typechecking, static checking) before moving to the next level, rather than doing all checks for a given node before checking the next node"},
027            { "Show Error Windows Automatically", "autoShowErrors", "Automatically popup windows showing errors as items are checked" },
028        };
029    
030        static final public Class guioptions = escjava.gui.GuiOptionsPanel.Settings.class;
031    
032        public GuiOptionsPanel() {
033            init();
034        }
035    
036        public void init() {
037            setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
038            removeAll();
039            JCheckBox cb;
040            for (int i = 0; i<info.length; ++i) {
041                String[] iinfo = info[i];
042                try {
043                    Field f = guioptions.getField(iinfo[1]);
044                    boolean b = f.getBoolean(settings);
045                    cb = new JCheckBox(iinfo[0],b);
046                    cb.setToolTipText(iinfo[2]);
047                    cb.addActionListener(this);
048                    add(cb);
049                } catch (Exception e) {
050                    System.out.println("FAILED TO RECOGNIZE OPTION " + i + ": " + iinfo[0] + " " + e);
051                }
052            }
053        }
054    
055        public void actionPerformed(ActionEvent e) {
056            // write back out to the Options structure
057    
058            Object source = e.getSource();
059            String name = null;
060            if (source instanceof JCheckBox) {
061                String fname = null;
062                name = ((JCheckBox)source).getText();
063                for (int i=0; i<info.length; ++i) {
064                    if (info[i][0].equals(name)) {
065                        fname = info[i][1];
066                        break;
067                    }
068                }
069                boolean value = ((JCheckBox)source).isSelected();
070                try {
071                    Field f = guioptions.getField(fname);
072                    f.setBoolean(settings,value);
073                } catch (Exception ee) {
074                    System.out.println("FAILED TO RECOGNIZE OPTION: " + name + " " + ee);
075                }
076            } else {
077                System.out.println("UNKNOWN GUI OPTION " + name);
078            }
079        }
080    
081    }