1 19 20 21 package org.netbeans.progress.spi; 22 23 import javax.swing.DefaultListModel ; 24 import javax.swing.DefaultListSelectionModel ; 25 import javax.swing.event.ListDataListener ; 26 import javax.swing.event.ListSelectionListener ; 27 import org.netbeans.progress.module.*; 28 29 33 public final class TaskModel { 34 private DefaultListSelectionModel selectionModel; 35 private DefaultListModel model; 36 private InternalHandle explicit; 37 38 public TaskModel() { 39 selectionModel = new DefaultListSelectionModel (); 40 model = new DefaultListModel (); 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 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 selectionModel.clearSelection(); 69 } 70 71 } 72 73 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 } 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 listener) { 132 selectionModel.addListSelectionListener(listener); 133 } 134 135 public void removeListSelectionListener(ListSelectionListener listener) { 136 selectionModel.removeListSelectionListener(listener); 137 } 138 139 public void addListDataListener(ListDataListener listener) { 140 model.addListDataListener(listener); 141 } 142 143 public void removeListDataListener(ListDataListener listener) { 144 model.removeListDataListener(listener); 145 } 146 } 147 | Popular Tags |