1 19 20 package org.netbeans.core.windows.view.ui.toolbars; 21 22 import org.openide.awt.Toolbar; 23 24 import java.awt.*; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import java.util.Iterator ; 28 import java.util.Vector ; 29 30 40 public class ToolbarConstraints { 41 static final long serialVersionUID =3065774641403311880L; 42 43 44 static final int LEFT_ANCHOR = -1; 45 static final int NO_ANCHOR = 0; 46 47 public static final String PREFERRED_SIZE = "preferredSize"; 48 49 50 private String name; 51 52 private int position; 53 54 private int anchor; 56 private boolean visible; 57 58 private Vector <ToolbarRow> ownRows; 59 60 private Vector <ToolbarConstraints> prevBars; 61 62 private Vector <ToolbarConstraints> nextBars; 63 64 private int prevEnd; 66 private int nextBeg; 68 private int prevBeg; 70 private int nextEnd; 72 73 private Dimension prefSize; 74 75 private Rectangle bounds; 76 77 private ToolbarConfiguration toolbarConfig; 78 79 private int rowCount; 80 81 private int prefLastWidth; 82 83 private int lastRowIndex; 84 86 private int initialIndexInRow; 87 88 private PropertyChangeSupport propSupport; 89 90 ToolbarConstraints (ToolbarConfiguration conf, String nam, Integer pos, Boolean vis) { 91 this( conf, nam, pos, vis, -1 ); 92 } 93 94 100 ToolbarConstraints (ToolbarConfiguration conf, String nam, Integer pos, Boolean vis, int initialIndexInRow) { 101 toolbarConfig = conf; 102 name = nam; 103 if (pos == null) { 104 position = 0; 105 anchor = LEFT_ANCHOR; 106 this.initialIndexInRow = initialIndexInRow; 107 } else { 108 position = pos.intValue(); 109 anchor = NO_ANCHOR; 110 this.initialIndexInRow = -1; 113 } 114 visible = vis.booleanValue(); 115 116 prefSize = new Dimension (); 117 rowCount = 0; 118 prefLastWidth = 0; 119 bounds = new Rectangle (); 120 121 initValues(); 122 } 123 124 125 void initValues () { 126 ownRows = new Vector <ToolbarRow>(); 127 prevBars = new Vector <ToolbarConstraints>(); 128 nextBars = new Vector <ToolbarConstraints>(); 129 130 resetPrev(); 131 resetNext(); 132 } 133 134 138 void checkNextPosition (Integer position, Boolean visible) { 139 if (position == null) { 140 this.position = 0; 141 this.anchor = LEFT_ANCHOR; 142 } else { 143 if (anchor == NO_ANCHOR) 144 this.position = (this.position + position.intValue()) / 2; 145 else 146 this.position = position.intValue(); 147 this.anchor = NO_ANCHOR; 148 } 149 this.visible = this.visible || visible.booleanValue(); 150 } 151 152 153 String getName () { 154 return name; 155 } 156 157 158 int getAnchor () { 159 return anchor; 160 } 161 162 165 void setAnchor (int anch) { 166 anchor = anch; 167 } 168 169 170 boolean isVisible () { 171 return visible; 172 } 173 174 177 void setVisible (boolean v) { 178 visible = v; 179 } 180 181 182 int getPosition () { 183 return position; 184 } 185 186 189 void setPosition (int pos) { 190 position = pos; 191 } 192 193 194 int getWidth () { 195 return prefSize.width; 196 } 197 198 199 int getRowCount () { 200 return rowCount; 201 } 202 203 int checkInitialIndexInRow() { 204 int retValue = initialIndexInRow; 205 initialIndexInRow = -1; 206 return retValue; 207 } 208 209 210 Rectangle getBounds () { 211 return new Rectangle (bounds); 212 } 213 214 217 boolean destroy () { 218 lastRowIndex = rowIndex(); 219 rowCount = ownRows.size(); 220 221 boolean emptyRow = false; 222 for (ToolbarRow row: ownRows) { 223 row.removeToolbar (this); 224 emptyRow = emptyRow || row.isEmpty(); 225 } 226 initValues(); 227 return emptyRow; 228 } 229 230 233 void addOwnRow (ToolbarRow row) { 234 ownRows.add (row); 235 } 236 237 240 void addPrevBar (ToolbarConstraints prev) { 241 if (prev == null) 242 return; 243 prevBars.add (prev); 244 } 245 246 249 void addNextBar (ToolbarConstraints next) { 250 if (next == null) 251 return; 252 nextBars.add (next); 253 } 254 255 258 void removePrevBar (ToolbarConstraints prev) { 259 if (prev == null) 260 return; 261 prevBars.remove (prev); 262 } 263 264 267 void removeNextBar (ToolbarConstraints next) { 268 if (next == null) 269 return; 270 nextBars.remove (next); 271 } 272 273 276 void setPreferredSize (Dimension size) { 277 Dimension oldSize = prefSize; 278 prefSize = size; 279 rowCount = Toolbar.rowCount (prefSize.height); 280 281 if (ownRows.isEmpty()) 282 return; 283 284 ToolbarRow row; 285 286 if (visible) { 287 boolean emptyRow = false; 288 while (rowCount < ownRows.size()) { 289 row = ownRows.lastElement(); 290 row.removeToolbar (this); 291 ownRows.remove (row); 292 emptyRow = emptyRow || row.isEmpty(); 293 } 294 if (emptyRow) 295 toolbarConfig.checkToolbarRows(); 296 while (rowCount > ownRows.size()) { 297 row = ownRows.lastElement(); 298 ToolbarRow nR = row.getNextRow(); 299 if (nR == null) 300 nR = toolbarConfig.createLastRow(); 301 nR.addToolbar (this, position); 302 } 303 } 304 updatePosition(); 305 propSupport.firePropertyChange(PREFERRED_SIZE, oldSize, prefSize); 306 } 307 308 309 int rowIndex () { 310 if (!visible) 311 return toolbarConfig.getRowCount(); 312 if (ownRows.isEmpty()) 313 return lastRowIndex; 314 return toolbarConfig.rowIndex (ownRows.firstElement()); 315 } 316 317 318 boolean isAlone () { 319 for (ToolbarRow row: ownRows) { 320 if (row.toolbarCount() != 1) 321 return false; 322 } 323 return true; 324 } 325 326 329 void updatePreferredSize (Dimension size) { 330 if (!prefSize.equals (size)) { 331 setPreferredSize (size); 332 } 333 } 334 335 336 void updateBounds () { 337 if (ownRows.size() > 0) { 338 Iterator <ToolbarRow> iter = ownRows.iterator(); 339 ToolbarRow firstRow = iter.next(); 340 int toolbarHeight = firstRow.getPreferredHeight(); 341 while (iter.hasNext()) { 342 toolbarHeight += iter.next().getPreferredHeight() + ToolbarLayout.VGAP; 343 } 344 bounds = new Rectangle (position, toolbarConfig.getRowVertLocation(firstRow), 345 nextBeg - position - ToolbarLayout.HGAP, toolbarHeight); 346 } 347 else { 348 bounds = new Rectangle(position, 0, 0, 0); 349 } 350 } 351 352 353 void updatePosition () { 354 updatePrev(); 355 if (anchor == NO_ANCHOR) { 356 if (position < (prevEnd + ToolbarLayout.HGAP)) { 357 position = prevEnd + ToolbarLayout.HGAP; 358 anchor = LEFT_ANCHOR; 359 } 360 } else { 361 position = prevEnd + ToolbarLayout.HGAP; 362 } 363 updatePrevBars(); 364 updateNextBars(); 365 updateBounds(); 366 updatePrefWidth(); 367 } 368 369 370 void updatePrevPosition () { 371 Iterator it = prevBars.iterator(); 372 ToolbarConstraints tc; 373 while (it.hasNext()) { 374 tc = (ToolbarConstraints)it.next(); 375 tc.updatePosition(); 376 } 377 } 378 379 380 void updatePrevBars () { 381 for (ToolbarConstraints tc: prevBars) { 382 tc.updateNext(); 383 } 384 } 385 386 387 void updateNextBars () { 388 Iterator <ToolbarConstraints> it = nextBars.iterator(); 389 ToolbarConstraints tc; 390 if (!it.hasNext()) { 391 resetNext(); 392 updatePrefWidth(); 393 } 394 while (it.hasNext()) { 395 tc = it.next(); 396 if (tc != this) tc.updatePosition(); 400 } 401 } 402 403 404 void updatePrefWidth () { 405 if (nextBars.size() == 0) { 406 prefLastWidth = getPosition() + getWidth() + ToolbarLayout.HGAP; 407 toolbarConfig.updatePrefWidth(); 408 } 409 } 410 411 412 int getPrefWidth () { 413 return prefLastWidth; 414 } 415 416 int getPrefHeight () { 417 return prefSize.height; 418 } 419 420 421 void updateNext () { 422 resetNext(); 423 Iterator it = nextBars.iterator(); 424 ToolbarConstraints tc; 425 int nextPos; 426 while (it.hasNext()) { 427 tc = (ToolbarConstraints)it.next(); 428 nextBeg = Math.min (nextBeg, nextPos = tc.getPosition()); 429 nextEnd = Math.min (nextEnd, nextPos + tc.getWidth()); 430 } 431 updateBounds(); 432 } 433 434 435 void updatePrev () { 436 resetPrev(); 437 Iterator it = prevBars.iterator(); 438 ToolbarConstraints tc; 439 int prevPos; 440 while (it.hasNext()) { 441 tc = (ToolbarConstraints)it.next(); 442 prevBeg = Math.max (prevBeg, prevPos = tc.getPosition()); 443 prevEnd = Math.max (prevEnd, prevPos + tc.getWidth()); 444 } 445 } 446 447 448 void resetPrev () { 449 prevBeg = 0; 450 prevEnd = 0; 451 } 452 453 454 void resetNext () { 455 nextBeg = Integer.MAX_VALUE; 456 nextEnd = Integer.MAX_VALUE; 457 } 458 459 462 void moveLeft (int dx) { 463 int wantX = position - dx; 464 465 position = wantX; 466 anchor = NO_ANCHOR; 467 if (wantX > prevEnd) { setAnchorTo (NO_ANCHOR, nextBars); 469 } else { 470 if (canSwitchLeft (getPosition(), getWidth(), prevBeg, prevEnd - prevBeg)) { switchToolbarLeft (); 472 } 473 } 474 } 475 476 479 void moveRight (int dx) { 480 int wantX = position + dx; 481 int wantXpWidth = wantX + getWidth(); 483 if (wantXpWidth < nextBeg) { anchor = NO_ANCHOR; 485 position = wantX; 486 } else { 487 if (canSwitchRight (wantX, getWidth(), nextBeg, nextEnd - nextBeg)) { position = wantX; 489 anchor = NO_ANCHOR; 490 switchToolbarRight (); 491 } else { 492 position = nextBeg - getWidth() - ToolbarLayout.HGAP; 493 anchor = NO_ANCHOR; 494 } 495 } 496 497 updatePrevPosition(); 498 } 499 500 501 void moveLeft2End (int dx) { 502 int wantX = position - dx; 503 504 anchor = NO_ANCHOR; 505 if (wantX < (prevEnd + ToolbarLayout.HGAP)) { 506 wantX = prevEnd + ToolbarLayout.HGAP; 507 } 508 move2End (wantX - position); 509 } 510 511 512 void moveRight2End (int dx) { 513 move2End (dx); 514 } 515 516 517 void move2End (int dx) { 518 position += dx; 519 Iterator it = nextBars.iterator(); 520 ToolbarConstraints tc; 521 int nextPos; 522 while (it.hasNext()) { 523 tc = (ToolbarConstraints)it.next(); 524 tc.move2End (dx); 525 } 526 } 527 528 532 void setAnchorTo (int anch, Vector bars) { 533 Iterator it = bars.iterator(); 534 ToolbarConstraints tc; 535 while (it.hasNext()) { 536 tc = (ToolbarConstraints)it.next(); 537 tc.setAnchor (anch); 538 } 539 } 540 541 542 void switchToolbarLeft () { 543 Iterator it = ownRows.iterator(); 544 ToolbarRow row; 545 while (it.hasNext()) { 546 row = (ToolbarRow)it.next(); 547 row.trySwitchLeft (this); 548 } 549 } 550 551 552 void switchToolbarRight () { 553 Iterator it = ownRows.iterator(); 554 ToolbarRow row; 555 while (it.hasNext()) { 556 row = (ToolbarRow)it.next(); 557 row.trySwitchRight (this); 558 } 559 } 560 561 public void addPropertyChangeListener (PropertyChangeListener l) { 562 if (propSupport == null) { 563 propSupport = new PropertyChangeSupport (this); 564 } 565 propSupport.addPropertyChangeListener(l); 566 } 567 568 public void removePropertyChangeListener (PropertyChangeListener l) { 569 if (propSupport != null) { 570 propSupport.removePropertyChangeListener(l); 571 } 572 } 573 574 581 static boolean canSwitchLeft (int p1, int w1, int p2, int w2) { 582 return (p1 < (p2)); 583 } 584 585 592 static boolean canSwitchRight (int p1, int w1, int p2, int w2) { 593 return (p1 > (p2)); 594 } 595 596 597 598 static class WritableToolbar { 599 600 String name; 601 602 int position; 603 604 int anchor; 605 606 boolean visible; 607 608 611 public WritableToolbar (ToolbarConstraints tc) { 612 name = tc.getName(); 613 position = tc.getPosition(); 614 anchor = tc.getAnchor(); 615 visible = tc.isVisible(); 616 } 617 618 619 public String toString () { 620 StringBuffer sb = new StringBuffer (); 621 String quotedName = name; 622 try { 623 quotedName = org.openide.xml.XMLUtil.toAttributeValue(name); 624 } 625 catch (java.io.IOException ignore) {} 626 627 sb.append (" <").append (ToolbarConfiguration.TAG_TOOLBAR); sb.append (" ").append (ToolbarConfiguration.ATT_TOOLBAR_NAME).append ("=\""). append (quotedName).append ("\""); if ((anchor == ToolbarConstraints.NO_ANCHOR) || !visible) 631 sb.append (" ").append (ToolbarConfiguration.ATT_TOOLBAR_POSITION).append ("=\""). append (position).append ("\""); if (!visible) 634 sb.append (" ").append (ToolbarConfiguration.ATT_TOOLBAR_VISIBLE).append ("=\""). append (visible).append ("\""); sb.append (" />\n"); 638 return sb.toString(); 639 } 640 } } 643 | Popular Tags |