001: // WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.swing.layouts;
019:
020: import java.awt.*;
021:
022: import javax.swing.*;
023:
024: import net.ar.webonswing.*;
025: import net.ar.webonswing.render.templates.*;
026: import net.ar.webonswing.render.templates.html.*;
027:
028: public class PropagateTemplateLayoutByName extends TemplateLayout
029: implements LayoutManager2 {
030: protected LayoutManager theOriginalLayout;
031: protected boolean completed;
032:
033: public PropagateTemplateLayoutByName(KeyPositionTemplate aTemplate) {
034: this (aTemplate, true);
035: }
036:
037: public PropagateTemplateLayoutByName(KeyPositionTemplate aTemplate,
038: boolean isCompleted) {
039: super (aTemplate);
040:
041: completed = isCompleted;
042: }
043:
044: public PropagateTemplateLayoutByName(KeyPositionTemplate aTemplate,
045: LayoutManager anOriginalLayout, boolean isCompleted) {
046: this (aTemplate, isCompleted);
047:
048: theOriginalLayout = anOriginalLayout;
049: }
050:
051: public void layoutContainer(Container aParent) {
052: KeyPositionHtmlTemplate theHtmlTemplate = (KeyPositionHtmlTemplate) theTemplate;
053: Component[] components = aParent.getComponents();
054:
055: for (int i = 0; i < components.length; i++) {
056: boolean tagFound = false;
057: Container theOriginalComponent = null;
058: Container theComponent = (Container) components[i];
059: theComponent.setBounds(0, 0, 0, 0);
060:
061: String theName = theComponent.getName();
062:
063: if (theName == null || theName.equals("")) {
064: theOriginalComponent = theComponent;
065: theComponent = (JComponent) theComponent
066: .getComponent(0);
067:
068: if (theComponent instanceof JViewport)
069: theComponent = (JComponent) ((JViewport) theComponent)
070: .getView();
071: else
072: theOriginalComponent
073: .setLayout(new TemplateFlowLayout());
074:
075: theOriginalComponent.setName(theComponent.getName());
076:
077: theName = theComponent.getName();
078: }
079:
080: if (theName != null) {
081: LayoutManager theLayout = theComponent.getLayout();
082: boolean replaceGridLayout = theLayout instanceof GridLayout
083: && (((GridLayout) theLayout).getRows() == 0 || ((GridLayout) theLayout)
084: .getColumns() == 0);
085:
086: if (replaceGridLayout
087: || theLayout instanceof TemplateFlowLayout) {
088: KeyPositionHtmlTemplate theClonedTemplate = new KeyPositionHtmlTemplate(
089: ((KeyPositionHtmlTemplate) theTemplate)
090: .getClonedSubTemplate(theComponent
091: .getName()));
092: theComponent
093: .setLayout(new PropagateTemplateLayoutByName(
094: theClonedTemplate,
095: new TemplateFlowLayout(), completed));
096: } else {
097: HtmlTemplate theSubTemplate = null;
098:
099: if (theName == null || theName.trim().equals(""))
100: theSubTemplate = theHtmlTemplate;
101: else {
102: tagFound = theHtmlTemplate
103: .getSubTemplate(theName) != null;
104: if (tagFound || completed)
105: theSubTemplate = theHtmlTemplate
106: .getClonedSubTemplate(theName);
107: }
108:
109: if ((completed || tagFound)
110: && theSubTemplate != null
111: && theSubTemplate.getElementCount() > 0
112: && !(theLayout instanceof PropagateTemplateLayoutByName))
113: try {
114: theComponent
115: .setLayout(new PropagateTemplateLayoutByName(
116: new KeyPositionHtmlTemplate(
117: theSubTemplate),
118: completed));
119: } catch (Exception e) {
120: }
121: }
122: }
123:
124: if (theOriginalComponent != null)
125: theComponent = theOriginalComponent;
126:
127: if ((theComponent instanceof JComponent) && theName != null
128: && !theName.equals("") && (completed || tagFound))
129: theHtmlTemplate.addKey(theName, WosFramework
130: .getInstance().getHierarchyWrapper()
131: .getComponentWrapper(theComponent));
132: }
133:
134: if (theOriginalLayout != null)
135: aParent.setLayout(theOriginalLayout);
136: else
137: aParent.setLayout(new TemplateLayout(theTemplate));
138: }
139:
140: public float getLayoutAlignmentX(Container aTarget) {
141: return 0;
142: }
143:
144: public float getLayoutAlignmentY(Container aTarget) {
145: return 0;
146: }
147:
148: public void invalidateLayout(Container aTarget) {
149: }
150:
151: public Dimension maximumLayoutSize(Container aTarget) {
152: return new Dimension(640, 480);
153: }
154:
155: public void addLayoutComponent(Component aComponent,
156: Object aConstraints) {
157: }
158: }
|