KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > layoutdesign > LayoutPersistenceManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.layoutdesign;
21
22 import java.util.*;
23 import org.w3c.dom.*;
24
25 /**
26  * Class responsible for loading and saving of layout model.
27  *
28  * @author Jan Stola
29  */

30 class LayoutPersistenceManager implements LayoutConstants {
31     /** Layout model to load/save. */
32     private LayoutModel layoutModel;
33     /** Currently processed layout root. */
34     private LayoutComponent root;
35     /** Currently processed dimension. */
36     private int dimension;
37     /** Map from component IDs to names or vice versa. */
38     private Map<String JavaDoc, String JavaDoc> idNameMap;
39     /** Determines whether constants should be replaces by human readable expressions. */
40     private boolean humanReadable;
41     /** Size of current indent. */
42     private int indent;
43     /** String buffer used to save layout. */
44     private StringBuffer JavaDoc sb;
45     
46     // elements names
47
static final String JavaDoc XML_DIMENSION_LAYOUT = "DimensionLayout"; // NOI18N
48
static final String JavaDoc XML_GROUP = "Group"; // NOI18N
49
static final String JavaDoc XML_COMPONENT = "Component"; // NOI18N
50
static final String JavaDoc XML_EMPTY_SPACE = "EmptySpace"; // NOI18N
51

52     // attributes names
53
static final String JavaDoc ATTR_DIMENSION_DIM = "dim"; // NOI18N
54
static final String JavaDoc ATTR_GROUP_TYPE = "type"; // NOI18N
55
static final String JavaDoc ATTR_SIZE_MIN = "min"; // NOI18N
56
static final String JavaDoc ATTR_SIZE_PREF = "pref"; // NOI18N
57
static final String JavaDoc ATTR_SIZE_MAX = "max"; // NOI18N
58
static final String JavaDoc ATTR_ALIGNMENT = "alignment"; // NOI18N
59
static final String JavaDoc ATTR_GROUP_ALIGNMENT = "groupAlignment"; // NOI18N
60
static final String JavaDoc ATTR_LINK_SIZE = "linkSize"; // NOI18N
61
static final String JavaDoc ATTR_COMPONENT_ID = "id"; // NOI18N
62
static final String JavaDoc ATTR_ATTRIBUTES = "attributes"; // NOI18N
63

64     // attribute values
65
static final String JavaDoc VALUE_DIMENSION_HORIZONTAL = "horizontal"; // NOI18N
66
static final String JavaDoc VALUE_DIMENSION_VERTICAL = "vertical"; // NOI18N
67
static final String JavaDoc VALUE_ALIGNMENT_LEADING = "leading"; // NOI18N
68
static final String JavaDoc VALUE_ALIGNMENT_TRAILING = "trailing"; // NOI18N
69
static final String JavaDoc VALUE_ALIGNMENT_CENTER = "center"; // NOI18N
70
static final String JavaDoc VALUE_ALIGNMENT_BASELINE = "baseline"; // NOI18N
71
static final String JavaDoc VALUE_SIZE_PREFERRED = "$pref"; // NOI18N
72
static final String JavaDoc VALUE_SIZE_MAX = "Short.MAX_VALUE"; // NOI18N
73
static final String JavaDoc VALUE_GROUP_PARALLEL = "parallel"; // NOI18N
74
static final String JavaDoc VALUE_GROUP_SEQUENTIAL = "sequential"; // NOI18N
75

76     /**
77      * Creates new <code>LayoutPersistenceManager</code>.
78      *
79      * @param layoutModel layout model to load/save.
80      */

81     LayoutPersistenceManager(LayoutModel layoutModel) {
82         this.layoutModel = layoutModel;
83     }
84
85     /**
86      * Returns dump of the layout model.
87      *
88      * @param indent determines size of indentation.
89      * @param root container layout model should be dumped.
90      * @param humanReadable determines whether constants should be replaced
91      * by human readable expressions.
92      * @return dump of the layout model.
93      */

94     String JavaDoc saveLayout(int indent, LayoutComponent root, Map idToNameMap, boolean humanReadable) {
95         this.root = root;
96         this.indent = indent;
97         this.idNameMap = idToNameMap;
98         this.humanReadable = humanReadable;
99         sb = new StringBuffer JavaDoc();
100         for (int i=0; i < DIM_COUNT; i++) {
101             indent().append('<').append(XML_DIMENSION_LAYOUT);
102             sb.append(' ').append(ATTR_DIMENSION_DIM).append("=\""); // NOI18N
103
if (humanReadable) {
104                 switch (i) {
105                     case HORIZONTAL: sb.append(VALUE_DIMENSION_HORIZONTAL); break;
106                     case VERTICAL: sb.append(VALUE_DIMENSION_VERTICAL); break;
107                     default: sb.append(i); break;
108                 }
109             } else {
110                 sb.append(i);
111             }
112             sb.append("\">\n"); // NOI18N
113
LayoutInterval interval = root.getLayoutRoot(i);
114             saveInterval(interval, i);
115             indent().append("</").append(XML_DIMENSION_LAYOUT).append(">\n"); // NOI18N
116
}
117         return sb.toString();
118     }
119     
120     /**
121      * Returns dump of the layout interval.
122      *
123      * @param indent determines size of indentation.
124      * @param root container layout model should be dumped.
125      * @param humanReadable determines whether constants should be replaced
126      * by human readable expressions.
127      * @return dump of the layout model.
128      */

129     String JavaDoc saveIntervalLayout(int indent, LayoutInterval interval, int dimension) {
130         this.indent = indent;
131         humanReadable = true;
132         sb = new StringBuffer JavaDoc();
133         saveInterval(interval, dimension);
134         return sb.toString();
135     }
136     
137     /**
138      * Dumps the information about the given layout interval.
139      *
140      * @param interval layout interval to dump.
141      */

142     private void saveInterval(LayoutInterval interval, int dimension) {
143         indent++;
144         indent();
145         if (interval.isGroup()) {
146             sb.append('<').append(XML_GROUP).append(' ');
147             sb.append(ATTR_GROUP_TYPE).append("=\""); // NOI18N
148
if (humanReadable) {
149                 sb.append(interval.isParallel() ? VALUE_GROUP_PARALLEL : VALUE_GROUP_SEQUENTIAL);
150             } else {
151                 sb.append(interval.getType());
152             }
153             sb.append("\""); // NOI18N
154
saveAlignment(interval.getRawAlignment(), false);
155             if (interval.isParallel()) {
156                 saveAlignment(interval.getGroupAlignment(), true);
157             }
158             saveSize(interval.getMinimumSize(), ATTR_SIZE_MIN);
159             saveSize(interval.getMaximumSize(), ATTR_SIZE_MAX);
160             saveAttributes(interval.getAttributes());
161             sb.append(">\n"); // NOI18N
162
indent++;
163             Iterator iter = interval.getSubIntervals();
164             while (iter.hasNext()) {
165                 LayoutInterval subInterval = (LayoutInterval)iter.next();
166                 saveInterval(subInterval, dimension);
167             }
168             indent--;
169             indent().append("</").append(XML_GROUP).append(">\n"); // NOI18N
170
} else {
171             if (interval.isComponent()) {
172                 String JavaDoc name = interval.getComponent().getId();
173                 if (idNameMap != null) {
174                     name = (String JavaDoc)idNameMap.get(name);
175                     assert (name != null);
176                 }
177                 sb.append('<').append(XML_COMPONENT).append(' ');
178                 sb.append(ATTR_COMPONENT_ID).append("=\"").append(name).append("\""); // NOI18N
179
saveLinkSize(interval.getComponent().getLinkSizeId(dimension));
180                 saveAlignment(interval.getRawAlignment(), false);
181             } else if (interval.isEmptySpace()) {
182                 sb.append('<').append(XML_EMPTY_SPACE);
183             } else {
184                 assert false;
185             }
186             saveSize(interval.getMinimumSize(), ATTR_SIZE_MIN);
187             saveSize(interval.getPreferredSize(), ATTR_SIZE_PREF);
188             saveSize(interval.getMaximumSize(), ATTR_SIZE_MAX);
189             saveAttributes(interval.getAttributes());
190             sb.append("/>\n"); // NOI18N
191
}
192         indent--;
193     }
194
195     /**
196      * Saves linkSize group identifier
197      *
198      * @param linksizeid
199      */

200     private void saveLinkSize(int linkSizeId) {
201         if (linkSizeId != NOT_EXPLICITLY_DEFINED) {
202             sb.append(" ").append(ATTR_LINK_SIZE).append("=\"").append(linkSizeId).append("\""); // NOI18N
203
}
204     }
205     
206     /**
207      * Saves group/interval alignemnt.
208      *
209      * @param alignemnt alignment to save.
210      * @param group determines whether it is a group alignment.
211      */

212     private void saveAlignment(int alignment, boolean group) {
213         String JavaDoc attrPrefix = " " + (group ? ATTR_GROUP_ALIGNMENT : ATTR_ALIGNMENT) + "=\""; // NOI18N
214
if (humanReadable) {
215             if (alignment != DEFAULT) {
216                 sb.append(attrPrefix);
217                 switch (alignment) {
218                     case LEADING: sb.append(VALUE_ALIGNMENT_LEADING); break;
219                     case TRAILING: sb.append(VALUE_ALIGNMENT_TRAILING); break;
220                     case CENTER: sb.append(VALUE_ALIGNMENT_CENTER); break;
221                     case BASELINE: sb.append(VALUE_ALIGNMENT_BASELINE); break;
222                     default: assert false;
223                 }
224                 sb.append("\""); // NOI18N
225
}
226         } else {
227             if (alignment != DEFAULT) {
228                 sb.append(attrPrefix).append(alignment).append("\""); // NOI18N
229
}
230         }
231     }
232     
233     /**
234      * Saves size parameter of some layout interval.
235      *
236      * @param size value of the size parameter.
237      * @param attr name of the size parameter.
238      */

239     private void saveSize(int size, String JavaDoc attr) {
240         String JavaDoc attrPrefix = " " + attr + "=\""; // NOI18N
241
if (humanReadable) {
242             if (size != NOT_EXPLICITLY_DEFINED) {
243                 sb.append(attrPrefix);
244                 if (size == USE_PREFERRED_SIZE) {
245                     sb.append(VALUE_SIZE_PREFERRED);
246                 } else {
247                     if (size == Short.MAX_VALUE) {
248                         sb.append(VALUE_SIZE_MAX);
249                     } else {
250                         sb.append(size);
251                     }
252                 }
253                 sb.append("\""); // NOI18N
254
}
255         } else {
256             if (size != NOT_EXPLICITLY_DEFINED) {
257                 sb.append(attrPrefix).append(size).append("\""); // NOI18N
258
}
259         }
260     }
261
262     /**
263      * Saves attributes of some layout interval.
264      *
265      * @param attributes attributes of some layout interval.
266      */

267     private void saveAttributes(int attributes) {
268         if (!humanReadable)
269             attributes &= LayoutInterval.ATTR_PERSISTENT_MASK;
270         sb.append(' ').append(ATTR_ATTRIBUTES).append("=\""); // NOI18N
271
sb.append(attributes).append("\""); // NOI18N
272
}
273     
274     /**
275      * Performs indentation.
276      *
277      * @return indented <code>StringBuffer</code>.
278      */

279     private StringBuffer JavaDoc indent() {
280         char[] spaces = new char[2*indent];
281         Arrays.fill(spaces, ' ');
282         return sb.append(spaces);
283     }
284     
285     /**
286      * Loads the layout of the given container. Does not load containers
287      * recursively, is called for each container separately.
288      *
289      * @param rootId ID of the layout root (the container whose layout should be loaded).
290      * @param dimLayoutList nodes holding the information about the layout.
291      * @param nameToIdMap map from component names to component IDs.
292      */

293     void loadModel(String JavaDoc rootId, NodeList dimLayoutList, Map nameToIdMap)
294         throws java.io.IOException JavaDoc
295     {
296         this.idNameMap = nameToIdMap;
297         resetMissingName(); // prepare for error recovery
298
LayoutComponent root = layoutModel.getLayoutComponent(rootId);
299         if (root == null) {
300             root = new LayoutComponent(rootId, true);
301             layoutModel.addRootComponent(root);
302         }
303         this.root = root;
304
305         for (int i=0; i<dimLayoutList.getLength(); i++) {
306             Node dimLayoutNode = dimLayoutList.item(i);
307             if (!(dimLayoutNode instanceof Element))
308                 continue;
309             Node dimAttrNode = dimLayoutNode.getAttributes().getNamedItem(ATTR_DIMENSION_DIM);
310             dimension = integerFromNode(dimAttrNode);
311             LayoutInterval dimLayoutInterval = root.getLayoutRoot(dimension);
312             NodeList childs = dimLayoutNode.getChildNodes();
313             for (int j=0; j<childs.getLength(); j++) {
314                 Node node = childs.item(j);
315                 if (node instanceof Element) {
316                     loadGroup(dimLayoutInterval, node, dimension);
317                     break;
318                 }
319             }
320         }
321
322         correctMissingName(); // recover from missing component name if needed
323
}
324
325     /**
326      * Loads layout of the given group.
327      *
328      * @param group group whose layout information should be loaded.
329      * @param groupNode node holding the information about the layout of the group.
330      */

331     private void loadGroup(LayoutInterval group, Node groupNode, int dimension)
332         throws java.io.IOException JavaDoc
333     {
334         NamedNodeMap attrMap = groupNode.getAttributes();
335         Node alignmentNode = attrMap.getNamedItem(ATTR_ALIGNMENT);
336         Node groupAlignmentNode = attrMap.getNamedItem(ATTR_GROUP_ALIGNMENT);
337         Node minNode = attrMap.getNamedItem(ATTR_SIZE_MIN);
338         Node maxNode = attrMap.getNamedItem(ATTR_SIZE_MAX);
339         int alignment = (alignmentNode == null) ? DEFAULT : integerFromNode(alignmentNode);
340         group.setAlignment(alignment);
341         if (group.isParallel()) {
342             int groupAlignment = (groupAlignmentNode == null) ? DEFAULT : integerFromNode(groupAlignmentNode);
343             if (groupAlignment != DEFAULT) {
344                 group.setGroupAlignment(groupAlignment);
345             }
346         }
347         int min = (minNode == null) ? NOT_EXPLICITLY_DEFINED : integerFromNode(minNode);
348         int max = (maxNode == null) ? NOT_EXPLICITLY_DEFINED : integerFromNode(maxNode);
349         group.setMinimumSize(min);
350         group.setMaximumSize(max);
351         loadAttributes(group, attrMap);
352         NodeList subNodes = groupNode.getChildNodes();
353         for (int i=0; i<subNodes.getLength(); i++) {
354             Node subNode = subNodes.item(i);
355             if (!(subNode instanceof Element))
356                 continue;
357             String JavaDoc nodeName = subNode.getNodeName();
358             if (XML_GROUP.equals(nodeName)) {
359                 Node typeNode = subNode.getAttributes().getNamedItem(ATTR_GROUP_TYPE);
360                 int type = integerFromNode(typeNode);
361                 LayoutInterval subGroup = new LayoutInterval(type);
362                 group.add(subGroup, -1);
363                 loadGroup(subGroup, subNode, dimension);
364             } else if (XML_EMPTY_SPACE.equals(nodeName)) {
365                 loadEmptySpace(group, subNode);
366             } else {
367                 assert XML_COMPONENT.equals(nodeName);
368                 loadComponent(group, subNode, dimension);
369             }
370         }
371         if (dimension == VERTICAL)
372             checkAndFixGroup(group);
373     }
374
375     /**
376      * Loads information about empty space.
377      *
378      * @param parent layout parent of the empty space.
379      * @param spaceNode node with the information about the empty space.
380      */

381     private void loadEmptySpace(LayoutInterval parent, Node spaceNode) {
382         LayoutInterval space = new LayoutInterval(SINGLE);
383         NamedNodeMap attrMap = spaceNode.getAttributes();
384         loadSizes(space, attrMap);
385         loadAttributes(space, attrMap);
386         parent.add(space, -1);
387     }
388     
389     /**
390      * Loads information about component.
391      *
392      * @param parent layout parent of the loaded layout interval.
393      * @param componentNode node with the information about the component.
394      * @param dimension loaded dimension
395      */

396     private void loadComponent(LayoutInterval parent, Node componentNode, int dimension)
397         throws java.io.IOException JavaDoc
398     {
399         NamedNodeMap attrMap = componentNode.getAttributes();
400         String JavaDoc name = attrMap.getNamedItem(ATTR_COMPONENT_ID).getNodeValue();
401         Node linkSizeId = attrMap.getNamedItem(ATTR_LINK_SIZE);
402         String JavaDoc id = (String JavaDoc)idNameMap.get(name);
403         if (id == null) { // try to workaround the missing name error (issue 77092)
404
id = useTemporaryId(name);
405         }
406         Node alignmentNode = attrMap.getNamedItem(ATTR_ALIGNMENT);
407         int alignment = (alignmentNode == null) ? DEFAULT : integerFromNode(alignmentNode);
408         LayoutComponent layoutComponent = layoutModel.getLayoutComponent(id);
409         if (layoutComponent == null) {
410             layoutComponent = new LayoutComponent(id, false /*PENDING*/);
411         }
412         if (layoutComponent.getParent() == null) {
413             layoutModel.addComponent(layoutComponent, root, -1);
414         }
415         LayoutInterval interval = layoutComponent.getLayoutInterval(dimension);
416         interval.setAlignment(alignment);
417         if (linkSizeId != null) {
418             layoutModel.addComponentToLinkSizedGroup(integerFromNode(linkSizeId), layoutComponent.getId(), dimension);
419         }
420         loadSizes(interval, attrMap);
421         loadAttributes(interval, attrMap);
422         parent.add(interval, -1);
423     }
424     
425     /**
426      * Loads size information of the given interval.
427      *
428      * @param interval layout interval whose size information should be loaded.
429      * @param attrMap map with size information.
430      */

431     private void loadSizes(LayoutInterval interval, org.w3c.dom.NamedNodeMap JavaDoc attrMap) {
432         Node minNode = attrMap.getNamedItem(ATTR_SIZE_MIN);
433         Node prefNode = attrMap.getNamedItem(ATTR_SIZE_PREF);
434         Node maxNode = attrMap.getNamedItem(ATTR_SIZE_MAX);
435         int min = (minNode == null) ? NOT_EXPLICITLY_DEFINED : integerFromNode(minNode);
436         int pref = (prefNode == null) ? NOT_EXPLICITLY_DEFINED : integerFromNode(prefNode);
437         int max = (maxNode == null) ? NOT_EXPLICITLY_DEFINED : integerFromNode(maxNode);
438         interval.setSizes(min, pref, max);
439     }
440     
441     /**
442      * Loads attributes of the given interval.
443      *
444      * @param interval layout interval whose attributes should be loaded.
445      * @param attrMap map with attribute information.
446      */

447     private void loadAttributes(LayoutInterval interval, org.w3c.dom.NamedNodeMap JavaDoc attrMap) {
448         Node attributesNode = attrMap.getNamedItem(ATTR_ATTRIBUTES);
449         int attributes = 0;
450         if (attributesNode != null) {
451             attributes = integerFromNode(attributesNode);
452             attributes &= LayoutInterval.ATTR_PERSISTENT_MASK;
453         }
454         interval.setAttributes(attributes);
455     }
456     
457     /**
458      * Extracts integer value from the given node.
459      *
460      * @param node node that has integer as its value.
461      * @return integer value extracted from the given node.
462      */

463     private static int integerFromNode(Node node) {
464         String JavaDoc nodeStr = node.getNodeValue();
465         return Integer.parseInt(nodeStr);
466     }
467
468     // -----
469
// error recovery
470

471     /**
472      * This method is used during loading to check the alignment validity of
473      * given group and its subintervals. It checks use of BASELINE alignment of
474      * the group and the subintervals. Some invalid combinations were allowed by
475      * GroupLayout in version 1.0. See issue 78035 for details. This method also
476      * fixes the invalid combinations and sets the 'corrected' flag. This is
477      * needed for loading because wrong layouts still might exist from the past
478      * (and we still can't quite exclude it can't be created even now).
479      * BASELINE group can only contain BASELINE intervals, and vice versa,
480      * BASELINE interval can only be placed in BASELINE group.
481      * BASELINE can be set only on individual components.
482      * LEADING, TRAILING and CENTER alignments can be combined freely.
483      */

484     void checkAndFixGroup(LayoutInterval group) {
485         if (group.isParallel()) {
486             int groupAlign = group.getGroupAlignment();
487             int baselineCount = 0;
488
489             Iterator iter = group.getSubIntervals();
490             while (iter.hasNext()) {
491                 LayoutInterval subInterval = (LayoutInterval)iter.next();
492                 if (subInterval.getAlignment() == BASELINE) {
493                     if (!subInterval.isComponent()) {
494                         subInterval.setAlignment(groupAlign == BASELINE ? LEADING : DEFAULT);
495                         layoutModel.setCorrected();
496                         System.err.println("WARNING: Invalid use of BASELINE [1], corrected automatically"); // NOI18N
497
}
498                     else baselineCount++;
499                 }
500             }
501
502             if (baselineCount > 0) {
503                 if (baselineCount < group.getSubIntervalCount()) {
504                     // separate baseline intervals to a subgroup
505
LayoutInterval subGroup = new LayoutInterval(PARALLEL);
506                     subGroup.setGroupAlignment(BASELINE);
507                     for (int i=0; i < group.getSubIntervalCount(); ) {
508                         LayoutInterval subInterval = group.getSubInterval(i);
509                         if (subInterval.getAlignment() == BASELINE) {
510                             group.remove(i);
511                             subGroup.add(subInterval, -1);
512                         }
513                         else i++;
514                     }
515                     if (groupAlign == BASELINE) {
516                         group.setGroupAlignment(LEADING);
517                     }
518                     group.add(subGroup, -1);
519                     layoutModel.setCorrected();
520                     System.err.println("WARNING: Invalid use of BASELINE [2], corrected automatically"); // NOI18N
521
}
522                 else if (groupAlign != BASELINE) {
523                     group.setGroupAlignment(BASELINE);
524                     layoutModel.setCorrected();
525                     System.err.println("WARNING: Invalid use of BASELINE [3], corrected automatically"); // NOI18N
526
}
527             }
528             else if (groupAlign == BASELINE && group.getSubIntervalCount() > 0) {
529                 group.setGroupAlignment(LEADING);
530                 layoutModel.setCorrected();
531                 System.err.println("WARNING: Invalid use of BASELINE [4], corrected automatically"); // NOI18N
532
}
533         }
534     }
535
536     // The following code tries to fix a missing component name in the layout
537
// definition. Due to a bug (see issues 77092, 76749) it may happen that
538
// one component in the layout XML has "null" or duplicate name (while it is
539
// correct in the metacomponent). If it is the only wrong name in the
540
// container then it can be deduced from the map provided for converting
541
// names to IDs (it is the only ID not used).
542

543     private final String JavaDoc TEMPORARY_ID = "<temp_id>"; // NOI18N
544
private String JavaDoc missingNameH;
545     private String JavaDoc missingNameV;
546
547     private void resetMissingName() {
548         missingNameH = missingNameV = null;
549     }
550
551     private String JavaDoc useTemporaryId(String JavaDoc name) throws java.io.IOException JavaDoc {
552         if (dimension == HORIZONTAL) {
553             if (missingNameH == null && (missingNameV == null || missingNameV.equals(name))) {
554                 missingNameH = name;
555                 return TEMPORARY_ID;
556             }
557         }
558         else if (dimension == VERTICAL) {
559             if (missingNameV == null && (missingNameH == null || missingNameH.equals(name))) {
560                 missingNameV = name;
561                 return TEMPORARY_ID;
562             }
563         }
564         throw new java.io.IOException JavaDoc("Undefined component referenced in layout: "+name); // NOI18N
565
}
566
567     private void correctMissingName() throws java.io.IOException JavaDoc {
568         if (missingNameH == null && missingNameV == null)
569             return; // no problem
570

571         if (missingNameH != null && missingNameV != null && missingNameH.equals(missingNameV)
572             && idNameMap.size() == root.getSubComponentCount())
573         { // we have one unknown name in each dimension, let's infer it from the idNameMap
574
for (Map.Entry<String JavaDoc, String JavaDoc> e : idNameMap.entrySet()) { // name -> id
575
String JavaDoc id = e.getValue();
576                 LayoutComponent comp = layoutModel.getLayoutComponent(id);
577                 if (comp == null) { // this is the missing id, set it to the temporary component
578
comp = layoutModel.getLayoutComponent(TEMPORARY_ID);
579                     layoutModel.changeComponentId(comp, id);
580                 }
581                 else if (comp.getParent() == null) { // already loaded as a container
582
// replace the temporary component
583
LayoutComponent tempComp = layoutModel.getLayoutComponent(TEMPORARY_ID);
584                     layoutModel.replaceComponent(tempComp, comp);
585                 }
586                 else continue;
587
588                 layoutModel.setCorrected();
589                 System.err.println("WARNING: Invalid component name in layout: "+missingNameH // NOI18N
590
+", corrected automatically to: "+e.getKey()); // NOI18N
591
resetMissingName();
592                 return;
593             }
594         }
595
596         layoutModel.removeComponent(TEMPORARY_ID, true);
597         resetMissingName();
598         throw new java.io.IOException JavaDoc("Undefined component referenced in layout: " // NOI18N
599
+ (missingNameH != null ? missingNameH : missingNameV));
600     }
601 }
602
Popular Tags