Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00017 package com.generalrobotix.ui.util;
00018
00019 import java.awt.AWTEventMulticaster;
00020 import java.awt.BorderLayout;
00021 import java.awt.Component;
00022 import java.awt.Dialog;
00023 import java.awt.Frame;
00024 import java.awt.GridBagConstraints;
00025 import java.awt.GridBagLayout;
00026 import java.awt.Window;
00027 import java.awt.event.ActionEvent;
00028 import java.awt.event.ActionListener;
00029 import java.awt.event.WindowEvent;
00030
00031 import javax.swing.Box;
00032 import javax.swing.BoxLayout;
00033 import javax.swing.JButton;
00034 import javax.swing.JDialog;
00035 import javax.swing.JLabel;
00036 import javax.swing.JPanel;
00037 import javax.swing.SwingUtilities;
00038 import javax.swing.border.EmptyBorder;
00039
00040
00044 @SuppressWarnings("serial")
00045 public class ModalDialog extends JDialog {
00046
00047
00048
00049
00050 public static final int NONE_TYPE = 0;
00051 public static final int OK_TYPE = 1;
00052 public static final int OK_CANCEL_TYPE = 2;
00053 public static final int YES_NO_TYPE = 3;
00054 public static final int YES_NO_CANCEL_TYPE = 4;
00055
00056
00057 public static final int INLINE_CAPTION = 1;
00058 public static final int MULTILINE_CAPTION = 2;
00059
00060
00061 public static final int CUSTOM_BUTTON = 0;
00062 public static final int OK_BUTTON = 1;
00063 public static final int CANCEL_BUTTON = 2;
00064 public static final int YES_BUTTON = 3;
00065 public static final int NO_BUTTON = 4;
00066 public static final int CLOSE_BUTTON = 5;
00067
00068
00069 private static final int BUTTON_GAP = 5;
00070 private static final int CAPTION_GAP = 11;
00071
00072
00073
00074 private int width_;
00075
00076 protected int buttonType_;
00077 protected String buttonCommand_;
00078 protected boolean closeButtonEnabled_ = true;
00079
00080 protected ActionListener listener_;
00081 protected Window owner_;
00082 protected JPanel inputArea_;
00083 protected JPanel buttonArea_;
00084 protected JPanel mainPanel_;
00085 protected GridBagConstraints gbc_;
00086
00087
00088
00089 public ModalDialog() {}
00090
00091 public ModalDialog(
00092 Frame owner,
00093 String title,
00094 String message,
00095 int buttonType
00096 ) {
00097 super(owner, title, true);
00098 owner_ = owner;
00099 _init(title, message, buttonType);
00100 }
00101
00102 public ModalDialog(
00103 Dialog owner,
00104 String title,
00105 String message,
00106 int buttonType
00107 ) {
00108 super(owner, title, true);
00109 owner_ = owner;
00110 _init(title, message, buttonType);
00111 }
00112
00113 private void _init(String title, String message, int buttonType) {
00114 JLabel messageLabel = new JLabel(message);
00115
00116
00117 inputArea_ = new JPanel();
00118 inputArea_.setLayout(new GridBagLayout());
00119 inputArea_.setBorder(new EmptyBorder(6, 12, 0, 0));
00120 gbc_ = new GridBagConstraints();
00121
00122 listener_ = new ActionListener() {
00123 public void actionPerformed(ActionEvent evt) {
00124 InnerButton button = (InnerButton)evt.getSource();
00125 buttonType_ = button.getButtonType();
00126 buttonCommand_ = evt.getActionCommand();
00127 SwingUtilities.invokeLater(
00128 new Runnable() {
00129 public void run() {
00130 ModalDialog.this.dispose();
00131 }
00132 }
00133 );
00134 }
00135 };
00136
00137 buttonArea_ = new JPanel();
00138 buttonArea_.setBorder(new EmptyBorder(0, 0, 11, 11));
00139 buttonArea_.setLayout(new BoxLayout(buttonArea_, BoxLayout.X_AXIS));
00140 buttonArea_.setAlignmentX(Component.LEFT_ALIGNMENT);
00141 buttonArea_.add(Box.createHorizontalGlue());
00142
00143 InnerButton okButton;
00144 switch (buttonType) {
00145 case NONE_TYPE:
00146 break;
00147 case OK_TYPE:
00148 okButton = new InnerButton(OK_BUTTON);
00149 getRootPane().setDefaultButton(okButton);
00150 buttonArea_.add(okButton);
00151 break;
00152 case OK_CANCEL_TYPE:
00153 okButton = new InnerButton(OK_BUTTON);
00154 getRootPane().setDefaultButton(okButton);
00155 buttonArea_.add(okButton);
00156 buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
00157 buttonArea_.add(new InnerButton(CANCEL_BUTTON));
00158 break;
00159 case YES_NO_TYPE:
00160 buttonArea_.add(new InnerButton(YES_BUTTON));
00161 buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
00162 buttonArea_.add(new InnerButton(NO_BUTTON));
00163 break;
00164 case YES_NO_CANCEL_TYPE:
00165 buttonArea_.add(new InnerButton(YES_BUTTON));
00166 buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
00167 buttonArea_.add(new InnerButton(NO_BUTTON));
00168 buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
00169 buttonArea_.add(new InnerButton(CANCEL_BUTTON));
00170 break;
00171 }
00172
00173 mainPanel_ = new JPanel();
00174 mainPanel_.setLayout(new BorderLayout());
00175 mainPanel_.setBorder(new EmptyBorder(12, 12, 17, 11));
00176 mainPanel_.add(messageLabel, BorderLayout.NORTH);
00177 mainPanel_.add(inputArea_, BorderLayout.CENTER);
00178
00179 getContentPane().add(mainPanel_, BorderLayout.CENTER);
00180 getContentPane().add(buttonArea_, BorderLayout.SOUTH);
00181 setResizable(false);
00182 }
00183
00184
00185
00186 public void setInputAreaWidth(int width) {
00187 width_ = width;
00188 gbc_.gridwidth = GridBagConstraints.REMAINDER;
00189 gbc_.fill = GridBagConstraints.HORIZONTAL;
00190 inputArea_.add(Box.createHorizontalStrut(width_), gbc_);
00191
00192 }
00193
00194 public int getInputAreaWidth() {
00195 return width_;
00196 }
00197
00199 public void addInputComponent(
00200 String caption,
00201 Component component,
00202 int location,
00203 boolean fill
00204 ) {
00205 gbc_.gridwidth = GridBagConstraints.REMAINDER;
00206 gbc_.fill = GridBagConstraints.HORIZONTAL;
00207 inputArea_.add(Box.createVerticalStrut(11), gbc_);
00208
00209 gbc_.anchor = GridBagConstraints.NORTHWEST;
00210 gbc_.fill = GridBagConstraints.NONE;
00211
00212 JLabel label;
00213
00214 if (caption == null || caption.equals("")) {
00215 label = new JLabel("");
00216 } else {
00217 label = new JLabel(caption + " :");
00218 }
00219
00220 switch (location) {
00221 case INLINE_CAPTION:
00222 gbc_.gridwidth = 2;
00223 inputArea_.add(label, gbc_);
00224 gbc_.gridwidth = 1;
00225 inputArea_.add(Box.createHorizontalStrut(CAPTION_GAP));
00226 break;
00227 case MULTILINE_CAPTION:
00228 gbc_.gridwidth = GridBagConstraints.REMAINDER;
00229 inputArea_.add(label, gbc_);
00230
00231 gbc_.gridwidth = GridBagConstraints.REMAINDER;
00232 gbc_.fill = GridBagConstraints.HORIZONTAL;
00233 inputArea_.add(Box.createVerticalStrut(6), gbc_);
00234
00235 gbc_.gridwidth = 1;
00236 inputArea_.add(Box.createHorizontalStrut(12));
00237 break;
00238 default:
00239 return;
00240 }
00241
00242 gbc_.anchor = GridBagConstraints.NORTHWEST;
00243 gbc_.gridwidth = GridBagConstraints.REMAINDER;
00244 if (fill) {
00245 gbc_.fill = GridBagConstraints.HORIZONTAL;
00246 } else {
00247 gbc_.fill = GridBagConstraints.NONE;
00248 }
00249
00250 inputArea_.add(component, gbc_);
00251 }
00252
00253 public void addButton(String caption) {
00254 buttonArea_.add(Box.createHorizontalStrut(BUTTON_GAP));
00255 InnerButton button = new InnerButton(CUSTOM_BUTTON);
00256 button.setCaption(caption);
00257 buttonArea_.add(button);
00258 }
00259
00260
00261 public int showModalDialog() {
00262 setModal(true);
00263 pack();
00264 setLocationRelativeTo(owner_);
00265 setVisible(true);
00266 return buttonType_;
00267 }
00268
00269 public void showNoWait() {
00270 setModal(false);
00271 owner_.setEnabled(false);
00272 pack();
00273 setLocationRelativeTo(owner_);
00274 setVisible(true);
00275 }
00276
00277 public void dispose() {
00278 super.dispose();
00279 owner_.setEnabled(true);
00280 }
00281
00282 public void addActionListener(ActionListener listener) {
00283 listener_ = AWTEventMulticaster.add(listener_, listener);
00284 }
00285
00286 public void removeActionListener(ActionListener listener) {
00287 listener_ = AWTEventMulticaster.remove(listener_, listener);
00288 }
00289
00290 public void setCloseButtonEnabled(boolean enabled) {
00291 closeButtonEnabled_ = enabled;
00292 }
00293
00294 public void processWindowEvent(WindowEvent evt) {
00295 if (closeButtonEnabled_) {
00296 super.processWindowEvent(evt);
00297 }
00298 }
00299
00300
00301
00302 protected class InnerButton extends JButton {
00303 private int type_;
00304
00305 public InnerButton(int type) {
00306 type_ = type;
00307 switch (type_) {
00308 case CUSTOM_BUTTON:
00309 break;
00310 case OK_BUTTON:
00311 setCaption(MessageBundle.get("dialog.okButton"));
00312 break;
00313 case CANCEL_BUTTON:
00314 setCaption(MessageBundle.get("dialog.cancelButton"));
00315 break;
00316 case YES_BUTTON:
00317 setCaption(MessageBundle.get("dialog.yesButton"));
00318 break;
00319 case NO_BUTTON:
00320 setCaption(MessageBundle.get("dialog.noButton"));
00321 break;
00322 }
00323
00324 this.addActionListener(ModalDialog.this.listener_);
00325 }
00326
00327 void setCaption(String caption) {
00328 setText(caption);
00329 setActionCommand(caption);
00330 }
00331
00332 public int getButtonType() { return type_; }
00333 }
00334 }