1 26 27 package swingwtx.swing; 28 29 import swingwtx.swing.event.*; 30 31 import java.util.*; 32 33 43 public class DefaultListSelectionModel implements ListSelectionModel { 44 45 52 protected JComponent component = null; 53 protected JTable table = null; 54 protected JList list = null; 55 protected boolean isTable = true; 56 protected Vector listeners = new Vector(); 57 protected int index0 = -1; 58 protected int index1 = -1; 59 protected int mode = ListSelectionModel.SINGLE_SELECTION; 60 protected boolean isAdjusting = false; 61 62 public DefaultListSelectionModel() {} 63 protected DefaultListSelectionModel(JComponent component) { 64 this.component = component; 65 isTable = component instanceof JTable; 66 if (isTable) 67 table = (JTable) component; 68 else 69 list = (JList) component; 70 } 71 72 public void addListSelectionListener(ListSelectionListener x) { 73 listeners.add(x); 74 } 75 76 public void addSelectionInterval(int index0, int index1) { 77 setSelectionInterval(index0, index1); 78 } 79 80 public void clearSelection() { 81 if (isTable) 82 table.clearSelection(); 83 else 84 list.clearSelection(); 85 } 86 87 public int getAnchorSelectionIndex() { 88 return index0; 89 } 90 91 public int getLeadSelectionIndex() { 92 return index1; 93 } 94 95 public int getMaxSelectionIndex() { 96 return index1; 97 } 98 99 public int getMinSelectionIndex() { 100 return index0; 101 } 102 103 public int getSelectionMode() { 104 return mode; 105 } 106 107 public boolean getValueIsAdjusting() { 108 return isAdjusting; 109 } 110 111 public void insertIndexInterval(int index, int length, boolean before) { 112 } 113 114 public boolean isSelectedIndex(int index) { 115 return index >= index0 && index <= index1; 116 } 117 118 public boolean isSelectionEmpty() { 119 return ((index0 == -1) && (index1 == -1)); 120 } 121 122 public void removeIndexInterval(int index0, int index1) { 123 index0 = -1; index1 = -1; 124 } 125 126 public void removeListSelectionListener(swingwtx.swing.event.ListSelectionListener x) { 127 listeners.remove(x); 128 } 129 130 public void removeSelectionInterval(int index0, int index1) { 131 index0 = -1; index1 = -1; 132 } 133 134 public void setAnchorSelectionIndex(int index) { 135 index0 = index; 136 updateComponentForSelection(); 137 } 138 139 public void setLeadSelectionIndex(int index) { 140 index1 = index; 141 updateComponentForSelection(); 142 } 143 144 public void setSelectionInterval(int index0, int index1) { 145 this.index0 = index0; this.index1 = index1; 146 updateComponentForSelection(); 147 } 148 149 public void setSelectionMode(int selectionMode) { 150 mode = selectionMode; 151 if (isTable) { 152 table.setModelDirty(true); 153 table.refreshTable(); 154 } 155 } 156 157 public void setValueIsAdjusting(boolean valueIsAdjusting) { 158 isAdjusting = valueIsAdjusting; 159 } 160 161 165 protected void updateComponentForSelection() { 166 if (isTable) { 167 if (mode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) 168 for (int i = index0; i <= index1; i++) 169 table.changeSelection(i, 0, true, false); 170 } 171 else { 172 if (mode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) { 173 int s[] = new int[index1 - index0 + 1]; 174 for (int i = 0; i < s.length; i++) 175 s[i] = index0 + 1; 176 list.setSelectedIndices(s); 177 } 178 } 179 } 180 181 189 void fireListSelectionEvent(Object source, int index0, int index1) { 190 191 this.index0 = index0; 192 this.index1 = index1; 193 194 if (listeners.size() == 0) return; 196 197 ListSelectionEvent e = new ListSelectionEvent(this, index0, index1, false); 199 for (int i = 0; i < listeners.size(); i++) { 200 ((ListSelectionListener) listeners.get(i)).valueChanged(e); 201 } 202 } 203 204 } 205 | Popular Tags |