1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// $Id: Dialog.java,v 1.3 2005/12/09 18:54:09 rastaman Exp $
/*
* Dialog.java
*
* Created on 11 June 2003, 22:54
*/
package org.tigris.swidgets;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
/**
* Base class for all dialogs, setting borders and component spacing.
*
* @author Bob Tarling
* @author Jeremy Jones
*/
public abstract class Dialog extends JDialog implements ActionListener {
// The set of available optionTypes
/** Option type: CLOSE_OPTION */
public static final int CLOSE_OPTION = 0;
/** Option type: YES_NO_OPTION */
public static final int YES_NO_OPTION = 1;
/** Option type: YES_NO_HELP_OPTION */
public static final int YES_NO_HELP_OPTION = 2;
/** Option type: YES_NO_CANCEL_OPTION */
public static final int YES_NO_CANCEL_OPTION = 3;
/** Option type: YES_NO_CANCEL_HELP_OPTION */
public static final int YES_NO_CANCEL_HELP_OPTION = 4;
/** Option type: OK_CANCEL_OPTION */
public static final int OK_CANCEL_OPTION = 5;
/** Option type: OK_CANCEL_HELP_OPTION */
public static final int OK_CANCEL_HELP_OPTION = 6;
/** Option type: DEFAULT_OPTION */
public static final int DEFAULT_OPTION = CLOSE_OPTION;
//TODO: These should be overridden on ArgoDialog to populate from
//the config file
private int leftBorder = 10;
private int rightBorder = 10;
private int topBorder = 10;
private int bottomBorder = 10;
/** The gap between components. */
private int componentGap = 10;
/** The gap between labels. */
private int labelGap = 5;
private int buttonGap = 5;
private JButton okButton = null;
private JButton cancelButton = null;
private JButton closeButton = null;
private JButton yesButton = null;
private JButton noButton = null;
private JButton helpButton = null;
private JPanel mainPanel;
private JComponent content;
private JPanel buttonPanel;
private int optionType;
/**
* Creates a new Dialog with no content component. The default set of
* button(s) will be displayed. After creating the Dialog, call setContent()
* to configure the dialog before calling show() to display it.
*
* @param owner the owning Frame
* @param title the title String for the dialog
* @param modal true if the dialog is modal
*/
public Dialog(Frame owner, String title, boolean modal) {
this(owner, title, DEFAULT_OPTION, modal);
}
/**
* Creates a new Dialog with no content component, using the specified
* optionType to determine the set of available buttons.
* After creating the Dialog, call setContent()
* to configure the dialog before calling show() to display it.
*
* @param owner the owning Frame
* @param title the title String for the dialog
* @param theOptionType defines which buttons will be
* available on the dialog
* @param modal true if the dialog is modal
*/
public Dialog(Frame owner, String title, int theOptionType, boolean modal) {
super(owner, title, modal);
optionType = theOptionType;
JButton[] buttons = createButtons();
nameButtons();
content = null;
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(0, bottomBorder));
mainPanel.setBorder(BorderFactory.createEmptyBorder(topBorder,
leftBorder,
bottomBorder,
rightBorder));
getContentPane().add(mainPanel);
buttonPanel = new JPanel(new SerialLayout(Horizontal.getInstance(),
SerialLayout.EAST,
SerialLayout.LEFTTORIGHT,
SerialLayout.TOP,
buttonGap));
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
for (int i = 0; i < buttons.length; ++i) {
buttonPanel.add(buttons[i]);
buttons[i].addActionListener(this);
}
getRootPane().setDefaultButton(buttons[0]);
}
/**
* Returns the main component that is displayed within the dialog.
*
* @return main component displayed in dialog
**/
public JComponent getContent() {
return content;
}
/**
* Sets the main component to be displayed within the dialog.
* Note: this method is final because it is most likely to be used
* in subclass constructors, and calling a class's overridable methods in
* its own constructor is not good practice.
*
* @param theContent main component to display in dialog
**/
public final void setContent(JComponent theContent) {
if (content != null) {
mainPanel.remove(content);
}
content = theContent;
mainPanel.add(content, BorderLayout.CENTER);
pack();
centerOnParent();
}
/**
* Adds a new button to the set of available option buttons on the dialog.
* The button will appear after the buttons specified by the optionType.
*
* @param button the button to add to the dialog.
**/
public void addButton(JButton button) {
buttonPanel.add(button);
}
/**
* Adds a new button to the set of available option buttons on the dialog.
* The button will appear at the specified index.
*
* @param button the button to add to the dialog.
* @param index index at which to insert new button (0 for first button)
**/
public void addButton(JButton button, int index) {
buttonPanel.add(button, index);
}
/**
* @return the requested button
*/
protected JButton getOkButton() {
return okButton;
}
/**
* @return the requested button
*/
protected JButton getCancelButton() {
return cancelButton;
}
/**
* @return the requested button
*/
protected JButton getCloseButton() {
return closeButton;
}
/**
* @return the requested button
*/
protected JButton getYesButton() {
return yesButton;
}
/**
* @return the requested button
*/
protected JButton getNoButton() {
return noButton;
}
/**
* @return the requested button
*/
protected JButton getHelpButton() {
return helpButton;
}
/**
* Default implementation simply closes the dialog when
* any of the standard buttons is pressed except the Help button.
*
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton
|| e.getSource() == cancelButton
|| e.getSource() == closeButton
|| e.getSource() == yesButton
|| e.getSource() == noButton) {
setVisible(false);
dispose();
}
}
/**
* Creates the set of JButtons for the current optionType.
**/
private JButton[] createButtons() {
JButton[] buttons;
switch(optionType) {
case YES_NO_OPTION:
yesButton = new JButton();
noButton = new JButton();
buttons = new JButton[] {
yesButton, noButton
};
break;
case YES_NO_HELP_OPTION:
yesButton = new JButton();
noButton = new JButton();
helpButton = new JButton();
buttons = new JButton[] {
yesButton, noButton, helpButton
};
break;
case YES_NO_CANCEL_OPTION:
yesButton = new JButton();
noButton = new JButton();
cancelButton = new JButton();
buttons = new JButton[] {
yesButton, noButton, cancelButton
};
break;
case YES_NO_CANCEL_HELP_OPTION:
yesButton = new JButton();
noButton = new JButton();
cancelButton = new JButton();
helpButton = new JButton();
buttons = new JButton[] {
yesButton, noButton, cancelButton, helpButton
};
break;
case OK_CANCEL_OPTION:
okButton = new JButton();
cancelButton = new JButton();
buttons = new JButton[] {
okButton, cancelButton
};
break;
case OK_CANCEL_HELP_OPTION:
okButton = new JButton();
cancelButton = new JButton();
helpButton = new JButton();
buttons = new JButton[] {
okButton, cancelButton, helpButton
};
break;
case CLOSE_OPTION:
default:
closeButton = new JButton();
buttons = new JButton[] {
closeButton
};
break;
}
return buttons;
}
/**
* Moves the dialog to be centered on its parent's location on the screen.
**/
private void centerOnParent() {
Dimension size = getSize();
Dimension p = getParent().getSize();
int x = (getParent().getX() - size.width)
+ (int) ((size.width + p.width) / 2d);
int y = (getParent().getY() - size.height)
+ (int) ((size.height + p.height) / 2d);
setLocation(x, y);
}
/**
* Subclasses may override this method to change the names and mnemonics of
* the various JButtons which appear at the bottom of the dialog.
**/
protected abstract void nameButtons();
/**
* @return Returns the componentGap.
*/
protected int getComponentGap() {
return componentGap;
}
/**
* @return Returns the labelGap.
*/
protected int getLabelGap() {
return labelGap;
}
}
|