001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.extensions.markup.html.tree.table;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import javax.swing.tree.TreeNode;
023:
024: import org.apache.wicket.Component;
025: import org.apache.wicket.RequestCycle;
026: import org.apache.wicket.Response;
027: import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Alignment;
028: import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Unit;
029: import org.apache.wicket.markup.MarkupStream;
030: import org.apache.wicket.markup.html.WebMarkupContainer;
031:
032: /**
033: * Class that renders cell of columns aligned to the left or to the right.
034: *
035: * @author Matej Knopp
036: */
037: final class SideColumnsView extends WebMarkupContainer {
038: private static final long serialVersionUID = 1L;
039:
040: private final List columns = new ArrayList();
041:
042: private final List components = new ArrayList();
043:
044: private TreeNode node;
045:
046: private final List renderables = new ArrayList();
047:
048: /**
049: * Constructor.
050: *
051: * @param id
052: * The component id
053: * @param node
054: * The tree node
055: */
056: public SideColumnsView(String id, TreeNode node) {
057: super (id);
058: setRenderBodyOnly(true);
059: this .node = node;
060: }
061:
062: /**
063: * Adds a column to be rendered.
064: *
065: * @param column
066: * The column to add
067: * @param component
068: * The component
069: * @param renderable
070: * The renderer
071: */
072: public void addColumn(IColumn column, Component component,
073: IRenderable renderable) {
074: if (column.isVisible()) {
075: // if the column is aligned to the left, just append it.
076: // Otherwise we prepend it, because we want columns aligned to right
077: // to be rendered reverse order (because they will have set
078: // float:right
079: // in css, so they will be displayed in reverse order too).
080: if (column.getLocation().getAlignment() == Alignment.LEFT) {
081: columns.add(column);
082: components.add(component);
083: renderables.add(renderable);
084: } else {
085: columns.add(0, column);
086: components.add(0, component);
087: renderables.add(0, renderable);
088: }
089: }
090: }
091:
092: /**
093: * Renders the columns.
094: *
095: * @param markupStream
096: * The markup stream of this component
097: */
098: protected void onRender(final MarkupStream markupStream) {
099: final int markupStart = markupStream.getCurrentIndex();
100: Response response = RequestCycle.get().getResponse();
101:
102: boolean firstLeft = true; // whether there was no left column rendered
103: // yet
104: boolean rendered = false;
105:
106: for (int i = 0; i < columns.size(); ++i) {
107: IColumn column = (IColumn) columns.get(i);
108: Component component = (Component) components.get(i);
109: IRenderable renderable = (IRenderable) renderables.get(i);
110:
111: // write wrapping markup
112: response.write("<span class=\"b_\" style=\""
113: + renderColumnStyle(column) + "\">");
114: if (column.getLocation().getAlignment() == Alignment.LEFT
115: && firstLeft == true) {
116: // for the first left column we have different style class
117: // (without the left border)
118: response.write("<span class=\"d_\">");
119: firstLeft = false;
120: } else {
121: response.write("<span class=\"c_\">");
122: }
123:
124: if (component != null) {
125: markupStream.setCurrentIndex(markupStart);
126: component.render(markupStream);
127: rendered = true;
128: } else if (renderable != null) {
129: renderable.render(node, response);
130: } else {
131: throw new IllegalStateException(
132: "Either renderable or cell component must be created for this noode");
133: }
134:
135: response.write("</span></span>\n");
136: }
137:
138: // if no component was rendered just advance in the markup stream
139: if (rendered == false) {
140: markupStream.skipComponent();
141: }
142: }
143:
144: /**
145: * Renders the float css atribute of the given column.
146: *
147: * @param column
148: * The
149: * @return The column as a string
150: */
151: private String renderColumnFloat(IColumn column) {
152: ColumnLocation location = column.getLocation();
153: if (location.getAlignment() == Alignment.LEFT) {
154: return "left";
155: } else if (location.getAlignment() == Alignment.RIGHT) {
156: return "right";
157: } else {
158: throw new IllegalStateException("Wrong column allignment.");
159: }
160: }
161:
162: /**
163: * Renders content of the style attribute for the given column.
164: *
165: * @param column
166: * The column to render the style attribute from
167: * @return The style as a string
168: */
169: private String renderColumnStyle(IColumn column) {
170: return "width:" + renderColumnWidth(column) + ";float:"
171: + renderColumnFloat(column);
172: }
173:
174: /**
175: * Renders width of given column as string.
176: *
177: * @param column
178: * The column to render as a string
179: * @return The column as a string
180: */
181: private String renderColumnWidth(IColumn column) {
182: ColumnLocation location = column.getLocation();
183: return "" + location.getSize() + renderUnit(location.getUnit());
184: }
185:
186: /**
187: * Renders given unit as string.
188: *
189: * @param unit
190: * The unit to render to a string
191: * @return The unit as a string
192: */
193: private String renderUnit(Unit unit) {
194: if (unit == Unit.EM) {
195: return "em";
196: } else if (unit == Unit.PX) {
197: return "px";
198: } else if (unit == Unit.PERCENT) {
199: return "%";
200: } else {
201: throw new IllegalStateException(
202: "Wrong column unit for column aligned left or right.");
203: }
204: }
205:
206: }
|