File: SourceListItem.java

package info (click to toggle)
mac-widgets 0.10.0%2Bsvn416-dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 2,003
  • sloc: java: 9,909; makefile: 13; sh: 12
file content (232 lines) | stat: -rw-r--r-- 6,675 bytes parent folder | download | duplicates (4)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.explodingpixels.macwidgets;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.Icon;

import com.explodingpixels.widgets.IconProvider;
import com.explodingpixels.widgets.TextProvider;

/**
 * An item in a {@link SourceList} which is contained within a {@link SourceListCategory}.
 */
public class SourceListItem implements TextProvider, IconProvider, SourceListBadgeContentProvider {

    public static final String TEXT = "text";
    public static final String ICON = "icon";
    public static final String COUNTER_VALUE = "counter";

    private List<SourceListItem> fChildItems = new ArrayList<SourceListItem>();

    private String fText;

    private Icon fIcon;

    private int fCounterValue;

    private PropertyChangeSupport fSupport = new PropertyChangeSupport(this);

    /**
     * Creates a {@code SourceListItem} with the given text.
     *
     * @param text the item text. Cannot be null.
     * @throws IllegalArgumentException if the text is null.
     */
    public SourceListItem(String text) {
        this(text, null);
    }

    /**
     * Creates a {@code SourceListItem} with the given text and icon.
     *
     * @param text the item text. Cannot be null.
     * @param icon the item icon. Can be null.
     * @throws IllegalArgumentException if the text is null.
     */
    public SourceListItem(String text, Icon icon) {
        checkText(text);
        fText = text;
        fIcon = icon;
    }

    /**
     * Gets the text to use for this item.
     *
     * @return the text to use for this item.
     */
    public String getText() {
        return fText;
    }

    /**
     * Sets the text to use for this item.
     *
     * @param text the text to use for this item. Cannot be null.
     * @throws IllegalArgumentException if the text is null.
     */
    public void setText(String text) {
        checkText(text);
        String oldText = fText;
        fText = text;
        fSupport.firePropertyChange(TEXT, oldText, fText);
    }

    /**
     * Gets the icon to use for this item.
     *
     * @return the icon to use for this item.
     */
    public Icon getIcon() {
        return fIcon;
    }

    /**
     * Sets the icon to use for this item.
     *
     * @param icon the icon to use for this item. Can be null.
     */
    public void setIcon(Icon icon) {
        Icon oldIcon = fIcon;
        fIcon = icon;
        fSupport.firePropertyChange(ICON, oldIcon, fIcon);
    }

    boolean hasCounterValue() {
        return getCounterValue() > 0;
    }

    /**
     * Gets the counter value to use for this item. The counter value will be displayed to the right
     * of the item.
     *
     * @return the counter value to use for this item.
     */
    public int getCounterValue() {
        return fCounterValue;
    }
    
    /**
     * {@inheritDoc}
     */
    public String getBadgeValue() {
    	return "" +  fCounterValue;
    }

    /**
     * Sets the counter value to use for this item. The counter value will be displayed to the right
     * of the item.
     *
     * @param counterValue the counter value to use for this item. Must be >= 0.
     * @throws IllegalArgumentException if the counter value is not >= 0.
     */
    public void setCounterValue(int counterValue) {
        checkCount(counterValue);
        int oldCounterValue = fCounterValue;
        fCounterValue = counterValue;
        fSupport.firePropertyChange(COUNTER_VALUE, oldCounterValue, fCounterValue);
    }

    /**
     * Returns {@code true} if the given {@link SourceListItem} is contained by this item, to
     * include being a sub-element of another child {@code SourceListItem}.
     *
     * @param item the {@code SourceListItem} to determine whether or not is contained by this
     *             item.
     * @return {@code true} if the given {@code SourceListItem} is contained within this item
     *         or within on of this items child {@code SourceListItem}s.
     */
    public boolean containsItem(SourceListItem item) {
        boolean contains = fChildItems.contains(item);
        if (!contains) {
            for (SourceListItem childItem : fChildItems) {
                contains = childItem.containsItem(item);
                if (contains) {
                    break;
                }
            }
        }
        return contains;
    }

    /**
     * Returns a {@link String} representation of this {@code SourceListItem}.
     *
     * @return a {@link String} representation of this {@code SourceListItem}.
     */
    @Override
    public String toString() {
        return getText();
    }

    /**
     * Gets a list of this {@code SourceListItem}'s child {@code SourceListItem}s.
     *
     * @return a list of this {@code SourceListItem}'s child {@code SourceListItem}s.
     */
    public List<SourceListItem> getChildItems() {
        return Collections.unmodifiableList(fChildItems);
    }

    int indexOfItem(SourceListItem item) {
        return fChildItems.indexOf(item);
    }

    int getChildCount() {
        return fChildItems.size();
    }

    void addItem(SourceListItem childItem) {
        fChildItems.add(childItem);
    }

    void addItem(int index, SourceListItem childItem) {
        fChildItems.add(index, childItem);
    }

    void removeItem(SourceListItem childItem) {
        fChildItems.remove(childItem);
    }

    SourceListItem removeItem(int index) {
        return fChildItems.remove(index);
    }

    // Property change support. ///////////////////////////////////////////////////////////////////

    /**
     * Adds a {@link PropertyChangeListener} on this {@code SourceListItem}.
     *
     * @param listener the listener to add.
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        fSupport.addPropertyChangeListener(listener);
    }

    /**
     * Removes a {@link PropertyChangeListener} from this {@code SourceListItem}.
     *
     * @param listener the listener to remove.
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        fSupport.removePropertyChangeListener(listener);
    }

    // Utility methods. ///////////////////////////////////////////////////////////////////////////

    private void checkCount(int count) {
        if (count < 0) {
            throw new IllegalArgumentException("Count must be zero or greater.");
        }
    }

    private void checkText(String text) {
        if (text == null) {
            throw new IllegalArgumentException("Text cannot be null.");
        }
    }

}