1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.beans.*; 23 import java.awt.*; 24 import java.awt.event.*; 25 import javax.swing.*; 26 import java.util.*; 27 28 import org.openide.nodes.*; 29 import org.openide.explorer.propertysheet.PropertySheet; 30 import org.openide.util.*; 31 import org.netbeans.api.javahelp.Help; 32 33 import org.netbeans.modules.form.*; 34 import org.netbeans.modules.form.layoutsupport.LayoutSupportManager; 35 36 40 final public class GridBagCustomizer extends JPanel implements Customizer 41 { 42 43 45 48 PropertyChangeSupport propertySupport = new PropertyChangeSupport(this); 49 50 static final int TOP = 1; 51 static final int BOTTOM = 2; 52 static final int LEFT = 4; 53 static final int RIGHT = 8; 54 55 static final int HORIZONTAL = LEFT + RIGHT; 56 static final int VERTICAL = TOP + BOTTOM; 57 58 static final int PLUS = 1; 59 static final int MINUS = -1; 60 61 private static final ImageIcon REMAINDER_ICON = new ImageIcon( 62 GridBagCustomizer.class.getResource("/org/netbeans/modules/form/layoutsupport/resources/remainder.gif")); 64 private GridBagLayoutSupport layoutSupport; 66 67 private FormModel formModel; 68 private FormModelListener formListener; 69 private RADVisualContainer radContainer; 70 private RADVisualComponent[] radComponents; 71 private GBComponentProxy[] gbcProxies; 72 73 private JSplitPane splitPane; 75 private JPanel designPanel; 76 77 private GridBagControlCenter controlCenter; 78 private GBContainerProxy containerProxy; 79 private PropertySheet propertySheet; 80 private javax.swing.JScrollPane designScrollPane; 81 private JLayeredPane designLayeredPane; 82 83 private GBComponentProxy.DragLabel dragLabel = null; 84 85 89 private boolean firstPaint = false; 90 91 static final long serialVersionUID =-632768048562391785L; 92 93 public GridBagCustomizer() { 94 initComponents(); 95 } 96 97 private void initialize() { 98 99 101 radContainer = ((LayoutSupportManager) 102 layoutSupport.getLayoutSupportHack()) 103 .getMetaContainer(); formModel = radContainer.getFormModel(); 105 radComponents = radContainer.getSubComponents(); 106 107 gbcProxies = new GBComponentProxy[radComponents.length]; 108 for (int i = 0; i < radComponents.length; i++) { 109 gbcProxies[i] = new GBComponentProxy(radComponents[i], containerProxy); 110 } 111 112 containerProxy.removeAll(); 113 114 FormDesigner designer = FormEditor.getFormDesigner(formModel); 115 if (!designer.isInDesignedTree(radContainer)) { 116 designer.setTopDesignComponent(radContainer, true); 117 invokeLater(2, new Runnable () { 119 public void run() { 120 containerProxy.addAllProxies(); 121 } 122 }); 123 } 124 else containerProxy.addAllProxies(); 125 126 formListener = new FormListener(); 127 formModel.addFormModelListener(formListener); 128 } 129 130 void customizerClosed() { 131 formModel.removeFormModelListener(formListener); 132 } 133 134 135 136 private void initComponents() { 137 138 setBorder(new javax.swing.border.EmptyBorder (4, 0, 8, 0)); 139 setLayout(new BorderLayout()); 141 propertySheet = new PropertySheet(); 142 try { 143 propertySheet.setSortingMode(PropertySheet.UNSORTED); 144 } 145 catch (java.beans.PropertyVetoException e) { 146 } 148 propertySheet.setPreferredSize(new Dimension(300, 380)); 149 150 JPanel panel = new JPanel(); 151 panel.setLayout(new BorderLayout()); 152 panel.add(propertySheet, BorderLayout.CENTER); 153 controlCenter = new GridBagControlCenter(this); 154 panel.add(controlCenter, BorderLayout.SOUTH); 155 156 designScrollPane = new javax.swing.JScrollPane (); 157 designPanel = new JPanel() { 158 public Dimension getPreferredSize() { 159 return containerProxy.getPreferredSize(); 161 } 162 }; 163 164 designPanel.setLayout(new GridBagLayout()); 165 designPanel.setBackground((java.awt.Color ) javax.swing.UIManager.getDefaults().get("desktop")); GridBagConstraints con = new GridBagConstraints(); 167 con.anchor = GridBagConstraints.CENTER; 168 con.fill = GridBagConstraints.NONE; 169 containerProxy = new GBContainerProxy(); 170 171 designPanel.add(containerProxy, con); 172 173 designLayeredPane = new JLayeredPane() { 174 public Dimension getPreferredSize() { 175 176 Dimension dpd = designPanel.getPreferredSize(); 177 Dimension spd = designScrollPane.getViewport().getExtentSize(); 178 179 int width = Math.max(dpd.width + 40, spd.width); 180 int height = Math.max(dpd.height + 40 , spd.height); 181 182 188 190 return new Dimension(Math.max(500, width), Math.max(300, height)); 191 } 192 }; 193 194 196 designLayeredPane.add(designPanel, JLayeredPane.DEFAULT_LAYER); 198 designPanel.setBounds(20, 20, designPanel.getPreferredSize().width, designPanel.getPreferredSize().height); 199 designLayeredPane.setOpaque(true); 200 designLayeredPane.setBackground((java.awt.Color ) javax.swing.UIManager.getDefaults().get("desktop")); 202 designScrollPane.setViewportView(designLayeredPane); 203 204 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 205 splitPane.setLeftComponent(panel); 206 splitPane.setRightComponent(designScrollPane); 207 splitPane.setUI(new javax.swing.plaf.basic.BasicSplitPaneUI ()); 208 splitPane.setBorder(BorderFactory.createEmptyBorder()); 209 splitPane.setContinuousLayout(true); 210 211 add(splitPane, BorderLayout.CENTER); 212 213 getAccessibleContext().setAccessibleDescription(GridBagLayoutSupport.getBundleHack().getString("ACSD_GridBagCustomizer")); 214 HelpCtx.setHelpIDString(this, "gui.layouts.gbcustomizer"); } 216 217 void setAnchor(int anchor) { 218 java.util.List selected = containerProxy.getSelectedProxies(); 219 Iterator it = selected.iterator(); 220 while (it.hasNext()) 221 setProperty((GBComponentProxy)it.next(), "anchor", new Integer (anchor)); } 224 225 void setFill(int fill) { 226 java.util.List selected = containerProxy.getSelectedProxies(); 227 Iterator it = selected.iterator(); 228 while (it.hasNext()) 229 setProperty((GBComponentProxy)it.next(), "fill", new Integer (fill)); }; 232 233 234 235 void modifyIPad(int action, int what) { 236 java.util.List selected = containerProxy.getSelectedProxies(); 237 Iterator it = selected.iterator(); 238 while (it.hasNext()) { 239 GBComponentProxy p =(GBComponentProxy)it.next(); 240 int value = what == HORIZONTAL ? p.getRealConstraints().ipadx : p.getRealConstraints().ipady; 241 value += action; 242 if (value < 0) 243 continue; 244 setProperty(p, 245 what == HORIZONTAL ? "ipadx" : "ipady" , new Integer (value)); 248 } 249 } 250 251 void modifyInsets(int action, int what) { 252 java.util.List selected = containerProxy.getSelectedProxies(); 253 Iterator it = selected.iterator(); 254 while (it.hasNext()) { 255 GBComponentProxy p =(GBComponentProxy)it.next(); 256 Insets old_insets = p.getRealConstraints().insets; 257 Insets new_insets =(Insets)old_insets.clone(); 258 259 if ((what & TOP) != 0) { 260 new_insets.top += action; 261 if (new_insets.top < 0) 262 new_insets.top = 0; 263 } 264 if ((what & BOTTOM) != 0) { 265 new_insets.bottom += action; 266 if (new_insets.bottom < 0) 267 new_insets.bottom = 0; 268 } 269 if ((what & LEFT) != 0) { 270 new_insets.left += action; 271 if (new_insets.left < 0) 272 new_insets.left = 0; 273 } 274 if ((what & RIGHT) != 0) { 275 new_insets.right += action; 276 if (new_insets.right < 0) 277 new_insets.right = 0; 278 } 279 280 setProperty(p, "insets", new_insets); } 283 } 284 285 286 void modifyGridSize(int action, int what) { 287 java.util.List selected = containerProxy.getSelectedProxies(); 288 Iterator it = selected.iterator(); 289 290 while (it.hasNext()) { 291 GBComponentProxy p =(GBComponentProxy)it.next(); 292 int value = what == HORIZONTAL ? p.getRealConstraints().gridwidth : p.getRealConstraints().gridheight; 293 294 if (action == 0) 295 value = value == 0 ? 1 : 0; 296 else { 297 value += action; 298 if (value < 1) { 299 value = 1; 300 } 301 } 302 303 setProperty(p, 304 what == HORIZONTAL ? "gridwidth" : "gridheight" , new Integer (value)); 307 } 308 } 309 310 private void setProperty(GBComponentProxy p, String name, Object value) { 311 Node.Property prop = p.getComponent().getPropertyByName( 312 "GridBagLayoutConstraints "+name); if (prop != null) { 314 try { 315 prop.setValue(value); 316 } 317 catch (Exception ex) { ex.printStackTrace(); 319 } 320 } 321 } 322 323 326 331 public void setObject(Object bean) { 332 layoutSupport = (GridBagLayoutSupport) bean; 333 initialize(); 335 firstPaint = true; 336 } 337 338 347 351 356 364 365 368 class FormListener implements FormModelListener { 369 public void formChanged(FormModelEvent[] events) { 370 if (events != null && GridBagCustomizer.this.isShowing()) { 371 boolean modifying = false; 372 for (int i=0; i < events.length; i++) 373 if (events[i].isModifying()) { 374 modifying = true; 375 break; 376 } 377 if (!modifying) 378 return; 379 380 invokeLater(3, new Runnable () { 386 public void run() { 387 containerProxy.updateAllProxies(); 388 } 389 }); 390 } 391 } 395 } 396 397 void waitForDesignerUpdate(int count) { 398 try { 399 while (count > 0) { 400 SwingUtilities.invokeAndWait(new Runnable () { 401 public void run() { 402 } 403 }); 404 count--; 405 } 406 } 407 catch (Exception e) { 408 e.printStackTrace(); 409 } 410 } 411 412 void invokeLater(final int count, final Runnable task) { 413 if (count == 0) 414 task.run(); 415 else 416 SwingUtilities.invokeLater(new Runnable () { 417 public void run() { 418 invokeLater(count-1, task); 419 } 420 }); 421 } 422 423 432 436 437 void innerLayoutChanged() { 438 439 452 454 455 containerProxy.widenEmpty(); 456 designPanel.setBounds(20, 20, designPanel.getPreferredSize().width, designPanel.getPreferredSize().height); 457 458 containerProxy.revalidate(); 461 462 466 designLayeredPane.revalidate(); 467 469 477 478 } 479 480 483 484 485 class GBComponentProxy extends JPanel { 486 493 494 private GBContainerProxy parentProxy; 495 private RADVisualComponent component; 496 private ComponentProxyNode node; 497 private javax.swing.border.CompoundBorder compoundBorder; 498 private javax.swing.border.MatteBorder insetsBorder; 499 private javax.swing.border.MatteBorder remainderBorder; 500 private JLabel componentLabel; 502 private GridBagLayout layout; 503 private Color INSETS_COLOR = new Color(255, 255, 204); 505 private Color CELL_COLOR = new Color(153, 153, 255); 506 507 private boolean isSelected = false; 508 509 static final long serialVersionUID =-6552012922564179923L; 510 511 GBComponentProxy(final RADVisualComponent component, GBContainerProxy parentProxy) { 512 513 this.component = component; 514 this.parentProxy = parentProxy; 515 516 518 componentLabel = new javax.swing.JLabel () 519 { 520 public void paint(Graphics g) { 521 int borderSize = 5; 522 Color borderColor = Color.blue; 523 524 super.paint(g); 525 526 if (GBComponentProxy.this.isSelected()) { 527 528 Dimension size = getSize(); 529 g.setColor(borderColor); 530 g.fillRect(0, 0, borderSize, borderSize); g.fillRect(size.width-borderSize, 0, borderSize, borderSize); g.fillRect(0, size.height-borderSize, borderSize, borderSize); g.fillRect(size.width-borderSize, size.height-borderSize, borderSize, borderSize); 535 536 544 } 545 546 } 547 548 public Dimension getPreferredSize() { 549 FormDesigner designer = FormEditor.getFormDesigner(formModel); 550 Component comp = (Component)designer.getComponent(component); 551 Dimension size; 552 if (comp == null) { 553 comp = (Component) component.getBeanInstance(); 554 size = comp.getPreferredSize(); 555 } 556 else { 557 if (designer.isOpened()) { 558 size = comp.getSize(); 559 if (size.width > 4096) size.width = comp.getPreferredSize().width; 561 } 562 else size = comp.getPreferredSize(); 563 } 564 565 size = new Dimension(size); 568 569 if (comp instanceof JComponent && !(comp instanceof JPanel)) { 570 Insets thisIns = getInsets(); 571 if (comp instanceof JComponent) { 572 javax.swing.border.Border b = ((JComponent)comp).getBorder(); 573 if (b != null) { 574 Insets ins = b.getBorderInsets(comp); 575 thisIns.top -= ins.top; 576 thisIns.left -= ins.left; 577 thisIns.bottom -= ins.bottom; 578 thisIns.right -= ins.right; 579 } 580 } 581 if (thisIns.top > 0) size.height += thisIns.top; 582 if (thisIns.bottom > 0) size.height += thisIns.bottom; 583 if (thisIns.left > 0) size.width += thisIns.left; 584 if (thisIns.right > 0) size.width += thisIns.right; 585 } 586 587 if (size.width < 6) size.width = 6; 588 if (size.height < 6) size.height = 6; 589 590 return size; 591 } 592 593 public Dimension getMinimumSize() { 594 Component comp = (Component) 595 FormEditor.getFormDesigner(formModel).getComponent(component); 596 if (comp == null) 597 comp = (Component) component.getBeanInstance(); 598 return comp.getMinimumSize(); 599 } 601 602 public Dimension getMaximumSize() { 603 Component comp = (Component) 604 FormEditor.getFormDesigner(formModel).getComponent(component); 605 if (comp == null) 606 comp = (Component) component.getBeanInstance(); 607 return comp.getMaximumSize(); 608 } 610 }; 611 componentLabel.setOpaque(true); 612 componentLabel.setBorder(new javax.swing.border.EtchedBorder ()); 613 componentLabel.setText(component.getName()); 614 componentLabel.setHorizontalAlignment(0); 615 componentLabel.addMouseListener(new java.awt.event.MouseAdapter () { 616 public void mouseClicked(java.awt.event.MouseEvent evt) { 617 GBComponentProxy.this.mouseClicked(evt); 618 } 619 620 public void mousePressed(java.awt.event.MouseEvent evt) { 621 GBComponentProxy.this.mousePressed(evt); 622 } 623 624 public void mouseReleased(java.awt.event.MouseEvent evt) { 625 GBComponentProxy.this.mouseReleased(evt); 626 } 627 628 }); 629 630 componentLabel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter () { 631 public void mouseDragged(java.awt.event.MouseEvent evt) { 632 GBComponentProxy.this.mouseDragged(evt); 633 } 634 } 635 ); 636 637 638 647 648 649 setLayout(layout = new GridBagLayout()); 650 setBackground(CELL_COLOR); 651 setOpaque(true); 652 updateByComponent(); 653 add(componentLabel, getInnerComponentConstraints()); 654 node = new ComponentProxyNode(component.getNodeReference()); 655 } 656 657 RADVisualComponent getComponent() { 658 return component; 659 } 660 661 665 GridBagConstraints getRealConstraints() { 666 return (GridBagConstraints) 667 component.getParentContainer().getLayoutSupport() 668 .getConstraints(component).getConstraintsObject(); 669 } 671 672 ComponentProxyNode getNode() { 673 return node; 674 } 675 676 void updateByComponent() { 677 layout.setConstraints(componentLabel, getInnerComponentConstraints()); 678 } 679 680 681 708 709 GridBagConstraints getInnerComponentConstraints() { 710 711 GridBagConstraints con = new GridBagConstraints(); 712 con.anchor = getRealConstraints().anchor; 713 con.fill = getRealConstraints().fill; 714 con.gridheight = 1; 715 con.gridwidth = 1; 716 con.gridx = 0; 717 con.gridy = 0; 718 con.ipadx = getRealConstraints().ipadx; 720 con.ipady = getRealConstraints().ipady; 721 con.weightx = 1.0; 722 con.weighty = 1.0; 723 724 return con; 725 } 726 727 728 GridBagConstraints getProxyConstraints() { 729 GridBagConstraints con = new GridBagConstraints(); 730 731 con.anchor = GridBagConstraints.CENTER; 732 con.fill = GridBagConstraints.BOTH; 733 con.gridheight = getRealConstraints().gridheight; 734 con.gridwidth = getRealConstraints().gridwidth; 735 con.gridx = getRealConstraints().gridx; 736 con.gridy = getRealConstraints().gridy; 737 con.insets = new Insets(3, 3, 3, 3); 738 con.ipadx = 0; 739 con.ipady = 0; 740 con.weightx = 1.0; 743 con.weighty = 1.0; 744 Insets in = getRealConstraints().insets; 745 746 insetsBorder = new javax.swing.border.MatteBorder (in.top, in.left, in.bottom, in.right, INSETS_COLOR); 747 remainderBorder = new javax.swing.border.MatteBorder (0, 0, con.gridheight == 0 ? 4 : 0, con.gridwidth == 0 ? 4 : 0, REMAINDER_ICON); 748 compoundBorder = new javax.swing.border.CompoundBorder (remainderBorder, insetsBorder); 749 setBorder(compoundBorder); 750 751 return con; 752 } 753 754 boolean isSelected() { 755 return isSelected; 756 } 757 758 void setSelected(boolean isSelected) { 759 if (this.isSelected == isSelected) 760 return; 761 762 this.isSelected = isSelected; 763 componentLabel.repaint(); 765 771 } 772 773 void mouseClicked(java.awt.event.MouseEvent evt) { 774 780 } 781 782 void mousePressed(java.awt.event.MouseEvent evt) { 783 if (evt.isControlDown()) 784 parentProxy.shiftSelect(this); 785 else 786 parentProxy.select(this); 787 } 788 789 void mouseReleased(java.awt.event.MouseEvent evt) { 790 if (dragLabel != null) { 791 792 if (!dragLabel.getLastIndex().equals(dragLabel.getOriginalIndex())) { 793 setProperty(this, "gridx", new Integer (dragLabel.getLastIndex().x)); setProperty(this, "gridy", new Integer (dragLabel.getLastIndex().y)); } 798 799 designLayeredPane.remove(dragLabel); 800 dragLabel = null; 801 componentLabel.setCursor(Cursor.getDefaultCursor()); 802 designLayeredPane.repaint(); 803 } 804 } 805 806 void mouseDragged(java.awt.event.MouseEvent evt) { 807 808 810 811 if (dragLabel == null) { 812 if (isSelected()) { 813 814 816 817 dragLabel = new DragLabel(); 818 dragLabel.setHotSpot(evt.getPoint()); 819 dragLabel.resolveOrigin(); 820 dragLabel.setLastIndex(dragLabel.getIndex(evt.getPoint())); 821 dragLabel.setOriginalIndex(dragLabel.getIndex(evt.getPoint())); 822 823 designLayeredPane.setLayer(dragLabel, JLayeredPane.DRAG_LAYER.intValue()); 824 designLayeredPane.add(dragLabel, BorderLayout.CENTER); 825 dragLabel.setBounds(evt.getPoint().x, evt.getPoint().y); 826 componentLabel.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 827 } 828 else { 829 831 } 832 } 833 else 834 { 835 836 Point newLoc; 837 if (!dragLabel.getLastIndex().equals(newLoc = dragLabel.getIndex(evt.getPoint()))) { 838 839 Point converted = SwingUtilities.convertPoint(dragLabel, evt.getPoint(), designLayeredPane); 840 841 843 844 GridBagConstraints con = getProxyConstraints(); 845 846 con.gridx = newLoc.x; 847 con.gridy = newLoc.y; 848 849 parentProxy.layout.setConstraints(this, con); 850 851 855 857 858 860 862 863 867 870 872 874 875 876 dragLabel.resolveOrigin(); 877 878 SwingUtilities.convertPoint(designLayeredPane, converted, dragLabel); 879 Rectangle r = dragLabel.getBounds(); 881 Point loc = r.getLocation(); 882 loc.x += r.width + 20; 883 loc.y += r.height + 20; 884 886 innerLayoutChanged(); 887 dragLabel.repaint(); 888 889 891 892 dragLabel.setLastIndex(newLoc); 893 894 } 895 else 896 898 dragLabel.setBounds(evt.getPoint().x, evt.getPoint().y); 899 } 900 } 901 902 903 class DragLabel extends JLabel { 904 905 906 Point origin; 907 Point hotSpot; 908 Point lastIndex; 909 Point originalIndex; 910 911 private Dimension preferredSize; 912 913 static final long serialVersionUID =992490305277357953L; 914 DragLabel() { 915 setOpaque(false); 916 setEnabled(false); 917 setBorder(new javax.swing.border.EtchedBorder ()); 918 setText(component.getName()); 919 setHorizontalAlignment(0); 920 921 preferredSize =(Dimension)componentLabel.getPreferredSize().clone(); 922 } 923 924 public Dimension getPreferredSize() { 925 preferredSize.height = componentLabel.getPreferredSize().height + getRealConstraints().ipady; 926 preferredSize.width = componentLabel.getPreferredSize().width + getRealConstraints().ipadx; 927 928 return preferredSize; 929 } 930 931 public Dimension getMinimumSize() { 932 return componentLabel.getMinimumSize(); 933 } 934 935 public Dimension getMaximumSize() { 936 return componentLabel.getMaximumSize(); 937 } 938 939 void setHotSpot(Point hotSpot) { 940 this.hotSpot = hotSpot; 941 } 942 943 void resolveOrigin() { 944 origin = SwingUtilities.convertPoint(componentLabel, 0, 0, designLayeredPane); 945 } 946 947 Point getLastIndex() { 948 return lastIndex; 949 } 950 951 void setLastIndex(Point lastIndex) { 952 this.lastIndex = lastIndex; 953 } 954 955 Point getOriginalIndex() { 956 return originalIndex; 957 } 958 959 void setOriginalIndex(Point originalIndex) { 960 this.originalIndex = originalIndex; 961 } 962 963 Point getIndex(Point p) { 964 return parentProxy.getLayoutLocation(SwingUtilities.convertPoint(componentLabel, p, parentProxy)); 965 } 966 967 public void setBounds(int x, int y) { 968 resolveOrigin(); 969 super.setBounds(origin.x + x - hotSpot.x , origin.y + y - hotSpot.y, getPreferredSize().width, getPreferredSize().height); 970 } 971 972 } 973 974 } 975 976 977 static class ComponentProxyNode extends FilterNode { 978 979 ComponentProxyNode(Node original) { 980 super(original); 981 } 982 983 public Node.PropertySet[] getPropertySets() { 984 Node.PropertySet[] sets = super.getPropertySets(); 985 for (int i=0; i < sets.length; i++) 986 if ("layout".equals(sets[i].getName())) return new Node.PropertySet[] { sets[i] }; 988 return new Node.PropertySet[0]; } 990 } 991 992 993 994 class GBContainerProxy extends JPanel { 995 996 private GridBagLayout layout; 997 private boolean[][] empties; 998 999 1000 static final long serialVersionUID =5113122235848232590L; 1001 1002 GBContainerProxy() { 1003 setLayout(layout = new GridBagLayout()); 1004 setBorder(new javax.swing.border.BevelBorder (javax.swing.border.BevelBorder.RAISED)); 1005 setOpaque(true); 1006 } 1007 1008 void addAllProxies() { 1009 for (int i = 0; i < gbcProxies.length; i++) { 1010 add(gbcProxies[i], gbcProxies[i].getProxyConstraints()); 1011 } 1012 invalidate(); 1013 validate(); 1014 innerLayoutChanged(); 1015 widenEmpty(); 1016 1017 if (gbcProxies.length > 0) 1018 select(gbcProxies[0]); 1019 } 1020 1021 void widenEmpty() { 1022 1023 layout.rowHeights = layout.columnWidths = null; 1024 1025 layout.layoutContainer(this); 1026 validate(); 1027 1028 int[][] dims = layout.getLayoutDimensions(); 1029 1030 empties = new boolean[2][]; 1031 1032 int[] widths = new int[ dims[0].length ]; 1033 empties[0] = new boolean[ dims[0].length ]; 1034 1035 for (int i = 0; i < widths.length; i++) { 1036 widths[i] = 25; 1038 empties[0][i] = dims[0][i] == 0 ? true : false; 1039 } 1040 layout.columnWidths = widths; 1041 1042 1043 int[] heights = new int[ dims[1].length ]; 1044 empties[1] = new boolean[ dims[1].length ]; 1045 for (int i = 0; i < heights.length; i++) { 1046 heights[i] = 25; 1048 empties[1][i] = dims[1][i] == 0 ? true : false; 1049 } 1050 layout.rowHeights = heights; 1051 1052 } 1054 1055 public void paint(Graphics g) { 1056 1057 if (firstPaint) { 1058 innerLayoutChanged(); 1059 firstPaint = false; 1060 } 1061 1062 super.paint(g); 1063 1064 if (gbcProxies.length > 0) { 1065 1066 Color emptyColor = new Color(255, 173, 173); 1067 1068 Point origin = layout.getLayoutOrigin(); 1069 1070 int[][] dims = layout.getLayoutDimensions(); 1071 1072 int width = 0; 1073 for (int i = 0; i < dims[0].length; i ++) { 1074 width += dims[0][i]; 1075 } 1077 1078 int height = 0; 1079 for (int i = 0; i < dims[1].length; i ++) { 1080 height += dims[1][i]; 1081 } 1083 1084 int yCoord = origin.y; 1086 g.setColor(emptyColor); 1087 for (int i = 0; i < dims[1].length; i ++) { 1088 if (empties[1][i]) { 1089 g.setColor(emptyColor); 1090 g.fillRect(origin.x, yCoord, width, dims[1][i]); 1091 } 1092 yCoord += dims[1][i]; 1093 } 1094 1095 int xCoord = origin.x; 1097 for (int i = 0; i < dims[0].length; i ++) { 1098 if (empties[0][i]) { 1099 g.setColor(emptyColor); 1100 g.fillRect(xCoord, origin.y, dims[0][i], height); 1101 } 1102 xCoord += dims[0][i]; 1103 } 1104 1105 1106 g.setColor(Color.black); 1108 yCoord = dims[1][0] + origin.y - 1; 1109 for (int i = 1; i < dims[1].length; i ++) { 1110 g.drawLine(origin.x, yCoord, origin.x + width - 1, yCoord); 1111 g.drawLine(origin.x, yCoord + 1, origin.x + width - 1, yCoord + 1); 1112 yCoord += dims[1][i]; 1113 } 1114 1115 xCoord = dims[0][0] + origin.x - 1; 1117 for (int i = 1; i < dims[0].length; i ++) { 1118 g.drawLine(xCoord, origin.y, xCoord, origin.y + height - 1); 1119 g.drawLine(xCoord + 1, origin.y, xCoord + 1, origin.y + height - 1); 1120 xCoord += dims[0][i]; 1121 } 1122 } 1123 1124 paintChildren(g); 1125 } 1126 1127 Point getLayoutLocation(Point p) { 1128 return layout.location(p.x, p.y); 1129 } 1130 1131 1132 void updateAllProxies() { 1133 1134 for (int i = 0; i < gbcProxies.length; i++) { 1135 updateProxy(gbcProxies[i]); 1136 } 1137 1138 1140 innerLayoutChanged(); 1141 controlCenter.newSelection(getSelectedProxies()); 1142 1143 } 1148 1149 1150 1151 void updateProxy(GBComponentProxy p) { 1152 p.updateByComponent(); 1153 1154 p.getProxyConstraints(); 1155 layout.setConstraints(p, p.getProxyConstraints()); 1156 1157 p.invalidate(); 1158 p.validate(); 1160 1161 } 1162 1163 void invalidateAllProxies() { 1164 for (int i = 0; i < gbcProxies.length; i++) { 1165 gbcProxies[i].invalidate(); 1166 } 1167 doLayout(); 1168 } 1169 1170 1171 java.util.List getSelectedProxies() { 1172 java.util.List selected = new ArrayList(gbcProxies.length); 1173 1174 for (int i = 0; i < gbcProxies.length; i++) { 1175 if (gbcProxies[i].isSelected) 1176 selected.add(gbcProxies[i]); 1177 } 1178 1179 return selected; 1180 } 1181 1182 Node[] getSelectedNodes() { 1183 java.util.List selected = getSelectedProxies(); 1184 1185 Node[] result = new Node[ selected.size() ]; 1186 1187 Iterator it = selected.iterator(); 1188 1189 for (int i = 0; it.hasNext(); i++) { 1190 result[i] =((GBComponentProxy)it.next()).getNode(); 1191 } 1192 1193 return result; 1194 } 1195 1196 1197 void select(GBComponentProxy p) { 1198 select(p, false); 1199 } 1200 1201 void shiftSelect(GBComponentProxy p) { 1202 select(p, true); 1203 } 1204 1205 void select(GBComponentProxy p, boolean shift) { 1206 1207 java.util.List selected = getSelectedProxies(); 1208 1209 if (p.isSelected()) { 1210 if (selected.size() == 1) { 1211 return; 1212 } 1213 else if (shift) { 1214 p.setSelected(false); 1215 } 1216 else { 1217 Iterator it = selected.iterator(); 1218 while (it.hasNext()) { 1219 ((GBComponentProxy)it.next()).setSelected(false); 1220 } 1221 p.setSelected(true); 1222 } 1223 1224 } 1225 else { 1226 if (!shift) { 1227 Iterator it = selected.iterator(); 1228 while (it.hasNext()) 1229 ((GBComponentProxy)it.next()).setSelected(false); 1230 } 1231 p.setSelected(true); 1232 } 1233 1234 propertySheet.setNodes(getSelectedNodes()); 1235 controlCenter.newSelection(getSelectedProxies()); 1236 } 1237 1238 } 1239 1240 1241 public static class Window extends JDialog implements Customizer, ActionListener { 1242 final private GridBagCustomizer customizerPanel; 1243 private boolean packCalled; 1244 public Window() { 1245 super(org.openide.windows.WindowManager.getDefault().getMainWindow()); 1246 java.util.ResourceBundle bundle = GridBagLayoutSupport.getBundleHack(); 1247 1248 setTitle(bundle.getString("CTL_CustomizerTitle")); 1250 getContentPane().setLayout(new BorderLayout(0, 0)); 1251 customizerPanel = new GridBagCustomizer(); 1252 getContentPane().add(customizerPanel, BorderLayout.CENTER); 1253 1254 JPanel buttonPanel = new JPanel(); 1255 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 6, 6)); 1256 1257 JButton closeButton = new JButton(); 1258 closeButton.setText(bundle.getString("CTL_CloseButton")); closeButton.setActionCommand("close"); closeButton.addActionListener(this); 1261 buttonPanel.add(closeButton); 1262 1263 JButton helpButton = new JButton(); 1264 helpButton.setText(bundle.getString("CTL_HelpButton")); helpButton.setActionCommand("help"); helpButton.addActionListener(this); 1267 buttonPanel.add(helpButton); 1268 1269 getContentPane().add(buttonPanel, BorderLayout.SOUTH); 1270 1271 addWindowListener(new WindowAdapter() { 1272 public void windowClosed(WindowEvent e) { 1273 customizerPanel.customizerClosed(); 1274 } 1275 }); 1276 1277 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 1278 } 1279 1280 public void pack() { 1281 if (packCalled) 1282 return; 1283 packCalled = true; 1284 1285 super.pack(); 1286 Dimension size = getSize(); 1287 Rectangle screenBounds = Utilities.getUsableScreenBounds(); 1288 if (size.width > screenBounds.width - 80) 1289 size.width = screenBounds.width * 4 / 5; 1290 if (size.height > screenBounds.height - 80) 1291 size.height = screenBounds.height * 4 / 5; 1292 setBounds(Utilities.findCenterBounds(size)); 1293 } 1294 1295 public void setObject(Object bean) { 1296 customizerPanel.setObject(bean); 1297 } 1298 1299 public void actionPerformed(ActionEvent ev) { 1300 if (ev.getActionCommand().equals("close")) dispose(); 1302 else if (ev.getActionCommand().equals("help")) { Help help = (Help) Lookup.getDefault().lookup(Help.class); 1304 if (help != null) 1305 help.showHelp(HelpCtx.findHelp(customizerPanel)); 1306 } 1307 } 1308 } 1309 1310 1311} 1312 | Popular Tags |