001: package org.osbl.inventory.gui;
002:
003: import org.conform.wings.Editor;
004: import org.conform.wings.PropertyAdapter;
005: import org.conform.wings.editor.DefaultEditorStyles;
006: import org.conform.wings.editor.ComponentInvalidator;
007: import org.conform.*;
008: import org.wings.*;
009: import org.osbl.client.wings.XIcons;
010:
011: import java.beans.PropertyChangeListener;
012: import java.beans.PropertyChangeEvent;
013:
014: import net.sourceforge.barbecue.*;
015: import net.sourceforge.barbecue.linear.code39.Code39Barcode;
016:
017: public class BarcodeEditor implements Editor {
018: public SComponent createComponent(PropertyMeta propertyMeta) {
019: PropertyDataLabel propertyDataLabel = new PropertyDataLabel(
020: propertyMeta);
021: propertyDataLabel.setPreferredSize(SDimension.AUTOAREA);
022: configureComponent(propertyMeta, propertyDataLabel, false);
023: return propertyDataLabel;
024: }
025:
026: public void configureComponent(PropertyMeta propertyMeta,
027: SComponent component, boolean erroneous) {
028: PropertyDataLabel propertyDataLabel = (PropertyDataLabel) component;
029: propertyDataLabel.setVisible(propertyMeta.isReadable());
030: propertyDataLabel.setEnabled(propertyMeta.isWritable());
031: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
032: component);
033: component.setStyle("formlabel_normal");
034: }
035:
036: public void setPropertyData(SComponent component,
037: PropertyData propertyData) {
038: BeanData beanData = propertyData.getBeanData();
039: if (beanData != null) {
040: beanData.addPropertyChangeListener(propertyData
041: .getPropertyMeta().getName(),
042: new ComponentInvalidator(component));
043: beanData.addPropertyChangeListener(propertyData
044: .getPropertyMeta().getName(),
045: ((PropertyDataLabel) component));
046: }
047: ((PropertyDataLabel) component).setPropertyData(propertyData);
048: }
049:
050: public PropertyData getPropertyData(SComponent component) {
051: return ((PropertyDataLabel) component).getPropertyData();
052: }
053:
054: static class PropertyDataLabel extends SLabel implements
055: PropertyAdapter, PropertyChangeListener {
056: private PropertyData propertyData;
057: private PropertyMeta propertyMeta;
058: private SIcon icon;
059: private Code39Barcode barcode;
060:
061: public PropertyDataLabel(PropertyMeta propertyMeta) {
062: this .propertyMeta = propertyMeta;
063: setVerticalTextPosition(SConstants.TOP);
064: setHorizontalTextPosition(SConstants.CENTER);
065: }
066:
067: public void setPropertyData(PropertyData propertyData) {
068: this .propertyData = propertyData;
069: refresh();
070: }
071:
072: public PropertyData getPropertyData() {
073: return propertyData;
074: }
075:
076: public SIcon getIcon() {
077: if (icon == null) {
078: try {
079: icon = propertyData.getValue() == null ? XIcons.EMPTY
080: : new SImageIcon(BarcodeImageHandler
081: .getImage(getBarcode()));
082: } catch (Exception e) {
083: e.printStackTrace();
084: }
085: }
086: return icon;
087: }
088:
089: private Code39Barcode getBarcode() {
090: if (barcode == null) {
091: try {
092: barcode = new Code39Barcode(""
093: + propertyData.getValue(), false);
094: barcode.setDrawingText(false);
095: } catch (Exception e) {
096: System.err.print(propertyData.getValue());
097: e.printStackTrace();
098: }
099: }
100: return barcode;
101: }
102:
103: public SIcon getDisabledIcon() {
104: return getIcon();
105: }
106:
107: public String getText() {
108: if (text == null)
109: text = propertyData.getValue() == null ? ""
110: : getBarcode().getLabel();
111:
112: return text;
113: }
114:
115: public void propertyChange(PropertyChangeEvent evt) {
116: refresh();
117: }
118:
119: private void refresh() {
120: barcode = null;
121: icon = null;
122: text = null;
123: reload();
124: }
125: }
126: }
|