1 30 package com.genimen.djeneric.tools.modeler.dialogs; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Frame ; 35 import java.awt.Toolkit ; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.KeyEvent ; 38 import java.awt.event.MouseEvent ; 39 import java.util.ArrayList ; 40 import java.util.Arrays ; 41 42 import javax.swing.DefaultComboBoxModel ; 43 import javax.swing.JButton ; 44 import javax.swing.JCheckBox ; 45 import javax.swing.JComboBox ; 46 import javax.swing.JDialog ; 47 import javax.swing.JLabel ; 48 import javax.swing.JList ; 49 import javax.swing.JPanel ; 50 import javax.swing.JScrollPane ; 51 import javax.swing.ListModel ; 52 import javax.swing.ListSelectionModel ; 53 54 import com.genimen.djeneric.language.Messages; 55 import com.genimen.djeneric.repository.DjExtent; 56 import com.genimen.djeneric.repository.DjPackage; 57 import com.genimen.djeneric.repository.DjRelation; 58 import com.genimen.djeneric.ui.Util; 59 import com.genimen.djeneric.util.DjLogger; 60 61 public class ExtentSelectorDialog extends JDialog 62 { 63 private static final long serialVersionUID = 1L; 64 JPanel panel1 = new JPanel (); 65 BorderLayout borderLayout1 = new BorderLayout (); 66 JPanel jPanel1 = new JPanel (); 67 BorderLayout borderLayout2 = new BorderLayout (); 68 JPanel jPanel3 = new JPanel (); 69 JButton _butOk = new JButton (); 70 JButton _butCancel = new JButton (); 71 JScrollPane jScrollPane1 = new JScrollPane (); 72 JList _lstExtents = new JList (); 73 boolean _canceled = true; 74 JCheckBox _chkSelectionRelated = new JCheckBox (); 75 DjExtent[] _extents; 76 DjExtent[] _selection; 77 JPanel jPanel2 = new JPanel (); 78 JLabel _lblPackage = new JLabel (); 79 JComboBox _cbbPackage = new JComboBox (); 80 BorderLayout borderLayout3 = new BorderLayout (); 81 DjPackage _defaultPackage = new DjPackage(Messages 82 .getString("ExtentSelectorDialog.DefaultPackage")); 83 DjPackage _allPackages = new DjPackage(Messages.getString("ExtentSelectorDialog.AllPackages")); 84 85 public ExtentSelectorDialog(Frame frame, String title, DjPackage[] packages, DjExtent[] extents, DjExtent[] selected) 86 { 87 super(frame, title, true); 88 try 89 { 90 _extents = extents; 91 92 setupSelection(extents, selected); 93 94 jbInit(); 95 96 ArrayList packs = new ArrayList (); 97 packs.add(_allPackages); 98 packs.add(_defaultPackage); 99 packs.addAll(Arrays.asList(packages)); 100 101 _cbbPackage.setModel(new DefaultComboBoxModel (packs.toArray())); 102 103 updateList(); 104 105 pack(); 106 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 107 Dimension frameSize = getSize(); 108 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 109 110 _lstExtents.requestFocus(); 111 } 112 catch (Exception ex) 113 { 114 DjLogger.log(ex); 115 } 116 } 117 118 public void setShowSelectionRelated(boolean b) 119 { 120 _chkSelectionRelated.setVisible(b); 121 } 122 123 private void addChoosableIfNotAdded(ArrayList choosable, ArrayList lst, DjExtent ext) 124 { 125 if (choosable.contains(ext) && !lst.contains(ext)) lst.add(ext); 126 } 127 128 private void setupSelection(DjExtent[] extents, DjExtent[] selected) 129 { 130 ArrayList result = new ArrayList (); 131 132 ArrayList choosable = new ArrayList (); 133 for (int i = 0; i < extents.length; i++) 134 choosable.add(extents[i]); 135 136 for (int i = 0; i < selected.length; i++) 137 { 138 addChoosableIfNotAdded(choosable, result, selected[i].getSuper()); 139 140 DjExtent[] specs = selected[i].getSpecializations(); 141 for (int s = 0; s < specs.length; s++) 142 addChoosableIfNotAdded(choosable, result, specs[s]); 143 144 DjRelation[] rels = selected[i].getDetailRelations(); 145 for (int s = 0; s < rels.length; s++) 146 addChoosableIfNotAdded(choosable, result, rels[s].getDetailExtent()); 147 148 rels = selected[i].getMasterRelations(); 149 for (int s = 0; s < rels.length; s++) 150 addChoosableIfNotAdded(choosable, result, rels[s].getMasterExtent()); 151 } 152 153 _selection = (DjExtent[]) result.toArray(new DjExtent[0]); 154 } 155 156 private void updateList() 157 { 158 159 ArrayList filtered = new ArrayList (); 160 161 if (_chkSelectionRelated.isSelected()) filtered.addAll(Arrays.asList(_selection)); 162 else filtered.addAll(Arrays.asList(_extents)); 163 164 DjPackage thisPackage = (DjPackage) _cbbPackage.getSelectedItem(); 165 if (thisPackage != _allPackages) 166 { 167 if (thisPackage == _defaultPackage) filterPackages(filtered, null); 168 else filterPackages(filtered, thisPackage); 169 } 170 171 _lstExtents.setListData(filtered.toArray()); 172 173 if (_lstExtents.getModel().getSize() > 0) _lstExtents.setSelectedIndex(0); 174 175 } 176 177 private void filterPackages(ArrayList filtered, DjPackage pack) 178 { 179 int i = 0; 180 while (i < filtered.size()) 181 { 182 DjExtent ext = (DjExtent) filtered.get(i); 183 184 if (ext.getPackage() != pack) filtered.remove(i); 185 else i++; 186 } 187 } 188 189 void jbInit() throws Exception 190 { 191 panel1.setLayout(borderLayout1); 192 jPanel1.setLayout(borderLayout2); 193 194 _butOk.setRequestFocusEnabled(false); 195 _butOk.setText(Messages.getString("global.Ok")); 196 _butOk.addActionListener(new java.awt.event.ActionListener () 197 { 198 public void actionPerformed(ActionEvent e) 199 { 200 _butOk_actionPerformed(e); 201 } 202 }); 203 _butCancel.setRequestFocusEnabled(false); 204 _butCancel.setText(Messages.getString("global.Cancel")); 205 _butCancel.addActionListener(new java.awt.event.ActionListener () 206 { 207 public void actionPerformed(ActionEvent e) 208 { 209 _butCancel_actionPerformed(e); 210 } 211 }); 212 _lstExtents.addMouseListener(new java.awt.event.MouseAdapter () 213 { 214 public void mouseClicked(MouseEvent e) 215 { 216 _lstExtents_mouseClicked(e); 217 } 218 }); 219 _lstExtents.addKeyListener(new java.awt.event.KeyAdapter () 220 { 221 public void keyPressed(KeyEvent e) 222 { 223 _lstExtents_keyPressed(e); 224 } 225 }); 226 _chkSelectionRelated.setText(Messages.getString("ExtentSelector.SelectionRelated")); 227 _chkSelectionRelated.setToolTipText(Messages.getString("ExtentSelector.SelectionRelatedTooltip")); 228 _chkSelectionRelated.addActionListener(new java.awt.event.ActionListener () 229 { 230 public void actionPerformed(ActionEvent e) 231 { 232 _chkSelectionRelated_actionPerformed(e); 233 } 234 }); 235 jPanel2.setLayout(borderLayout3); 236 _lblPackage.setText(" Package "); 237 _cbbPackage.addActionListener(new java.awt.event.ActionListener () 238 { 239 public void actionPerformed(ActionEvent e) 240 { 241 _cbbPackage_actionPerformed(e); 242 } 243 }); 244 _lstExtents.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 245 getContentPane().add(panel1); 246 panel1.add(jPanel1, BorderLayout.SOUTH); 247 jPanel1.add(jPanel3, BorderLayout.EAST); 248 jPanel3.add(_butCancel, null); 249 jPanel3.add(_butOk, null); 250 jPanel1.add(_chkSelectionRelated, BorderLayout.WEST); 251 panel1.add(jScrollPane1, BorderLayout.CENTER); 252 panel1.add(jPanel2, BorderLayout.NORTH); 253 jPanel2.add(_lblPackage, BorderLayout.WEST); 254 jScrollPane1.getViewport().add(_lstExtents, null); 255 jPanel2.add(_cbbPackage, BorderLayout.CENTER); 256 Util.sizeButtons(jPanel3); 257 } 258 259 void _butOk_actionPerformed(ActionEvent e) 260 { 261 _canceled = false; 262 setVisible(false); 263 } 264 265 public boolean wasCancelled() 266 { 267 return _canceled; 268 } 269 270 public DjExtent[] getSelectedExtents() 271 { 272 ArrayList result = new ArrayList (); 273 ListModel mdl = _lstExtents.getModel(); 274 for (int i = 0; i < mdl.getSize(); i++) 275 { 276 if (_lstExtents.isSelectedIndex(i)) result.add(mdl.getElementAt(i)); 277 } 278 return (DjExtent[]) result.toArray(new DjExtent[0]); 279 } 280 281 public DjExtent[] getUnSelectedExtents() 282 { 283 ArrayList result = new ArrayList (); 284 ListModel mdl = _lstExtents.getModel(); 285 for (int i = 0; i < mdl.getSize(); i++) 286 { 287 if (!_lstExtents.isSelectedIndex(i)) result.add(mdl.getElementAt(i)); 288 } 289 return (DjExtent[]) result.toArray(new DjExtent[0]); 290 } 291 292 void _butCancel_actionPerformed(ActionEvent e) 293 { 294 setVisible(false); 295 } 296 297 void _lstExtents_mouseClicked(MouseEvent e) 298 { 299 if (e.getClickCount() > 1) _butOk_actionPerformed(null); 300 } 301 302 void handleEnterEscape(KeyEvent e) 303 { 304 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null); 305 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 306 } 307 308 void _lstExtents_keyPressed(KeyEvent e) 309 { 310 handleEnterEscape(e); 311 } 312 313 void _chkSelectionRelated_actionPerformed(ActionEvent e) 314 { 315 updateList(); 316 } 317 318 void _cbbPackage_actionPerformed(ActionEvent e) 319 { 320 updateList(); 321 322 } 323 324 public void setMultiSelect(boolean b) 325 { 326 if (b) _lstExtents.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 327 else _lstExtents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 328 } 329 330 } 331 | Popular Tags |