This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 192420
Collapse All | Expand All

(-)api.progress/src/org/netbeans/modules/progress/spi/TaskModel.java (-29 / +128 lines)
Lines 45-53 Link Here
45
45
46
package org.netbeans.modules.progress.spi;
46
package org.netbeans.modules.progress.spi;
47
47
48
import java.awt.EventQueue;
49
import java.util.LinkedHashSet;
48
import javax.swing.DefaultListModel;
50
import javax.swing.DefaultListModel;
49
import javax.swing.DefaultListSelectionModel;
51
import javax.swing.DefaultListSelectionModel;
52
import javax.swing.event.ListDataEvent;
50
import javax.swing.event.ListDataListener;
53
import javax.swing.event.ListDataListener;
54
import javax.swing.event.ListSelectionEvent;
51
import javax.swing.event.ListSelectionListener;
55
import javax.swing.event.ListSelectionListener;
52
56
53
/**
57
/**
Lines 57-74 Link Here
57
 */
61
 */
58
public final class TaskModel {
62
public final class TaskModel {
59
    private DefaultListSelectionModel selectionModel;
63
    private DefaultListSelectionModel selectionModel;
60
    private DefaultListModel model;
64
    private final DefaultListModel model;
61
    private InternalHandle explicit;
65
    private InternalHandle explicit;
66
    private final LinkedHashSet<ListDataListener> dataListeners;
67
    private final LinkedHashSet<ListSelectionListener> selectionListeners;
62
    /** Creates a new instance of TaskModel */
68
    /** Creates a new instance of TaskModel */
63
    public TaskModel() {
69
    public TaskModel() {
64
        selectionModel = new DefaultListSelectionModel();
70
        selectionModel = new DefaultListSelectionModel();
65
        model = new DefaultListModel();
71
        model = new DefaultListModel();
72
        dataListeners = new LinkedHashSet<ListDataListener>();
73
        selectionListeners = new LinkedHashSet<ListSelectionListener>();
74
        TaskListener list = new TaskListener();
75
        model.addListDataListener(list);
76
        selectionModel.addListSelectionListener(list);
66
    }
77
    }
67
    
78
    
68
    
79
    
69
    
80
    
70
    public void addHandle(InternalHandle handle) {
81
    public void addHandle(InternalHandle handle) {
71
        model.addElement(handle);
82
        synchronized (model) {
83
            model.addElement(handle);
84
        }
72
        updateSelection();
85
        updateSelection();
73
    }
86
    }
74
    
87
    
Lines 76-82 Link Here
76
        if (explicit == handle) {
89
        if (explicit == handle) {
77
            explicit = null;
90
            explicit = null;
78
        }
91
        }
79
        model.removeElement(handle);
92
        synchronized (model) {
93
            model.removeElement(handle);
94
        }
80
        updateSelection();
95
        updateSelection();
81
    }
96
    }
82
    
97
    
Lines 96-112 Link Here
96
111
97
        // select last added that is not in sleep mode and preferrably userInitiated
112
        // select last added that is not in sleep mode and preferrably userInitiated
98
        InternalHandle toSelect = null;
113
        InternalHandle toSelect = null;
99
        for (int i = 0; i < model.size(); i++) {
114
        synchronized (model) {
100
            InternalHandle curHandle = (InternalHandle)model.getElementAt(i);
115
            for (int i = 0; i < model.size(); i++) {
101
            if (getSelectionRating(curHandle) >= getSelectionRating(toSelect)) {
116
                InternalHandle curHandle = (InternalHandle) model.getElementAt(i);
102
                toSelect = curHandle;
117
                if (getSelectionRating(curHandle) >= getSelectionRating(toSelect)) {
118
                    toSelect = curHandle;
119
                }
103
            }
120
            }
121
            if (toSelect != null) {
122
                selectionModel.setSelectionInterval(model.indexOf(toSelect), model.indexOf(toSelect));
123
            } else {
124
                selectionModel.clearSelection();
125
            }
104
        }
126
        }
105
        if (toSelect != null) {
106
            selectionModel.setSelectionInterval(model.indexOf(toSelect), model.indexOf(toSelect));
107
        } else {
108
            selectionModel.clearSelection();
109
        }
110
    }
127
    }
