1 package net.suberic.util.swing; 2 import javax.swing.*; 3 import java.awt.event.*; 4 import java.lang.reflect.Method ; 5 6 41 42 public class ScrollingDesktopManager extends DefaultDesktopManager 43 implements ContainerListener, AdjustmentListener { 44 45 private JScrollPane scrollPane = null; 46 47 private JDesktopPane pane = null; 48 49 private boolean updating = false; 50 51 private static Integer SIMPLE_SCROLL_MODE; 52 53 59 public ScrollingDesktopManager(JDesktopPane pane, JScrollPane scrollPane) { 60 super(); 61 setDesktopPane(pane); 62 setScrollPane(scrollPane); 63 } 64 65 74 public void closeFrame(JInternalFrame f) { 75 super.closeFrame(f); 76 updateDesktopSize(); 77 78 SwingUtilities.invokeLater(new Runnable () { 80 public void run() { 81 82 java.awt.KeyboardFocusManager mgr = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager(); 83 pane.requestFocusInWindow(); 84 } 85 }); 86 } 87 88 97 public void minimizeFrame(JInternalFrame f) { 98 super.minimizeFrame(f); 99 updateDesktopSize(); 100 101 } 102 103 112 public void iconifyFrame(JInternalFrame f) { 113 super.iconifyFrame(f); 114 updateDesktopSize(); 115 } 116 117 126 public void deiconifyFrame(JInternalFrame f) { 127 super.deiconifyFrame(f); 128 updateDesktopSize(); 129 } 130 131 140 public void endDraggingFrame(JComponent f) { 141 super.endDraggingFrame(f); 142 143 updateDesktopSize(); 144 } 145 146 155 156 public void endResizingFrame(JComponent f) { 157 super.endResizingFrame(f); 158 updateDesktopSize(); 159 } 160 161 170 public void maximizeFrame(JInternalFrame f) { 171 if (scrollPane != null) { 172 java.awt.Dimension newSize = scrollPane.getViewport().getSize(); 173 pane.setSize(newSize); 174 pane.setPreferredSize(newSize); 175 } 176 177 super.maximizeFrame(f); 178 179 } 180 181 192 public void componentRemoved(java.awt.event.ContainerEvent e) { 193 } 195 196 199 public void componentAdded(java.awt.event.ContainerEvent e) { 200 updateDesktopSize(); 201 } 202 203 206 public void adjustmentValueChanged(java.awt.event.AdjustmentEvent e) { 207 updateDesktopSize(); 208 } 209 210 213 public void updateDesktopSize() { 214 if (!updating && scrollPane != null && scrollPane.isShowing()) { 215 updating = true; 216 217 JScrollBar hsb = scrollPane.getHorizontalScrollBar(); 218 JScrollBar vsb = scrollPane.getVerticalScrollBar(); 219 220 JInternalFrame[] allFrames = pane.getAllFrames(); 222 int min_x = 0, min_y = 0, max_x = 0, max_y = 0; 223 java.awt.Rectangle bounds = null; 224 226 if (allFrames.length > 0) { 227 bounds = allFrames[0].getBounds(bounds); 228 min_x = bounds.x; 229 min_y = bounds.y; 230 max_x = bounds.width + bounds.x; 231 max_y = bounds.height + bounds.y; 232 for (int i = 1; i < allFrames.length; i++) { 233 bounds = allFrames[i].getBounds(bounds); 234 min_x = Math.min(min_x, bounds.x); 235 min_y = Math.min(min_y, bounds.y); 236 max_x = Math.max(max_x, bounds.width + bounds.x); 237 max_y = Math.max(max_y, bounds.height + bounds.y); 238 } 239 } 240 241 int windowsWidth = max_x; 242 int windowsHeight = max_y; 243 244 bounds = scrollPane.getViewport().getViewRect(); 245 min_x = Math.min(min_x, bounds.x); 246 min_y = Math.min(min_y, bounds.y); 247 max_x = Math.max(max_x, bounds.width + bounds.x); 248 max_y = Math.max(max_y, bounds.height + bounds.y); 249 250 251 if (min_x != 0 || min_y != 0) { 252 for (int i = 0; i < allFrames.length; i++) { 253 allFrames[i].setLocation(allFrames[i].getX() - min_x, allFrames[i].getY() - min_y); 254 255 } 256 257 windowsWidth = windowsWidth - min_x; 258 windowsHeight = windowsHeight - min_y; 259 } 260 261 int hval = hsb.getValue(); 262 int vval = vsb.getValue(); 263 264 bounds = scrollPane.getViewport().getViewRect(); 265 int oldViewWidth = bounds.width + hval; 266 int oldViewHeight = bounds.height + vval; 267 268 int portWidth = scrollPane.getViewport().getSize().width; 269 int portHeight = scrollPane.getViewport().getSize().height; 270 271 java.awt.Dimension dim = pane.getSize(); 272 int oldWidth = dim.width; 273 int oldHeight = dim.height; 274 275 pane.setSize(max_x - min_x, max_y - min_y); 276 277 278 279 int prefWidth = max_x - min_x; 280 int prefHeight = max_y - min_y; 281 282 283 boolean hasVsb = false, needsVsb = false, hasHsb = false, needsHsb = false; 284 288 if (vsb.isVisible()) { 289 hasVsb = true; 290 } else { 291 hasVsb = false; 292 } 293 294 if (hsb.isVisible()) { 295 hasHsb = true; 296 } else { 297 hasHsb = false; 298 } 299 300 if (max_x - min_x > scrollPane.getViewport().getViewRect().width) 301 needsHsb = true; 302 else 303 needsHsb = false; 304 305 if (max_y - min_y > scrollPane.getViewport().getViewRect().height) 306 needsVsb = true; 307 else 308 needsVsb = false; 309 310 if (hasVsb == false && needsVsb == true) { 311 if (windowsWidth < bounds.width + bounds.x - vsb.getPreferredSize().width) { 312 prefWidth-=vsb.getPreferredSize().width; 313 } 314 } else if (hasVsb == true && needsVsb == false) { 315 if (windowsWidth <= bounds.width + bounds.x) { 316 prefWidth+= vsb.getPreferredSize().width; 317 } 318 } 319 320 if (hasHsb == false && needsHsb == true) { 321 if (windowsHeight < bounds.height + bounds.y - hsb.getPreferredSize().height) { 322 prefHeight-=hsb.getPreferredSize().height; 323 } 324 } else if (hasHsb == true && needsHsb == false) { 325 if (windowsHeight <= bounds.height + bounds.y) { 326 prefHeight+= hsb.getPreferredSize().height; 327 } 328 } 329 330 331 332 pane.setPreferredSize(new java.awt.Dimension (prefWidth, prefHeight)); 333 scrollPane.validate(); 334 335 hsb = scrollPane.getHorizontalScrollBar(); 336 vsb = scrollPane.getVerticalScrollBar(); 337 338 if (min_x != 0 && hval - min_x + hsb.getModel().getExtent() > hsb.getMaximum()) { 339 hsb.setMaximum(hval - min_x + hsb.getModel().getExtent()); 340 } 341 342 if (min_y != 0 && vval - min_y + vsb.getModel().getExtent() > vsb.getMaximum()) { 343 vsb.setMaximum(vval - min_y + vsb.getModel().getExtent()); 344 } 345 346 hsb.setValue(hval - min_x); 347 348 vsb.setValue(vval - min_y); 349 350 updating = false; 351 } 352 } 353 354 359 public void setScrollPane(JScrollPane newScrollPane) { 360 if (scrollPane != null) { 361 scrollPane.getHorizontalScrollBar().removeAdjustmentListener(this); 362 scrollPane.getVerticalScrollBar().removeAdjustmentListener(this); 363 } 364 scrollPane = newScrollPane; 365 scrollPane.getHorizontalScrollBar().addAdjustmentListener(this); 366 scrollPane.getVerticalScrollBar().addAdjustmentListener(this); 367 } 368 369 public JScrollPane getScrollPane() { 370 return scrollPane; 371 } 372 373 378 public void setDesktopPane(JDesktopPane newDesktopPane) { 379 if (pane != null) 380 pane.removeContainerListener(this); 381 pane = newDesktopPane; 382 pane.addContainerListener(this); 383 } 384 385 public JDesktopPane getDesktopPane() { 386 return pane; 387 } 388 } 389 390 391 | Popular Tags |