59 private static final int PAINT_HSCROLL = 4;
60 private static final int PAINT_ITEMS = 8;
61 private static final int PAINT_FOCUS = 16;
62 private static final int PAINT_BACKGROUND = 32;
63 private static final int PAINT_HIDEFOCUS = 64;
64 private static final int PAINT_ALL =
65 PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND;
66 private static final int COPY_AREA = 128;
67
68 XVerticalScrollbar vsb;
69 XHorizontalScrollbar hsb;
70 ListPainter painter;
71
72 // TODO: ick - Vector?
73 Vector<String> items;
74 boolean multipleSelections;
75 int active = NONE;
76
77 // Holds the array of the indexes of the elements which is selected
78 // This array should be kept sorted, low to high.
79 int selected[];
80 int fontHeight;
81 int fontAscent;
82 int fontLeading;
83
84 // Holds the index of the item used in the previous operation (selectItem, deselectItem)
85 // Adding of an item or clearing of the list sets this index to -1
86 // The index is used at the moment of the post of ACTION_PERFORMED event after the mouse double click event.
87 int currentIndex = -1;
88
89 // Used for tracking selection/deselection between mousePress/Release
90 // and for ItemEvents
91 int eventIndex = -1;
92 int eventType = NONE;
93
94 // Holds the index of the item that receive focus
95 // This variable is reasonable only for multiple list
96 // since 'focusIndex' and 'selected[0]' are equal for single-selection list
97 int focusIndex;
98
99 int maxLength;
157 // do we really want/need a copy of all the items?
158 // get all items from target
159 List l = (List)target;
160 int stop = l.getItemCount();
161 for (int i = 0 ; i < stop; i++) {
162 items.addElement(l.getItem(i));
163 }
164
165 /* make the visible position visible. */
166 int index = l.getVisibleIndex();
167 if (index >= 0) {
168 // Can't call makeVisible since it check scroll bar,
169 // initialize scroll bar instead
170 vsb.setValues(index, 0, 0, items.size());
171 }
172
173 // NOTE: needs to have target set
174 maxLength = maxLength();
175
176 // get the index containing all indexes to selected items
177 int sel[] = l.getSelectedIndexes();
178 selected = new int[sel.length];
179 // TODO: shouldn't this be arraycopy()?
180 for (int i = 0 ; i < sel.length ; i ++) {
181 selected[i] = sel[i];
182 }
183 // The select()ed item should become the focused item, but we don't
184 // get the select() call because the peer generally hasn't yet been
185 // created during app initialization.
186 // TODO: For multi-select lists, it should be the highest selected index
187 if (sel.length > 0) {
188 setFocusIndex(sel[sel.length - 1]);
189 }
190 else {
191 setFocusIndex(0);
192 }
193
194 multipleSelections = l.isMultipleMode();
195 }
196
197
1130
1131 // check for end point greater than the size of the list
1132 if (e >= items.size()) {
1133 e = items.size() - 1;
1134 }
1135
1136 // determine whether we're going to delete any visible elements
1137 // repaint must also be done if scrollbars appear/disappear, which
1138 // can happen from removing a non-showing list item
1139 /*
1140 boolean repaintNeeded =
1141 ((s <= lastItemDisplayed()) && (e >= vsb.getValue()));
1142 */
1143 boolean repaintNeeded = (s >= getFirstVisibleItem() && s <= getLastVisibleItem());
1144
1145 // delete the items out of the items list and out of the selected list
1146 for (int i = s ; i <= e ; i++) {
1147 items.removeElementAt(s);
1148 int j = posInSel(i);
1149 if (j != -1) {
1150 int newsel[] = new int[selected.length - 1];
1151 System.arraycopy(selected, 0, newsel, 0, j);
1152 System.arraycopy(selected, j + 1, newsel, j, selected.length - (j + 1));
1153 selected = newsel;
1154 }
1155
1156 }
1157
1158 // update the indexes in the selected array
1159 int diff = (e - s) + 1;
1160 for (int i = 0 ; i < selected.length ; i++) {
1161 if (selected[i] > e) {
1162 selected[i] -= diff;
1163 }
1164 }
1165
1166 int options = PAINT_VSCROLL;
1167 // focusedIndex updating according to native (Window, Motif) behaviour
1168 if (getFocusIndex() > e) {
1169 setFocusIndex(getFocusIndex() - (e - s + 1));
1170 options |= PAINT_FOCUS;
1229 currentIndex = index;
1230
1231 if (isSelected(index)) {
1232 return;
1233 }
1234 if (!multipleSelections) {
1235 if (selected.length == 0) { // No current selection
1236 selected = new int[1];
1237 selected[0] = index;
1238 }
1239 else {
1240 int oldSel = selected[0];
1241 selected[0] = index;
1242 if (!isItemHidden(oldSel)) {
1243 // Only bother painting if item is visible (4895367)
1244 repaint(oldSel, oldSel, PAINT_ITEMS);
1245 }
1246 }
1247 } else {
1248 // insert "index" into the selection array
1249 int newsel[] = new int[selected.length + 1];
1250 int i = 0;
1251 while (i < selected.length && index > selected[i]) {
1252 newsel[i] = selected[i];
1253 i++;
1254 }
1255 newsel[i] = index;
1256 System.arraycopy(selected, i, newsel, i+1, selected.length - i);
1257 selected = newsel;
1258 }
1259 if (!isItemHidden(index)) {
1260 // Only bother painting if item is visible (4895367)
1261 repaint(index, index, PAINT_ITEMS);
1262 }
1263 }
1264
1265 /**
1266 * ListPeer method
1267 * focusedIndex isn't updated according to native (Window, Motif) behaviour
1268 */
1269 public void deselect(int index) {
1270 deselectItem(index);
1271 }
1272
1273 /**
1274 * deselect the index
1275 * redraw the list to the screen
1276 */
1277 void deselectItem(int index) {
1278 if (!isSelected(index)) {
1279 return;
1280 }
1281 if (!multipleSelections) {
1282 // TODO: keep an int[0] and int[1] around and just use them instead
1283 // creating new ones all the time
1284 selected = new int[0];
1285 } else {
1286 int i = posInSel(index);
1287 int newsel[] = new int[selected.length - 1];
1288 System.arraycopy(selected, 0, newsel, 0, i);
1289 System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1));
1290 selected = newsel;
1291 }
1292 currentIndex = index;
1293 if (!isItemHidden(index)) {
1294 // Only bother repainting if item is visible
1295 repaint(index, index, PAINT_ITEMS);
1296 }
1297 }
1298
1299 /**
1300 * ensure that the given index is visible, scrolling the List
1301 * if necessary, or doing nothing if the item is already visible.
1302 * The List must be repainted for changes to be visible.
1303 */
1304 public void makeVisible(int index) {
1305 if (index < 0 || index >= items.size()) {
1306 return;
1307 }
|
59 private static final int PAINT_HSCROLL = 4;
60 private static final int PAINT_ITEMS = 8;
61 private static final int PAINT_FOCUS = 16;
62 private static final int PAINT_BACKGROUND = 32;
63 private static final int PAINT_HIDEFOCUS = 64;
64 private static final int PAINT_ALL =
65 PAINT_VSCROLL | PAINT_HSCROLL | PAINT_ITEMS | PAINT_FOCUS | PAINT_BACKGROUND;
66 private static final int COPY_AREA = 128;
67
68 XVerticalScrollbar vsb;
69 XHorizontalScrollbar hsb;
70 ListPainter painter;
71
72 // TODO: ick - Vector?
73 Vector<String> items;
74 boolean multipleSelections;
75 int active = NONE;
76
77 // Holds the array of the indexes of the elements which is selected
78 // This array should be kept sorted, low to high.
79 int[] selected;
80 int fontHeight;
81 int fontAscent;
82 int fontLeading;
83
84 // Holds the index of the item used in the previous operation (selectItem, deselectItem)
85 // Adding of an item or clearing of the list sets this index to -1
86 // The index is used at the moment of the post of ACTION_PERFORMED event after the mouse double click event.
87 int currentIndex = -1;
88
89 // Used for tracking selection/deselection between mousePress/Release
90 // and for ItemEvents
91 int eventIndex = -1;
92 int eventType = NONE;
93
94 // Holds the index of the item that receive focus
95 // This variable is reasonable only for multiple list
96 // since 'focusIndex' and 'selected[0]' are equal for single-selection list
97 int focusIndex;
98
99 int maxLength;
157 // do we really want/need a copy of all the items?
158 // get all items from target
159 List l = (List)target;
160 int stop = l.getItemCount();
161 for (int i = 0 ; i < stop; i++) {
162 items.addElement(l.getItem(i));
163 }
164
165 /* make the visible position visible. */
166 int index = l.getVisibleIndex();
167 if (index >= 0) {
168 // Can't call makeVisible since it check scroll bar,
169 // initialize scroll bar instead
170 vsb.setValues(index, 0, 0, items.size());
171 }
172
173 // NOTE: needs to have target set
174 maxLength = maxLength();
175
176 // get the index containing all indexes to selected items
177 int[] sel = l.getSelectedIndexes();
178 selected = new int[sel.length];
179 // TODO: shouldn't this be arraycopy()?
180 for (int i = 0 ; i < sel.length ; i ++) {
181 selected[i] = sel[i];
182 }
183 // The select()ed item should become the focused item, but we don't
184 // get the select() call because the peer generally hasn't yet been
185 // created during app initialization.
186 // TODO: For multi-select lists, it should be the highest selected index
187 if (sel.length > 0) {
188 setFocusIndex(sel[sel.length - 1]);
189 }
190 else {
191 setFocusIndex(0);
192 }
193
194 multipleSelections = l.isMultipleMode();
195 }
196
197
1130
1131 // check for end point greater than the size of the list
1132 if (e >= items.size()) {
1133 e = items.size() - 1;
1134 }
1135
1136 // determine whether we're going to delete any visible elements
1137 // repaint must also be done if scrollbars appear/disappear, which
1138 // can happen from removing a non-showing list item
1139 /*
1140 boolean repaintNeeded =
1141 ((s <= lastItemDisplayed()) && (e >= vsb.getValue()));
1142 */
1143 boolean repaintNeeded = (s >= getFirstVisibleItem() && s <= getLastVisibleItem());
1144
1145 // delete the items out of the items list and out of the selected list
1146 for (int i = s ; i <= e ; i++) {
1147 items.removeElementAt(s);
1148 int j = posInSel(i);
1149 if (j != -1) {
1150 int[] newsel = new int[selected.length - 1];
1151 System.arraycopy(selected, 0, newsel, 0, j);
1152 System.arraycopy(selected, j + 1, newsel, j, selected.length - (j + 1));
1153 selected = newsel;
1154 }
1155
1156 }
1157
1158 // update the indexes in the selected array
1159 int diff = (e - s) + 1;
1160 for (int i = 0 ; i < selected.length ; i++) {
1161 if (selected[i] > e) {
1162 selected[i] -= diff;
1163 }
1164 }
1165
1166 int options = PAINT_VSCROLL;
1167 // focusedIndex updating according to native (Window, Motif) behaviour
1168 if (getFocusIndex() > e) {
1169 setFocusIndex(getFocusIndex() - (e - s + 1));
1170 options |= PAINT_FOCUS;
1229 currentIndex = index;
1230
1231 if (isSelected(index)) {
1232 return;
1233 }
1234 if (!multipleSelections) {
1235 if (selected.length == 0) { // No current selection
1236 selected = new int[1];
1237 selected[0] = index;
1238 }
1239 else {
1240 int oldSel = selected[0];
1241 selected[0] = index;
1242 if (!isItemHidden(oldSel)) {
1243 // Only bother painting if item is visible (4895367)
1244 repaint(oldSel, oldSel, PAINT_ITEMS);
1245 }
1246 }
1247 } else {
1248 // insert "index" into the selection array
1249 int[] newsel = new int[selected.length + 1];
1250 int i = 0;
1251 while (i < selected.length && index > selected[i]) {
1252 newsel[i] = selected[i];
1253 i++;
1254 }
1255 newsel[i] = index;
1256 System.arraycopy(selected, i, newsel, i+1, selected.length - i);
1257 selected = newsel;
1258 }
1259 if (!isItemHidden(index)) {
1260 // Only bother painting if item is visible (4895367)
1261 repaint(index, index, PAINT_ITEMS);
1262 }
1263 }
1264
1265 /**
1266 * ListPeer method
1267 * focusedIndex isn't updated according to native (Window, Motif) behaviour
1268 */
1269 public void deselect(int index) {
1270 deselectItem(index);
1271 }
1272
1273 /**
1274 * deselect the index
1275 * redraw the list to the screen
1276 */
1277 void deselectItem(int index) {
1278 if (!isSelected(index)) {
1279 return;
1280 }
1281 if (!multipleSelections) {
1282 // TODO: keep an int[0] and int[1] around and just use them instead
1283 // creating new ones all the time
1284 selected = new int[0];
1285 } else {
1286 int i = posInSel(index);
1287 int[] newsel = new int[selected.length - 1];
1288 System.arraycopy(selected, 0, newsel, 0, i);
1289 System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1));
1290 selected = newsel;
1291 }
1292 currentIndex = index;
1293 if (!isItemHidden(index)) {
1294 // Only bother repainting if item is visible
1295 repaint(index, index, PAINT_ITEMS);
1296 }
1297 }
1298
1299 /**
1300 * ensure that the given index is visible, scrolling the List
1301 * if necessary, or doing nothing if the item is already visible.
1302 * The List must be repainted for changes to be visible.
1303 */
1304 public void makeVisible(int index) {
1305 if (index < 0 || index >= items.size()) {
1306 return;
1307 }
|