1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import javax.swing.*; 24 25 import org.netbeans.modules.form.layoutsupport.*; 26 27 32 33 public class JToolBarSupport extends AbstractLayoutSupport { 34 35 38 public Class getSupportedClass() { 39 return JToolBar.class; 40 } 41 42 public void addComponentsToContainer(Container container, 43 Container containerDelegate, 44 Component[] components, 45 int index) { 46 LayoutManager lm = containerDelegate.getLayout(); 48 if (lm instanceof LayoutManager2) { 51 ((LayoutManager2)lm).invalidateLayout(containerDelegate); 52 } 53 super.addComponentsToContainer(container, containerDelegate, components, index); 54 } 55 56 71 public int getNewIndex(Container container, 72 Container containerDelegate, 73 Component component, 74 int index, 75 Point posInCont, 76 Point posInComp) 77 { 78 if (!(container instanceof JToolBar)) 79 return -1; 80 81 int orientation = ((JToolBar)container).getOrientation(); 82 83 assistantParams = 0; 84 Component[] components = container.getComponents(); 85 for (int i = 0; i < components.length; i++) { 86 if (component == components[i]) { 87 assistantParams--; 88 continue; 89 } 90 Rectangle b = components[i].getBounds(); 91 if (orientation == SwingConstants.HORIZONTAL) { 92 if (posInCont.x < b.x + b.width / 2) { 93 assistantParams += i; 94 return i; 95 } 96 } 97 else { 98 if (posInCont.y < b.y + b.height / 2) { 99 assistantParams += i; 100 return i; 101 } 102 } 103 } 104 105 assistantParams += components.length; 106 return components.length; 107 } 108 109 private int assistantParams; 110 public String getAssistantContext() { 111 return "toolbarLayout"; } 113 114 public Object [] getAssistantParams() { 115 return new Object [] {Integer.valueOf(assistantParams+1)}; 116 } 117 118 133 public boolean paintDragFeedback(Container container, 134 Container containerDelegate, 135 Component component, 136 LayoutConstraints newConstraints, 137 int newIndex, 138 Graphics g) 139 { 140 if (!(container instanceof JToolBar)) 141 return false; 142 143 int orientation = ((JToolBar)container).getOrientation(); 144 Component[] components = container.getComponents(); 145 Rectangle rect; 146 147 if ((newIndex >= 0) && (newIndex < components.length) && (component == components[newIndex])) { 148 newIndex++; 149 } 150 if ((components.length == 0) || ((components.length == 1) && (components[0] == component))) { 151 Insets ins = container.getInsets(); 152 rect = orientation == SwingConstants.HORIZONTAL ? 153 new Rectangle(ins.left, 154 ins.top + (container.getHeight() - ins.top 155 - ins.bottom - 20) / 2, 156 30, 20) : 157 new Rectangle(ins.left + (container.getWidth() - ins.left 158 - ins.right - 30) / 2, 159 ins.top, 160 30, 20); 161 } 162 else if (newIndex < 0 || newIndex >= components.length) { 163 int index = (components[components.length-1] == component) ? components.length-2 : components.length-1; 164 Rectangle b = components[index].getBounds(); 165 rect = orientation == SwingConstants.HORIZONTAL ? 166 new Rectangle(b.x + b.width - 10, b.y, 20, b.height) : 167 new Rectangle(b.x, b.y + b.height - 10, b.width, 20); 168 } 169 else { 170 Rectangle b = components[newIndex].getBounds(); 171 rect = orientation == SwingConstants.HORIZONTAL ? 172 new Rectangle(b.x - 10, b.y, 20, b.height) : 173 new Rectangle(b.x, b.y - 10, b.width, 20); 174 } 175 176 g.drawRect(rect.x, rect.y, rect.width, rect.height); 177 178 return true; 179 } 180 } 181 | Popular Tags |