1 19 20 package org.netbeans.modules.form.layoutdesign.support; 21 22 import java.io.*; 23 import java.awt.*; 24 import java.util.*; 25 import java.util.List ; 26 import javax.swing.*; 27 28 import org.netbeans.modules.form.layoutdesign.*; 29 30 35 public class SwingLayoutCodeGenerator { 36 private static final String LAYOUT_VAR_NAME = "layout"; 38 private LayoutModel layoutModel; 39 private String layoutVarName; 40 private boolean useLayoutLibrary; 41 42 45 private Map componentIDMap; 46 47 52 public SwingLayoutCodeGenerator(LayoutModel layoutModel) { 53 componentIDMap = new HashMap(); 54 this.layoutModel = layoutModel; 55 } 56 57 67 public void generateContainerLayout(Writer writer, LayoutComponent container, 68 String contExprStr, String contVarName, ComponentInfo infos[], 69 boolean useLibrary) throws IOException { 70 useLayoutLibrary = useLibrary; 71 if (contVarName == null) { 72 layoutVarName = LAYOUT_VAR_NAME; 73 } else { 74 layoutVarName = contVarName + Character.toUpperCase(LAYOUT_VAR_NAME.charAt(0)) 75 + LAYOUT_VAR_NAME.substring(1); 76 } 77 fillMap(infos); 78 generateInstantiation(writer, contExprStr); 79 StringBuffer sb = new StringBuffer (); 80 LayoutInterval horizontalInterval = container.getLayoutRoot(LayoutConstants.HORIZONTAL); 81 composeGroup(sb, horizontalInterval, true, true); 82 String horizontalGroup = sb.toString(); 83 writer.write(layoutVarName + ".setHorizontalGroup(\n" + horizontalGroup + "\n);\n"); 85 sb = new StringBuffer (); 86 composeLinks(sb, container, layoutVarName, LayoutConstants.HORIZONTAL); 87 String horizontalLinks = sb.toString(); 88 writer.write(horizontalLinks); 89 90 sb = new StringBuffer (); 91 LayoutInterval verticalInterval = container.getLayoutRoot(LayoutConstants.VERTICAL); 92 composeGroup(sb, verticalInterval, true, true); 93 String verticalGroup = sb.toString(); 94 writer.write(layoutVarName + ".setVerticalGroup(\n" + verticalGroup + "\n);\n"); 96 97 sb = new StringBuffer (); 98 composeLinks(sb, container, layoutVarName, LayoutConstants.VERTICAL); 99 String verticalLinks = sb.toString(); 100 writer.write(verticalLinks); 101 } 102 103 108 private void fillMap(ComponentInfo[] infos) { 109 for (int counter = 0; counter < infos.length; counter++) { 110 componentIDMap.put(infos[counter].id, infos[counter]); 111 } 112 } 113 114 118 private void generateInstantiation(Writer writer, String contExprStr) throws IOException { 119 writer.write(getLayoutName() + " " + layoutVarName + " "); writer.write("= new " + getLayoutName() + "(" + contExprStr + ");\n"); writer.write(contExprStr + ".setLayout(" + layoutVarName + ");\n"); } 123 124 131 private void composeGroup(StringBuffer layout, LayoutInterval interval, 132 boolean first, boolean last) throws IOException { 133 if (interval.isGroup()) { 134 int groupAlignment = interval.getGroupAlignment(); 135 if (interval.isParallel()) { 136 boolean notResizable = interval.getMaximumSize(false) == LayoutConstants.USE_PREFERRED_SIZE; 137 String alignmentStr = convertAlignment(groupAlignment); 138 layout.append(layoutVarName).append(".createParallelGroup("); layout.append(alignmentStr); 140 if (notResizable) { 141 layout.append(", false"); } 143 layout.append(")"); } else if (interval.isSequential()) { 145 layout.append(layoutVarName).append(".createSequentialGroup()"); } else { 147 assert false; 148 } 149 if (interval.getSubIntervalCount() > 0) { 150 layout.append("\n"); } 152 Iterator subIntervals = interval.getSubIntervals(); 153 while (subIntervals.hasNext()) { 154 LayoutInterval subInterval = (LayoutInterval)subIntervals.next(); 155 fillGroup(layout, subInterval, first, 156 last && (!interval.isSequential() || !subIntervals.hasNext()), 157 groupAlignment); 158 if (first && interval.isSequential()) { 159 first = false; 160 } 161 if (subIntervals.hasNext()) { 162 layout.append("\n"); } 164 } 165 } else { 166 layout.append(layoutVarName).append(".createSequentialGroup()\n"); fillGroup(layout, interval, true, true, LayoutConstants.DEFAULT); 168 } 169 } 170 171 178 private void fillGroup(StringBuffer layout, LayoutInterval interval, 179 boolean first, boolean last, int groupAlignment) throws IOException { 180 if (interval.isGroup()) { 181 layout.append(getAddGroupStr()); 182 int alignment = interval.getAlignment(); 183 if ((alignment != LayoutConstants.DEFAULT) && interval.getParent().isParallel() && alignment != groupAlignment 184 && alignment != LayoutConstants.BASELINE && groupAlignment != LayoutConstants.BASELINE) { 185 String alignmentStr = convertAlignment(alignment); 186 layout.append(alignmentStr).append(", "); } 188 composeGroup(layout, interval, first, last); 189 } else { 190 int min = interval.getMinimumSize(false); 191 int pref = interval.getPreferredSize(false); 192 int max = interval.getMaximumSize(false); 193 if (interval.isComponent()) { 194 layout.append(getAddComponentStr()); 195 int alignment = interval.getAlignment(); 196 LayoutComponent layoutComp = interval.getComponent(); 197 ComponentInfo info = (ComponentInfo)componentIDMap.get(layoutComp.getId()); 198 if (min == LayoutConstants.NOT_EXPLICITLY_DEFINED) { 199 int dimension = (layoutComp.getLayoutInterval(LayoutConstants.HORIZONTAL) == interval) ? LayoutConstants.HORIZONTAL : LayoutConstants.VERTICAL; 200 if ((dimension == LayoutConstants.HORIZONTAL) && info.clazz.getName().equals("javax.swing.JComboBox")) { min = 0; 202 } else if (pref >= 0) { 203 int compMin = (dimension == LayoutConstants.HORIZONTAL) ? info.minSize.width : info.minSize.height; 204 if (compMin > pref) { 205 min = LayoutConstants.USE_PREFERRED_SIZE; 206 } 207 } 208 } 209 assert (info.variableName != null); 210 if (interval.getParent().isSequential() || (alignment == LayoutConstants.DEFAULT) || (alignment == groupAlignment) 211 || alignment == LayoutConstants.BASELINE || groupAlignment == LayoutConstants.BASELINE) { 212 layout.append(info.variableName); 213 } else { 214 String alignmentStr = convertAlignment(alignment); 215 if (useLayoutLibrary()) 216 layout.append(alignmentStr).append(", ").append(info.variableName); else layout.append(info.variableName).append(", ").append(alignmentStr); } 220 int status = SwingLayoutUtils.getResizableStatus(info.clazz); 221 222 if (!((pref == LayoutConstants.NOT_EXPLICITLY_DEFINED) && 223 ((min == LayoutConstants.NOT_EXPLICITLY_DEFINED) 224 || ((min == LayoutConstants.USE_PREFERRED_SIZE) 225 && !info.sizingChanged 226 && (status == SwingLayoutUtils.STATUS_NON_RESIZABLE))) && 227 ((max == LayoutConstants.NOT_EXPLICITLY_DEFINED) 228 || ((max == LayoutConstants.USE_PREFERRED_SIZE) 229 && !info.sizingChanged 230 && (status == SwingLayoutUtils.STATUS_NON_RESIZABLE)) 231 || ((max == Short.MAX_VALUE) 232 && !info.sizingChanged 233 && (status == SwingLayoutUtils.STATUS_RESIZABLE))))) { 234 layout.append(", "); generateSizeParams(layout, min, pref, max); 236 } 237 } else if (interval.isEmptySpace()) { 238 if (interval.isDefaultPadding(false)) { 239 if (first || last) { 240 layout.append(getAddContainerGapStr()); 241 } else { 242 layout.append(getAddPreferredGapStr()); 243 layout.append(getLayoutStyleName()); 244 if (!useLayoutLibrary()) 245 layout.append(".ComponentPlacement"); layout.append(".RELATED"); } 248 if ((pref != LayoutConstants.NOT_EXPLICITLY_DEFINED) 249 || ((max != LayoutConstants.NOT_EXPLICITLY_DEFINED) 250 && (max != LayoutConstants.USE_PREFERRED_SIZE))) { 252 if (!first && !last) { 253 layout.append(',').append(' '); 254 } 255 layout.append(convertSize(pref)).append(", "); layout.append(convertSize(max)); 257 } 258 } else { 259 if (min == LayoutConstants.USE_PREFERRED_SIZE) { 260 min = pref; 261 } 262 if (max == LayoutConstants.USE_PREFERRED_SIZE) { 263 max = pref; 264 } 265 layout.append(getAddGapStr()); 266 if (min < 0) min = pref; min = Math.min(pref, min); 268 max = Math.max(pref, max); 269 generateSizeParams(layout, min, pref, max); 270 } 271 } else { 272 assert false; 273 } 274 } 275 layout.append(")"); } 277 278 286 private void generateSizeParams(StringBuffer layout, int min, int pref, int max) { 287 layout.append(convertSize(min)).append(", "); layout.append(convertSize(pref)).append(", "); layout.append(convertSize(max)); 290 } 291 292 300 private String convertAlignment(int alignment) { 301 String groupAlignment = null; 302 switch (alignment) { 303 case LayoutConstants.LEADING: groupAlignment = "LEADING"; break; case LayoutConstants.TRAILING: groupAlignment = "TRAILING"; break; case LayoutConstants.CENTER: groupAlignment = "CENTER"; break; case LayoutConstants.BASELINE: groupAlignment = "BASELINE"; break; default: assert false; break; 308 } 309 return useLayoutLibrary() ? 310 getLayoutName() + "." + groupAlignment : getLayoutName() + ".Alignment." + groupAlignment; } 313 314 322 private String convertSize(int size) { 323 String convertedSize; 324 switch (size) { 325 case LayoutConstants.NOT_EXPLICITLY_DEFINED: convertedSize = getLayoutName() + ".DEFAULT_SIZE"; break; case LayoutConstants.USE_PREFERRED_SIZE: convertedSize = getLayoutName() + ".PREFERRED_SIZE"; break; case Short.MAX_VALUE: convertedSize = "Short.MAX_VALUE"; break; default: assert (size >= 0); convertedSize = Integer.toString(size); break; 329 } 330 return convertedSize; 331 } 332 333 private void composeLinks(StringBuffer layout, LayoutComponent containerLC, String layoutVarName, int dimension) throws IOException { 334 335 Map linkSizeGroups = SwingLayoutUtils.createLinkSizeGroups(containerLC, dimension); 336 337 Collection linkGroups = linkSizeGroups.values(); 338 Iterator linkGroupsIt = linkGroups.iterator(); 339 while (linkGroupsIt.hasNext()) { 340 List l = (List )linkGroupsIt.next(); 341 Collections.sort(l, new Comparator() { 343 public int compare(Object o1, Object o2) { 344 String id1 =(String )o1; 345 String id2 =(String )o2; 346 ComponentInfo info1 = (ComponentInfo)componentIDMap.get(id1); 347 ComponentInfo info2 = (ComponentInfo)componentIDMap.get(id2); 348 return info1.variableName.compareTo(info2.variableName); 349 } 350 }); 351 if (l.size() > 1) { 352 layout.append("\n\n" + layoutVarName + ".linkSize("); if (!useLayoutLibrary()) { 354 layout.append("javax.swing.SwingConstants"); layout.append(dimension == LayoutConstants.HORIZONTAL ? 356 ".HORIZONTAL, " : ".VERTICAL, "); } 358 layout.append("new java.awt.Component[] {"); Iterator i = l.iterator(); 360 boolean first = true; 361 while (i.hasNext()) { 362 String cid = (String )i.next(); 363 ComponentInfo info = (ComponentInfo)componentIDMap.get(cid); 364 if (first) { 365 first = false; 366 layout.append(info.variableName); 367 } else { 368 layout.append(", " + info.variableName); } 370 } 371 layout.append( "}"); if (useLayoutLibrary()) { 373 layout.append( ", "); layout.append(getLayoutName()); 375 layout.append(dimension == LayoutConstants.HORIZONTAL ? 376 ".HORIZONTAL" : ".VERTICAL"); } 378 layout.append(");\n\n"); } 380 } 381 } 382 383 386 public static class ComponentInfo { 387 388 public String id; 389 390 public String variableName; 391 392 public Class clazz; 393 394 public Dimension minSize; 395 399 public boolean sizingChanged; 400 } 401 402 405 boolean useLayoutLibrary() { 406 return useLayoutLibrary; 407 } 408 409 private String getLayoutName() { 410 return useLayoutLibrary() ? "org.jdesktop.layout.GroupLayout" : "javax.swing.GroupLayout"; } 412 413 private String getLayoutStyleName() { 414 return useLayoutLibrary() ? "org.jdesktop.layout.LayoutStyle" : "javax.swing.LayoutStyle"; } 416 417 private String getAddComponentStr() { 418 return useLayoutLibrary() ? ".add(" : ".addComponent("; } 420 421 private String getAddGapStr() { 422 return useLayoutLibrary() ? ".add(" : ".addGap("; } 424 425 private String getAddPreferredGapStr() { 426 return ".addPreferredGap("; } 428 429 private String getAddContainerGapStr() { 430 return ".addContainerGap("; } 432 433 private String getAddGroupStr() { 434 return useLayoutLibrary() ? ".add(" : ".addGroup("; } 436 } 437 | Popular Tags |