001: package com.opensymphony.workflow.designer.dialogs;
002:
003: import java.awt.event.*;
004: import java.awt.*;
005: import javax.swing.*;
006: import javax.swing.border.Border;
007:
008: import com.jgoodies.forms.factories.ButtonBarFactory;
009: import com.opensymphony.workflow.designer.swing.BannerPanel;
010: import com.opensymphony.workflow.designer.Utils;
011: import com.opensymphony.workflow.designer.ResourceManager;
012:
013: /**
014: * @author Hani Suleiman (hani@formicary.net)
015: * Date: Jan 11, 2004
016: * Time: 5:28:16 PM
017: */
018: public class BaseDialog extends JDialog {
019: private BannerPanel banner;
020: private JPanel contentPane;
021: private boolean cancelClicked;
022: public final static Border WINDOW_BORDER = BorderFactory
023: .createEmptyBorder(4, 10, 10, 10);
024:
025: private Action okAction = new AbstractAction() {
026: public void actionPerformed(ActionEvent e) {
027: ok();
028: }
029: };
030:
031: private Action cancelOrCloseAction = new AbstractAction() {
032: public void actionPerformed(ActionEvent e) {
033: cancel();
034: }
035: };
036: private static final Border CONTENT_BORDER = BorderFactory
037: .createEmptyBorder(3, 3, 3, 3);
038:
039: public BaseDialog() throws HeadlessException {
040: super ();
041: buildUI();
042: }
043:
044: public BaseDialog(Frame owner) throws HeadlessException {
045: super (owner);
046: buildUI();
047: }
048:
049: public BaseDialog(Frame owner, boolean modal)
050: throws HeadlessException {
051: super (owner, modal);
052: buildUI();
053: }
054:
055: public BaseDialog(Frame owner, String title)
056: throws HeadlessException {
057: super (owner, title);
058: buildUI();
059: }
060:
061: public BaseDialog(Frame owner, String title, boolean modal)
062: throws HeadlessException {
063: super (owner, title, modal);
064: buildUI();
065: }
066:
067: public final BannerPanel getBanner() {
068: return banner;
069: }
070:
071: public final Container getContentPane() {
072: return contentPane;
073: }
074:
075: /**
076: * Returns true if OK was clicked, false if CANCEL or CLOSE was clicked
077: */
078: public boolean ask() {
079: show();
080: return !cancelClicked;
081: }
082:
083: /**
084: * Returns true if OK was clicked, false if CANCEL or CLOSE was clicked
085: */
086: public boolean ask(Component parent) {
087: show(parent);
088: return !cancelClicked;
089: }
090:
091: protected void ok() {
092: cancelClicked = false;
093: setVisible(false);
094: }
095:
096: protected void cancel() {
097: cancelClicked = true;
098: setVisible(false);
099: }
100:
101: private void buildUI() {
102: JPanel container = (JPanel) super .getContentPane();
103: container.setLayout(new BorderLayout(0, 0));
104: banner = new BannerPanel();
105: container.add(banner, BorderLayout.NORTH);
106:
107: JPanel mainPanel = new JPanel();
108: mainPanel.setBorder(WINDOW_BORDER);
109: mainPanel.setLayout(new BorderLayout());
110: container.add(mainPanel, BorderLayout.CENTER);
111:
112: contentPane = new JPanel();
113: contentPane.setLayout(new BorderLayout(3, 3));
114: contentPane.setBorder(CONTENT_BORDER);
115: mainPanel.add(contentPane, BorderLayout.CENTER);
116:
117: JButton ok = new JButton(ResourceManager.getString("ok"));
118: ok.setDefaultCapable(true);
119: getRootPane().setDefaultButton(ok);
120: ok.addActionListener(okAction);
121: JButton cancel = new JButton(ResourceManager
122: .getString("cancel"));
123: cancel.addActionListener(cancelOrCloseAction);
124: mainPanel.add(ButtonBarFactory.buildOKCancelBar(ok, cancel),
125: BorderLayout.SOUTH);
126:
127: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
128:
129: container.getActionMap().put("cancel", cancelOrCloseAction);
130: container.getInputMap(
131: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
132: .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
133: "cancel");
134: addWindowListener(new WindowAdapter() {
135: public void windowClosing(WindowEvent e) {
136: cancel();
137: }
138: });
139: }
140:
141: /**
142: * Show the dialog.
143: * This method will pack the dialog and center it
144: * relative to the specified parent.
145: * @param parent
146: */
147: public void show(Component parent) {
148: pack();
149: if (parent == null) {
150: Utils.centerComponent(this);
151: } else {
152: Utils.centerComponent(parent, this);
153: }
154: super.show();
155: }
156:
157: }
|