001: package desktop;
002:
003: import java.beans.*;
004: import java.io.*;
005: import java.util.prefs.BackingStoreException;
006: import java.util.prefs.Preferences;
007:
008: public abstract class AbstractDesktopItem implements DesktopItem {
009:
010: protected java.util.Map<String, Object> attributes = new java.util.HashMap<String, Object>();
011: protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
012: this );
013: protected ItemContainer container;
014:
015: protected Preferences pref;
016:
017: public static org.wings.util.SessionLocal<Integer> itemNo = new org.wings.util.SessionLocal<Integer>() {
018: @Override
019: protected Integer initialValue() {
020: return 0;
021: }
022: };
023:
024: protected AbstractDesktopItem() {
025:
026: pref = Preferences.userRoot().node("desktopitems").node(
027: "desktopitem" + itemNo.get().toString());
028: this .putValue(KEY, "desktopitem" + itemNo.get().toString());
029: itemNo.set(itemNo.get() + 1);
030: pref.putInt(FIRST_FREE_INDEX, itemNo.get());
031:
032: addPropertyChangeListener(new PropertyChangeListener() {
033: public void propertyChange(PropertyChangeEvent e) {
034: if (e.getPropertyName() == NAME) {
035: if (container != null)
036: container.setTitle((String) e.getNewValue());
037: } else if (e.getPropertyName() == ICON) {
038: if (container != null)
039: container.setIcon((org.wings.SURLIcon) e
040: .getNewValue());
041: }
042: }
043: });
044: }
045:
046: protected AbstractDesktopItem(String name) {
047:
048: pref = Preferences.userRoot().node("desktopitems").node(name);
049: this .putValue(KEY, name);
050: addPropertyChangeListener(new PropertyChangeListener() {
051: public void propertyChange(PropertyChangeEvent e) {
052: if (e.getPropertyName() == NAME) {
053: if (container != null)
054: container.setTitle((String) e.getNewValue());
055: } else if (e.getPropertyName() == ICON) {
056: if (container != null)
057: container.setIcon((org.wings.SURLIcon) e
058: .getNewValue());
059: }
060: }
061: });
062: }
063:
064: public Object getValue(String key) {
065: return attributes.get(key);
066: }
067:
068: public void putValue(String key, Object value) {
069: Object oldValue = attributes.put(key, value);
070: changeSupport.firePropertyChange(key, oldValue, value);
071: if (value instanceof String)
072: pref.put(key, (String) value);
073: else if (value instanceof Double)
074: pref.putDouble(key, (Double) value);
075: else if (value instanceof Boolean)
076: pref.putBoolean(key, (Boolean) value);
077: else if (value instanceof Integer)
078: pref.putInt(key, (Integer) value);
079: else if (value instanceof Long)
080: pref.putLong(key, (Long) value);
081: else if (value instanceof Float)
082: pref.putFloat(key, (Float) value);
083:
084: try {
085: pref.flush();
086: } catch (Exception ex) {
087: ex.printStackTrace();
088: }
089:
090: }
091:
092: public void addPropertyChangeListener(
093: PropertyChangeListener listener) {
094: changeSupport.addPropertyChangeListener(listener);
095: }
096:
097: public void removePropertyChangeListener(
098: PropertyChangeListener listener) {
099: changeSupport.removePropertyChangeListener(listener);
100: }
101:
102: public String toString() {
103: return (String) attributes.get(NAME);
104: }
105:
106: public ItemContainer getContainer() {
107: return container;
108: }
109:
110: public void setContainer(ItemContainer container) {
111: this .container = container;
112: container.setItem(this );
113:
114: }
115:
116: protected byte[] getByteArray(Object object) throws IOException {
117: ByteArrayOutputStream bos = new ByteArrayOutputStream();
118: ObjectOutputStream oos = new ObjectOutputStream(bos);
119: oos.writeObject(object);
120: oos.flush();
121: oos.close();
122: bos.close();
123: return bos.toByteArray();
124: }
125:
126: public void destroyed() {
127: try {
128: pref.removeNode();
129: //pref.flush();
130: } catch (BackingStoreException ex) {
131: ex.printStackTrace();
132: }
133:
134: }
135:
136: }
|