1 77 78 79 package swingwtx.swing; 80 81 import swingwt.awt.BorderLayout; 82 import swingwt.awt.Component; 83 import swingwt.awt.Container; 84 import swingwt.awt.Dimension; 85 import swingwt.awt.event.AdjustmentEvent; 86 import swingwt.awt.event.AdjustmentListener; 87 import swingwt.awt.event.ComponentEvent; 88 import swingwt.awt.event.ComponentListener; 89 import swingwt.awt.event.ContainerEvent; 90 import swingwt.awt.event.ContainerListener; 91 import swingwtx.swing.border.Border; 92 93 public class JScrollPane extends JPanel implements AdjustmentListener { 94 95 public final static int VERTICAL_SCROLLBAR_ALWAYS = 0; 96 public final static int VERTICAL_SCROLLBAR_AS_NEEDED = 1; 97 public final static int VERTICAL_SCROLLBAR_NEVER = 2; 98 public final static int HORIZONTAL_SCROLLBAR_ALWAYS = 3; 99 public final static int HORIZONTAL_SCROLLBAR_AS_NEEDED = 4; 100 public final static int HORIZONTAL_SCROLLBAR_NEVER = 5; 101 102 public final static String VIEWPORT = "VIEWPORT"; 103 public final static String VERTICAL_SCROLLBAR = "VERTICAL_SCROLLBAR"; 104 public final static String HORIZONTAL_SCROLLBAR = "HORIZONTAL_SCROLLBAR"; 105 public final static String ROW_HEADER = "ROW_HEADER"; 106 public final static String COLUMN_HEADER = "COLUMN_HEADER"; 107 public final static String LOWER_LEFT_CORNER = "LOWER_LEFT_CORNER"; 108 public final static String LOWER_RIGHT_CORNER = "LOWER_RIGHT_CORNER"; 109 public final static String UPPER_LEFT_CORNER = "UPPER_LEFT_CORNER"; 110 public final static String UPPER_RIGHT_CORNER = "UPPER_RIGHT_CORNER"; 111 public final static String LOWER_LEADING_CORNER = "LOWER_LEADING_CORNER"; 112 public final static String LOWER_TRAILING_CORNER = "LOWER_TRAILING_CORNER"; 113 public final static String UPPER_LEADING_CORNER = "UPPER_LEADING_CORNER"; 114 public final static String UPPER_TRAILING_CORNER = "UPPER_TRAILING_CORNER"; 115 public final static String VERTICAL_SCROLLBAR_POLICY = "VERTICAL_SCROLLBAR_POLICY"; 116 public final static String HORIZONTAL_SCROLLBAR_POLICY = "HORIZONTAL_SCROLLBAR_POLICY"; 117 118 protected final static int BAR_SCALE_FACTOR = 10; 120 protected int verticalPolicy = VERTICAL_SCROLLBAR_AS_NEEDED; 121 protected int horizontalPolicy = HORIZONTAL_SCROLLBAR_AS_NEEDED; 122 123 protected JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL); 124 protected JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL); 125 126 protected swingwt.awt.Component childComponent = null; 127 128 protected JPanel northPanel = new JPanel(); 129 protected JPanel westPanel = new JPanel(); 130 protected JPanel southPanel = new JPanel(); 131 132 protected Component columnView = null; 133 protected Component rowView = null; 134 135 protected int ypix = 0; protected int xpix = 0; 138 protected int showY = 0; protected int realY = 0; protected int diffY = 0; protected int showX = 0; protected int realX = 0; protected int diffX = 0; 145 protected boolean deactivated = false; 148 149 protected JViewport view = new JViewport(this); 150 151 public JScrollPane() { this(null, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED); } 152 public JScrollPane(swingwt.awt.Component component) { this(component, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED); } 153 public JScrollPane(int vsbPolicy, int hsbPolicy) { this(null, vsbPolicy, hsbPolicy);} 154 155 public JScrollPane(swingwt.awt.Component component, int vsbPolicy, int hsbPolicy) { 156 157 verticalPolicy = vsbPolicy; 158 horizontalPolicy = hsbPolicy; 159 160 layoutScrollPane(); 161 add(component); 162 163 if (!deactivated) { 164 addComponentListener(new ComponentListener() { 165 public void componentResized(ComponentEvent e) { 166 calculateBarSize(); 167 } 168 public void componentMoved(ComponentEvent e) { 169 calculateBarSize(); 170 } 171 public void componentShown(ComponentEvent e) { 172 calculateBarSize(); 173 } 174 public void componentHidden(ComponentEvent e) { 175 calculateBarSize(); 176 } 177 }); 178 addContainerListener(new ContainerListener() { 179 public void componentAdded(ContainerEvent e) {calculateBarSize();} 180 public void componentRemoved(ContainerEvent e) {calculateBarSize();} 181 }); 182 } 183 } 184 185 189 protected void layoutScrollPane() { 190 191 setLayout(new swingwt.awt.BorderLayout()); 192 193 hbar.setUnitIncrement(1); 195 vbar.setUnitIncrement(1); 196 hbar.setBlockIncrement(5); 197 vbar.setBlockIncrement(5); 198 199 northPanel.setLayout(new BorderLayout()); 201 westPanel.setLayout(new BorderLayout()); 202 203 super.add(northPanel, BorderLayout.NORTH); 204 super.add(westPanel, BorderLayout.WEST); 205 206 super.add(hbar, swingwt.awt.BorderLayout.SOUTH); 207 super.add(vbar, swingwt.awt.BorderLayout.EAST); 208 209 handleBarVisibility(); 210 hbar.addAdjustmentListener(this); 211 vbar.addAdjustmentListener(this); 212 213 } 214 215 protected void handleBarVisibility() { 216 217 if (deactivated) { 221 vbar.setVisible(false); 222 hbar.setVisible(false); 223 return; 224 } 225 226 if (verticalPolicy == VERTICAL_SCROLLBAR_ALWAYS) { 227 vbar.setVisible(true); 228 } 229 else if (verticalPolicy == VERTICAL_SCROLLBAR_NEVER) { 230 vbar.setVisible(false); 231 } 232 else if (diffY > 0) { 233 vbar.setVisible(true); 234 } 235 else { 236 vbar.setVisible(false); 237 } 238 239 if (horizontalPolicy == HORIZONTAL_SCROLLBAR_ALWAYS) { 240 hbar.setVisible(true); 241 } 242 else if (horizontalPolicy == HORIZONTAL_SCROLLBAR_NEVER) { 243 hbar.setVisible(false); 244 } 245 else if (diffX > 0) { 246 hbar.setVisible(true); 247 } 248 else { 249 hbar.setVisible(false); 250 } 251 252 invalidate(); 253 repaint(); 254 255 } 256 257 protected void calculateBarSize() { 258 259 if (childComponent == null || deactivated) return; 261 262 showY = this.getSize().height - hbar.getPeerSize().height; 265 if (columnView != null) showY -= columnView.getPeerSize().height; 266 267 realY = childComponent.getSize().height; 269 diffY = realY - showY; 270 271 vbar.setMinimum(0); 272 vbar.setMaximum(diffY / BAR_SCALE_FACTOR); 273 vbar.setValue(0); 274 275 showX = this.getSize().width - vbar.getPeerSize().width; 277 if (rowView != null) showX -= rowView.getPeerSize().width; 278 279 realX = childComponent.getSize().width; 281 diffX = realX - showX; 282 283 hbar.setMinimum(0); 284 hbar.setMaximum(diffX / BAR_SCALE_FACTOR); 285 hbar.setValue(0); 286 287 handleBarVisibility(); 289 290 } 291 292 public JViewport getViewport() { return view; } 293 294 public void setViewportBorder(Border b) { setBorder(b); } 295 public Border getViewportBorder() { return getBorder(); } 296 297 public Component add(Component c) { 298 299 if (c == null) return null; 300 301 checkIfComponentCanHandleItself(c); 303 304 childComponent = (swingwt.awt.Component) c; 308 JSWTScrollable s = (JSWTScrollable) childComponent; 309 s.setHorizontalScrollPane( horizontalPolicy == HORIZONTAL_SCROLLBAR_ALWAYS || horizontalPolicy == HORIZONTAL_SCROLLBAR_AS_NEEDED ); 310 s.setVerticalScrollPane( verticalPolicy == VERTICAL_SCROLLBAR_ALWAYS || verticalPolicy == VERTICAL_SCROLLBAR_AS_NEEDED ); 311 s.setScrollPane(this); 312 313 return doAdd(c); 315 } 316 317 public void add(Component c, Object layoutmodifier) { 318 add(c); 319 } 320 321 public void setViewportView(Component component) { 322 removeAll(); 323 add(component); 324 } 325 326 public int getHorizontalScrollBarPolicy() { return horizontalPolicy; } 327 public void setHorizontalScrollBarPolicy(int policy) { horizontalPolicy = policy; calculateBarSize(); } 328 public int getVerticalScrollBarPolicy() { return verticalPolicy; } 329 public void setVerticalScrollBarPolicy(int policy) { verticalPolicy = policy; calculateBarSize();; } 330 public void setHorizontalScrollBar(JScrollBar bar) { hbar = bar; layoutScrollPane(); calculateBarSize(); } 331 public void setVerticalScrollBar(JScrollBar bar) { vbar = bar; layoutScrollPane(); calculateBarSize(); } 332 public JScrollBar getHorizontalScrollBar() { return hbar; } 333 public JScrollBar getVerticalScrollBar() { return vbar; } 334 335 public swingwt.awt.Component[] getComponents() { return new swingwt.awt.Component[] { childComponent }; } 336 337 public void setCorner(String key, Component corner) { 338 if (key.equals(JScrollPane.UPPER_LEFT_CORNER) || key.equals(JScrollPane.UPPER_LEADING_CORNER)) 339 northPanel.add(corner, BorderLayout.WEST); 340 if (key.equals(JScrollPane.UPPER_RIGHT_CORNER) || key.equals(JScrollPane.UPPER_TRAILING_CORNER)) 341 northPanel.add(corner, BorderLayout.EAST); 342 } 343 public void setColumnHeaderView(Component view) { columnView = view; northPanel.add(view, BorderLayout.CENTER); } 344 public void setRowHeaderView(Component view) { rowView = view; westPanel.add(view, BorderLayout.CENTER); } 345 346 public void setSwingWTParent(swingwt.awt.Container parent) throws Exception { 347 348 super.setSwingWTParent(parent); 350 351 handleBarVisibility(); 354 355 if (!deactivated) 357 SwingUtilities.invokeIn(new Runnable () { 358 public void run() { 359 calculateBarSize(); 360 adjustmentValueChanged(null); 361 } 362 }, 500); 363 } 364 365 370 protected void checkIfComponentCanHandleItself(Component c) { 371 deactivated = !(c instanceof JPanel) && !(c instanceof JLabel); 372 } 373 374 378 public void adjustmentValueChanged(AdjustmentEvent e) { 379 if (e != null) 380 if (e.getSource() == vbar) { 381 ypix = e.getValue() * BAR_SCALE_FACTOR; 383 } 384 else { 385 xpix = e.getValue() * BAR_SCALE_FACTOR; 387 } 388 389 Dimension d = childComponent.getSize(); childComponent.setBounds(xpix * -1, ypix * -1, d.width, d.height); 391 392 if (childComponent instanceof Container) 394 ((Container) childComponent).invalidate(); 395 else 396 childComponent.repaint(); 397 } 398 399 400 } 401 | Popular Tags |