1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.lang.reflect.Method ; 25 import javax.swing.JButton ; 26 import javax.swing.JSplitPane ; 27 28 import org.openide.nodes.Node; 29 30 import org.netbeans.modules.form.layoutsupport.*; 31 import org.netbeans.modules.form.codestructure.*; 32 import org.netbeans.modules.form.FormProperty; 33 34 38 39 public class JSplitPaneSupport extends AbstractLayoutSupport { 40 41 private static Method setLeftComponentMethod; 42 private static Method setRightComponentMethod; 43 private static Method setTopComponentMethod; 44 private static Method setBottomComponentMethod; 45 46 private static final String LEFT_TOP_BUTTON = "cp_left_top_button"; private static final String RIGHT_BOTTOM_BUTTON = "cp_right_bottom_button"; 49 52 public Class getSupportedClass() { 53 return JSplitPane .class; 54 } 55 56 70 public LayoutConstraints getNewConstraints(Container container, 71 Container containerDelegate, 72 Component component, 73 int index, 74 Point posInCont, 75 Point posInComp) 76 { 77 if (!(container instanceof JSplitPane )) 78 return null; 79 80 JSplitPane splitPane = (JSplitPane ) container; 81 Dimension sz = splitPane.getSize(); 82 int orientation = splitPane.getOrientation(); 83 84 JButton left = (JButton ) splitPane.getClientProperty(LEFT_TOP_BUTTON); 85 JButton right = (JButton ) splitPane.getClientProperty(RIGHT_BOTTOM_BUTTON); 86 87 if ( (left == null && right == null) || 88 (left != null && right != null) ) 89 { 90 String freePosition; 91 if (orientation == JSplitPane.HORIZONTAL_SPLIT) { 92 if (posInCont.x <= sz.width / 2) 93 freePosition = JSplitPane.LEFT; 94 else 95 freePosition = JSplitPane.RIGHT; 96 } 97 else { 98 if (posInCont.y <= sz.height / 2) 99 freePosition = JSplitPane.TOP; 100 else 101 freePosition = JSplitPane.BOTTOM; 102 } 103 assistantParams = freePosition; 104 return new SplitConstraints(freePosition); 105 } 106 107 assistantParams = findFreePosition(); 108 return new SplitConstraints(assistantParams); 109 } 110 111 private String assistantParams; 112 public String getAssistantContext() { 113 return "splitPaneLayout"; } 115 116 public Object [] getAssistantParams() { 117 return new Object [] {assistantParams}; 118 } 119 120 132 public boolean paintDragFeedback(Container container, 133 Container containerDelegate, 134 Component component, 135 LayoutConstraints newConstraints, 136 int newIndex, 137 Graphics g) 138 { 139 if (!(container instanceof JSplitPane )) 140 return false; 141 142 String position = (String ) newConstraints.getConstraintsObject(); 143 if (position == null) 144 return false; 145 146 JSplitPane splitPane = (JSplitPane ) container; 147 int orientation = splitPane.getOrientation(); 148 149 Dimension sz = splitPane.getSize(); 150 Insets insets = container.getInsets(); 151 sz.width -= insets.left + insets.right; 152 sz.height -= insets.top + insets.bottom; 153 154 Rectangle rect = new Rectangle(insets.left, insets.top, sz.width, sz.height); 155 156 if (orientation == JSplitPane.HORIZONTAL_SPLIT) { 157 Component left = splitPane.getLeftComponent(); 158 Component right = splitPane.getRightComponent(); 159 160 if (position == JSplitPane.LEFT) { 161 if ((right == null) || (right == component)) { 162 rect.width = sz.width / 2; 163 } 164 else { 165 rect.width = right.getBounds().x - rect.x; 166 } 167 } 168 else { 169 if ((left == null) || (left == component)) { 170 rect.x = insets.left + sz.width / 2; 171 rect.width = sz.width - rect.x; 172 } 173 else { 174 rect.x = left.getBounds().x + left.getBounds().width; 175 rect.width = sz.width - rect.x; 176 } 177 } 178 } 179 else { 180 Component top = splitPane.getTopComponent(); 181 Component bottom = splitPane.getBottomComponent(); 182 183 if (position == JSplitPane.TOP) { 184 if ((bottom == null) || (bottom == component)) { 185 rect.height /= 2; 186 } 187 else { 188 rect.height = bottom.getBounds().y - rect.y; 189 } 190 } 191 else { 192 if ((top == null) || (top == component)) { 193 rect.y = insets.top + sz.height / 2; 194 rect.height = sz.height - rect.y; 195 } 196 else { 197 rect.y = top.getBounds().y + top.getBounds().height; 198 rect.height = sz.height - rect.y; 199 } 200 } 201 } 202 g.drawRect(rect.x, rect.y, rect.width, rect.height); 203 return true; 204 } 205 206 213 public void addComponentsToContainer(Container container, 214 Container containerDelegate, 215 Component[] components, 216 int index) 217 { 218 if (!(container instanceof JSplitPane )) 219 return; 220 221 for (int i=0; i < components.length; i++) { 222 JSplitPane splitPane = (JSplitPane ) container; 223 224 int descPos = convertPosition(getConstraints(i + index)); 225 if (descPos == 0) { 226 if(splitPane.getClientProperty(LEFT_TOP_BUTTON)==null) { 227 splitPane.putClientProperty(LEFT_TOP_BUTTON, splitPane.getLeftComponent()); 230 } 231 splitPane.setLeftComponent(components[i]); 232 } 233 else if (descPos == 1) { 234 if(splitPane.getClientProperty(RIGHT_BOTTOM_BUTTON)==null) { 235 splitPane.putClientProperty(RIGHT_BOTTOM_BUTTON, splitPane.getRightComponent()); 238 } 239 splitPane.setRightComponent(components[i]); 240 } 241 242 } 243 } 244 245 252 public boolean removeComponentFromContainer(Container container, 253 Container containerDelegate, 254 Component component) 255 { 256 if( !(containerDelegate instanceof JSplitPane ) ) { 257 return false; } 259 260 JSplitPane splitPane = (JSplitPane ) containerDelegate; 261 262 if( component == splitPane.getLeftComponent() ) { 263 if( super.removeComponentFromContainer(container, containerDelegate, component) ) { 264 JButton left = (JButton ) splitPane.getClientProperty(LEFT_TOP_BUTTON); 265 if( left != null ) { 266 splitPane.setLeftComponent(left); 268 splitPane.putClientProperty(LEFT_TOP_BUTTON, null); 269 } 270 return true; 271 } 272 } else if ( component == splitPane.getRightComponent() ) { 273 if( super.removeComponentFromContainer(container, containerDelegate, component) ) { 274 JButton right = (JButton ) splitPane.getClientProperty(RIGHT_BOTTOM_BUTTON); 275 if( right != null ) { 276 splitPane.setRightComponent(right); 278 splitPane.putClientProperty(RIGHT_BOTTOM_BUTTON, null); 279 } 280 return true; 281 } 282 } 283 284 return false; 285 } 286 287 294 public boolean clearContainer(Container container, 295 Container containerDelegate) 296 { 297 298 300 JSplitPane splitPane = (JSplitPane ) container; 301 JButton left = (JButton ) splitPane.getClientProperty(LEFT_TOP_BUTTON); 302 JButton right = (JButton ) splitPane.getClientProperty(RIGHT_BOTTOM_BUTTON); 303 304 if(left != null) { 305 removeComponentFromContainer(container, containerDelegate, splitPane.getLeftComponent()); 307 } 308 if(right != null) { 309 removeComponentFromContainer(container, containerDelegate, splitPane.getRightComponent()); 311 } 312 313 return true; 314 } 315 316 317 319 329 protected CodeExpression readComponentCode(CodeStatement statement, 330 CodeGroup componentCode) 331 { 332 CodeExpression[] params = statement.getStatementParameters(); 333 if (params.length != 1) 334 return null; 335 336 String position; 337 338 Object connectingObject = statement.getMetaObject(); 339 if (getSetLeftComponentMethod().equals(connectingObject)) 340 position = JSplitPane.LEFT; 341 else if (getSetRightComponentMethod().equals(connectingObject)) 342 position = JSplitPane.RIGHT; 343 else if (getSetTopComponentMethod().equals(connectingObject)) 344 position = JSplitPane.TOP; 345 else if (getSetBottomComponentMethod().equals(connectingObject)) 346 position = JSplitPane.BOTTOM; 347 else return null; 348 349 SplitConstraints constr = new SplitConstraints(position); 350 getConstraintsList().add(constr); 351 352 componentCode.addStatement(statement); 353 354 return params[0]; 355 } 356 357 365 protected void createComponentCode(CodeGroup componentCode, 366 CodeExpression componentExpression, 367 int index) 368 { 369 LayoutConstraints constr = getConstraints(index); 370 if (!(constr instanceof SplitConstraints)) 371 return; 373 ((SplitConstraints)constr).createComponentCode( 374 componentCode, 375 getLayoutContext().getContainerCodeExpression(), 376 componentExpression); 377 } 378 379 384 protected LayoutConstraints createDefaultConstraints() { 385 return new SplitConstraints(findFreePosition()); 386 } 387 388 390 private int convertPosition(LayoutConstraints desc) { 391 Object position = desc.getConstraintsObject(); 392 if (JSplitPane.LEFT.equals(position) || JSplitPane.TOP.equals(position)) 393 return 0; 394 if (JSplitPane.RIGHT.equals(position) || JSplitPane.BOTTOM.equals(position)) 395 return 1; 396 return -1; 397 } 398 399 private String findFreePosition() { 400 int leftTop = 0, rightBottom = 0; 401 int orientation = JSplitPane.HORIZONTAL_SPLIT; 402 403 for (int i=0, n=getComponentCount(); i < n; i++) { 404 LayoutConstraints constraints = getConstraints(i); 405 if (!(constraints instanceof SplitConstraints)) 406 continue; 407 408 int constrPos = convertPosition(constraints); 409 if (constrPos == 0) 410 leftTop++; 411 else if (constrPos == 1) 412 rightBottom++; 413 } 414 415 if (leftTop == 0 || leftTop < rightBottom) 416 return orientation == JSplitPane.HORIZONTAL_SPLIT ? 417 JSplitPane.LEFT : JSplitPane.TOP; 418 else 419 return orientation == JSplitPane.HORIZONTAL_SPLIT ? 420 JSplitPane.RIGHT : JSplitPane.BOTTOM; 421 } 422 423 425 private static Method getSetLeftComponentMethod() { 426 if (setLeftComponentMethod == null) 427 setLeftComponentMethod = getAddMethod("setLeftComponent"); return setLeftComponentMethod; 429 } 430 431 private static Method getSetRightComponentMethod() { 432 if (setRightComponentMethod == null) 433 setRightComponentMethod = getAddMethod("setRightComponent"); return setRightComponentMethod; 435 } 436 437 private static Method getSetTopComponentMethod() { 438 if (setTopComponentMethod == null) 439 setTopComponentMethod = getAddMethod("setTopComponent"); return setTopComponentMethod; 441 } 442 443 private static Method getSetBottomComponentMethod() { 444 if (setBottomComponentMethod == null) 445 setBottomComponentMethod = getAddMethod("setBottomComponent"); return setBottomComponentMethod; 447 } 448 449 private static Method getAddMethod(String name) { 450 try { 451 return JSplitPane .class.getMethod(name, 452 new Class [] { Component.class }); 453 } 454 catch (NoSuchMethodException ex) { ex.printStackTrace(); 456 } 457 return null; 458 } 459 460 462 465 public static class SplitConstraints implements LayoutConstraints { 466 private String position; 467 468 private Node.Property[] properties; 469 470 private CodeExpression containerExpression; 471 private CodeExpression componentExpression; 472 private CodeGroup componentCode; 473 474 public SplitConstraints(String position) { 475 this.position = position; 476 } 477 478 public Node.Property[] getProperties() { 479 if (properties == null) { 480 properties = new Node.Property[] { 481 new FormProperty( 482 "SplitConstraints splitPosition", String .class, 484 getBundle().getString("PROP_splitPos"), getBundle().getString("HINT_splitPos")) { 487 public Object getTargetValue() { 488 return position; 489 } 490 public void setTargetValue(Object value) { 491 position = (String )value; 492 } 493 public PropertyEditor getExpliciteEditor() { 494 return new SplitPositionEditor(); 495 } 496 protected void propertyValueChanged(Object old, 497 Object current) { 498 if (isChangeFiring()) 499 updateCode(); 500 super.propertyValueChanged(old, current); 501 } 502 public void setPropertyContext( 503 org.netbeans.modules.form.FormPropertyContext ctx) 504 { } } 507 }; 508 properties[0].setValue("NOI18N", Boolean.TRUE); } 510 511 return properties; 512 } 513 514 public Object getConstraintsObject() { 515 return position; 516 } 517 518 public LayoutConstraints cloneConstraints() { 519 return new SplitConstraints(position); 520 } 521 522 private void createComponentCode(CodeGroup compCode, 523 CodeExpression contExp, 524 CodeExpression compExp) 525 { 526 componentCode = compCode; 527 containerExpression = contExp; 528 componentExpression = compExp; 529 updateCode(); 530 } 531 532 private void updateCode() { 533 if (componentCode == null) 534 return; 535 536 CodeStructure.removeStatements( 537 componentCode.getStatementsIterator()); 538 componentCode.removeAll(); 539 540 Method addMethod; 541 if (JSplitPane.LEFT.equals(position)) 542 addMethod = getSetLeftComponentMethod(); 543 else if (JSplitPane.RIGHT.equals(position)) 544 addMethod = getSetRightComponentMethod(); 545 else if (JSplitPane.TOP.equals(position)) 546 addMethod = getSetTopComponentMethod(); 547 else if (JSplitPane.BOTTOM.equals(position)) 548 addMethod = getSetBottomComponentMethod(); 549 else return; 550 551 componentCode.addStatement( 552 CodeStructure.createStatement( 553 containerExpression, 554 addMethod, 555 new CodeExpression[] { componentExpression })); 556 } 557 } 558 559 static class SplitPositionEditor extends PropertyEditorSupport { 560 private final String [] values = { 561 JSplitPane.LEFT, 562 JSplitPane.RIGHT, 563 JSplitPane.TOP, 564 JSplitPane.BOTTOM 565 }; 566 567 public String [] getTags() { 568 return values; 569 } 570 571 public String getAsText() { 572 return (String )getValue(); 573 } 574 575 public void setAsText(String str) { 576 for (int i = 0; i < values.length; i++) 577 if (str.equals(values[i])) { 578 setValue(str); 579 break; 580 } 581 } 582 } 583 584 } 585 | Popular Tags |