1 56 package org.objectstyle.cayenne.swing; 57 58 import java.awt.Color ; 59 import java.awt.Component ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 63 import javax.swing.JComboBox ; 64 import javax.swing.JComponent ; 65 66 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog; 67 import org.objectstyle.cayenne.validation.ValidationException; 68 69 72 public class ComboSelectionBinding extends BindingBase { 73 74 protected JComboBox comboBox; 75 76 protected Color defaultBGColor; 77 protected Color errorColor; 78 protected String defaultToolTip; 79 protected String noSelectionValue; 80 81 85 public ComboSelectionBinding(JComboBox comboBox, String expression, 86 String noSelectionValue) { 87 super(expression); 88 this.comboBox = comboBox; 89 this.noSelectionValue = noSelectionValue; 90 91 comboBox.addActionListener(new ActionListener () { 92 93 public void actionPerformed(ActionEvent e) { 94 if (!modelUpdateDisabled) { 95 updateModel(); 96 } 97 } 98 }); 99 100 initComponentDefaults(); 102 } 103 104 protected void initComponentDefaults() { 105 this.errorColor = ValidatorDialog.WARNING_COLOR; 106 107 if (comboBox.getEditor() != null) { 108 109 Component editor = comboBox.getEditor().getEditorComponent(); 110 if (editor instanceof JComponent ) { 111 JComponent jEditor = (JComponent ) editor; 112 this.defaultBGColor = jEditor.getBackground(); 113 this.defaultToolTip = jEditor.getToolTipText(); 114 } 115 } 116 } 117 118 public void updateView() { 119 Object value = getValue(); 120 modelUpdateDisabled = true; 121 try { 122 clear(); 123 if (value != null) { 124 this.comboBox.setSelectedItem(value.toString()); 125 } 126 else if (noSelectionValue != null) { 127 this.comboBox.setSelectedItem(noSelectionValue); 128 } 129 else { 130 this.comboBox.setSelectedIndex(-1); 131 } 132 } 133 finally { 134 modelUpdateDisabled = false; 135 } 136 } 137 138 protected void updateModel() { 139 try { 140 Object value = comboBox.getSelectedItem(); 141 if (noSelectionValue != null && noSelectionValue.equals(value)) { 142 value = null; 143 } 144 setValue(value); 145 clear(); 146 } 147 catch (ValidationException vex) { 148 initWarning(vex.getLocalizedMessage()); 149 } 150 } 151 152 public Component getView() { 153 return comboBox; 154 } 155 156 protected void clear() { 157 if (comboBox.getEditor() != null) { 158 159 Component editor = comboBox.getEditor().getEditorComponent(); 160 if (editor instanceof JComponent ) { 161 JComponent jEditor = (JComponent ) editor; 162 jEditor.setBackground(defaultBGColor); 163 jEditor.setToolTipText(defaultToolTip); 164 } 165 } 166 } 167 168 protected void initWarning(String message) { 169 if (comboBox.getEditor() != null) { 170 171 Component editor = comboBox.getEditor().getEditorComponent(); 172 if (editor instanceof JComponent ) { 173 JComponent jEditor = (JComponent ) editor; 174 jEditor.setBackground(errorColor); 175 jEditor.setToolTipText(message); 176 } 177 } 178 } 179 } | Popular Tags |