1 7 package org.ejtools.graph.dialog; 8 9 import java.awt.Component ; 10 import java.awt.GridBagConstraints ; 11 import java.awt.GridBagLayout ; 12 import java.awt.GridLayout ; 13 import java.awt.Insets ; 14 import java.awt.event.ActionEvent ; 15 import java.awt.event.ActionListener ; 16 import java.util.ResourceBundle ; 17 18 import javax.swing.JButton ; 19 import javax.swing.JComboBox ; 20 import javax.swing.JDialog ; 21 import javax.swing.JLabel ; 22 import javax.swing.JPanel ; 23 24 28 public class SelectGraphDialog extends JDialog 29 { 30 31 protected JComboBox choices; 32 33 protected Object initialSelectionValue; 34 35 protected Object selectedValue = null; 36 37 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService"); 38 39 40 47 public SelectGraphDialog(Component parentComponent, Object [] selectionValues, Object initialSelectionValue) 48 { 49 super(); 50 this.setModal(true); 51 this.setTitle(resources.getString("graph.dialog.title")); 52 53 JPanel main = new JPanel (new GridBagLayout ()); 54 GridBagConstraints constraints = new GridBagConstraints (); 55 constraints.insets = new Insets (5, 5, 5, 5); 56 57 constraints.gridwidth = GridBagConstraints.REMAINDER; 58 constraints.weightx = 1.0; 59 constraints.fill = GridBagConstraints.HORIZONTAL; 60 constraints.anchor = GridBagConstraints.WEST; 61 JLabel messageLabel = new JLabel (resources.getString("graph.dialog.text.description")); 62 main.add(messageLabel, constraints); 63 64 this.initialSelectionValue = initialSelectionValue; 65 66 this.choices = new JComboBox (selectionValues); 67 this.choices.setSelectedItem(this.initialSelectionValue); 68 this.choices.setEditable(true); 69 if (selectionValues.length == 0) 70 { 71 choices.getEditor().setItem(this.initialSelectionValue); 72 } 73 74 main.add(choices, constraints); 75 76 JPanel buttons = new JPanel (new GridLayout (1, 2, 2, 2)); 77 78 JButton buttonSelect = new JButton (resources.getString("graph.dialog.button.select")); 79 buttons.add(buttonSelect); 80 buttonSelect.addActionListener( 81 new ActionListener () 82 { 83 86 public void actionPerformed(ActionEvent e) 87 { 88 select(); 89 dispose(); 90 } 91 }); 92 93 JButton buttonCancel = new JButton (resources.getString("graph.dialog.button.cancel")); 94 buttons.add(buttonCancel); 95 buttonCancel.addActionListener( 96 new ActionListener () 97 { 98 101 public void actionPerformed(ActionEvent e) 102 { 103 selectedValue = null; 104 dispose(); 105 } 106 }); 107 108 constraints.fill = GridBagConstraints.NONE; 109 constraints.anchor = GridBagConstraints.SOUTH; 110 main.add(buttons, constraints); 111 112 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 113 this.getContentPane().add(main); 114 this.pack(); 115 this.setLocationRelativeTo(parentComponent); 116 } 117 118 119 124 public Object getSelectedValue() 125 { 126 return selectedValue; 127 } 128 129 130 131 private void select() 132 { 133 boolean found = false; 134 135 Object choice = this.choices.getSelectedItem(); 136 Object text = this.choices.getEditor().getItem(); 137 138 for (int i = 0; i < this.choices.getModel().getSize(); i++) 139 { 140 choice = this.choices.getModel().getElementAt(i); 141 if (choice.toString().equals(text.toString())) 142 { 143 found = true; 144 break; 145 } 146 } 147 if (!found) 148 { 149 choice = text; 150 } 151 152 this.selectedValue = choice; 153 } 154 } 155 | Popular Tags |