1 19 20 package org.netbeans.core.windows.view.ui.toolbars; 21 22 import org.openide.awt.Toolbar; 23 import org.openide.awt.ToolbarPool; 24 25 import java.awt.*; 26 import java.beans.PropertyChangeEvent ; 27 import java.beans.PropertyChangeListener ; 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 31 41 public class ToolbarRow { 42 43 ToolbarConfiguration toolbarConfig; 44 45 ToolbarRow prevRow; 46 47 ToolbarRow nextRow; 48 49 50 private Vector <ToolbarConstraints> toolbars; 51 52 private PropertyChangeListener constraintsL; 53 54 private int prefHeight; 55 56 59 ToolbarRow (ToolbarConfiguration config) { 60 toolbarConfig = config; 61 toolbars = new Vector <ToolbarConstraints>(); 62 prevRow = nextRow = null; 63 constraintsL = new PropertyChangeListener () { 65 public void propertyChange (PropertyChangeEvent evt) { 66 if (ToolbarConstraints.PREFERRED_SIZE.equals(evt.getPropertyName())) { 67 Dimension oldTCSize = (Dimension)evt.getOldValue(); 68 Dimension newTCSize = (Dimension)evt.getNewValue(); 69 if (oldTCSize.height != newTCSize.height) { 70 updateRowsBelow(); 71 } 72 } 73 } 74 }; 75 } 76 77 80 void addToolbar (ToolbarConstraints tc) { 81 addToolbar2 (tc, toolbars.size()); 82 } 83 84 88 void addToolbar (ToolbarConstraints newTC, int pos) { 89 int index = newTC.checkInitialIndexInRow(); 90 if( index >= 0 ) { 91 index = Math.min( index, toolbars.size() ); 94 } else { 95 index = 0; 96 Iterator it = toolbars.iterator(); 97 ToolbarConstraints tc; 98 while (it.hasNext()) { 99 tc = (ToolbarConstraints)it.next(); 100 if (pos <= tc.getPosition()) 101 break; 102 index++; 103 } 104 } 105 addToolbar2 (newTC, index); 106 } 107 108 112 private void addToolbar2 (ToolbarConstraints tc, int index) { 113 if (toolbars.contains (tc)) 114 return; 115 ToolbarConstraints prev = null; 116 ToolbarConstraints next = null; 117 if (index != 0) { 118 prev = (ToolbarConstraints)toolbars.elementAt (index - 1); 119 prev.addNextBar (tc); 120 tc.addPrevBar (prev); 121 } 122 if (index < toolbars.size()) { 123 next = (ToolbarConstraints)toolbars.elementAt (index); 124 tc.addNextBar (next); 125 next.addPrevBar (tc); 126 } 127 if ((prev != null) && (next != null)) { 128 prev.removeNextBar (next); 129 next.removePrevBar (prev); 130 } 131 132 int oldHeight = getPreferredHeight(); 133 134 tc.addOwnRow (this); 135 toolbars.insertElementAt (tc, index); 136 137 tc.updatePosition(); 138 139 tc.addPropertyChangeListener(constraintsL); 140 } 141 142 145 void removeToolbar (ToolbarConstraints tc) { 146 int index = toolbars.indexOf (tc); 147 148 ToolbarConstraints prev = null; 149 ToolbarConstraints next = null; 150 try { 151 prev = (ToolbarConstraints)toolbars.elementAt (index - 1); 152 prev.removeNextBar (tc); 153 } catch (ArrayIndexOutOfBoundsException e) { } 154 try { 155 next = (ToolbarConstraints)toolbars.elementAt (index + 1); 156 next.removePrevBar (tc); 157 } catch (ArrayIndexOutOfBoundsException e) { } 158 if ((prev != null) && (next != null)) { 159 prev.addNextBar (next); 160 next.addPrevBar (prev); 161 } 162 163 toolbars.removeElement (tc); 164 165 if (prev != null) { 166 prev.updatePosition(); 167 } else { 168 if (next != null) { 169 next.updatePosition(); 170 } 171 } 172 tc.removePropertyChangeListener(constraintsL); 173 } 174 175 176 Iterator <ToolbarConstraints> iterator () { 177 return toolbars.iterator(); 178 } 179 180 183 void setPrevRow (ToolbarRow prev) { 184 prevRow = prev; 185 } 186 187 188 ToolbarRow getPrevRow () { 189 return prevRow; 190 } 191 192 195 void setNextRow (ToolbarRow next) { 196 nextRow = next; 197 } 198 199 200 ToolbarRow getNextRow () { 201 return nextRow; 202 } 203 204 205 int getPrefWidth () { 206 if (toolbars.isEmpty()) 207 return -1; 208 return ((ToolbarConstraints)toolbars.lastElement()).getPrefWidth(); 209 } 210 211 212 boolean isEmpty () { 213 return toolbars.isEmpty(); 214 } 215 216 217 int toolbarCount () { 218 return toolbars.size(); 219 } 220 221 222 void updateBounds () { 223 Iterator it = toolbars.iterator(); 224 ToolbarConstraints tc; 225 while (it.hasNext()) { 226 tc = (ToolbarConstraints)it.next(); 227 tc.updateBounds(); 228 } 229 } 230 231 234 private void updateRowsBelow () { 235 for (int i = toolbarConfig.rowIndex(this) + 1; i < toolbarConfig.getRowCount(); i++) { 236 toolbarConfig.getRow(i).updateBounds(); 237 } 238 } 239 240 244 void switchBars (ToolbarConstraints left, ToolbarConstraints right) { 245 int leftIndex = toolbars.indexOf (left); 246 int rightIndex = toolbars.indexOf (right); 247 ToolbarConstraints leftPrev = null; 248 ToolbarConstraints rightNext = null; 249 250 try { 251 leftPrev = (ToolbarConstraints)toolbars.elementAt (leftIndex - 1); 252 } catch (ArrayIndexOutOfBoundsException e) { } 253 try { 254 rightNext = (ToolbarConstraints)toolbars.elementAt (rightIndex + 1); 255 } catch (ArrayIndexOutOfBoundsException e) { } 256 257 if (leftPrev != null) 258 leftPrev.removeNextBar (left); 259 left.removePrevBar (leftPrev); 260 left.removeNextBar (right); 261 262 right.removePrevBar (left); 263 right.removeNextBar (rightNext); 264 if (rightNext != null) 265 rightNext.removePrevBar (right); 266 267 if (leftPrev != null) 268 leftPrev.addNextBar (right); 269 left.addPrevBar (right); 270 left.addNextBar (rightNext); 271 272 right.addPrevBar (leftPrev); 273 right.addNextBar (left); 274 if (rightNext != null) 275 rightNext.addPrevBar (left); 276 277 toolbars.setElementAt (left, rightIndex); 278 toolbars.setElementAt (right, leftIndex); 279 } 280 281 284 void trySwitchLeft (ToolbarConstraints tc) { 285 int index = toolbars.indexOf (tc); 286 if (index == 0) 287 return; 288 289 try { 290 ToolbarConstraints prev = (ToolbarConstraints)toolbars.elementAt (index - 1); 291 if (ToolbarConstraints.canSwitchLeft (tc.getPosition(), tc.getWidth(), prev.getPosition(), prev.getWidth())) { 292 switchBars (prev, tc); 293 } 294 } catch (ArrayIndexOutOfBoundsException e) { } 295 } 296 297 300 void trySwitchRight (ToolbarConstraints tc) { 301 int index = toolbars.indexOf (tc); 302 303 try { 304 ToolbarConstraints next = (ToolbarConstraints)toolbars.elementAt (index + 1); 305 if (ToolbarConstraints.canSwitchRight (tc.getPosition(), tc.getWidth(), next.getPosition(), next.getWidth())) { 306 switchBars (tc, next); 307 next.setPosition (tc.getPosition() - next.getWidth() - ToolbarLayout.HGAP); 308 } 309 } catch (ArrayIndexOutOfBoundsException e) { } 310 } 311 312 315 int getPreferredHeight () { 316 ToolbarConstraints curConstr = null; 317 ToolbarPool pool = ToolbarPool.getDefault(); 318 prefHeight = 0; 319 int curHeight = 0; 320 for (Iterator iter = toolbars.iterator(); iter.hasNext(); ) { 321 curConstr = (ToolbarConstraints)iter.next(); 322 if (curConstr.getRowCount() == 1) { 324 Toolbar curToolbar = pool.findToolbar(curConstr.getName()); 325 if (curToolbar != null) { 328 curHeight = curToolbar.getPreferredSize().height; 329 if (prefHeight < curHeight) { 330 prefHeight = curHeight; 331 } 332 } 333 } 334 } 335 prefHeight = prefHeight <= 0 ? Toolbar.getBasicHeight() : Math.min(Toolbar.getBasicHeight(), prefHeight); 336 return prefHeight; 337 } 338 339 340 static class WritableToolbarRow { 341 342 Vector <ToolbarConstraints.WritableToolbar> toolbars; 343 344 346 public WritableToolbarRow () { 347 toolbars = new Vector <ToolbarConstraints.WritableToolbar>(); 348 } 349 350 353 public WritableToolbarRow (ToolbarRow row) { 354 this(); 355 initToolbars (row); 356 } 357 358 359 void initToolbars (ToolbarRow r) { 360 Iterator <ToolbarConstraints> it = r.toolbars.iterator(); 361 while (it.hasNext()) { 362 toolbars.addElement (new ToolbarConstraints.WritableToolbar (it.next())); 363 } 364 } 365 366 369 void addToolbar (ToolbarConstraints newTC) { 370 int index = 0; 371 Iterator it = toolbars.iterator(); 372 ToolbarConstraints.WritableToolbar tc; 373 while (it.hasNext()) { 374 tc = (ToolbarConstraints.WritableToolbar)it.next(); 375 if (newTC.getPosition() < tc.position) 376 break; 377 index++; 378 } 379 380 toolbars.insertElementAt (new ToolbarConstraints.WritableToolbar (newTC), index); 381 } 382 383 384 boolean isEmpty () { 385 return toolbars.isEmpty(); 386 } 387 388 389 public String toString () { 390 StringBuffer sb = new StringBuffer (); 391 392 sb.append (" <").append (ToolbarConfiguration.TAG_ROW).append (">\n"); Iterator it = toolbars.iterator(); 394 while (it.hasNext()) { 395 sb.append (it.next().toString()); 396 } 397 sb.append (" </").append (ToolbarConfiguration.TAG_ROW).append (">\n"); 399 return sb.toString(); 400 } 401 } } 404 | Popular Tags |