1 30 package com.genimen.djeneric.tools.specifier.components; 31 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 35 import javax.swing.JCheckBox ; 36 import javax.swing.JComponent ; 37 38 import com.genimen.djeneric.language.Messages; 39 import com.genimen.djeneric.repository.DjDomainValue; 40 import com.genimen.djeneric.repository.exceptions.DjenericException; 41 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer; 42 import com.genimen.djeneric.util.DjLogger; 43 44 public class DjCheckBox extends JCheckBox implements DjBindable, ActionListener 45 { 46 private static final long serialVersionUID = 1L; 47 private BindingMediator _mediator; 48 DjDomainValue[] _domainValues; 49 50 public DjCheckBox(ObjectViewer viewer, String propertyName, DjDomainValue[] domainValues) throws DjenericException 51 { 52 super(); 53 _domainValues = domainValues; 54 55 if (domainValues.length != 2) 56 { 57 throw new DjenericException(Messages.getString("DjCheckBox.No2values", propertyName)); 58 } 59 addActionListener(this); 60 _mediator = new BindingMediator(this, viewer, propertyName); 61 } 62 63 public void clear() 64 { 65 setText(""); 66 } 67 68 public void apply() throws DjenericException 69 { 70 boolean wasChecked = _domainValues[0].getDescription().equals(_mediator.getPropertyValueString()); 71 if (wasChecked != isSelected()) 72 { 73 if (wasChecked) 74 { 75 _mediator.setPropertyValue(_domainValues[1].getDescription()); 76 } 77 else 78 { 79 _mediator.setPropertyValue(_domainValues[0].getDescription()); 80 } 81 } 82 } 83 84 public void actionPerformed(ActionEvent e) 85 { 86 try 87 { 88 apply(); 89 } 90 catch (Exception x) 91 { 92 DjLogger.log(x); 93 } 94 } 95 96 public void setPropertyName(String propertyName) throws DjenericException 97 { 98 _mediator.setPropertyName(propertyName); 99 } 100 101 public void setViewer(ObjectViewer viewer) 102 { 103 _mediator.setViewer(viewer); 104 } 105 106 public void synchronize() throws DjenericException 107 { 108 if (_domainValues == null || _domainValues.length == 0) 109 { 110 System.err.println(Messages.getString("DjCheckBox.NoValidValues")); 111 return; 112 } 113 setEnabled(_mediator.isComponentWritable()); 114 115 String curVal = _mediator.getPropertyValueString(); 116 setSelected(_domainValues[0].getDescription().equals(curVal)); 117 } 118 119 public Object getDisplayedValue() 120 { 121 if (isSelected()) return _domainValues[0].getValue(); 122 return _domainValues[1].getValue(); 123 } 124 125 public String getPropertyName() 126 { 127 return _mediator.getPropertyName(); 128 } 129 130 public JComponent getFocussableComponent() 131 { 132 return this; 133 } 134 135 public boolean isComponentWritable() 136 { 137 return _mediator.isComponentWritable(); 138 } 139 } | Popular Tags |