[jmsl] DialogAdapter class (again)

jmsl at music.columbia.edu jmsl at music.columbia.edu
Thu Jul 10 14:49:44 EDT 2008


Whoops, tried to send this as HTML

For those of you who find yourselves using dialogs often, here's an  
adapter that I created.  It takes advantage of the Preferences API to  
store information about the location of the dialog as well as the  
previous state of the controls.  Any feedback on this is welcome.

thanks,
Peter McCulloch


DialogAdapter.java
----------------------------------------------------------

package com.petermcculloch.megalo.gui.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.LineBorder;

/**
  * @author peter
  *
  */
/**
  * @author peter
  *
  */
public class DialogAdapter extends JDialog {

	/**
	 *
	 */
	private static final long	serialVersionUID	= 1L;

	public static final String	DIALOG_WIDTH		= "Dialog Width";
	public static final String	DIALOG_HEIGHT		= "Dialog Height";
	public static final String	DIALOG_Y			= "Dialog Y Position";
	public static final String	DIALOG_X			= "Dialog X Position";

	private JOptionPane			okCancelPane;

	private JPanel				panel;

	Preferences					prefs;

	public DialogAdapter() {
		super();
		System.out.println("Use the preferences constructor...");
		initGUI();
	}

	public DialogAdapter(Preferences prefs) {
		super();
		this.prefs = prefs;
		initGUI();
	}

	private void initGUI() {
		getContentPane().add(getPanel(), BorderLayout.CENTER);
		getContentPane().add(getOkCancelPane(), BorderLayout.SOUTH);

		if (prefs != null) {
			this.setLocation(prefs.getInt(DIALOG_X, 0), prefs.getInt(DIALOG_Y,  
0));
			setPreferredSize(new Dimension(prefs.getInt(DIALOG_WIDTH, 275),  
prefs.getInt(
				DIALOG_HEIGHT, 200)));
			okCancelPane.setPreferredSize(new  
java.awt.Dimension(Math.max(prefs.getInt(
				DIALOG_WIDTH, 275), 150), 49));
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Use this to test preferences
		Preferences prefs =  
Preferences.userNodeForPackage(DialogAdapter.class);

		JFrame frame = new JFrame();
		frame.setLayout(new FlowLayout());
		JButton button = new JButton("Show dialog");
		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Preferences prefs =  
Preferences.userNodeForPackage(DialogAdapter.class);
				DialogAdapter template = new DialogAdapter(prefs) {

					JToggleButton	button;

					// @Override
					// public JPanel getPanel() {
					// JPanel panel = new JPanel();
					// button = new JToggleButton("Foo / Bar");
					// if (getPreferences() != null) {
					// button.getModel().setSelected(
					// getPreferences().getBoolean("Foo Bar", false));
					// }
					// panel.add(button);
					// return panel;
					// }
					//
					// @Override
					// public void selectOk() {
					// getPreferences().putBoolean("Foo Bar",
					// button.getModel().isSelected());
					// }

				};
				template.pack();
				template.setVisible(true);
			}

		});
		frame.add(button);
		frame.pack();
		frame.setVisible(true);

	}

	/**
	 * This gets the okCancelPane. It also adds a propertyChangeListener to
	 * okCancelPane. The listener references the methods selectCancel() and
	 * selectOk() respectively. To customize behavior, override these  
methods.
	 *
	 * @return
	 */
	public JOptionPane getOkCancelPane() {
		if (okCancelPane == null) {
			okCancelPane = new JOptionPane();
			okCancelPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
			okCancelPane.setMessage("");

			okCancelPane.addPropertyChangeListener(new PropertyChangeListener() {

				public void propertyChange(PropertyChangeEvent evt) {
					if (evt.getPropertyName().equals("value")) {
						if (evt.getNewValue().equals(JOptionPane.OK_OPTION)) {
							// Do selectOk, then store prefs for position. If
							// you wanted to store preferences for the state of
							// panel, you would need to handle putting them into
							// the prefs there
							selectOk();

							if (prefs != null) {
								prefs.putInt(DIALOG_X, getLocation().x);
								prefs.putInt(DIALOG_Y, getLocation().y);
								prefs.putInt(DIALOG_HEIGHT, getHeight());
								prefs.putInt(DIALOG_WIDTH, getWidth());
								try {
									prefs.flush();
								} catch (BackingStoreException e) {
									JOptionPane.showMessageDialog(new JFrame(),
										"Backing store not found!", "Error",
										JOptionPane.ERROR_MESSAGE);
									e.printStackTrace();
								}
							}
						} else {
							selectCancel();
						}
						nullElements();
						dispose();

					}

				}

			});
		}
		return okCancelPane;
	}

	// Is this necessary?
	private void nullElements() {
		okCancelPane = null;
		panel = null;
	}

	// Stub method to override
	public void selectOk() {
		System.out.println("Stub method for ok to be overridden");
	}

	public void selectCancel() {
		this.dispose();
	}

	/**
	 * Override this method to add controls
	 *
	 * @return
	 */
	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBorder(new LineBorder(Color.red, 2));
			panel
				.add(new JLabel(
					"<HTML>Test area.  You should override <B>getPanel()</B><BR>to  
add your own controls here.</HTML>"));
		}
		return panel;
	}

	public Preferences getPreferences() {
		return prefs;
	}

	public void setPreferences(Preferences prefs) {
		this.prefs = prefs;
	}

}



More information about the jmsl mailing list