2117 // this is done to restore the anchor and lead 2118 SwingUtilities2.setLeadAnchorWithoutSelection(lsm, anchor, lead); 2119 2120 list.setValueIsAdjusting(false); 2121 } 2122 } 2123 } 2124 2125 private int getNextPageIndex(JList<?> list, int direction) { 2126 if (list.getModel().getSize() == 0) { 2127 return -1; 2128 } 2129 2130 int index = -1; 2131 Rectangle visRect = list.getVisibleRect(); 2132 ListSelectionModel lsm = list.getSelectionModel(); 2133 int lead = adjustIndex(lsm.getLeadSelectionIndex(), list); 2134 Rectangle leadRect = 2135 (lead==-1) ? new Rectangle() : list.getCellBounds(lead, lead); 2136 2137 if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && 2138 list.getVisibleRowCount() <= 0) { 2139 if (!list.getComponentOrientation().isLeftToRight()) { 2140 direction = -direction; 2141 } 2142 // apply for horizontal scrolling: the step for next 2143 // page index is number of visible columns 2144 if (direction < 0) { 2145 // left 2146 visRect.x = leadRect.x + leadRect.width - visRect.width; 2147 Point p = new Point(visRect.x - 1, leadRect.y); 2148 index = list.locationToIndex(p); 2149 Rectangle cellBounds = list.getCellBounds(index, index); 2150 if (visRect.intersects(cellBounds)) { 2151 p.x = cellBounds.x - 1; 2152 index = list.locationToIndex(p); 2153 cellBounds = list.getCellBounds(index, index); 2154 } 2155 // this is necessary for right-to-left orientation only 2156 if (cellBounds.y != leadRect.y) { 2157 p.x = cellBounds.x + cellBounds.width; 2158 index = list.locationToIndex(p); 2159 } 2160 } 2161 else { 2162 // right 2163 visRect.x = leadRect.x; 2164 Point p = new Point(visRect.x + visRect.width, leadRect.y); 2165 index = list.locationToIndex(p); 2166 Rectangle cellBounds = list.getCellBounds(index, index); 2167 if (visRect.intersects(cellBounds)) { 2168 p.x = cellBounds.x + cellBounds.width; 2169 index = list.locationToIndex(p); 2170 cellBounds = list.getCellBounds(index, index); 2171 } 2172 if (cellBounds.y != leadRect.y) { 2173 p.x = cellBounds.x - 1; 2174 index = list.locationToIndex(p); 2175 } 2176 } 2177 } 2178 else { 2179 if (direction < 0) { 2180 // up 2181 // go to the first visible cell 2182 Point p = new Point(leadRect.x, visRect.y); 2183 index = list.locationToIndex(p); 2184 if (lead <= index) { 2185 // if lead is the first visible cell (or above it) 2186 // adjust the visible rect up 2187 visRect.y = leadRect.y + leadRect.height - visRect.height; 2188 p.y = visRect.y; 2189 index = list.locationToIndex(p); 2190 Rectangle cellBounds = list.getCellBounds(index, index); 2191 // go one cell down if first visible cell doesn't fit 2192 // into adjasted visible rectangle 2193 if (cellBounds.y < visRect.y) { 2194 p.y = cellBounds.y + cellBounds.height; 2195 index = list.locationToIndex(p); 2196 cellBounds = list.getCellBounds(index, index); 2197 } 2198 // if index isn't less then lead 2199 // try to go to cell previous to lead 2200 if (cellBounds.y >= leadRect.y) { 2201 p.y = leadRect.y - 1; 2202 index = list.locationToIndex(p); 2203 } 2204 } 2205 } 2206 else { 2207 // down 2208 // go to the last completely visible cell 2209 Point p = new Point(leadRect.x, 2210 visRect.y + visRect.height - 1); 2211 index = list.locationToIndex(p); 2212 Rectangle cellBounds = list.getCellBounds(index, index); 2213 // go up one cell if last visible cell doesn't fit 2214 // into visible rectangle 2215 if (cellBounds.y + cellBounds.height > 2216 visRect.y + visRect.height) { 2217 p.y = cellBounds.y - 1; 2218 index = list.locationToIndex(p); 2219 cellBounds = list.getCellBounds(index, index); 2220 index = Math.max(index, lead); 2221 } 2222 2223 if (lead >= index) { 2224 // if lead is the last completely visible index 2225 // (or below it) adjust the visible rect down 2226 visRect.y = leadRect.y; 2227 p.y = visRect.y + visRect.height - 1; 2228 index = list.locationToIndex(p); 2229 cellBounds = list.getCellBounds(index, index); 2230 // go one cell up if last visible cell doesn't fit 2231 // into adjasted visible rectangle 2232 if (cellBounds.y + cellBounds.height > 2233 visRect.y + visRect.height) { 2234 p.y = cellBounds.y - 1; 2235 index = list.locationToIndex(p); 2236 cellBounds = list.getCellBounds(index, index); 2237 } 2238 // if index isn't greater then lead 2239 // try to go to cell next after lead 2240 if (cellBounds.y <= leadRect.y) { 2241 p.y = leadRect.y + leadRect.height; 2242 index = list.locationToIndex(p); 2243 } 2244 } 2245 } 2246 } 2247 return index; 2248 } 2249 2250 private void changeSelection(JList<?> list, int type, 2251 int index, int direction) { 2252 if (index >= 0 && index < list.getModel().getSize()) { 2253 ListSelectionModel lsm = list.getSelectionModel(); 2254 2255 // CHANGE_LEAD is only valid with multiple interval selection 2256 if (type == CHANGE_LEAD && 2257 list.getSelectionMode() 2258 != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) { 2259 2260 type = CHANGE_SELECTION; 2261 } 2262 2263 // IMPORTANT - This needs to happen before the index is changed. 2264 // This is because JFileChooser, which uses JList, also scrolls 2265 // the selected item into view. If that happens first, then 2266 // this method becomes a no-op. 2293 private void adjustScrollPositionIfNecessary(JList<?> list, int index, 2294 int direction) { 2295 if (direction == 0) { 2296 return; 2297 } 2298 Rectangle cellBounds = list.getCellBounds(index, index); 2299 Rectangle visRect = list.getVisibleRect(); 2300 if (cellBounds != null && !visRect.contains(cellBounds)) { 2301 if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && 2302 list.getVisibleRowCount() <= 0) { 2303 // horizontal 2304 if (list.getComponentOrientation().isLeftToRight()) { 2305 if (direction > 0) { 2306 // right for left-to-right 2307 int x =Math.max(0, 2308 cellBounds.x + cellBounds.width - visRect.width); 2309 int startIndex = 2310 list.locationToIndex(new Point(x, cellBounds.y)); 2311 Rectangle startRect = list.getCellBounds(startIndex, 2312 startIndex); 2313 if (startRect.x < x && startRect.x < cellBounds.x) { 2314 startRect.x += startRect.width; 2315 startIndex = 2316 list.locationToIndex(startRect.getLocation()); 2317 startRect = list.getCellBounds(startIndex, 2318 startIndex); 2319 } 2320 cellBounds = startRect; 2321 } 2322 cellBounds.width = visRect.width; 2323 } 2324 else { 2325 if (direction > 0) { 2326 // left for right-to-left 2327 int x = cellBounds.x + visRect.width; 2328 int rightIndex = 2329 list.locationToIndex(new Point(x, cellBounds.y)); 2330 Rectangle rightRect = list.getCellBounds(rightIndex, 2331 rightIndex); 2332 if (rightRect.x + rightRect.width > x && 2333 rightRect.x > cellBounds.x) { 2334 rightRect.width = 0; 2335 } 2336 cellBounds.x = Math.max(0, 2337 rightRect.x + rightRect.width - visRect.width); 2338 cellBounds.width = visRect.width; 2339 } 2340 else { 2341 cellBounds.x += Math.max(0, 2342 cellBounds.width - visRect.width); 2343 // adjust width to fit into visible rectangle 2344 cellBounds.width = Math.min(cellBounds.width, 2345 visRect.width); 2346 } 2347 } 2348 } 2349 else { 2350 // vertical 2351 if (direction > 0 && 2352 (cellBounds.y < visRect.y || 2353 cellBounds.y + cellBounds.height 2354 > visRect.y + visRect.height)) { 2355 //down 2356 int y = Math.max(0, 2357 cellBounds.y + cellBounds.height - visRect.height); 2358 int startIndex = 2359 list.locationToIndex(new Point(cellBounds.x, y)); 2360 Rectangle startRect = list.getCellBounds(startIndex, 2361 startIndex); 2362 if (startRect.y < y && startRect.y < cellBounds.y) { 2363 startRect.y += startRect.height; 2364 startIndex = 2365 list.locationToIndex(startRect.getLocation()); 2366 startRect = 2367 list.getCellBounds(startIndex, startIndex); 2368 } 2369 cellBounds = startRect; 2370 cellBounds.height = visRect.height; 2371 } 2372 else { 2373 // adjust height to fit into visible rectangle 2374 cellBounds.height = Math.min(cellBounds.height, visRect.height); 2375 } 2376 } 2377 list.scrollRectToVisible(cellBounds); 2378 } 2379 } 2380 2381 private int getNextColumnIndex(JList<?> list, BasicListUI ui, 2382 int amount) { 2383 if (list.getLayoutOrientation() != JList.VERTICAL) { 2384 int index = adjustIndex(list.getLeadSelectionIndex(), list); 2385 int size = list.getModel().getSize(); 2386 2387 if (index == -1) { 2388 return 0; 2389 } else if (size == 1) { 2390 // there's only one item so we should select it | 2117 // this is done to restore the anchor and lead 2118 SwingUtilities2.setLeadAnchorWithoutSelection(lsm, anchor, lead); 2119 2120 list.setValueIsAdjusting(false); 2121 } 2122 } 2123 } 2124 2125 private int getNextPageIndex(JList<?> list, int direction) { 2126 if (list.getModel().getSize() == 0) { 2127 return -1; 2128 } 2129 2130 int index = -1; 2131 Rectangle visRect = list.getVisibleRect(); 2132 ListSelectionModel lsm = list.getSelectionModel(); 2133 int lead = adjustIndex(lsm.getLeadSelectionIndex(), list); 2134 Rectangle leadRect = 2135 (lead==-1) ? new Rectangle() : list.getCellBounds(lead, lead); 2136 2137 if (leadRect != null) { 2138 if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && 2139 list.getVisibleRowCount() <= 0) { 2140 if (!list.getComponentOrientation().isLeftToRight()) { 2141 direction = -direction; 2142 } 2143 // apply for horizontal scrolling: the step for next 2144 // page index is number of visible columns 2145 if (direction < 0) { 2146 // left 2147 visRect.x = leadRect.x + leadRect.width - visRect.width; 2148 Point p = new Point(visRect.x - 1, leadRect.y); 2149 index = list.locationToIndex(p); 2150 Rectangle cellBounds = list.getCellBounds(index, index); 2151 if (cellBounds != null && visRect.intersects(cellBounds)) { 2152 p.x = cellBounds.x - 1; 2153 index = list.locationToIndex(p); 2154 cellBounds = list.getCellBounds(index, index); 2155 } 2156 // this is necessary for right-to-left orientation only 2157 if (cellBounds != null && cellBounds.y != leadRect.y) { 2158 p.x = cellBounds.x + cellBounds.width; 2159 index = list.locationToIndex(p); 2160 } 2161 } 2162 else { 2163 // right 2164 visRect.x = leadRect.x; 2165 Point p = new Point(visRect.x + visRect.width, leadRect.y); 2166 index = list.locationToIndex(p); 2167 Rectangle cellBounds = list.getCellBounds(index, index); 2168 if (cellBounds != null && visRect.intersects(cellBounds)) { 2169 p.x = cellBounds.x + cellBounds.width; 2170 index = list.locationToIndex(p); 2171 cellBounds = list.getCellBounds(index, index); 2172 } 2173 if (cellBounds != null && cellBounds.y != leadRect.y) { 2174 p.x = cellBounds.x - 1; 2175 index = list.locationToIndex(p); 2176 } 2177 } 2178 } 2179 else { 2180 if (direction < 0) { 2181 // up 2182 // go to the first visible cell 2183 Point p = new Point(leadRect.x, visRect.y); 2184 index = list.locationToIndex(p); 2185 if (lead <= index) { 2186 // if lead is the first visible cell (or above it) 2187 // adjust the visible rect up 2188 visRect.y = leadRect.y + leadRect.height - visRect.height; 2189 p.y = visRect.y; 2190 index = list.locationToIndex(p); 2191 Rectangle cellBounds = list.getCellBounds(index, index); 2192 // go one cell down if first visible cell doesn't fit 2193 // into adjasted visible rectangle 2194 if (cellBounds != null && cellBounds.y < visRect.y) { 2195 p.y = cellBounds.y + cellBounds.height; 2196 index = list.locationToIndex(p); 2197 cellBounds = list.getCellBounds(index, index); 2198 } 2199 // if index isn't less then lead 2200 // try to go to cell previous to lead 2201 if (cellBounds != null && cellBounds.y >= leadRect.y) { 2202 p.y = leadRect.y - 1; 2203 index = list.locationToIndex(p); 2204 } 2205 } 2206 } 2207 else { 2208 // down 2209 // go to the last completely visible cell 2210 Point p = new Point(leadRect.x, 2211 visRect.y + visRect.height - 1); 2212 index = list.locationToIndex(p); 2213 Rectangle cellBounds = list.getCellBounds(index, index); 2214 // go up one cell if last visible cell doesn't fit 2215 // into visible rectangle 2216 if (cellBounds != null && 2217 cellBounds.y + cellBounds.height > 2218 visRect.y + visRect.height) 2219 { 2220 p.y = cellBounds.y - 1; 2221 index = list.locationToIndex(p); 2222 cellBounds = list.getCellBounds(index, index); 2223 index = Math.max(index, lead); 2224 } 2225 2226 if (lead >= index) { 2227 // if lead is the last completely visible index 2228 // (or below it) adjust the visible rect down 2229 visRect.y = leadRect.y; 2230 p.y = visRect.y + visRect.height - 1; 2231 index = list.locationToIndex(p); 2232 cellBounds = list.getCellBounds(index, index); 2233 // go one cell up if last visible cell doesn't fit 2234 // into adjasted visible rectangle 2235 if (cellBounds != null && 2236 cellBounds.y + cellBounds.height > 2237 visRect.y + visRect.height) 2238 { 2239 p.y = cellBounds.y - 1; 2240 index = list.locationToIndex(p); 2241 cellBounds = list.getCellBounds(index, index); 2242 } 2243 // if index isn't greater then lead 2244 // try to go to cell next after lead 2245 if (cellBounds != null && cellBounds.y <= leadRect.y) { 2246 p.y = leadRect.y + leadRect.height; 2247 index = list.locationToIndex(p); 2248 } 2249 } 2250 } 2251 } 2252 } 2253 return index; 2254 } 2255 2256 private void changeSelection(JList<?> list, int type, 2257 int index, int direction) { 2258 if (index >= 0 && index < list.getModel().getSize()) { 2259 ListSelectionModel lsm = list.getSelectionModel(); 2260 2261 // CHANGE_LEAD is only valid with multiple interval selection 2262 if (type == CHANGE_LEAD && 2263 list.getSelectionMode() 2264 != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) { 2265 2266 type = CHANGE_SELECTION; 2267 } 2268 2269 // IMPORTANT - This needs to happen before the index is changed. 2270 // This is because JFileChooser, which uses JList, also scrolls 2271 // the selected item into view. If that happens first, then 2272 // this method becomes a no-op. 2299 private void adjustScrollPositionIfNecessary(JList<?> list, int index, 2300 int direction) { 2301 if (direction == 0) { 2302 return; 2303 } 2304 Rectangle cellBounds = list.getCellBounds(index, index); 2305 Rectangle visRect = list.getVisibleRect(); 2306 if (cellBounds != null && !visRect.contains(cellBounds)) { 2307 if (list.getLayoutOrientation() == JList.VERTICAL_WRAP && 2308 list.getVisibleRowCount() <= 0) { 2309 // horizontal 2310 if (list.getComponentOrientation().isLeftToRight()) { 2311 if (direction > 0) { 2312 // right for left-to-right 2313 int x =Math.max(0, 2314 cellBounds.x + cellBounds.width - visRect.width); 2315 int startIndex = 2316 list.locationToIndex(new Point(x, cellBounds.y)); 2317 Rectangle startRect = list.getCellBounds(startIndex, 2318 startIndex); 2319 if (startRect != null && 2320 startRect.x < x && startRect.x < cellBounds.x) { 2321 startRect.x += startRect.width; 2322 startIndex = 2323 list.locationToIndex(startRect.getLocation()); 2324 startRect = list.getCellBounds(startIndex, 2325 startIndex); 2326 } 2327 cellBounds = startRect != null ? startRect : null; 2328 } 2329 if (cellBounds != null) { 2330 cellBounds.width = visRect.width; 2331 } 2332 } 2333 else { 2334 if (direction > 0) { 2335 // left for right-to-left 2336 int x = cellBounds.x + visRect.width; 2337 int rightIndex = 2338 list.locationToIndex(new Point(x, cellBounds.y)); 2339 Rectangle rightRect = list.getCellBounds(rightIndex, 2340 rightIndex); 2341 if (rightRect != null) { 2342 if (rightRect.x + rightRect.width > x && 2343 rightRect.x > cellBounds.x) { 2344 rightRect.width = 0; 2345 } 2346 cellBounds.x = Math.max(0, 2347 rightRect.x + rightRect.width - visRect.width); 2348 cellBounds.width = visRect.width; 2349 } 2350 } 2351 else { 2352 cellBounds.x += Math.max(0, 2353 cellBounds.width - visRect.width); 2354 // adjust width to fit into visible rectangle 2355 cellBounds.width = Math.min(cellBounds.width, 2356 visRect.width); 2357 } 2358 } 2359 } 2360 else { 2361 // vertical 2362 if (direction > 0 && 2363 (cellBounds.y < visRect.y || 2364 cellBounds.y + cellBounds.height 2365 > visRect.y + visRect.height)) { 2366 //down 2367 int y = Math.max(0, 2368 cellBounds.y + cellBounds.height - visRect.height); 2369 int startIndex = 2370 list.locationToIndex(new Point(cellBounds.x, y)); 2371 Rectangle startRect = list.getCellBounds(startIndex, 2372 startIndex); 2373 if (startRect != null && 2374 startRect.y < y && startRect.y < cellBounds.y) { 2375 startRect.y += startRect.height; 2376 startIndex = 2377 list.locationToIndex(startRect.getLocation()); 2378 startRect = 2379 list.getCellBounds(startIndex, startIndex); 2380 } 2381 cellBounds = startRect != null ? startRect : null; 2382 if (cellBounds != null) { 2383 cellBounds.height = visRect.height; 2384 } 2385 } 2386 else { 2387 // adjust height to fit into visible rectangle 2388 cellBounds.height = Math.min(cellBounds.height, visRect.height); 2389 } 2390 } 2391 list.scrollRectToVisible(cellBounds); 2392 } 2393 } 2394 2395 private int getNextColumnIndex(JList<?> list, BasicListUI ui, 2396 int amount) { 2397 if (list.getLayoutOrientation() != JList.VERTICAL) { 2398 int index = adjustIndex(list.getLeadSelectionIndex(), list); 2399 int size = list.getModel().getSize(); 2400 2401 if (index == -1) { 2402 return 0; 2403 } else if (size == 1) { 2404 // there's only one item so we should select it |