package jas2.swingstudio;
import jas2.util.*;
import jas2.jds.interfaces.CutProperties;
import jas2.jds.interfaces.CutInterface;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

abstract public class CutAdjustmentDialog extends JASDialog implements CutPropertiesDialog
{
	/**
	 * A dialog that allows the user to change parameters of a cut.
	 * Subclasses must take the name type + "CutAdjustmentDialog" where "type"
	 * is what's returned by the partition's getType()
	 * method, and subclasses must be in the jas2.cuts package.
	 * Subclass constructors must take two arguments: CutPeer and CutProperties.
	 * See the DoubleCut as an example.
	 *  @see jas2.cuts.DoubleCutAdjustmentDialog
	 *  @param peer the cut to modify
	 *  @param initialProperties the current properties of the cut
	 *  @param panel a properties panel that already reflects the
	 *         properties in the object specified by the parameter
	 *         initialProperties
	 */
	public CutAdjustmentDialog(final CutInterface peer, final CutProperties initialProperties,
		final CutPropertiesPanel panel)
	{
		super(Application.getApplication().getFrame(), "Cut parameters", false,
			OK_BUTTON | CANCEL_BUTTON | APPLY_BUTTON | HELP_BUTTON);

		// set up general things:
		setApplyLabel("Apply and run");
		m_oldProperties = initialProperties;
		m_peer = peer;
		m_panel = panel;
		final Container c = getContentPane();
		c.setLayout(new GridBagLayout());
		final GridBagConstraints gbc = new GridBagConstraints();
		panel.m_dialog = this;

		// add properties panel:
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.BOTH;
		c.add(panel, gbc);

		// add check boxes:
		final JPanel boxesPanel = new JPanel(null);
		boxesPanel.setLayout(new BoxLayout(boxesPanel, BoxLayout.Y_AXIS));
		boxesPanel.add(m_showPlot);
		final JCheckBox showSlider = new JCheckBox("Show slider", false);
		boxesPanel.add(showSlider);
		boxesPanel.add(m_autoRun);
		boxesPanel.setBorder(new javax.swing.border.EtchedBorder());
		gbc.gridx = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.NORTH;
		c.add(boxesPanel, gbc);

		// details of check boxes:
		final boolean hasPlot = initialProperties.m_hasPartition && (this instanceof HasPlot);
		final boolean hasSlider = (this instanceof HasSlider);
		m_showPlot.setEnabled(hasPlot);
		showSlider.setEnabled(hasSlider);
		if (this instanceof HasPlot)
		{
			m_showPlot.addItemListener(new ItemListener()
			{
				public void itemStateChanged(ItemEvent e)
				{
					final Point p = getLocation();
					if (e.getStateChange() == ItemEvent.SELECTED) // we must add the panel
					{
						if (m_plot == null)
						{
							m_plot = ((HasPlot) CutAdjustmentDialog.this).getPlotPanel(m_peer);
							m_plot.m_width = c.getSize().width;
							m_const = new GridBagConstraints();
							m_const.gridx = 0;
							m_const.gridy = 0;
							m_const.gridwidth = 2;
							m_const.fill = GridBagConstraints.BOTH;
						}
						if (! m_plot.beforeShowing()) return;
						c.add(m_plot, m_const);
						defaultPack();
						p.y -= m_plot.getSize().height;
						if (p.y < 0) p.y = 0;
					}
					else if (e.getStateChange() == ItemEvent.DESELECTED) // we must remove the panel
					{
						c.remove(m_plot);
						defaultPack();
						p.y += m_plot.getSize().height;
						m_plot.afterClosing();
					}
					setLocation(p);
				}
				private CutDialogPanel m_plot;
				private GridBagConstraints m_const;
			});
		}
		if (hasSlider)
		{
			showSlider.addItemListener(new ItemListener()
			{
				public void itemStateChanged(ItemEvent e)
				{
					if (e.getStateChange() == ItemEvent.SELECTED) // we must add the panel
					{
						if (m_slider == null)
						{
							m_slider = ((HasSlider) CutAdjustmentDialog.this).getSliderPanel(m_peer);
							m_slider.m_width = c.getSize().width;
							m_const = new GridBagConstraints();
							m_const.gridx = 0;
							m_const.gridy = 2;
							m_const.gridwidth = 2;
							m_const.fill = GridBagConstraints.BOTH;
						}
						if (! m_slider.beforeShowing()) return;
						c.add(m_slider, m_const);
					}
					else if (e.getStateChange() == ItemEvent.DESELECTED) // we must remove the panel
					{
						c.remove(m_slider);
						m_slider.afterClosing();
					}
					defaultPack();
				}
				private CutDialogPanel m_slider;
				private GridBagConstraints m_const;
			});
		}
		setResizable(false);
		pack();
	}

	/**
	 * Call when the properties of the cut have changed, so that the apply and OK
	 * buttons can enable.  Should be called automatically when
	 * changeNotify() is called in a CutPropertiesPanel.
	 *  @param reRunJob whether the change was significant enough to merit re-running
	 *                  the job if that option is selected
	 *  @see CutPropertiesPanel
	 *  @see CutPropertiesPanel#changeNotify()
	 */
	public final void changeNotify(final boolean reRunJob)
	{
		if (reRunJob && m_autoRun.isSelected())
			onApply();
		m_changed = true;
		callEnable();
	}

	/** protected as implementation side effect; do not call */
	final protected void onApply()
	{
		if (setProperties())
			callEnable();
	}
	/** protected as implementation side effect; do not call */
	final protected void enableApply(JASState state)
	{
		JASJob job = JavaAnalysisStudio.getApp().getJob();
		state.setEnabled(m_changed && job != null && job.getJob().isRunnable());
	}
	/** protected as implementation side effect; do not call */
	final protected void enableOK(JASState state)
	{
		state.setEnabled(m_changed);
	}
	/** protected as implementation side effect; do not call */
	final protected void onOK()
	{
		if (setProperties())
		{
			Application.getApplication().getUserProperties().setBoolean(m_auto_Key, m_autoRun.isSelected());
			super.onOK();
		}
	}
	final protected CutPropertiesPanel getPropertiesPanel()
	{
		return m_panel;
	}
	// try to apply the dialog's settings to the peer
	// return whether they were set (false indicates error in user input)
	private boolean setProperties()
	{
		final CutProperties prop = m_panel.getProperties();
		if (prop != null && !prop.equals(m_oldProperties))
		{
			m_oldProperties = prop;
			m_changed = false;
			m_peer.setProperties(prop);
			m_showPlot.setEnabled(prop.m_hasPartition && this instanceof HasPlot);
			return true;
		}
		return false;
	}
	private boolean m_changed = false;
	private CutPropertiesPanel m_panel;
	private CutInterface m_peer;
	private CutProperties m_oldProperties;

	final JCheckBox m_showPlot = new JCheckBox("Show plot", false);
	final private String m_auto_Key = "AutoRun";
	final private JCheckBox m_autoRun = new JCheckBox("Run job on change",
		Application.getApplication().getUserProperties().getBoolean(m_auto_Key, false));
}

 

Ads help maintain this website.