From: Erik V. <ev...@us...> - 2010-01-08 21:28:16
|
Update of /cvsroot/rails/18xx/rails/ui/swing/elements In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv23981/rails/ui/swing/elements Modified Files: CheckBoxDialog.java DialogOwner.java Added Files: RadioButtonDialog2.java Log Message: Added nonmodal RadioButtonDialog2 and apply to Repay Loans Index: CheckBoxDialog.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/elements/CheckBoxDialog.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CheckBoxDialog.java 7 Jan 2010 20:48:13 -0000 1.4 --- CheckBoxDialog.java 8 Jan 2010 21:27:54 -0000 1.5 *************** *** 12,16 **** /** ! * A generic dialog for presenting choices by radio buttons. */ public class CheckBoxDialog extends JDialog implements ActionListener { --- 12,16 ---- /** ! * A generic dialog for presenting choices by checkboxes. */ public class CheckBoxDialog extends JDialog implements ActionListener { *************** *** 42,46 **** public CheckBoxDialog(DialogOwner owner, String title, String message, String[] options, boolean[] selectedOptions) { ! super((Frame) null, title, false); // Modal !? this.owner = owner; this.message = message; --- 42,46 ---- public CheckBoxDialog(DialogOwner owner, String title, String message, String[] options, boolean[] selectedOptions) { ! super((Frame) null, title, false); // Non-modal this.owner = owner; this.message = message; *************** *** 131,134 **** --- 131,135 ---- } } else if (arg0.getSource().equals(cancelButton)) { + return; } setVisible(false); Index: DialogOwner.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/ui/swing/elements/DialogOwner.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** DialogOwner.java 7 Jan 2010 20:48:14 -0000 1.1 --- DialogOwner.java 8 Jan 2010 21:27:54 -0000 1.2 *************** *** 1,6 **** --- 1,16 ---- package rails.ui.swing.elements; + import javax.swing.JDialog; + + import rails.game.action.PossibleAction; + public interface DialogOwner { public void dialogActionPerformed (); + + public JDialog getCurrentDialog(); + + public PossibleAction getCurrentDialogAction(); + + public void setCurrentDialog (JDialog dialog, PossibleAction action); } --- NEW FILE: RadioButtonDialog2.java --- /* $Header: /cvsroot/rails/18xx/rails/ui/swing/elements/RadioButtonDialog2.java,v 1.1 2010/01/08 21:27:54 evos Exp $*/ package rails.ui.swing.elements; import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.apache.log4j.Logger; import rails.util.LocalText; /** * A generic dialog for presenting choices by radio buttons. */ public class RadioButtonDialog2 extends JDialog implements ActionListener { private static final long serialVersionUID = 1L; GridBagConstraints gc; JPanel optionsPane, buttonPane; JButton okButton, cancelButton; JRadioButton[] choiceButtons; Dimension size, optSize; ButtonGroup group; DialogOwner owner; String message; int numOptions; String[] options; int selectedOption; int chosenOption = -1; protected static Logger log = Logger.getLogger(RadioButtonDialog2.class.getPackage().getName()); public RadioButtonDialog2(DialogOwner owner, String title, String message, String[] options, int selectedOption) { super((Frame) null, title, false); // Non-modal this.owner = owner; this.message = message; this.options = options; this.numOptions = options.length; this.selectedOption = selectedOption; initialize(); pack(); // Center on owner /* int x = (int) owner.getLocationOnScreen().getX() + (owner.getWidth() - getWidth()) / 2; int y = (int) owner.getLocationOnScreen().getY() + (owner.getHeight() - getHeight()) / 2; */ int x = 400; int y = 400; setLocation(x, y); this.setVisible(true); } private void initialize() { gc = new GridBagConstraints(); optionsPane = new JPanel(); buttonPane = new JPanel(); okButton = new JButton(LocalText.getText("OK")); okButton.setMnemonic(KeyEvent.VK_O); okButton.addActionListener(this); buttonPane.add(okButton); if (selectedOption < 0) { // If an option has been preselected, selection is mandatory. cancelButton = new JButton(LocalText.getText("Cancel")); cancelButton.setMnemonic(KeyEvent.VK_C); cancelButton.addActionListener(this); buttonPane.add(cancelButton); } choiceButtons = new JRadioButton[numOptions]; this.getContentPane().setLayout(new GridBagLayout()); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); optionsPane.setLayout(new GridBagLayout()); // optionsPane.setBorder(BorderFactory.createLoweredBevelBorder()); optionsPane.add(new JLabel(message), constraints(0, 0, 10, 10, 10, 10)); choiceButtons = new JRadioButton[numOptions]; group = new ButtonGroup(); for (int i = 0; i < numOptions; i++) { choiceButtons[i] = new JRadioButton(options[i], i == selectedOption); optionsPane.add(choiceButtons[i], constraints(0, 1 + i, 0, 0, 0, 0)); choiceButtons[i].setPreferredSize(size); group.add(choiceButtons[i]); } getContentPane().add(optionsPane, constraints(0, 0, 0, 0, 0, 0)); getContentPane().add(buttonPane, constraints(0, 1, 0, 0, 0, 0)); } private GridBagConstraints constraints(int gridx, int gridy, int leftinset, int topinset, int rightinset, int bottominset) { if (gridx >= 0) gc.gridx = gridx; if (gridy >= 0) gc.gridy = gridy; gc.fill = GridBagConstraints.BOTH; gc.weightx = 0.5; gc.weighty = 0.5; if (leftinset >= 0) gc.insets.left = leftinset; if (topinset >= 0) gc.insets.top = topinset; if (rightinset >= 0) gc.insets.right = rightinset; if (bottominset >= 0) gc.insets.bottom = bottominset; return gc; } public void actionPerformed(ActionEvent arg0) { if (arg0.getSource().equals(okButton)) { for (int i = 0; i < numOptions; i++) { if (choiceButtons[i].isSelected()) { chosenOption = i; break; } } } else if (arg0.getSource().equals(cancelButton)) { return; } this.setVisible(false); this.dispose(); owner.dialogActionPerformed(); } public synchronized int getSelectedOption() { return chosenOption; } } |