package net.sf.jpluck.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import net.sf.jpluck.swing.GUIUtil;
import net.sf.jpluck.util.Platform;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.Sizes;
public abstract class StandardDialog extends JDialog {
private CancelAction cancelAction = new CancelAction();
private boolean cancelled = false;
public StandardDialog(JFrame owner) {
super(owner);
}
public StandardDialog(JDialog owner) {
super(owner);
}
public boolean isCancelled() {
return cancelled;
}
protected void cancel() {
}
protected void initButtonBar(ButtonBarBuilder builder) {
}
protected void initDialog(Banner banner, JComponent mainComponent) {
FormLayout formLayout = new FormLayout("pref, 6dlu, pref");
PanelBuilder mainBuilder = new PanelBuilder(formLayout);
JButton okButton = new JButton(new OKAction());
JButton cancelButton = new JButton(cancelAction);
GUIUtil.configureEscapeKey(getRootPane(), cancelAction);
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
cancelAction.actionPerformed(null);
}
});
getRootPane().setDefaultButton(okButton);
ButtonBarBuilder buttonBuilder = new ButtonBarBuilder();
buttonBuilder.setBorder(Borders.createEmptyBorder(Sizes.DLUY4, Sizes.DLUX7, Sizes.DLUY7, Sizes.DLUX7));
initButtonBar(buttonBuilder);
buttonBuilder.addGlue();
if (Platform.isMacOSXAqua()) {
buttonBuilder.addGridded(cancelButton);
buttonBuilder.addRelatedGap();
buttonBuilder.addGridded(okButton);
} else {
buttonBuilder.addGridded(okButton);
buttonBuilder.addRelatedGap();
buttonBuilder.addGridded(cancelButton);
}
Container c = getContentPane();
c.setLayout(new BorderLayout());
if (banner != null) {
c.add(banner, BorderLayout.NORTH);
}
c.add(mainComponent, BorderLayout.CENTER);
c.add(buttonBuilder.getPanel(), BorderLayout.SOUTH);
setResizable(false);
setModal(true);
pack();
}
protected abstract boolean ok();
private class CancelAction extends AbstractAction {
CancelAction() {
super("Cancel");
}
public void actionPerformed(ActionEvent ev) {
cancel();
cancelled = true;
hide();
}
}
private class OKAction extends AbstractAction {
OKAction() {
super("OK");
}
public void actionPerformed(ActionEvent ev) {
if (ok()) {
hide();
}
}
}
}