1 30 package com.genimen.djeneric.tools.specifier.components; 31 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import java.awt.event.FocusEvent ; 35 import java.awt.event.FocusListener ; 36 37 import javax.swing.DefaultComboBoxModel ; 38 import javax.swing.JComboBox ; 39 import javax.swing.JComponent ; 40 41 import com.genimen.djeneric.repository.DjDomainValue; 42 import com.genimen.djeneric.repository.exceptions.DjenericException; 43 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 44 import com.genimen.djeneric.ui.DjFocusMeLater; 45 import com.genimen.djeneric.util.DjLogger; 46 47 public class DjComboBox extends JComboBox implements DjBindable, FocusListener , ActionListener 48 { 49 50 private static final long serialVersionUID = 1L; 51 private BindingMediator _mediator; 52 53 public DjComboBox(ObjectViewer viewer, String propertyName, DjDomainValue[] domainValues) throws DjenericException 54 { 55 super(); 56 setModel(new DefaultComboBoxModel (domainValues)); 57 setEditable(true); 58 59 if (isEditable()) 60 { 61 DjFocusMeLater.getNeededComponent(this).addFocusListener(this); 62 } 63 else addActionListener(this); 64 65 _mediator = new BindingMediator(this, viewer, propertyName); 66 } 67 68 public void actionPerformed(ActionEvent e) 69 { 70 try 71 { 72 apply(); 73 } 74 catch (Exception x) 75 { 76 DjLogger.log(x); 77 } 78 } 79 80 public void clear() 81 { 82 setSelectedIndex(-1); 83 setEnabled(_mediator.isComponentWritable()); 84 } 85 86 public void apply() throws DjenericException 87 { 88 if (isEditable()) 89 { 90 _mediator.setValue(getEditor().getItem()); 91 } 92 else 93 { 94 if (getSelectedIndex() != -1) 95 { 96 DjDomainValue dv = (DjDomainValue) getSelectedItem(); 97 _mediator.setValue(dv.getValue()); 98 } 99 } 100 } 101 102 public void focusGained(FocusEvent e) 103 { 104 if (isEditable()) getEditor().selectAll(); 105 } 106 107 public void focusLost(FocusEvent e) 108 { 109 try 110 { 111 apply(); 112 synchronize(); 113 } 114 catch (Exception x) 115 { 116 DjLogger.log(x); 117 } 118 } 119 120 public void setPropertyName(String propertyName) throws DjenericException 121 { 122 _mediator.setPropertyName(propertyName); 123 } 124 125 public void setViewer(ObjectViewer viewer) 126 { 127 _mediator.setViewer(viewer); 128 } 129 130 public void synchronize() throws DjenericException 131 { 132 setEnabled(_mediator.isComponentWritable()); 133 if (isEditable()) 134 { 135 try 136 { 137 setSelectedIndex(-1); 138 getEditor().setItem(_mediator.getPropertyValueString()); 139 } 140 catch (Exception x) 141 { 142 DjLogger.log(x); 143 } 144 } 145 else 146 { 147 String curVal = _mediator.getPropertyValueString(); 148 int idx = -1; 149 if (curVal.length() != 0) 150 { 151 for (int i = 0; i < getItemCount(); i++) 152 { 153 DjDomainValue dv = (DjDomainValue) getItemAt(i); 154 155 if (dv.getValue().equals(curVal)) 156 { 157 idx = i; 158 break; 159 } 160 } 161 } 162 163 setSelectedIndex(idx); 164 } 165 } 166 167 public Object getDisplayedValue() 168 { 169 if (getSelectedIndex() != -1) return getEditor().getItem(); 170 return null; 171 } 172 173 public String getPropertyName() 174 { 175 return _mediator.getPropertyName(); 176 } 177 178 public JComponent getFocussableComponent() 179 { 180 return this; 181 } 182 183 public boolean isComponentWritable() 184 { 185 return _mediator.isComponentWritable(); 186 } 187 } | Popular Tags |