1 19 27 28 package org.netbeans.modules.xml.schema.abe; 29 30 import java.awt.BorderLayout ; 31 import java.awt.Component ; 32 import java.awt.Dimension ; 33 import java.awt.Graphics ; 34 import java.awt.Graphics2D ; 35 import java.awt.event.ComponentEvent ; 36 import java.awt.event.ComponentListener ; 37 import java.util.ArrayList ; 38 import java.util.LinkedList ; 39 import java.util.List ; 40 import javax.swing.JPanel ; 41 import javax.swing.SpringLayout ; 42 import javax.swing.SwingConstants ; 43 import javax.swing.SwingUtilities ; 44 import org.netbeans.api.progress.ProgressHandle; 45 import org.netbeans.api.progress.ProgressHandleFactory; 46 import org.netbeans.modules.xml.axi.AXIComponent; 47 import org.netbeans.modules.xml.axi.AXIDocument; 48 import org.netbeans.modules.xml.axi.AnyAttribute; 49 import org.netbeans.modules.xml.axi.AnyElement; 50 import org.netbeans.modules.xml.axi.Attribute; 51 import org.netbeans.modules.xml.axi.Compositor; 52 import org.netbeans.modules.xml.axi.ContentModel; 53 import org.netbeans.modules.xml.axi.Element; 54 import org.netbeans.modules.xml.axi.datatype.Datatype; 55 import org.netbeans.modules.xml.axi.visitor.AXIVisitor; 56 import org.netbeans.modules.xml.schema.abe.palette.DnDHelper; 57 import org.openide.util.NbBundle; 58 import org.openide.util.RequestProcessor; 59 60 64 public abstract class ContainerPanel extends AnnotatedBorderPanel implements AXIVisitor{ 65 private static final long serialVersionUID = 7526472295622776147L; 66 private AXIComponent axiParent; 67 Component parentPanel; 68 private static final int INTER_PANEL_VERTICAL_SPACE = 0; 69 private int panelIndendation = InstanceDesignConstants.TAG_INDENT ; 70 private LinkedList <Component > childrenList = new LinkedList <Component >(); 71 protected Component visitorResult = null; 72 boolean openByDefault = true; 73 74 public ContainerPanel(InstanceUIContext context, AXIComponent axiParent, 75 Component parentPanel, boolean openByDefault) { 76 super(context); 77 this.setAXIParent(axiParent); 78 this.parentPanel = parentPanel; 79 this.openByDefault = openByDefault; 80 initialize(); 81 } 82 83 protected abstract void setupAXIComponentListener(); 84 public abstract List <? extends AXIComponent> getAXIChildren(); 85 86 private void initialize(){ 87 setOpaque(false); 88 setLayout(new BorderLayout ()); 89 addHeaderPanel(); 90 initChildrenPanel(); 91 if(openByDefault) 92 addAllChildren(); 93 setupAXIComponentListener(); 94 } 95 96 97 public void addHeaderPanel(){ 98 } 99 100 JPanel childrenPanel; 101 SpringLayout childrenPanelLayout; 102 Component childrenPanelLastComponent; 103 public void initChildrenPanel(){ 104 childrenPanelLayout = new SpringLayout (); 106 childrenPanel = new JPanel (childrenPanelLayout); 107 childrenPanel.setOpaque(false); 108 109 add(childrenPanel, BorderLayout.CENTER); 110 111 112 TweenerPanel tweener = new TweenerPanel(SwingConstants.HORIZONTAL, context); 114 childrenPanel.add(tweener); 116 getChildrenList().add(tweener); 117 addTweenerListener(tweener); 118 119 childrenPanelLayout.putConstraint(SpringLayout.WEST, tweener, 121 getChildrenIndent(), SpringLayout.WEST, childrenPanel); 122 childrenPanelLayout.putConstraint(SpringLayout.NORTH, tweener, 123 getInterComponentVerticalSpacing(), SpringLayout.NORTH, childrenPanel); 124 adjustChildrenPanelSize(); 125 addComponentEventListener(tweener); 126 childrenPanelLastComponent = tweener; 127 128 } 129 130 131 public void verifyChildrenWithModel(){ 132 List <Component > noTweenerList = new ArrayList <Component >(); 133 for(Component comp : getChildrenList()){ 134 if(comp instanceof TweenerPanel) 135 continue; 136 noTweenerList.add(comp); 137 } 138 139 if(noTweenerList.size() != getAXIChildren().size()){ 140 removeAndAddAllChildren(); 142 return; 143 } 144 145 for(AXIComponent axiComponent : getAXIChildren()){ 146 if(isAlreadyAdded(axiComponent) == null){ 148 removeAndAddAllChildren(); 149 return; 150 } 151 } 152 } 153 154 155 public void removeAndAddAllChildren(){ 156 this.remove(childrenPanel); 157 childrenPanel = null; 158 initChildrenPanel(); 159 addAllChildren(); 161 } 162 163 boolean childrenAdded = false; 164 public void addAllChildren(){ 165 if(childrenAdded) 166 return; 167 childrenAdded = true; 168 final List <? extends AXIComponent> children = getAXIChildren(); 169 if(children.size() < InstanceDesignerPanel.EXPAND_BY_DEFAULT_LIMIT){ 170 addAllChildren(children) ; 171 }else{ 172 RequestProcessor.getDefault().post(new Runnable () { 173 public void run() { 174 addAllChildren(children) ; 175 } 176 }); 177 } 178 } 179 180 private void addAllChildren(List <? extends AXIComponent> children){ 181 initProgress(children); 182 for(final AXIComponent axiComp: children){ 183 showProgress(); 184 visitorResult = null; 185 axiComp.accept(ContainerPanel.this); 186 final Component compToAdd = visitorResult; 187 if(visitorResult != null){ 188 appendChild(compToAdd, false); 189 TweenerPanel tweener = new TweenerPanel(SwingConstants.HORIZONTAL, context); 190 addTweenerListener(tweener); 191 appendChild(tweener, false); 192 } 193 } 194 adjustChildrenPanelSize(); 195 revalidate(); 196 finishProgress(); 197 } 198 199 200 ProgressHandle progressHandle = null; 201 int stepCount = 0; 202 private void initProgress(List <? extends AXIComponent> children) { 203 final int size = children.size(); 204 if( size < InstanceDesignerPanel.EXPAND_BY_DEFAULT_LIMIT){ 205 return; 206 } 207 UIUtilities.showBulbMessage(NbBundle.getMessage(ContainerPanel.class, 208 "MSG_RENDERING_CHILDREN")+"...", this.context); 209 progressHandle= ProgressHandleFactory.createHandle(NbBundle.getMessage( 210 ContainerPanel.class, "MSG_RENDERING_CHILDREN")+": "); 211 progressHandle.setInitialDelay(1); 212 progressHandle.start(size+10); 213 UIUtilities.setBusyCursor(this.context); 214 stepCount = 0; 215 } 216 217 private void showProgress(){ 218 if(progressHandle != null){ 219 progressHandle.progress(stepCount++); 220 } 221 } 222 223 private void finishProgress(){ 224 if(progressHandle != null){ 225 progressHandle.finish(); 226 progressHandle = null; 227 UIUtilities.setDefaultCursor(this.context); 228 UIUtilities.hideGlassMessage(); 229 stepCount = 0; 230 } 231 } 232 233 public int getChildrenIndent(){ 234 return getPanelIndendation()+InstanceDesignConstants.COMPOSITOR_CHILDREN_INDENT; } 236 237 public void appendChild(Component component, boolean resize){ 238 appendChild(component, 0, resize); 239 } 240 241 public void appendChild(Component component, int vPadding, boolean resize){ 242 childrenPanel.add(component); 243 getChildrenList().add(component); 244 childrenPanelLayout.putConstraint(SpringLayout.WEST, component, 246 getChildrenIndent(), SpringLayout.WEST, childrenPanel); 247 childrenPanelLayout.putConstraint(SpringLayout.NORTH, component, 248 getInterComponentVerticalSpacing() + vPadding, SpringLayout.SOUTH, childrenPanelLastComponent); 249 250 childrenPanelLastComponent = component; 251 if(resize){ 252 adjustChildrenPanelSize(); 253 revalidate(); 254 } 255 addComponentEventListener(component); 256 } 257 258 public boolean addChildAt(Component component, int index) { 259 childrenPanel.add(component); 260 Component cAbove = null; 261 Component cBelow = null; 262 try{ 263 cAbove = getAboveAdjacentComponent(index); 264 cBelow = getBelowAdjacentComponent(index); 265 }catch(Exception e){ 266 removeAndAddAllChildren(); 267 return false; 268 } 269 if(cAbove == null){ 270 return false; 272 } 273 childrenPanelLayout.putConstraint(SpringLayout.WEST, component, 275 getChildrenIndent(), SpringLayout.WEST, childrenPanel); 276 277 childrenPanelLayout.putConstraint(SpringLayout.NORTH, component, 278 getInterComponentVerticalSpacing(), SpringLayout.SOUTH, cAbove); 279 TweenerPanel tweener = new TweenerPanel(SwingConstants.HORIZONTAL, context); 281 addTweenerListener(tweener); 282 childrenPanel.add(tweener); 283 childrenPanelLayout.putConstraint(SpringLayout.WEST, tweener, 284 getChildrenIndent(), SpringLayout.WEST, childrenPanel); 285 286 childrenPanelLayout.putConstraint(SpringLayout.NORTH, tweener, 287 getInterComponentVerticalSpacing(), SpringLayout.SOUTH, component); 288 289 if(cBelow != null){ 290 childrenPanelLayout.putConstraint(SpringLayout.NORTH, cBelow, 292 getInterComponentVerticalSpacing(), SpringLayout.SOUTH, tweener); 293 } 294 295 if(getChildrenList().getLast() == cAbove){ 297 getChildrenList().add(component); 299 getChildrenList().add(tweener); 300 }else{ 301 getChildrenList().add(getChildrenList().indexOf(cAbove) + 1, component); 302 getChildrenList().add(getChildrenList().indexOf(component) + 1, tweener); 303 } 304 305 adjustChildrenPanelSize(); 306 revalidate(); 307 addComponentEventListener(component); 309 addComponentEventListener(tweener); 310 return true; 311 } 312 313 314 public void removeComponent(Component component){ 315 int cIndex = getChildrenList().indexOf(component); 316 int belowTIndex = cIndex + 1; 318 int aboveTIndex = cIndex - 1; 319 Component belowTweener = getChildrenList().get(belowTIndex); 320 Component aboveTweener = getChildrenList().get(aboveTIndex); 321 Component belowComponent = null; 322 if(getChildrenList().getLast() != belowTweener){ 323 belowComponent = getChildrenList().get(belowTIndex + 1); 324 } 325 326 childrenPanelLayout.removeLayoutComponent(component); 327 childrenPanelLayout.removeLayoutComponent(belowTweener); 328 childrenPanel.remove(component); 329 childrenPanel.remove(belowTweener); 330 331 if(belowComponent != null){ 332 childrenPanelLayout.putConstraint(SpringLayout.NORTH, belowComponent, 333 getInterComponentVerticalSpacing(), SpringLayout.SOUTH, aboveTweener); 334 } 335 336 getChildrenList().remove(component); 337 getChildrenList().remove(belowTweener); 338 339 adjustChildrenPanelSize(); 340 revalidate(); 341 repaint(); 342 343 } 344 345 346 347 private void addComponentEventListener(Component comp){ 348 comp.addComponentListener(new ComponentListener () { 349 public void componentHidden(ComponentEvent e) { 350 adjustChildrenPanelSize(); 351 } 352 public void componentMoved(ComponentEvent e) { 353 } 354 int callCount = 1; 355 public void componentResized(ComponentEvent e) { 356 if( (callCount <= 2) && ( (ContainerPanel.this instanceof GlobalComplextypeContainerPanel) 357 ||(ContainerPanel.this instanceof GlobalElementsContainerPanel) ) ){ 358 callCount++; 362 return; 363 } 364 adjustChildrenPanelSize(); 365 } 366 public void componentShown(ComponentEvent e) { 367 adjustChildrenPanelSize(); 368 } 369 }); 370 } 371 372 public Component getAboveAdjacentComponent(int index){ 373 return getChildrenList().get(index * 2); 374 } 375 376 public Component getBelowAdjacentComponent(int index){ 377 int ind = (index * 2) + 1; 378 if(ind >= getChildrenList().size()) 379 return null; 380 return getChildrenList().get(ind); 381 } 382 383 384 public void adjustChildrenPanelSize() { 385 int width = 0; 386 int height = 0; 387 int indent = getChildrenIndent(); 388 int spacing = getInterComponentVerticalSpacing(); 389 for(Component child: childrenPanel.getComponents()){ 390 if(!child.isVisible()) 391 break; 392 Dimension dim = child.getPreferredSize(); 393 int curWidth = dim.width + 394 indent; if(curWidth > width) 396 width = curWidth; 397 height += dim.height + spacing; 398 } 399 width += 20; 401 Dimension dim = new Dimension (width, height+3); 402 403 Dimension old = childrenPanel.getPreferredSize(); 404 boolean revalidateChild = false; 405 if( (old.height != dim.height) || (old.width != dim.width) ){ 406 childrenPanel.setPreferredSize(dim); 407 childrenPanel.setMinimumSize(dim); 408 revalidateChild = true; 409 } 410 411 dim = _getMinimumSize(); 412 old = getPreferredSize(); 413 boolean revalidateMe = false; 414 if( (old.height != dim.height) || (old.width != dim.width) ){ 415 setMinimumSize(dim); 416 setPreferredSize(dim); 417 revalidateMe = true; 418 } 419 if(revalidateChild) 420 childrenPanel.revalidate(); 421 if(revalidateMe) 422 revalidate(); 423 424 } 425 426 public Dimension getPreferredSize() { 428 return getMinimumSize(); 429 } 430 431 public Dimension _getMinimumSize() { 432 int width = 0; 433 int height = 0; 434 for(Component child: this.getComponents()){ 435 if(!child.isVisible()) 436 break; 437 Dimension dim = child.getPreferredSize(); 438 height += dim.getHeight(); 439 int thisW = dim.width ; 440 width = width < thisW ? thisW : width; 441 } 442 return new Dimension (width, height); 443 } 444 445 446 447 public ABEBaseDropPanel isAlreadyAdded(AXIComponent axiComp){ 448 for(Component comp: getChildrenList()){ 449 if(comp instanceof ABEBaseDropPanel){ 451 if(((ABEBaseDropPanel)comp).getAXIComponent() == axiComp ) 452 return (ABEBaseDropPanel)comp; 453 } 454 } 455 return null; 456 } 457 458 public boolean tweenerDragAccept(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) { 459 return true; 460 } 461 462 public void tweenerDrop(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) { 463 } 464 465 public void tweenerDragExited(TweenerPanel tweener) { 466 } 467 468 public void tweenerDragEntered(TweenerPanel tweener, DnDHelper.PaletteItem paletteItem) { 469 } 470 471 472 protected void addTweenerListener(final TweenerPanel tweener) { 473 tweener.addTweenerListener(new TweenerListener(){ 474 public boolean dragAccept(DnDHelper.PaletteItem paletteItem) { 475 return ContainerPanel.this.tweenerDragAccept(tweener, paletteItem); 476 } 477 478 public void drop(DnDHelper.PaletteItem paletteItem) { 479 ContainerPanel.this.tweenerDrop(tweener, paletteItem); 480 } 481 482 public void dragExited() { 483 ContainerPanel.this.tweenerDragExited(tweener); 484 } 485 486 public void dragEntered(DnDHelper.PaletteItem paletteItem) { 487 ContainerPanel.this.tweenerDragEntered(tweener, paletteItem); 488 } 489 }); 490 } 491 492 493 public int getInterComponentVerticalSpacing(){ 494 return INTER_PANEL_VERTICAL_SPACE; 495 } 496 497 498 public void paintComponent(Graphics g){ 499 Graphics2D g2d = (Graphics2D ) g; 500 super.paintComponent(g2d); 501 } 502 503 public AXIComponent getAXIParent() { 504 return axiParent; 505 } 506 507 public void setAXIParent(AXIComponent axiParent) { 508 this.axiParent = axiParent; 509 } 510 511 512 public int getPanelIndendation() { 513 return panelIndendation; 514 } 515 516 public void setPanelIndendation(int panelIndendation) { 517 this.panelIndendation = panelIndendation; 518 } 519 520 521 522 public LinkedList <Component > getChildrenList() { 523 return childrenList; 524 } 525 526 public void setChildrenList(LinkedList <Component > childrenList) { 527 this.childrenList = childrenList; 528 } 529 530 public void visit(AnyAttribute attribute) { 531 } 532 533 public void visit(Element element) { 534 } 535 536 public void visit(ContentModel element) { 537 } 538 539 public void visit(Datatype datatype) { 540 } 541 542 public void visit(AnyElement element) { 543 } 544 545 public void visit(AXIDocument root) { 546 } 547 548 public void visit(Attribute attribute) { 549 } 550 551 public void visit(Compositor compositor) { 552 } 553 554 public AXIComponent getAXIComponent() { 555 return getAXIParent(); 556 } 557 558 public ABEBaseDropPanel getChildUIComponentFor(AXIComponent axiComponent){ 559 this.addAllChildren(); 560 this.setVisible(true); 561 for(Component comp: getChildrenList()){ 562 if(comp instanceof ABEBaseDropPanel){ 563 ABEBaseDropPanel uiComp = ((ABEBaseDropPanel)comp).getUIComponentFor(axiComponent); 564 if( uiComp != null) 565 return uiComp; 566 } 567 } 568 return null; 569 } 570 571 public ABEBaseDropPanel getParentContainerPanel(){ 572 return (ABEBaseDropPanel)this.parentPanel; 573 } 574 } 575 | Popular Tags |