import java.util.*; import java.io.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; /** * Specifies user-definable preferences for the Araucaria interface. * To add a new preference: * 1. Add a field to the PrefParams class. Make sure the new field is Serializable. * 2. Add a line to applyPrefs() to apply the pref to whatever parameter is affects. * 3. Add code to refreshDisplay() if necessary. This is for prefs that affect the display of Araucaria. * Shouldn't be needed for things like pathnames. * 4. Add code to the panel setup method so that the pref appears as a choice. For colors, * add to addColourPanel() * 5. Add code to the event handler to apply the user's choice. For colours, this is runColorChooser() * in ColorObject. */ public class Preferences extends JDialog implements ActionListener, ChangeListener { public Araucaria owner; JTabbedPane m_tabbedPane; JPanel m_colourPanel, m_directoryPanel, m_databasePanel; public PrefParams params; JButton okButton, cancelButton, resetButton; // Colour panel JList colourList; public Preferences(Frame parent, boolean modal) { super(parent, modal); owner = (Araucaria)parent; WindowHandler windowHandler = new WindowHandler(); addWindowListener(windowHandler); setTitle("Preferences"); setSize(300, 300); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setLocation(d.width/2 - getSize().width/2, d.height/2 - getSize().height/2); readPrefs(); initComponents(); } private void initComponents() { m_tabbedPane = new JTabbedPane(); addColourPanel(); addDirectoryPanel(); addDatabasePanel(); getContentPane().add(m_tabbedPane, BorderLayout.CENTER); okButton = new JButton("OK"); okButton.addActionListener(this); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(this); addDialogCloser(okButton); this.getRootPane().setDefaultButton(okButton); resetButton = new JButton("Reset"); resetButton.addActionListener(this); JPanel buttonPanel = new JPanel(new FlowLayout()); buttonPanel.add(okButton); buttonPanel.add(cancelButton); buttonPanel.add(resetButton); cancelButton.setVisible(false); getContentPane().add(buttonPanel, BorderLayout.SOUTH); m_tabbedPane.addChangeListener(this); } private void okButtonPressed() { setDirectories(); setDatabases(); this.setVisible(false); } public void actionPerformed(ActionEvent e) { if (e.getSource() == okButton) { okButtonPressed(); } else if (e.getSource() == resetButton) { int yesNo = JOptionPane.showConfirmDialog(null, "This will reset all your " + "preferences to the default values.\nDo you want to continue?", "Reset all parameters", JOptionPane.YES_NO_OPTION); if (yesNo == 0) { owner.resetDefaultParams(); params = new PrefParams(); refreshDisplay(); refreshDialog(); } } else if (e.getSource() == cancelButton) { this.setVisible(false); } } public void addDialogCloser(JComponent comp) { AbstractAction closeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { setVisible(false); } }; // Then create a keystroke to use for it KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); // Finally, bind the keystroke and the action to *any* component // within the dialog. Note the WHEN_IN_FOCUSED bit...this is what // stops you having to do it for all components comp.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, "close"); comp.getActionMap().put("close", closeAction); } public void stateChanged(ChangeEvent e) { if (m_tabbedPane.getSelectedIndex() == 0) { cancelButton.setVisible(false); } else { cancelButton.setVisible(true); } } void readPrefs() { try { FileInputStream prefStream = new FileInputStream("prefs.dat"); ObjectInputStream objStream = new ObjectInputStream(prefStream); params = (PrefParams)objStream.readObject(); objStream.close(); } catch (Exception ex) { // If we get an error, redefine prefs.dat from defaults params = new PrefParams(); writePrefs(); } refreshDisplay(); } void writePrefs() { try { FileOutputStream prefStream = new FileOutputStream("prefs.dat"); ObjectOutputStream objStream = new ObjectOutputStream(prefStream); objStream.writeObject(params); objStream.close(); } catch (Exception ex) { System.out.println ("Exception in writePrefs: " + ex.toString()); } } public void applyPrefs() { TreeCanvas.DIAGRAM_BACKGROUND = params.backgroundColor; SelectText.TEXT_BACKGROUND = params.backgroundText; Araucaria.STATUSBAR_BACKGROUND = params.backgroundStatusBar; Araucaria.textDirectory = params.textDirectory; Araucaria.amlDirectory = params.amlDirectory; Araucaria.schemeDirectory = params.schemeDirectory; Araucaria.ipAddress = params.ipAddress; Araucaria.databaseName = params.databaseName; Araucaria.username = params.username; Araucaria.password = params.password; Araucaria.databaseType = params.databaseType; owner.initializeDatabase(); } public void refreshDisplay() { applyPrefs(); writePrefs(); owner.getTreeCanvas().setBackground(TreeCanvas.DIAGRAM_BACKGROUND); owner.getTreeCanvas().repaint(); owner.m_selectText.setBackground(SelectText.TEXT_BACKGROUND); owner.m_selectText.repaint(); owner.m_messagePanel.setBackground(Araucaria.STATUSBAR_BACKGROUND); owner.m_messagePanel.repaint(); } public void refreshDialog() { ListModel model = colourList.getModel(); for (int i = 0; i