001: package snow.utils.gui;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006:
007: /** A simple utility to add a close button in your dialogs or frames.
008: * use getWasAccepted() AND/OR getWasCancelled() to know which close type occured.
009: * <P> if you want to ensure that the user accepted it, check getWasAccepted() if you
010: * just want to know if the user explicitely cancelled it => getWasCancelled().
011: *<P> Don't forget that the user nurmally can also close the dialog with the upper right
012: * close button in the frame title OR ALT+F4.
013: * You can explicitely defend through setting of the (JDialog dialog).setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
014: */
015: public class CloseControlPanel extends JPanel {
016: private final JButton cancelBT = new JButton("Cancel",
017: Icons.sharedCross);
018:
019: private final JButton okBT = new JButton("Close");
020:
021: private boolean accepted = false;
022: private boolean cancelled = false;
023:
024: public JButton getOkButton() {
025: return okBT;
026: }
027:
028: public boolean getWasAccepted() {
029: return accepted;
030: }
031:
032: public boolean getWasCancelled() {
033: return cancelled;
034: }
035:
036: final Object frame;
037:
038: public CloseControlPanel(final Object frame, boolean showCancel,
039: final boolean shouldCancelCallClose, final String okText) {
040: super (new FlowLayout(FlowLayout.RIGHT));
041: this .frame = frame;
042:
043: setOpaque(false);
044:
045: add(cancelBT);
046: if (!showCancel)
047: cancelBT.setVisible(false);
048:
049: okBT.setText(okText);
050: add(okBT);
051: okBT.setBackground(Color.orange);
052:
053: if (okText.equalsIgnoreCase("ok") || showCancel) {
054: okBT.setIcon(Icons.sharedOk); // otherwise, the text may be "close" and it's not nice to see an Ok icon.
055: }
056:
057: okBT.addActionListener(new ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: accepted = true;
060: cancelled = false;
061: closeFrame();
062: }
063: });
064: cancelBT.addActionListener(new ActionListener() {
065: public void actionPerformed(ActionEvent e) {
066: cancelled = true;
067: accepted = false;
068: if (shouldCancelCallClose) {
069: closeFrame();
070: }
071: }
072: });
073:
074: okBT.setMargin(new Insets(0, 2, 0, 2));
075: cancelBT.setMargin(new Insets(0, 2, 0, 2));
076:
077: JComponent parentComp = null;
078: if (frame instanceof JFrame) {
079: try {
080: parentComp = (JComponent) ((JFrame) frame)
081: .getContentPane();
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: }
085: } else if (frame instanceof JDialog) {
086: try {
087: parentComp = (JComponent) ((JDialog) frame)
088: .getContentPane();
089: } catch (Exception ex) {
090: ex.printStackTrace();
091: }
092: } else if (frame instanceof JInternalFrame) {
093: try {
094: parentComp = (JComponent) ((JInternalFrame) frame)
095: .getContentPane();
096: } catch (Exception ex) {
097: ex.printStackTrace();
098: }
099: } else {
100: if (frame == null) {
101: new Throwable("null frame").printStackTrace();
102: } else {
103: new Throwable("invalid frame "
104: + frame.getClass().getName()).printStackTrace();
105: }
106: }
107:
108: if (parentComp != null) {
109: parentComp.registerKeyboardAction(new ActionListener() {
110: public void actionPerformed(ActionEvent ae) {
111: cancelAndClose();
112: }
113: }, "Escape", KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0,
114: false),
115: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
116: }
117:
118: } // Constructor
119:
120: private void closeFrame() {
121: if (frame instanceof JFrame) {
122: ((JFrame) frame).setVisible(false);
123: } else if (frame instanceof JDialog) {
124: ((JDialog) frame).setVisible(false);
125: }
126: }
127:
128: public void acceptAndClose() {
129: accepted = true;
130: cancelled = false;
131: closeFrame();
132: }
133:
134: public void cancelAndClose() {
135: accepted = false;
136: cancelled = true;
137: closeFrame();
138: }
139:
140: public void _setOkButtonText(String txt) {
141: okBT.setText(txt);
142: }
143:
144: public void setOkEnabled(boolean is) {
145: okBT.setEnabled(is);
146: }
147:
148: public void setOkBackground(Color c) {
149: okBT.setBackground(c);
150: }
151:
152: }
|