111
128
112
    private int getSelectionRating (InternalHandle handle) {
129
    private int getSelectionRating (InternalHandle handle) {
Lines 125-135 Link Here
125
    
142
    
126
    public void explicitlySelect(InternalHandle handle) {
143
    public void explicitlySelect(InternalHandle handle) {
127
        explicit = handle;
144
        explicit = handle;
128
        int index = model.indexOf(explicit);
145
        synchronized (model) {
129
        if (index == -1) {
146
            int index = model.indexOf(explicit);
130
            //TODO what?
147
            if (index == -1) {
148
                return;
149
            }
150
            selectionModel.setSelectionInterval(index, index);
131
        }
151
        }
132
        selectionModel.setSelectionInterval(index, index);
133
    }
152
    }
134
    
153
    
135
    public InternalHandle getExplicitSelection() {
154
    public InternalHandle getExplicitSelection() {
Lines 137-175 Link Here
137
    }
156
    }
138
    
157
    
139
    public int getSize() {
158
    public int getSize() {
140
        return model.size();
159
        synchronized (model) {
160
            return model.size();
161
        }
141
    }
162
    }
142
           
163
           
143
    
164
    
144
    public InternalHandle[] getHandles() {
165
    public InternalHandle[] getHandles() {
145
        InternalHandle[] handles = new InternalHandle[model.size()];
166
        InternalHandle[] handles;
146
        model.copyInto(handles);
167
        synchronized (model) {
168
            handles = new InternalHandle[model.size()];
169
            model.copyInto(handles);
170
        }
147
        return handles;
171
        return handles;
148
    }
172
    }
149
    
173
    
150
    public InternalHandle getSelectedHandle() {
174
    public InternalHandle getSelectedHandle() {
151
        int select = selectionModel.getMinSelectionIndex();
175
        synchronized (model) {
152
        if (select != -1) {
176
            int select = selectionModel.getMinSelectionIndex();
153
            if (select >= 0 && select < model.size()) {
177
            if (select != -1) {
154
                return (InternalHandle)model.getElementAt(selectionModel.getMinSelectionIndex());
178
                if (select >= 0 && select < model.size()) {
179
                    return (InternalHandle) model.getElementAt(select);
180
                }
155
            }
181
            }
156
        }
182
        }
157
        return null;
183
        return null;
158
    }
184
    }
159
    
185
    
160
    public void addListSelectionListener(ListSelectionListener listener) {
186
    public void addListSelectionListener(ListSelectionListener listener) {        
161
        selectionModel.addListSelectionListener(listener);
187
        synchronized (selectionListeners) {
188
            selectionListeners.add(listener);
189
        }
162
    }
190
    }
163
    
191
    
164
    public void removeListSelectionListener(ListSelectionListener listener) {
192
    public void removeListSelectionListener(ListSelectionListener listener) {
165
        selectionModel.removeListSelectionListener(listener);
193
        synchronized (selectionListeners) {
194
            selectionListeners.remove(listener);
195
        }
166
    }
196
    }
167
    
197
    
168
    public void addListDataListener(ListDataListener listener) {
198
    public void addListDataListener(ListDataListener listener) {
169
        model.addListDataListener(listener);
199
        synchronized (dataListeners) {
200
            dataListeners.add(listener);
201
        }
170
    }
202
    }
171
    
203
    
172
    public void removeListDataListener(ListDataListener listener) {
204
    public void removeListDataListener(ListDataListener listener) {        
173
        model.removeListDataListener(listener);
205
        synchronized (dataListeners) {
206
            dataListeners.remove(listener);
207
        }
174
    }
208
    }
209
    
210
    private ListDataListener[] getDataListeners() {
211
        synchronized (dataListeners) {
212
            return dataListeners.toArray(new ListDataListener[dataListeners.size()]);
213
        }
214
    } 
215
    
216
    private ListSelectionListener[] getSelectionListeners() {
217
        synchronized (selectionListeners) {
218
            return selectionListeners.toArray(new ListSelectionListener[selectionListeners.size()]);
219
        }
220
    }
221
     
222
     
223
    private class TaskListener implements ListDataListener, ListSelectionListener {
224
225
        @Override
226
        public void intervalAdded(final ListDataEvent e) {
227
            EventQueue.invokeLater(new Runnable() {
228
                @Override
229
                public void run() {
230
                    for (ListDataListener list : getDataListeners()) {
231
                        list.intervalAdded(e);
232
                    }
233
                }
234
            });
235
        }
236
237
        @Override
238
        public void intervalRemoved(final ListDataEvent e) {
239
            EventQueue.invokeLater(new Runnable() {
240
                @Override
241
                public void run() {
242
                    for (ListDataListener list : getDataListeners()) {
243
                        list.intervalRemoved(e);
244
                    }
245
                }
246
            });
247
        }
248
249
        @Override
250
        public void contentsChanged(final ListDataEvent e) {
251
             EventQueue.invokeLater(new Runnable() {
252
                @Override
253
                public void run() {
254
                    for (ListDataListener list : getDataListeners()) {
255
                        list.contentsChanged(e);
256
                    }
257
                }
258
            });
259
        }
260
261
        @Override
262
        public void valueChanged(final ListSelectionEvent e) {
263
             EventQueue.invokeLater(new Runnable() {
264
                @Override
265
                public void run() {
266
                    for (ListSelectionListener list : getSelectionListeners()) {
267
                        list.valueChanged(e);
268
                    }
269
                }
270
            });
271
        }
272
        
273
    }
175
}
274
}

Return to bug 192420