1 30 package com.genimen.djeneric.tools.specifier.components; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.ActionListener ; 36 import java.awt.event.FocusListener ; 37 import java.awt.event.KeyEvent ; 38 import java.awt.event.MouseEvent ; 39 import java.util.ArrayList ; 40 41 import javax.swing.AbstractAction ; 42 import javax.swing.JButton ; 43 import javax.swing.JComponent ; 44 import javax.swing.JDialog ; 45 import javax.swing.JPanel ; 46 import javax.swing.JTextField ; 47 import javax.swing.KeyStroke ; 48 49 import com.genimen.djeneric.repository.DjExtent; 50 import com.genimen.djeneric.repository.DjSession; 51 import com.genimen.djeneric.repository.exceptions.DjenericException; 52 import com.genimen.djeneric.tools.specifier.dialogs.ChooserDialog; 53 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 54 import com.genimen.djeneric.ui.Util; 55 import com.genimen.djeneric.util.DjLogger; 56 57 public class DjChooserField extends JPanel implements DjBindable 58 { 59 private static final long serialVersionUID = 1L; 60 private BorderLayout borderLayout1 = new BorderLayout (); 61 private JTextField _displayField = new JTextField (); 62 private JButton _butChoose = new JButton (); 63 private DjExtent _extent; 64 private DjSession _session; 65 private BindingMediator _mediator = null; 66 private Object _dispayedValue = null; 67 private String _dialogTitle = null; 68 private boolean _required = true; 69 70 ArrayList _listeners = new ArrayList (); 71 72 static boolean _onJdk13 = System.getProperty("java.specification.version").startsWith("1.3"); 73 74 public DjChooserField(DjSession session, DjExtent extent) throws DjenericException 75 { 76 this(null, null, session, extent); 77 } 78 79 public DjChooserField(ObjectViewer viewer, String propertyName, DjSession session, DjExtent extent) 80 throws DjenericException 81 { 82 if (viewer != null) _mediator = new BindingMediator(this, viewer, propertyName); 83 _session = session; 84 _extent = extent; 85 86 if (!_onJdk13) 87 { 88 setFocusable(false); 89 _displayField.setFocusable(false); 90 } 91 92 setLayout(borderLayout1); 93 _butChoose.setPreferredSize(new Dimension (21, 21)); 94 _butChoose.setText("..."); 95 _butChoose.addActionListener(new ChooserField__butChoose_actionAdapter(this)); 96 add(_displayField, BorderLayout.CENTER); 97 add(_butChoose, BorderLayout.EAST); 98 _displayField.setEditable(false); 99 100 _displayField.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)); 101 102 _displayField.registerKeyboardAction(new KickButtonAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), 103 JComponent.WHEN_FOCUSED); 104 105 _displayField.registerKeyboardAction(new KickButtonAction(), KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), 106 JComponent.WHEN_FOCUSED); 107 108 _displayField.addMouseListener(new ChooserField__displayField_mouseAdapter(this)); 109 } 110 111 public boolean isComponentWritable() 112 { 113 return _mediator.isComponentWritable(); 114 } 115 116 public void setLookupExtent(DjExtent extent) 117 { 118 _extent = extent; 119 } 120 121 public void clear() 122 { 123 _displayField.setText(""); 124 } 125 126 public void apply() 127 { 128 } 131 132 public void setPropertyName(String propertyName) throws DjenericException 133 { 134 if (_mediator != null) _mediator.setPropertyName(propertyName); 135 } 136 137 public void setViewer(ObjectViewer viewer) 138 { 139 if (_mediator != null) _mediator.setViewer(viewer); 140 } 141 142 public void synchronize() throws DjenericException 143 { 144 if (_mediator != null) 145 { 146 _displayField.setText(_mediator.getPropertyValueString()); 147 _dispayedValue = _mediator.getPropertyValue(); 148 } 149 } 150 151 void _butChoose_actionPerformed(ActionEvent e) 152 { 153 showChooseDialog(e); 154 } 155 156 public void showChooseDialog(ActionEvent e) 157 { 158 try 159 { 160 int x = 0; 161 int y = 0; 162 JDialog parent = (JDialog ) Util.findParent(this, JDialog .class); 163 if (parent != null) 164 { 165 x = parent.getX() + 30; 166 y = parent.getY() + 30; 167 } 168 169 ChooserDialog cd = new ChooserDialog(Util.findFrame(this), _session, _extent, null, _dialogTitle, _required, x, y); 170 if (!cd.wasCancelled()) 171 { 172 Object o = cd.getSelectedObject(); 173 if (_mediator != null) _mediator.setValue(o); 174 setDisplayedValue(o); 175 } 176 for (int i = 0; i < _listeners.size(); i++) 177 { 178 ActionListener al = (ActionListener ) _listeners.get(i); 179 al.actionPerformed(e); 180 } 181 } 182 catch (DjenericException x) 183 { 184 DjLogger.log(x); 185 } 186 } 187 188 public boolean isEditable() 189 { 190 return _butChoose.isEnabled(); 191 } 192 193 public boolean isEnabled() 194 { 195 return _butChoose.isEnabled(); 196 } 197 198 public void requestFocus() 199 { 200 _butChoose.requestFocus(); 201 } 202 203 public void setEditable(boolean b) 204 { 205 _butChoose.setEnabled(b); 206 } 207 208 public void setEnabled(boolean b) 209 { 210 _butChoose.setEnabled(b); 211 } 212 213 public void setSession(DjSession session) 214 { 215 _session = session; 216 } 217 218 public void setDisplayedValue(Object value) 219 { 220 _dispayedValue = value; 221 if (value != null) _displayField.setText(value.toString()); 222 else _displayField.setText(""); 223 } 224 225 public JTextField getDisplayField() 226 { 227 return _displayField; 228 } 229 230 public JTextField getEditor() 231 { 232 return _displayField; 233 } 234 235 public Object getDisplayedValue() 236 { 237 return _dispayedValue; 238 } 239 240 public String getPropertyName() 241 { 242 return _mediator.getPropertyName(); 243 } 244 245 public JComponent getFocussableComponent() 246 { 247 return _butChoose; 248 } 249 250 public void addActionListener(ActionListener l) 251 { 252 _listeners.add(l); 253 } 254 255 public void removeActionListener(ActionListener l) 256 { 257 _listeners.remove(l); 258 } 259 260 private class KickButtonAction extends AbstractAction 261 { 262 private static final long serialVersionUID = 1L; 263 264 public void actionPerformed(ActionEvent e) 265 { 266 _butChoose_actionPerformed(e); 267 } 268 } 269 270 public void setDialogTitle(String title) 271 { 272 _dialogTitle = title; 273 } 274 275 public boolean isRequired() 276 { 277 return _required; 278 } 279 280 public void setRequired(boolean b) 281 { 282 _required = b; 283 } 284 285 public synchronized void addFocusListener(FocusListener l) 286 { 287 if (_butChoose != null) _butChoose.addFocusListener(l); 288 } 289 290 public void _displayField_mouseClicked(MouseEvent e) 291 { 292 if (e.getClickCount() > 1) showChooseDialog(new ActionEvent (this, 0, "")); 293 294 } 295 } 296 297 class ChooserField__butChoose_actionAdapter implements java.awt.event.ActionListener 298 { 299 DjChooserField adaptee; 300 301 ChooserField__butChoose_actionAdapter(DjChooserField adaptee) 302 { 303 this.adaptee = adaptee; 304 } 305 306 public void actionPerformed(ActionEvent e) 307 { 308 adaptee._butChoose_actionPerformed(e); 309 } 310 } 311 312 class ChooserField__displayField_mouseAdapter extends java.awt.event.MouseAdapter 313 { 314 DjChooserField adaptee; 315 316 ChooserField__displayField_mouseAdapter(DjChooserField adaptee) 317 { 318 this.adaptee = adaptee; 319 } 320 321 public void mouseClicked(MouseEvent e) 322 { 323 adaptee._displayField_mouseClicked(e); 324 } 325 } | Popular Tags |