[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Sat Jun 9 22:03:44 UTC 2007


gordonp
Sat Jun  9 18:03:44 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui
In directory dev.open-bio.org:/tmp/cvs-serv5422/src/main/ca/ucalgary/seahawk/gui

Modified Files:
	SeahawkOptionsGUI.java 
Log Message:
First version of working options dialog
moby-live/Java/src/main/ca/ucalgary/seahawk/gui SeahawkOptionsGUI.java,1.2,1.3
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/SeahawkOptionsGUI.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/SeahawkOptionsGUI.java	2007/06/08 20:30:21	1.2
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/SeahawkOptionsGUI.java	2007/06/09 22:03:44	1.3
@@ -1,5 +1,372 @@
 package ca.ucalgary.seahawk.gui;
 
-public class SeahawkOptionsGUI{
+import ca.ucalgary.seahawk.util.SeahawkOptions;
+
+import org.biomoby.registry.meta.*;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+
+/**
+ * A dialog box that allows the Seahawk user to configure
+ * options such as cache expiry and location, as well as
+ * the MOBY registry to use, and whether referrer headers
+ * should be sent to services.  Settings are saved to a file
+ * by the SeahawkOptions class.
+ */
+public class SeahawkOptionsGUI extends JDialog implements ActionListener, WindowListener{
+    public static final String CUSTOM_REGISTRY_SYNONYM = "Custom (set your own location & namespace)";
+    public static final String CUSTOM_REGISTRY_DESC = "User-defined MOBY Central registry.";
+    public static final String DIALOG_TITLE = "Seahawk User Preferences";
+    public static final boolean DIALOG_MODAL = true;
+
+    private static final int MAX_DESC_WIDTH = 60;
+
+    private JComboBox registryComboBox;
+    private JTextPane registryDescTextArea;
+    private JTextField registryEndpoint;
+    private JTextField registryNamespace;
+
+    private JButton deleteCacheButton;
+    private JButton selectCacheDirButton;
+    private JTextField cacheExpiryTextField;
+    private JFileChooser cacheDirFileChooser;
+
+    private JCheckBox sendReferrerCheckBox;
+
+    private JButton resetButton;
+    private JButton okButton;
+    private JButton cancelButton;
+
+    private RegistriesList registriesList;
+    private String[] registryNames;
+    private String lastCustomEndpoint = "http://yourdomain.org/cgi-bin/MOBY-Central.pl";
+    private String lastCustomNamespace = "http://yourdomain.org/MOBY/Central";
+
+    /**
+     * @param owner the frame that launches this dialog
+     */
+    public SeahawkOptionsGUI(Frame owner){
+	super(owner, DIALOG_TITLE, DIALOG_MODAL);
+
+	// Custom handling of window close operation
+	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
+	addWindowListener(this);
+
+	// The dreaded GridBagLayout...
+	GridBagLayout gridbag = new GridBagLayout();
+	GridBagConstraints c = new GridBagConstraints();
+	c.fill = GridBagConstraints.BOTH;
+	setLayout(gridbag);
+	
+	registriesList = new RegistriesList();
+	Registry[] registries = registriesList.getAll();
+	registryNames = new String[registries.length+1];
+	int i = 0;
+	for(; i < registries.length; i++){
+	    registryNames[i] = registries[i].getSynonym();
+	}
+	registryNames[i] = CUSTOM_REGISTRY_SYNONYM;
+
+	JLabel label = new JLabel("Registry server:");
+	c.gridwidth = 1;
+	c.weightx = 0.0;
+	gridbag.setConstraints(label, c);
+	add(label);
+	registryComboBox = new JComboBox(registryNames);
+	registryComboBox.addActionListener(this);
+	c.gridwidth = GridBagConstraints.REMAINDER;
+	c.weightx = 1.0;
+	gridbag.setConstraints(registryComboBox, c);
+	add(registryComboBox);
+
+	label = new JLabel("Description:");
+	c.gridwidth = 1;
+	c.weightx = 0.0;
+	gridbag.setConstraints(label, c);
+	add(label);
+	registryDescTextArea = new JTextPane();
+	registryDescTextArea.setEditable(false);
+	c.gridwidth = GridBagConstraints.REMAINDER;
+	c.weightx = 0.0;   // 0 so long lines wrap in text area
+	c.weighty = 1.0;  // absorb all extra vertical space in the desc
+	gridbag.setConstraints(registryDescTextArea, c);
+	add(registryDescTextArea);
+	c.weighty = 0.0; //reset
+
+	label = new JLabel("Location:");
+	c.gridwidth = 1;
+	c.weightx = 0.0;
+	gridbag.setConstraints(label, c);
+	add(label);
+	registryEndpoint = new JTextField(50);
+	registryEndpoint.setEditable(false);
+	c.gridwidth = GridBagConstraints.REMAINDER;
+	c.weightx = 1.0;
+	gridbag.setConstraints(registryEndpoint, c);
+	add(registryEndpoint);
+
+	label = new JLabel("Namespace:");
+	c.gridwidth = 1;
+	c.weightx = 0.0;
+	gridbag.setConstraints(label, c);
+	add(label);
+	registryNamespace = new JTextField(50);
+	registryNamespace.setEditable(false);
+	c.gridwidth = GridBagConstraints.REMAINDER;
+	c.weightx = 1.0;
+	gridbag.setConstraints(registryNamespace, c);
+	add(registryNamespace);
+
+	JSeparator line = new JSeparator();
+	c.gridwidth = GridBagConstraints.REMAINDER; 
+	gridbag.setConstraints(line, c);
+
+	sendReferrerCheckBox = new JCheckBox("Send referrer data to services");
+	c.gridwidth = GridBagConstraints.REMAINDER; 
+	gridbag.setConstraints(sendReferrerCheckBox, c);
+	add(sendReferrerCheckBox);
+
+	line = new JSeparator();
+	c.gridwidth = GridBagConstraints.REMAINDER; 
+	gridbag.setConstraints(line, c);
+
+	JPanel expiryPanel = new JPanel(new GridLayout(1,0));
+	label = new JLabel("Cache expiry (in hours)");
+	expiryPanel.add(label);
+	cacheExpiryTextField = new JTextField(3);
+	expiryPanel.add(cacheExpiryTextField);
+	c.gridwidth = 1;
+	gridbag.setConstraints(expiryPanel, c);
+	add(expiryPanel);
+	deleteCacheButton = new JButton("Delete cached items");
+	deleteCacheButton.addActionListener(this);
+	c.gridwidth = 1;
+	gridbag.setConstraints(deleteCacheButton, c);
+	add(deleteCacheButton);
+
+	selectCacheDirButton = new JButton("Change cache directory");
+	selectCacheDirButton.addActionListener(this);
+	c.gridwidth = GridBagConstraints.REMAINDER;
+	gridbag.setConstraints(selectCacheDirButton, c);
+	add(selectCacheDirButton);
+	cacheDirFileChooser = new JFileChooser();
+	cacheDirFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+
+	line = new JSeparator();
+	c.gridwidth = GridBagConstraints.REMAINDER; 
+	gridbag.setConstraints(line, c);
+
+	resetButton = new JButton("Restore program defaults");
+	resetButton.addActionListener(this);
+	c.gridwidth = 1;
+	gridbag.setConstraints(resetButton, c);
+	add(resetButton);
+	cancelButton = new JButton("Cancel");
+	cancelButton.addActionListener(this);
+	c.gridwidth = 1;
+	gridbag.setConstraints(cancelButton, c);
+	add(cancelButton);
+	okButton = new JButton("Apply changes");
+	okButton.addActionListener(this);
+	c.gridwidth = GridBagConstraints.REMAINDER;
+	gridbag.setConstraints(okButton, c);
+	add(okButton);
+
+	setCurrentValues();
+	pack();
+    }
+
+    public void actionPerformed(ActionEvent event){
+	Object source = event.getSource();
+	if(source == cancelButton){
+	    setCurrentValues();
+	    setVisible(false);
+	}
+	else if(source == okButton){
+	    saveSettings();
+	}
+	else if(source == resetButton){
+	    setDefaultValues();
+	}
+	else if(source == selectCacheDirButton){
+	    int choice = cacheDirFileChooser.showOpenDialog(this);
+	    if (choice != JFileChooser.APPROVE_OPTION) {
+		cacheDirFileChooser.setSelectedFile(SeahawkOptions.getTempDir());
+	    }
+	}
+	else if(source == deleteCacheButton){
+	    RegistryCache.deleteAllCacheFiles();
+	    // Can we also delete the in-memory cache somehow?
+	}
+	else if(source == registryComboBox){
+	    updateRegistryFields();
+	}
+    }
     
+    /**
+     * Use the values defined by SeahawkOptions.
+     */
+    private void setCurrentValues(){
+	Registry currentRegistry = SeahawkOptions.getRegistry();
+	if(currentRegistry == null){
+	    registryComboBox.setSelectedItem(RegistriesList.DEFAULT_REGISTRY_SYNONYM);
+	}
+	else if(currentRegistry.getSynonym().equals(SeahawkOptions.CUSTOM_REGISTRY_SYNONYM)){
+	    registryComboBox.setSelectedItem(CUSTOM_REGISTRY_SYNONYM);  // a bit more verbose than SO.CRS
+	    lastCustomEndpoint = currentRegistry.getEndpoint();
+	    lastCustomNamespace = currentRegistry.getNamespace();
+	}
+	else{
+	    registryComboBox.setSelectedItem(currentRegistry.getSynonym());
+	}
+	updateRegistryFields(); 
+	sendReferrerCheckBox.setSelected(SeahawkOptions.getSendReferrerPolicy());
+	cacheExpiryTextField.setText(""+SeahawkOptions.getCacheExpiry());
+	cacheDirFileChooser.setSelectedFile(SeahawkOptions.getTempDir());
+    }
+
+    private void saveSettings(){
+	double expiry = SeahawkOptions.getCacheExpiry();
+	try{
+	    expiry = Double.parseDouble(cacheExpiryTextField.getText());
+	} catch(Exception e){
+	    JOptionPane.showMessageDialog(null, 
+					  "The cache expiry field is not a number,\nplease correct it to continue", 
+					  "Invalid Formatting", 
+					  JOptionPane.ERROR_MESSAGE);
+	    return;
+	}
+
+	Registry previousRegistry = SeahawkOptions.getRegistry();
+	String selectedRegistryName = registryComboBox.getSelectedItem().toString();
+	if(selectedRegistryName.equals(CUSTOM_REGISTRY_SYNONYM)){
+	    SeahawkOptions.setRegistry(new Registry(SeahawkOptions.CUSTOM_REGISTRY_SYNONYM,
+						    registryEndpoint.getText(),
+						    registryNamespace.getText()));
+	}
+	else{
+	    try{
+		SeahawkOptions.setRegistry(registriesList.get(selectedRegistryName));
+	    } catch(Exception e){
+		e.printStackTrace();
+		JOptionPane.showMessageDialog(null, 
+					      "The registry information could not be saved properly,\n" +
+					      "see the console for more details.  Aborting save.", 
+					      "Registry Specification Error", 
+					      JOptionPane.ERROR_MESSAGE);
+		return;
+	    }
+	}
+
+	// Update SeahawkOptions with the values from the form
+	try{
+	    SeahawkOptions.setTempDir(cacheDirFileChooser.getSelectedFile());
+	} catch(Exception e){
+	    e.printStackTrace();
+	    JOptionPane.showMessageDialog(null, 
+					  "Please select another directory for the cache.\n"+
+					  "The specified cache directory ("+
+					  cacheDirFileChooser.getSelectedFile()+
+					  ") is not suitable:\n"+
+					  e.getMessage(), 
+					  "Cache Specification Error", 
+					  JOptionPane.ERROR_MESSAGE);
+	    SeahawkOptions.setRegistry(previousRegistry); // Roll back the change
+	    return;
+	}
+
+	SeahawkOptions.setSendReferrerPolicy(sendReferrerCheckBox.isSelected());
+	SeahawkOptions.setCacheExpiry(expiry);
+
+	// Saves the new values to a file, so they will perpetuate themselves between sessions
+	if(!SeahawkOptions.saveSettings()){
+	    JOptionPane.showMessageDialog(null, 
+					  "Sorry, but there was an error saving your preferences.\n"+
+					  "Please see the console for more details.", 
+					  "Error Saving Preferences", 
+					  JOptionPane.ERROR_MESSAGE);
+	}
+	setVisible(false);
+    }
+
+    private void setDefaultValues(){
+	// Restore the program defaults in subsequent JVM's by deleting the preferences file! 
+	java.io.File defaultsFile = SeahawkOptions.getDefaultsFile();
+	if(defaultsFile.exists()){
+	    defaultsFile.delete();
+	}
+
+	// We also need to reset the current session...TODO
+	JOptionPane.showMessageDialog(null, 
+				      "Please restart Seahawk to continue with the default settings.", 
+				      "Custom Settings Deleted", 
+				      JOptionPane.INFORMATION_MESSAGE);
+	setVisible(false);
+    }
+
+    private void updateRegistryFields(){
+	// Update all the registry fields if the name switches
+	String selectedRegistryName = registryComboBox.getSelectedItem().toString();
+	if(selectedRegistryName.equals(CUSTOM_REGISTRY_SYNONYM)){
+	    registryDescTextArea.setText(CUSTOM_REGISTRY_DESC);
+	    registryEndpoint.setEditable(true);
+	    registryEndpoint.setText(lastCustomEndpoint);
+	    registryNamespace.setEditable(true);
+	    registryNamespace.setText(lastCustomNamespace);
+	    pack();
+	    return;
+	}
+	if(registryDescTextArea.getText().equals(CUSTOM_REGISTRY_DESC)){
+	    lastCustomEndpoint = registryEndpoint.getText();
+	    lastCustomNamespace = registryNamespace.getText();
+	}
+	
+	try{
+	    Registry selectedRegistry = registriesList.get(registryComboBox.getSelectedItem().toString());
+	    String desc = selectedRegistry.getDescription();
+	    String newDesc = "";
+	    if(desc != null){
+		int charCount = 0;
+		int curLine = 0;
+		for(String word: desc.split(" ")){
+		    charCount += word.length() + 1;
+		    if(charCount/MAX_DESC_WIDTH > curLine){
+			curLine++;
+			charCount--;
+			newDesc += "\n";
+		    }
+		    newDesc += word + " ";
+		}
+	    }		
+	    registryDescTextArea.setText(newDesc);
+	    registryEndpoint.setText(selectedRegistry.getEndpoint());
+	    registryEndpoint.setEditable(false);
+	    registryNamespace.setText(selectedRegistry.getNamespace());
+	    registryNamespace.setEditable(false);
+	} catch(Exception e){
+	    e.printStackTrace();
+	    return;
+	}
+
+	pack();
+    }
+
+
+    public void windowActivated(WindowEvent e){}
+    public void windowClosed(WindowEvent e){}
+    /**
+     * Invoked when the user attempts to close the window from the window's system menu.
+     * Implemented as resetting the fields to their original values, and closing the
+     * window (same effect as the "Cancel" button).
+     */
+    public void windowClosing(WindowEvent e){
+	setCurrentValues();
+	setVisible(false);
+    }
+    public void windowDeactivated(WindowEvent e){}
+    public void windowDeiconified(WindowEvent e){}
+    public void windowIconified(WindowEvent e){}
+    public void windowOpened(WindowEvent e){}
 }




More information about the MOBY-guts mailing list