KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > progress > spi > TaskModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.progress.spi;
22
23 import javax.swing.DefaultListModel JavaDoc;
24 import javax.swing.DefaultListSelectionModel JavaDoc;
25 import javax.swing.event.ListDataListener JavaDoc;
26 import javax.swing.event.ListSelectionListener JavaDoc;
27 import org.netbeans.progress.module.*;
28
29 /**
30  *
31  * @author Milos Kleint (mkleint@netbeans.org)
32  */

33 public final class TaskModel {
34     private DefaultListSelectionModel JavaDoc selectionModel;
35     private DefaultListModel JavaDoc model;
36     private InternalHandle explicit;
37     /** Creates a new instance of TaskModel */
38     public TaskModel() {
39         selectionModel = new DefaultListSelectionModel JavaDoc();
40         model = new DefaultListModel JavaDoc();
41     }
42     
43     
44     
45     public void addHandle(InternalHandle handle) {
46         model.addElement(handle);
47         if (handle.isUserInitialized() && explicit == null) {
48             selectionModel.setSelectionInterval(model.size() - 1, model.size() - 1);
49         }
50     }
51     
52     public void removeHandle(InternalHandle handle) {
53         if (explicit == handle) {
54             explicit = null;
55         }
56         int index = model.indexOf(handle);
57         if (selectionModel.getMinSelectionIndex() == index) {
58             // if we are removing the handle that is selected, do tricks with selection
59
// too figure out which one should be sleected now
60
changeSelection(index);
61         }
62         InternalHandle selectedHandle = getSelectedHandle();
63         model.removeElement(handle);
64         if (selectedHandle != null) {
65             selectionModel.setSelectionInterval(model.indexOf(selectedHandle), model.indexOf(selectedHandle));
66         } else {
67            //TODO what to do here?
68
selectionModel.clearSelection();
69         }
70         
71     }
72     
73     /**
74      * if we are removing the handle that is selected, do tricks with selection
75      * too figure out which one should be sleected no
76      */

77     private void changeSelection(int current) {
78         InternalHandle last = null;
79         for (int i = 0; i < model.size(); i++) {
80             if (current != i) {
81                 InternalHandle handle = (InternalHandle)model.getElementAt(i);
82                 if (handle.isUserInitialized()) {
83                     last = handle;
84                 } else if (last == null) {
85                     last = handle;
86                 }
87             }
88         }
89         if (last != null) {
90             selectionModel.setSelectionInterval(model.indexOf(last), model.indexOf(last));
91         } else {
92             selectionModel.clearSelection();
93         }
94         
95     }
96     
97     public void explicitlySelect(InternalHandle handle) {
98         explicit = handle;
99         int index = model.indexOf(explicit);
100         if (index == -1) {
101             //TODO what?
102
}
103         selectionModel.setSelectionInterval(index, index);
104     }
105     
106     public InternalHandle getExplicitSelection() {
107         return explicit;
108     }
109     
110     public int getSize() {
111         return model.size();
112     }
113            
114     
115     public InternalHandle[] getHandles() {
116         InternalHandle[] handles = new InternalHandle[model.size()];
117         model.copyInto(handles);
118         return handles;
119     }
120     
121     public InternalHandle getSelectedHandle() {
122         int select = selectionModel.getMinSelectionIndex();
123         if (select != -1) {
124             if (select >= 0 && select < model.size()) {
125                 return (InternalHandle)model.getElementAt(selectionModel.getMinSelectionIndex());
126             }
127         }
128         return null;
129     }
130     
131     public void addListSelectionListener(ListSelectionListener JavaDoc listener) {
132         selectionModel.addListSelectionListener(listener);
133     }
134     
135     public void removeListSelectionListener(ListSelectionListener JavaDoc listener) {
136         selectionModel.removeListSelectionListener(listener);
137     }
138     
139     public void addListDataListener(ListDataListener JavaDoc listener) {
140         model.addListDataListener(listener);
141     }
142     
143     public void removeListDataListener(ListDataListener JavaDoc listener) {
144         model.removeListDataListener(listener);
145     }
146 }
147
Popular Tags