001: /*
002: * @(#)InstallData.java 4/22/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: /**
009: * The element used by CheckBoxList's ListModel. In order to allow check box
010: * in JList without messing up when list model changes, we use this class
011: * to store the object itself and a boolean to indicated if the row is selected.
012: */
013: public class DefaultSelectable implements Selectable {
014: protected Object _object;
015: protected boolean _selected = false;
016: protected boolean _enabled = true;
017:
018: /**
019: * Creates CheckBoxListElement with an actual object. In the case of CheckBoxList,
020: * instead of add the object directly to ListModel, you should wrap it in CheckBoxListElement and
021: * add CheckBoxListElement into ListModel.
022: *
023: * @param object the actual object
024: */
025: public DefaultSelectable(Object object) {
026: _object = object;
027: }
028:
029: /**
030: * Sets the actual element.
031: *
032: * @param object
033: */
034: public void setObject(Object object) {
035: _object = object;
036: }
037:
038: /**
039: * Gets the actual element.
040: *
041: * @return the actual element.
042: */
043: public Object getObject() {
044: return _object;
045: }
046:
047: /**
048: * Sets it as selected.
049: *
050: * @param selected
051: */
052: public void setSelected(boolean selected) {
053: _selected = selected;
054: }
055:
056: /**
057: * Inverts the selection status.
058: */
059: public void invertSelected() {
060: setSelected(!_selected);
061: }
062:
063: /**
064: * Gets the selected status.
065: *
066: * @return true if it is selected. Otherwise, false.
067: */
068: public boolean isSelected() {
069: return _selected;
070: }
071:
072: /**
073: * Enabled selection change. Enabled false doesn't mean selected is false. If it is selected before,
074: * setEnable(false) won't make selected beceome false. In the other word, setEnabled won't change the
075: * the value of isSelected().
076: *
077: * @param enabled
078: */
079: public void setEnabled(boolean enabled) {
080: _enabled = enabled;
081: }
082:
083: /**
084: * Checks if selection change is allowed.
085: *
086: * @return true if selection change is allowed.
087: */
088: public boolean isEnabled() {
089: return _enabled;
090: }
091:
092: /**
093: * Overrides to consider the hash code of the object only. From outside point of view,
094: * this class should behave just like object itself. That's why we override hashCode.
095: *
096: * @return the hash code.
097: */
098: @Override
099: public int hashCode() {
100: return (_object != null ? _object.hashCode() : 0);
101: }
102:
103: /**
104: * Overrides to consider the toString() of object only. From outside point of view,
105: * this class should behave just like object itself. That's why we override toString.
106: *
107: * @return toString() of object.
108: */
109: @Override
110: public String toString() {
111: return (_object != null ? _object.toString() : "");
112: }
113:
114: @Override
115: public boolean equals(Object obj) {
116: if (obj instanceof DefaultSelectable) {
117: if (getObject() == null
118: && ((DefaultSelectable) obj).getObject() == null) {
119: return true;
120: } else if (getObject() == null
121: && ((DefaultSelectable) obj).getObject() != null) {
122: return false;
123: }
124: return getObject().equals(
125: ((DefaultSelectable) obj).getObject());
126: } else if (obj == null && getObject() == null) {
127: return true;
128: } else {
129: return false;
130: }
131: }
132: }
|