1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 import javax.swing.*; 26 import java.lang.reflect.Method ; 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.*; 33 34 39 40 public class JTabbedPaneSupport extends AbstractLayoutSupport { 41 42 private int selectedTab = -1; 43 44 private static Method addTabMethod1; 45 private static Method addTabMethod2; 46 private static Method addTabMethod3; 47 48 51 public Class getSupportedClass() { 52 return JTabbedPane.class; 53 } 54 55 59 public void removeComponent(int index) { 60 super.removeComponent(index); 61 if (selectedTab >= getComponentCount()) 62 selectedTab = getComponentCount() - 1; 63 } 64 65 71 public void processMouseClick(Point p, 72 Container container, 73 Container containerDelegate) 74 { 75 if (!(container instanceof JTabbedPane)) 76 return; 77 78 JTabbedPane tabbedPane = (JTabbedPane)container; 79 int n = tabbedPane.getTabCount(); 80 for (int i=0; i < n; i++) { 81 if (tabbedPane.getBoundsAt(i).contains(p)) { 82 selectedTab = i; 83 tabbedPane.setSelectedIndex(i); 84 break; 85 } 86 } 87 } 88 89 93 public void selectComponent(int index) { 94 selectedTab = index; } 96 97 103 public void arrangeContainer(Container container, 104 Container containerDelegate) 105 { 106 if (!(container instanceof JTabbedPane)) 107 return; 108 109 JTabbedPane tabbedPane = (JTabbedPane) container; 110 if (selectedTab >= 0) { 111 if (tabbedPane.getTabCount() > selectedTab) { 112 tabbedPane.setSelectedIndex(selectedTab); 114 115 Component comp = tabbedPane.getSelectedComponent(); 117 if (comp != null) 118 comp.setVisible(true); 119 tabbedPane.repaint(); 120 } 121 } 122 else if (tabbedPane.getTabCount() > 0) { 123 tabbedPane.getComponentAt(0).setVisible(true); 125 } 126 } 127 128 142 public int getNewIndex(Container container, 143 Container containerDelegate, 144 Component component, 145 int index, 146 Point posInCont, 147 Point posInComp) 148 { 149 if (!(container instanceof JTabbedPane)) 150 return -1; 151 return ((JTabbedPane)container).getTabCount(); 152 } 153 154 public String getAssistantContext() { 155 return "tabbedPaneLayout"; } 157 158 171 public boolean paintDragFeedback(Container container, 172 Container containerDelegate, 173 Component component, 174 LayoutConstraints newConstraints, 175 int newIndex, 176 Graphics g) 177 { 178 if (!(container instanceof JTabbedPane)) 179 return false; 180 181 JTabbedPane tabbedPane = (JTabbedPane) container; 182 if ((tabbedPane.getTabCount() == 0) || (component == tabbedPane.getComponentAt(0))) { 183 Dimension sz = container.getSize(); 184 Insets insets = container.getInsets(); 185 sz.width -= insets.left + insets.right; 186 sz.height -= insets.top + insets.bottom; 187 g.drawRect(0, 0, sz.width, sz.height); 188 } 189 else { 190 Rectangle rect = tabbedPane.getComponentAt(0).getBounds(); 191 g.drawRect(rect.x, rect.y, rect.width, rect.height); 192 } 193 return true; 194 } 195 196 203 public void addComponentsToContainer(Container container, 204 Container containerDelegate, 205 Component[] components, 206 int index) 207 { 208 if (!(container instanceof JTabbedPane)) 209 return; 210 211 for (int i=0; i < components.length; i++) { 212 LayoutConstraints constraints = getConstraints(i + index); 213 if (constraints instanceof TabConstraints) { 214 JTabbedPane tabbedPane = (JTabbedPane) container; 215 try { 216 Object title = 217 ((FormProperty)constraints.getProperties()[0]) 218 .getRealValue(); 219 Object icon = 220 ((FormProperty)constraints.getProperties()[1]) 221 .getRealValue(); 222 Object tooltip = 223 ((FormProperty)constraints.getProperties()[2]) 224 .getRealValue(); 225 226 tabbedPane.addTab( 227 title instanceof String ? (String ) title : null, 228 icon instanceof Icon ? (Icon) icon : null, 229 components[i], 230 tooltip instanceof String ? (String ) tooltip : null); 231 } 232 catch (Exception ex) { 233 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 234 } 235 } 236 } 237 } 238 239 241 251 protected CodeExpression readComponentCode(CodeStatement statement, 252 CodeGroup componentCode) 253 { 254 CodeExpression compExp; 255 int[] constrPropsIndices; 256 CodeExpression[] params = statement.getStatementParameters(); 257 258 Object connectingObject = statement.getMetaObject(); 259 if (getAddTabMethod1().equals(connectingObject)) { 260 compExp = params[2]; 261 constrPropsIndices = new int[] { 0, 1, -1, 2 }; } 263 else if (getAddTabMethod2().equals(connectingObject)) { 264 compExp = params[2]; 265 constrPropsIndices = new int[] { 0, 1, -1 }; } 267 else if (getAddTabMethod3().equals(connectingObject)) { 268 compExp = params[1]; 269 constrPropsIndices = new int[] { 0, -1 }; } 271 else return null; 272 273 TabConstraints constr = new TabConstraints("tab"); Node.Property[] props = constr.getProperties(); 275 for (int i=0; i < params.length; i++) { 276 if (params[i] != compExp) 277 FormCodeSupport.readPropertyExpression( 278 params[i], 279 props[constrPropsIndices[i]], 280 false); 281 } 282 getConstraintsList().add(constr); 283 284 componentCode.addStatement(statement); 285 286 return compExp; 287 } 288 289 297 protected void createComponentCode(CodeGroup componentCode, 298 CodeExpression componentExpression, 299 int index) 300 { 301 LayoutConstraints constr = getConstraints(index); 302 if (!(constr instanceof TabConstraints)) 303 return; 305 ((TabConstraints)constr).createComponentCode( 306 componentCode, 307 getLayoutContext().getContainerCodeExpression(), 308 componentExpression); 309 } 310 311 316 protected LayoutConstraints createDefaultConstraints() { 317 return new TabConstraints("tab"+(getComponentCount())); } 319 320 322 private static Method getAddTabMethod1() { 324 if (addTabMethod1 == null) { 325 try { 326 addTabMethod1 = JTabbedPane.class.getMethod( 327 "addTab", new Class [] { String .class, Icon.class, 329 Component.class, String .class }); 330 } 331 catch (NoSuchMethodException ex) { ex.printStackTrace(); 333 } 334 } 335 return addTabMethod1; 336 } 337 338 private static Method getAddTabMethod2() { 340 if (addTabMethod2 == null) { 341 try { 342 addTabMethod2 = JTabbedPane.class.getMethod( 343 "addTab", new Class [] { String .class, Icon.class, 345 Component.class }); 346 } 347 catch (NoSuchMethodException ex) { ex.printStackTrace(); 349 } 350 } 351 return addTabMethod2; 352 } 353 354 private static Method getAddTabMethod3() { 356 if (addTabMethod3 == null) { 357 try { 358 addTabMethod3 = JTabbedPane.class.getMethod( 359 "addTab", new Class [] { String .class, Component.class }); 361 } 362 catch (NoSuchMethodException ex) { ex.printStackTrace(); 364 } 365 } 366 return addTabMethod3; 367 } 368 369 371 374 public static class TabConstraints implements LayoutConstraints { 375 private String title; 376 private Icon icon; 377 private String toolTip; 378 379 private FormProperty[] properties; 380 381 private CodeExpression containerExpression; 382 private CodeExpression componentExpression; 383 private CodeGroup componentCode; 384 private CodeExpression[] propertyExpressions; 385 386 public TabConstraints(String title) { 387 this.title = title; 388 } 389 390 public TabConstraints(String title, Icon icon, String toolTip) { 391 this.title = title; 392 this.icon = icon; 393 this.toolTip = toolTip; 394 } 395 396 public String getTitle() { 397 return title; 398 } 399 400 public Icon getIcon() { 401 return icon; 402 } 403 404 public String getToolTip() { 405 return toolTip; 406 } 407 408 410 public Node.Property[] getProperties() { 411 if (properties == null) { 412 properties = new FormProperty[] { 413 new FormProperty("TabConstraints.tabTitle", String .class, 415 getBundle().getString("PROP_tabTitle"), getBundle().getString("HINT_tabTitle")) { 418 public Object getTargetValue() { 419 return title; 420 } 421 422 public void setTargetValue(Object value) { 423 title = (String )value; 424 } 425 426 protected Object getRealValue(Object value) { 427 Object realValue = super.getRealValue(value); 428 if (realValue == FormDesignValue.IGNORED_VALUE) 429 realValue = ((FormDesignValue)value).getDescription(); 430 return realValue; 431 } 432 433 protected void propertyValueChanged(Object old, Object current) { 434 if (isChangeFiring()) 435 updateCode(); 436 super.propertyValueChanged(old, current); 437 } 438 }, 439 440 new FormProperty("TabConstraints tabIcon", Icon.class, 442 getBundle().getString("PROP_tabIcon"), getBundle().getString("HINT_tabIcon")) { 445 public Object getTargetValue() { 446 return icon; 447 } 448 449 public void setTargetValue(Object value) { 450 icon = (Icon)value; 451 } 452 453 public boolean supportsDefaultValue() { 454 return true; 455 } 456 457 public Object getDefaultValue() { 458 return null; 459 } 460 461 protected void propertyValueChanged(Object old, Object current) { 462 if (isChangeFiring()) 463 updateCode(); 464 super.propertyValueChanged(old, current); 465 } 466 }, 467 468 new FormProperty("TabConstraints tabToolTip", String .class, 470 getBundle().getString("PROP_tabToolTip"), getBundle().getString("HINT_tabToolTip")) { 473 public Object getTargetValue() { 474 return toolTip; 475 } 476 477 public void setTargetValue(Object value) { 478 toolTip = (String )value; 479 } 480 481 protected Object getRealValue(Object value) { 482 Object realValue = super.getRealValue(value); 483 if (realValue == FormDesignValue.IGNORED_VALUE) 484 realValue = ((FormDesignValue)value).getDescription(); 485 return realValue; 486 } 487 488 public boolean supportsDefaultValue() { 489 return true; 490 } 491 492 public Object getDefaultValue() { 493 return null; 494 } 495 496 protected void propertyValueChanged(Object old, Object current) { 497 if (isChangeFiring()) 498 updateCode(); 499 super.propertyValueChanged(old, current); 500 } 501 } 502 }; 503 504 properties[0].setChanged(true); 505 } 506 507 return properties; 508 } 509 510 public Object getConstraintsObject() { 511 return title; 512 } 513 514 public LayoutConstraints cloneConstraints() { 515 LayoutConstraints constr = new TabConstraints(title); 516 org.netbeans.modules.form.FormUtils.copyProperties( 517 getProperties(), 518 constr.getProperties(), 519 FormUtils.CHANGED_ONLY | FormUtils.DISABLE_CHANGE_FIRING); 520 return constr; 521 } 522 523 525 private void createComponentCode(CodeGroup compCode, 526 CodeExpression contExp, 527 CodeExpression compExp) 528 { 529 this.componentCode = compCode; 530 this.containerExpression = contExp; 531 this.componentExpression = compExp; 532 this.propertyExpressions = null; 533 updateCode(); 534 } 535 536 private void updateCode() { 537 if (componentCode == null) 538 return; 539 540 CodeStructure.removeStatements( 541 componentCode.getStatementsIterator()); 542 componentCode.removeAll(); 543 544 getProperties(); 545 546 Method addTabMethod; 547 CodeExpression[] params; 548 549 if (properties[2].isChanged()) { 550 addTabMethod = getAddTabMethod1(); 551 params = new CodeExpression[] { getPropertyExpression(0), getPropertyExpression(1), componentExpression, 554 getPropertyExpression(2) }; } 556 else if (properties[1].isChanged()) { 557 addTabMethod = getAddTabMethod2(); 558 params = new CodeExpression[] { getPropertyExpression(0), getPropertyExpression(1), componentExpression }; 561 } 562 else { addTabMethod = getAddTabMethod3(); 564 params = new CodeExpression[] { getPropertyExpression(0), componentExpression }; 566 } 567 568 CodeStatement addTabStatement = CodeStructure.createStatement( 569 containerExpression, 570 addTabMethod, 571 params); 572 componentCode.addStatement(addTabStatement); 573 } 574 575 private CodeExpression getPropertyExpression(int index) { 576 if (propertyExpressions == null) { 577 propertyExpressions = new CodeExpression[properties.length]; 578 for (int i=0; i < properties.length; i++) { 579 propertyExpressions[i] = 580 componentExpression.getCodeStructure().createExpression( 581 FormCodeSupport.createOrigin(properties[i])); 582 } 583 } 584 return propertyExpressions[index]; 585 } 586 } 587 } 588 | Popular Tags |