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