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.Constructor ; 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 41 public class BoxLayoutSupport extends AbstractLayoutSupport 43 { 44 private int axis = BoxLayout.LINE_AXIS; 45 46 private FormProperty[] properties; 47 48 private static Constructor boxLayoutConstructor; 49 50 53 public Class getSupportedClass() { 54 return BoxLayout.class; 55 } 56 57 63 public void acceptContainerLayoutChange(PropertyChangeEvent ev) 64 throws PropertyVetoException 65 { updateLayoutInstance(); 68 super.acceptContainerLayoutChange(ev); 69 } 70 71 88 public int getNewIndex(Container container, 89 Container containerDelegate, 90 Component component, 91 int index, 92 Point posInCont, 93 Point posInComp) 94 { 95 if (!(containerDelegate.getLayout() instanceof BoxLayout)) 96 return -1; 97 98 assistantParams = 0; 99 Component[] components = containerDelegate.getComponents(); 100 for (int i = 0; i < components.length; i++) { 101 if (components[i] == component) { 102 assistantParams--; 103 continue; 104 } 105 Rectangle b = components[i].getBounds(); 106 if ((axis == BoxLayout.X_AXIS) || (axis == BoxLayout.LINE_AXIS)) { 107 if (posInCont.x < b.x + b.width / 2) { 108 assistantParams += i; 109 return i; 110 } 111 } 112 else { 113 if (posInCont.y < b.y + b.height / 2) { 114 assistantParams += i; 115 return i; 116 } 117 } 118 } 119 120 assistantParams += components.length; 121 return components.length; 122 } 123 124 private int assistantParams; 125 public String getAssistantContext() { 126 return "boxLayout"; } 128 129 public Object [] getAssistantParams() { 130 return new Object [] {Integer.valueOf(assistantParams+1)}; 131 } 132 133 134 149 public boolean paintDragFeedback(Container container, 150 Container containerDelegate, 151 Component component, 152 LayoutConstraints newConstraints, 153 int newIndex, 154 Graphics g) 155 { 156 if (!(containerDelegate.getLayout() instanceof BoxLayout)) 157 return false; 158 159 Component[] components = containerDelegate.getComponents(); 160 Rectangle rect; 161 162 if ((components.length == 0) || ((components.length == 1) && (components[0] == component))) { 163 Insets ins = containerDelegate.getInsets(); 164 rect = (axis == BoxLayout.X_AXIS || axis == BoxLayout.LINE_AXIS) ? 165 new Rectangle(ins.left, 166 ins.top + (containerDelegate.getHeight() 167 - ins.top - ins.bottom - 20) / 2, 168 30, 20) : 169 new Rectangle(ins.left + (containerDelegate.getWidth() 170 - ins.left - ins.right - 30) / 2, 171 ins.top, 172 30, 20); 173 } 174 else if (newIndex < 0 || newIndex >= components.length) { 175 Component comp = components[components.length - 1]; 176 if (comp == component) { 177 comp = components[components.length - 2]; 178 } 179 Rectangle b = comp.getBounds(); 180 rect = (axis == BoxLayout.X_AXIS || axis == BoxLayout.LINE_AXIS) ? 181 new Rectangle(b.x + b.width - 10, b.y, 20, b.height) : 182 new Rectangle(b.x, b.y + b.height - 10, b.width, 20); 183 } 184 else { 185 Rectangle b = components[newIndex].getBounds(); 186 rect = (axis == BoxLayout.X_AXIS || axis == BoxLayout.LINE_AXIS) ? 187 new Rectangle(b.x - 10, b.y, 20, b.height) : 188 new Rectangle(b.x, b.y - 10, b.width, 20); 189 } 190 191 g.drawRect(rect.x, rect.y, rect.width, rect.height); 192 193 return true; 194 } 195 196 205 public void setLayoutToContainer(Container container, 206 Container containerDelegate) 207 { 208 containerDelegate.setLayout(cloneLayoutInstance(container, 209 containerDelegate)); 210 } 211 212 public void addComponentsToContainer(Container container, 213 Container containerDelegate, 214 Component[] components, 215 int index) { 216 ((LayoutManager2)containerDelegate.getLayout()).invalidateLayout(containerDelegate); 218 super.addComponentsToContainer(container, containerDelegate, components, index); 219 } 220 221 223 228 protected LayoutManager createDefaultLayoutInstance() { 229 return new BoxLayout(new JPanel(), BoxLayout.LINE_AXIS); 230 } 231 232 241 protected LayoutManager cloneLayoutInstance(Container container, 242 Container containerDelegate) 243 { 244 return new BoxLayout(containerDelegate, axis); 245 } 246 247 255 protected void readInitLayoutCode(CodeExpression layoutExp, 256 CodeGroup layoutCode) 257 { 258 CodeExpression[] params = layoutExp.getOrigin().getCreationParameters(); 259 if (params.length == 2) { 260 FormCodeSupport.readPropertyExpression( 261 params[1], getProperties()[0], false); 262 updateLayoutInstance(); 263 } 264 } 265 266 275 protected CodeExpression createInitLayoutCode(CodeGroup layoutCode) { 276 CodeStructure codeStructure = getCodeStructure(); 277 278 CodeExpression[] params = new CodeExpression[2]; 279 params[0] = getLayoutContext().getContainerDelegateCodeExpression(); 280 params[1] = codeStructure.createExpression( 281 FormCodeSupport.createOrigin(getProperties()[0])); 282 283 return codeStructure.createExpression(getBoxLayoutConstructor(), 284 params); 285 } 286 287 294 protected FormProperty[] getProperties() { 295 if (properties == null) { 296 properties = new FormProperty[1]; 299 300 properties[0] = new FormProperty( 301 "axis", Integer.TYPE, 303 getBundle().getString("PROP_axis"), getBundle().getString("HINT_axis")) { 306 public Object getTargetValue() { 307 return new Integer (axis); 308 } 309 310 public void setTargetValue(Object value) { 311 int ax = ((Integer )value).intValue(); 312 if (ax == BoxLayout.X_AXIS || ax == BoxLayout.Y_AXIS 313 || ax == BoxLayout.LINE_AXIS || ax == BoxLayout.PAGE_AXIS) { 314 axis = ax; 315 } 316 } 317 318 public boolean supportsDefaultValue() { 319 return true; 320 } 321 322 public Object getDefaultValue() { 323 return new Integer (BoxLayout.LINE_AXIS); 324 } 325 326 public PropertyEditor getExpliciteEditor() { 327 return new BoxAxisEditor(); 328 } 329 }; 330 } 334 335 return properties; 336 } 337 338 343 protected Node.Property getProperty(String propName) { 344 return "axis".equals(propName) ? getProperties()[0] : null; } 346 347 349 private static Constructor getBoxLayoutConstructor() { 350 if (boxLayoutConstructor == null) { 351 try { 352 boxLayoutConstructor = BoxLayout.class.getConstructor( 353 new Class [] { Container.class, Integer.TYPE }); 354 } 355 catch (NoSuchMethodException ex) { ex.printStackTrace(); 357 } 358 } 359 return boxLayoutConstructor; 360 } 361 362 364 366 public static final class BoxAxisEditor extends PropertyEditorSupport { 367 private final String [] tags = { 368 getBundle().getString("VALUE_axis_line"), getBundle().getString("VALUE_axis_page"), getBundle().getString("VALUE_axis_x"), getBundle().getString("VALUE_axis_y") }; 373 private final Integer [] values = { 374 new Integer (BoxLayout.LINE_AXIS), 375 new Integer (BoxLayout.PAGE_AXIS), 376 new Integer (BoxLayout.X_AXIS), 377 new Integer (BoxLayout.Y_AXIS) 378 }; 379 private final String [] javaInitStrings = { 380 "javax.swing.BoxLayout.LINE_AXIS", "javax.swing.BoxLayout.PAGE_AXIS", "javax.swing.BoxLayout.X_AXIS", "javax.swing.BoxLayout.Y_AXIS" }; 385 386 public String [] getTags() { 387 return tags; 388 } 389 390 public String getAsText() { 391 Object value = getValue(); 392 for (int i=0; i<values.length; i++) { 393 if (values[i].equals(value)) return tags[i]; 394 } 395 return null; 396 } 397 398 public void setAsText(String str) { 399 for (int i=0; i<values.length; i++) { 400 if (tags[i].equals(str)) { 401 setValue(values[i]); 402 break; 403 } 404 } 405 } 406 407 public String getJavaInitializationString() { 408 Object value = getValue(); 409 for (int i=0; i < values.length; i++) 410 if (values[i].equals(value)) 411 return javaInitStrings[i]; 412 return null; 413 } 414 } 415 } 416 | Popular Tags |