File: DefaultSelectable.java

package info (click to toggle)
libjide-oss-java 3.7.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,264 kB
  • sloc: java: 89,463; xml: 268; makefile: 35
file content (130 lines) | stat: -rw-r--r-- 3,668 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * @(#)InstallData.java 4/22/2005
 *
 * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
 */
package com.jidesoft.swing;

/**
 * The element used by CheckBoxList's ListModel. In order to allow check box in JList without messing up when list model
 * changes, we use this class to store the object itself and a boolean to indicated if the row is selected.
 */
public class DefaultSelectable implements Selectable {
    protected Object _object;
    protected boolean _selected = false;
    protected boolean _enabled = true;

    /**
     * Creates CheckBoxListElement with an actual object. In the case of CheckBoxList, instead of add the object
     * directly to ListModel, you should wrap it in CheckBoxListElement and add CheckBoxListElement into ListModel.
     *
     * @param object the actual object
     */
    public DefaultSelectable(Object object) {
        _object = object;
    }

    /**
     * Sets the actual element.
     *
     * @param object
     */
    public void setObject(Object object) {
        _object = object;
    }

    /**
     * Gets the actual element.
     *
     * @return the actual element.
     */
    public Object getObject() {
        return _object;
    }

    /**
     * Sets it as selected.
     *
     * @param selected
     */
    public void setSelected(boolean selected) {
        _selected = selected;
    }

    /**
     * Inverts the selection status.
     */
    public void invertSelected() {
        setSelected(!_selected);
    }

    /**
     * Gets the selected status.
     *
     * @return true if it is selected. Otherwise, false.
     */
    public boolean isSelected() {
        return _selected;
    }

    /**
     * Enabled selection change. Enabled false doesn't mean selected is false. If it is selected before,
     * setEnable(false) won't make selected become false. In the other word, setEnabled won't change the the value of
     * isSelected().
     *
     * @param enabled
     */
    public void setEnabled(boolean enabled) {
        _enabled = enabled;
    }

    /**
     * Checks if selection change is allowed.
     *
     * @return true if selection change is allowed.
     */
    public boolean isEnabled() {
        return _enabled;
    }

    /**
     * Overrides to consider the hash code of the object only. From outside point of view, this class should behave just
     * like object itself. That's why we override hashCode.
     *
     * @return the hash code.
     */
    @Override
    public int hashCode() {
        return (_object != null ? _object.hashCode() : 0);
    }

    /**
     * Overrides to consider the toString() of object only. From outside point of view, this class should behave just
     * like object itself. That's why we override toString.
     *
     * @return toString() of object.
     */
    @Override
    public String toString() {
        return (_object != null ? _object.toString() : "");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DefaultSelectable) {
            if (getObject() == null && ((DefaultSelectable) obj).getObject() == null) {
                return true;
            }
            else if (getObject() == null && ((DefaultSelectable) obj).getObject() != null) {
                return false;
            }
            return getObject().equals(((DefaultSelectable) obj).getObject());
        }
        else if (obj == null && getObject() == null) {
            return true;
        }
        else {
            return false;
        }
    }
}