1 30 package com.genimen.djeneric.tools.specifier.editor; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Color ; 34 import java.awt.FlowLayout ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.KeyEvent ; 37 import java.awt.event.MouseEvent ; 38 import java.util.ArrayList ; 39 import java.util.Iterator ; 40 41 import javax.swing.DefaultListModel ; 42 import javax.swing.JButton ; 43 import javax.swing.JLabel ; 44 import javax.swing.JList ; 45 import javax.swing.JPanel ; 46 import javax.swing.JScrollPane ; 47 import javax.swing.ListSelectionModel ; 48 49 import com.genimen.djeneric.language.Messages; 50 import com.genimen.djeneric.repository.DjCursor; 51 import com.genimen.djeneric.repository.DjExtent; 52 import com.genimen.djeneric.repository.DjList; 53 import com.genimen.djeneric.repository.DjObject; 54 import com.genimen.djeneric.repository.DjOql; 55 import com.genimen.djeneric.repository.DjProperty; 56 import com.genimen.djeneric.repository.DjSession; 57 import com.genimen.djeneric.repository.exceptions.DjenericException; 58 import com.genimen.djeneric.tools.scriptengine.core.util.Variable; 59 import com.genimen.djeneric.tools.specifier.base.BasePanel; 60 import com.genimen.djeneric.tools.specifier.base.FilterPanel; 61 import com.genimen.djeneric.tools.specifier.interfaces.Chooser; 62 import com.genimen.djeneric.tools.specifier.interfaces.EditorEventListener; 63 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer; 64 import com.genimen.djeneric.ui.Util; 65 import com.genimen.djeneric.util.DjLogger; 66 67 public class ChooserPanel extends BasePanel implements Chooser 68 { 69 private static final long serialVersionUID = 1L; 70 public static final int MAX_RESULT_SIZE = 200; 71 72 BorderLayout borderLayout1 = new BorderLayout (); 73 JPanel jPanel2 = new JPanel (); 74 BorderLayout borderLayout2 = new BorderLayout (); 75 JPanel _buttonPanel = new JPanel (); 76 JScrollPane jScrollPane1 = new JScrollPane (); 77 JList _lstResults = new JList (); 78 BorderLayout borderLayout3 = new BorderLayout (); 79 BorderLayout borderLayout4 = new BorderLayout (); 80 JPanel _buttonPanelRight = new JPanel (); 81 FlowLayout flowLayout1 = new FlowLayout (); 82 JButton _butSelect = new JButton (); 83 JButton _butCancel = new JButton (); 84 BorderLayout borderLayout5 = new BorderLayout (); 85 FilterPanel _propertyPanel; 86 DjExtent _extent; 87 DjSession _session; 88 ArrayList _editorEventListeners = new ArrayList (); 89 String _eventContext = null; 90 SpecifierPanelContainer _editorContainer; 91 Variable _destVariable = null; 92 DjObject _selectedObject = null; 93 JLabel _edtStatus = new JLabel (); 94 FlowLayout flowLayout2 = new FlowLayout (); 95 JButton _butRequery = new JButton (); 96 JPanel _middleButtonPanel = new JPanel (); 97 boolean _disableDefaultAccept = false; 98 99 public ChooserPanel() 100 { 101 } 102 103 public ChooserPanel(DjSession session, DjExtent extent) 104 { 105 construct(null, null, session, extent, null); 106 } 107 108 public void construct(SpecifierPanelContainer editorContainer, DjOql restrictingOql, DjSession session, 109 DjExtent extent, ArrayList excludeTheseProperties) 110 { 111 try 112 { 113 _editorContainer = editorContainer; 114 _extent = extent; 115 _session = session; 116 _propertyPanel = new FilterPanel(restrictingOql, session, extent, excludeTheseProperties) 117 { 118 private static final long serialVersionUID = 1L; 119 120 protected void enterHit() 121 { 122 ChooserPanel.this.enterHit(); 123 } 124 125 protected void acceptHit() 126 { 127 ChooserPanel.this.acceptHit(); 128 } 129 130 protected void escapeHit() 131 { 132 ChooserPanel.this.escapeHit(); 133 } 134 }; 135 jbInit(); 136 137 _lstResults.addKeyListener(new java.awt.event.KeyAdapter () 138 { 139 public void keyPressed(KeyEvent e) 140 { 141 handleListKeyPressed(e); 142 } 143 }); 144 } 145 catch (Exception x) 146 { 147 DjLogger.log(x); 148 } 149 } 150 151 void jbInit() throws Exception 152 { 153 this.setLayout(borderLayout1); 154 jPanel2.setLayout(borderLayout2); 155 _buttonPanel.setLayout(borderLayout4); 156 _buttonPanelRight.setLayout(flowLayout1); 157 _butSelect.setText(Messages.getString("ChooserPanel.Select")); 158 _butSelect.addActionListener(new java.awt.event.ActionListener () 159 { 160 public void actionPerformed(ActionEvent e) 161 { 162 _butSelect_actionPerformed(e); 163 } 164 }); 165 _butCancel.setText(Messages.getString("global.Cancel")); 166 _butCancel.addActionListener(new java.awt.event.ActionListener () 167 { 168 public void actionPerformed(ActionEvent e) 169 { 170 _butCancel_actionPerformed(e); 171 } 172 }); 173 _lstResults.addMouseListener(new java.awt.event.MouseAdapter () 174 { 175 public void mouseClicked(MouseEvent e) 176 { 177 _lstResults_mouseClicked(e); 178 } 179 }); 180 _edtStatus.setForeground(Color.red); 181 _edtStatus.setText(""); 182 183 flowLayout2.setAlignment(FlowLayout.RIGHT); 184 _butRequery.setText(Messages.getString("global.Query")); 185 _butRequery.addActionListener(new java.awt.event.ActionListener () 186 { 187 public void actionPerformed(ActionEvent e) 188 { 189 _butRequery_actionPerformed(e); 190 } 191 }); 192 _middleButtonPanel.setLayout(flowLayout2); 193 _lstResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 194 jScrollPane1.getViewport().add(_lstResults, null); 195 196 jPanel2.add(jScrollPane1, BorderLayout.CENTER); 197 jPanel2.add(_buttonPanel, BorderLayout.SOUTH); 198 199 _buttonPanel.add(_buttonPanelRight, BorderLayout.EAST); 200 _buttonPanelRight.add(_butCancel, null); 201 _buttonPanelRight.add(_butSelect, null); 202 jPanel2.add(_middleButtonPanel, BorderLayout.NORTH); 203 _middleButtonPanel.add(_butRequery, null); 204 205 this.add(_propertyPanel, BorderLayout.NORTH); 206 this.add(jPanel2, BorderLayout.CENTER); 207 this.add(_edtStatus, BorderLayout.SOUTH); 208 Util.sizeButtons(_buttonPanelRight); 209 } 210 211 void _butRequery_actionPerformed(ActionEvent e) 212 { 213 214 try 215 { 216 requery(); 217 } 218 catch (DjenericException e1) 219 { 220 setStatusMessage(e1); 221 } 222 } 223 224 public void requestFocus() 225 { 226 _propertyPanel.requestFocus(); 227 } 228 229 protected void requery() throws DjenericException 230 { 231 DjOql oql = _propertyPanel.getOql(); 232 DjCursor lst = _session.getObjectsCursor(oql); 233 DefaultListModel model = new DefaultListModel (); 234 235 DjList result = new DjList(); 236 237 DjObject obj = lst.getNext(); 238 239 for (int i = 0; i < MAX_RESULT_SIZE && obj != null; i++) 240 { 241 result.add(obj); 242 obj = lst.getNext(); 243 } 244 lst.close(); 245 result.sort(); 246 247 Iterator it = result.iterator(); 248 while (it.hasNext()) 249 model.addElement(it.next()); 250 251 _lstResults.setModel(model); 252 253 if (model.size() > 0) _lstResults.setSelectedIndex(0); 254 if (model.size() == MAX_RESULT_SIZE) setStatusMessage(Messages.getString("ChooserPanel.MoreThan", String 255 .valueOf(MAX_RESULT_SIZE), _extent.getNamePlural()), false); 256 else if (model.size() == 0) setStatusMessage(Messages.getString("ChooserPanel.No", _extent.getNameSingular()), 257 false); 258 else setStatusMessage(Messages.getString("global.Ok"), true); 259 260 if (model.size() != 0) _lstResults.requestFocus(); 261 } 262 263 void _butCancel_actionPerformed(ActionEvent e) 264 { 265 try 266 { 267 if (close()) fireEvent("cancel"); 268 } 269 catch (DjenericException dje) 270 { 271 setStatusMessage(dje.getMessage(), false); 272 } 273 } 274 275 void _butSelect_actionPerformed(ActionEvent e) 276 { 277 try 278 { 279 select(); 280 if (close()) fireEvent("accept"); 281 } 282 catch (DjenericException dje) 283 { 284 setStatusMessage(dje); 285 } 286 } 287 288 protected void cleanup() throws DjenericException 289 { 290 fireEvent("close"); 291 if (_editorContainer != null) 292 { 293 _editorContainer.notifyClosed(this); 294 } 295 } 296 297 protected void select() throws DjenericException 298 { 299 300 if (_lstResults.getSelectedIndex() == -1) 301 { 302 throw new DjenericException(Messages.getString("ChooserPanel.PleaseSelect", _extent.getNameSingular())); 303 } 304 fireEvent("validate"); 305 306 _selectedObject = (DjObject) _lstResults.getSelectedValue(); 307 if (_destVariable != null) 308 { 309 _destVariable.setObject(_selectedObject); 310 } 311 } 312 313 void _lstResults_mouseClicked(MouseEvent e) 314 { 315 if (!_disableDefaultAccept && e.getClickCount() > 1) acceptHit(); 316 } 317 318 public void hideDefaultOkCCancelButtons(boolean hide) 319 { 320 _butSelect.setVisible(!hide); 321 _butCancel.setVisible(!hide); 322 } 323 324 public void registerEvent(String eventName, String eventTitle, String eventOption) 325 { 326 hideDefaultOkCCancelButtons(true); 327 JButton newButton = new JButton (); 328 329 newButton.setText(eventTitle); 330 newButton.setActionCommand(eventName + "," + eventOption); 331 newButton.addActionListener(new java.awt.event.ActionListener () 332 { 333 public void actionPerformed(ActionEvent e) 334 { 335 fireCustomEvent(e); 336 } 337 }); 338 _buttonPanelRight.add(newButton, null); 339 Util.sizeButtons(_buttonPanelRight); 340 _disableDefaultAccept = true; 341 } 342 343 protected void fireCustomEvent(ActionEvent e) 344 { 345 String event = e.getActionCommand(); 346 String option = "none"; 347 int idx = event.indexOf(","); 348 if (idx != -1) 349 { 350 option = event.substring(idx + 1); 351 event = event.substring(0, idx); 352 } 353 354 try 355 { 356 if ("accept".equals(option)) 357 { 358 select(); 359 close(); 360 fireEvent(event); 361 } 362 else if ("cancel".equals(option)) 363 { 364 close(); 365 fireEvent(event); 366 } 367 else fireEvent(event); 368 } 369 catch (Exception x) 370 { 371 setStatusMessage(x); 372 } 373 374 } 375 376 public void setStatusMessage(String msg, boolean isInformative) 377 { 378 if (_editorContainer != null) _editorContainer.setStatusMessage(msg, isInformative); 379 else if (!isInformative) _edtStatus.setText(msg); 380 else _edtStatus.setText(""); 381 } 382 383 public DjProperty[] getQueryableProperties() 384 { 385 return _propertyPanel.getQueryableProperties(); 386 } 387 388 protected void fireEvent(String eventName) throws DjenericException 389 { 390 String fullEventName; 391 392 if (_eventContext == null) fullEventName = eventName; 393 else fullEventName = _eventContext + "." + eventName; 394 395 for (int i = 0; i < _editorEventListeners.size(); i++) 396 { 397 EditorEventListener eel = (EditorEventListener) _editorEventListeners.get(i); 398 eel.handleEvent(fullEventName); 399 } 400 } 401 402 public void addEditorEventListener(EditorEventListener lsnr) 403 { 404 _editorEventListeners.add(lsnr); 405 } 406 407 public void removeEditorEventListener(EditorEventListener lsnr) 408 { 409 _editorEventListeners.remove(lsnr); 410 } 411 412 public String getEventContext() 413 { 414 return _eventContext; 415 } 416 417 public void setEventContext(String string) 418 { 419 _eventContext = string; 420 } 421 422 public void setDestinationVariable(Variable destVariable) 423 { 424 _destVariable = destVariable; 425 } 426 427 public DjObject getSelectedObject() 428 { 429 if (_selectedObject == null) return (DjObject) _lstResults.getSelectedValue(); 430 return _selectedObject; 431 } 432 433 protected void enterHit() 434 { 435 _butRequery_actionPerformed(null); 436 } 437 438 protected void acceptHit() 439 { 440 _butSelect_actionPerformed(null); 441 } 442 443 protected void escapeHit() 444 { 445 _butCancel_actionPerformed(null); 446 } 447 448 void handleListKeyPressed(KeyEvent e) 449 { 450 if (!_disableDefaultAccept && e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) acceptHit(); 451 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) escapeHit(); 452 } 453 454 } | Popular Tags |