1 19 20 package org.netbeans.modules.form.layoutsupport.delegates; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 26 import org.openide.nodes.Node; 27 28 import org.netbeans.modules.form.layoutsupport.*; 29 import org.netbeans.modules.form.codestructure.*; 30 import org.netbeans.modules.form.FormProperty; 31 32 38 public class BorderLayoutSupport extends AbstractLayoutSupport 40 { 41 44 public Class getSupportedClass() { 45 return BorderLayout.class; 46 } 47 48 65 public LayoutConstraints getNewConstraints(Container container, 66 Container containerDelegate, 67 Component component, 68 int index, 69 Point posInCont, 70 Point posInComp) 71 { 72 if (component != null && component.getParent() != containerDelegate) 73 component = null; 74 75 String primary = BorderLayout.CENTER; 76 String alternateX = null; 77 String alternateY = null; 78 79 int w = containerDelegate.getSize().width; 80 int h = containerDelegate.getSize().height; 81 82 Insets contInsets = containerDelegate.getInsets(); 83 int marginW = getMargin(w - contInsets.left - contInsets.right); 84 int marginH = getMargin(h - contInsets.top - contInsets.bottom); 85 86 int xC = 1; int yC = 1; 89 if (w > 25) { 90 if (posInCont.x < contInsets.left+marginW) xC = 0; else if (posInCont.x >= w-marginW-contInsets.right) xC = 2; } 93 if (h > 25) { 94 if (posInCont.y < contInsets.top+marginH) yC = 0; else if (posInCont.y >= h-marginH-contInsets.bottom) yC = 2; } 97 98 if (xC == 0) primary = BorderLayout.LINE_START; 99 else if (xC == 2) primary = BorderLayout.LINE_END; 100 else alternateX = posInCont.x - contInsets.left < 101 (w - contInsets.left - contInsets.right)/2 ? 102 BorderLayout.LINE_START : BorderLayout.LINE_END; 103 104 if (yC == 0) { alternateX = primary; 106 primary = BorderLayout.PAGE_START; 107 } 108 else if (yC == 2) { alternateX = primary; 110 primary = BorderLayout.PAGE_END; 111 } 112 else alternateY = posInCont.y - contInsets.top < 113 (h - contInsets.top - contInsets.bottom)/2 ? 114 BorderLayout.PAGE_START : BorderLayout.PAGE_END; 115 116 String [] suggested = new String [] { primary, alternateY, alternateX }; 117 String [] free = findFreePositions(); 118 119 for (int i=0; i < suggested.length; i++) { 120 String str = suggested[i]; 121 if (str == null) continue; 122 123 for (int j=0; j < free.length; j++) 124 if (free[j].equals(str)) { 125 assistantParams = str; 126 return new BorderConstraints(str); 127 } 128 129 if (component != null) { 130 int idx = getComponentOnPosition(str); 131 if (containerDelegate.getComponent(idx) == component) { 132 assistantParams = str; 133 return new BorderConstraints(str); 134 } 135 } 136 } 137 assistantParams = free[0]; 138 return new BorderConstraints(free[0]); 139 } 140 141 private String assistantParams; 142 public String getAssistantContext() { 143 return "borderLayout"; } 145 146 public Object [] getAssistantParams() { 147 return new Object [] {assistantParams}; 148 } 149 150 165 public boolean paintDragFeedback(Container container, 166 Container containerDelegate, 167 Component component, 168 LayoutConstraints newConstraints, 169 int newIndex, 170 Graphics g) 171 { 172 String position = (String ) newConstraints.getConstraintsObject(); 173 Component[] comps = containerDelegate.getComponents(); 174 int index; 175 176 Dimension contSize = containerDelegate.getSize(); 177 Insets contInsets = containerDelegate.getInsets(); 178 Dimension compPrefSize = 179 component != null ? component.getPreferredSize() : new Dimension(0,0); 180 181 int x1, y1, x2, y2; 182 int marginW = getMargin(contSize.width - contInsets.left - contInsets.right); 183 int marginH = getMargin(contSize.height - contInsets.top - contInsets.bottom); 184 185 if (BorderLayout.PAGE_START.equals(position)) { 186 x1 = contInsets.left; 187 x2 = contSize.width - contInsets.right; 188 y1 = contInsets.top; 189 y2 = contInsets.top + (compPrefSize.height > 0 ? 190 compPrefSize.height : marginH); 191 } 192 else if (BorderLayout.PAGE_END.equals(position)) { 193 x1 = contInsets.left; 194 x2 = contSize.width - contInsets.right; 195 y1 = contSize.height - contInsets.bottom 196 - (compPrefSize.height > 0 ? compPrefSize.height : marginH); 197 y2 = contSize.height - contInsets.bottom; 198 } 199 else { if (BorderLayout.LINE_START.equals(position)) { 201 x1 = contInsets.left; 202 x2 = contInsets.left + (compPrefSize.width > 0 ? 203 compPrefSize.width : marginW); 204 } 205 else if (BorderLayout.LINE_END.equals(position)) { 206 x1 = contSize.width - contInsets.right 207 - (compPrefSize.width > 0 ? compPrefSize.width : marginW); 208 x2 = contSize.width - contInsets.right; 209 } 210 else { index = getComponentOnPosition(BorderLayout.LINE_START); 212 x1 = contInsets.left; 213 if (index >= 0) 214 x1 += comps[index].getSize().width; 215 216 index = getComponentOnPosition(BorderLayout.LINE_END); 217 x2 = contSize.width - contInsets.right; 218 if (index >= 0) 219 x2 -= comps[index].getSize().width; 220 } 221 222 index = getComponentOnPosition(BorderLayout.PAGE_START); 224 y1 = contInsets.top; 225 if (index >= 0) 226 y1 += comps[index].getSize().height; 227 228 index = getComponentOnPosition(BorderLayout.PAGE_END); 229 y2 = contSize.height - contInsets.bottom; 230 if (index >= 0) 231 y2 -= comps[index].getSize().height; 232 } 233 234 if (x1 >= x2) { 235 x1 = contInsets.left; 236 x2 = contSize.width - contInsets.right; 237 if (x1 >= x2) return true; } 239 if (y1 >= y2) { 240 y1 = contInsets.top; 241 x2 = contSize.height - contInsets.bottom; 242 if (y1 >= y2) return true; } 244 245 g.drawRect(x1, y1, x2-x1-1, y2-y1-1); 246 247 return true; 248 } 249 250 252 264 protected LayoutConstraints readConstraintsCode(CodeExpression constrExp, 265 CodeGroup constrCode, 266 CodeExpression compExp) 267 { 268 BorderConstraints constr = new BorderConstraints(BorderLayout.CENTER); 269 FormCodeSupport.readPropertyExpression(constrExp, 270 constr.getProperties()[0], 271 false); 272 return constr; 273 } 274 275 285 protected CodeExpression createConstraintsCode(CodeGroup constrCode, 286 LayoutConstraints constr, 287 CodeExpression compExp, 288 int index) 289 { 290 if (!(constr instanceof BorderConstraints)) 291 return null; 293 return getCodeStructure().createExpression( 294 FormCodeSupport.createOrigin(constr.getProperties()[0])); 295 } 296 297 301 protected LayoutConstraints createDefaultConstraints() { 302 return new BorderConstraints(findFreePositions()[0]); 303 } 304 305 307 private String [] findFreePositions() { 308 ArrayList positions = new ArrayList(6); 309 310 if (getComponentOnPosition(BorderLayout.CENTER) == -1) 311 positions.add(BorderLayout.CENTER); 312 if (getComponentOnPosition(BorderLayout.PAGE_START) == -1) 313 positions.add(BorderLayout.PAGE_START); 314 if (getComponentOnPosition(BorderLayout.PAGE_END) == -1) 315 positions.add(BorderLayout.PAGE_END); 316 if (getComponentOnPosition(BorderLayout.LINE_END) == -1) 317 positions.add(BorderLayout.LINE_END); 318 if (getComponentOnPosition(BorderLayout.LINE_START) == -1) 319 positions.add(BorderLayout.LINE_START); 320 if (positions.size() == 0) 321 positions.add(BorderLayout.CENTER); 322 323 String [] free = new String [positions.size()]; 324 positions.toArray(free); 325 return free; 326 } 327 328 private int getComponentOnPosition(String position) { 329 java.util.List constraints = getConstraintsList(); 330 if (constraints == null) 331 return -1; 332 333 position = (String )toAbsolute(position); 334 for (int i=0, n=constraints.size(); i < n; i++) { 335 LayoutConstraints constr = (LayoutConstraints) constraints.get(i); 336 if (constr != null && position.equals(toAbsolute(constr.getConstraintsObject()))) 337 return i; 338 } 339 340 return -1; 341 } 342 343 private static Object toAbsolute(Object constraint) { 344 if (BorderLayout.LINE_START.equals(constraint)) { 345 constraint = BorderLayout.WEST; 346 } else if (BorderLayout.LINE_END.equals(constraint)) { 347 constraint = BorderLayout.EAST; 348 } else if (BorderLayout.PAGE_START.equals(constraint)) { 349 constraint = BorderLayout.NORTH; 350 } else if (BorderLayout.PAGE_END.equals(constraint)) { 351 constraint = BorderLayout.SOUTH; 352 } 353 return constraint; 354 } 355 356 private int getMargin(int size) { 357 int margin = size/8; 358 if (margin < 10) margin = 10; 359 if (margin > 50) margin = 50; 360 return margin; 361 } 362 363 365 368 public static class BorderConstraints implements LayoutConstraints { 369 private String direction; 370 371 private Node.Property[] properties; 372 373 public BorderConstraints(String direction) { 374 this.direction = direction; 375 } 376 377 public Node.Property[] getProperties() { 378 if (properties == null) { 379 properties = new FormProperty[] { 380 new FormProperty( 381 "BorderConstraints direction", String .class, 383 getBundle().getString("PROP_direction"), getBundle().getString("HINT_direction")) { 386 public Object getTargetValue() { 387 return direction; 388 } 389 390 public void setTargetValue(Object value) { 391 direction = (String )value; 392 } 393 394 public PropertyEditor getExpliciteEditor() { 395 return new BorderDirectionEditor(); 396 } 397 public void setPropertyContext( 398 org.netbeans.modules.form.FormPropertyContext ctx) 399 { } } 402 }; 403 properties[0].setValue("NOI18N", Boolean.TRUE); } 405 406 return properties; 407 } 408 409 public Object getConstraintsObject() { 410 return direction; 411 } 412 413 public LayoutConstraints cloneConstraints() { 414 return new BorderConstraints(direction); 415 } 416 } 417 418 420 422 static class BorderDirectionEditor extends PropertyEditorSupport { 423 private final String [] values = { 424 BorderLayout.CENTER, 425 BorderLayout.LINE_START, 426 BorderLayout.LINE_END, 427 BorderLayout.PAGE_START, 428 BorderLayout.PAGE_END, 429 BorderLayout.WEST, 430 BorderLayout.EAST, 431 BorderLayout.NORTH, 432 BorderLayout.SOUTH 433 }; 434 private final String [] javaInitStrings = { 435 "java.awt.BorderLayout.CENTER", "java.awt.BorderLayout.LINE_START", "java.awt.BorderLayout.LINE_END", "java.awt.BorderLayout.PAGE_START", "java.awt.BorderLayout.PAGE_END", "java.awt.BorderLayout.WEST", "java.awt.BorderLayout.EAST", "java.awt.BorderLayout.NORTH", "java.awt.BorderLayout.SOUTH" }; 445 446 public String [] getTags() { 447 return values; 448 } 449 450 public String getAsText() { 451 return (String )getValue(); 452 } 453 454 public void setAsText(String str) { 455 for (int i = 0; i < values.length; i++) 456 if (str.equals(values[i])) { 457 setValue(str); 458 break; 459 } 460 } 461 462 public String getJavaInitializationString() { 463 Object value = getValue(); 464 for (int i=0; i < values.length; i++) 465 if (values[i].equals(value)) 466 return javaInitStrings[i]; 467 return null; 468 } 469 } 470 } 471 | Popular Tags |