View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.tree.cell;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.beanutils.BeanUtils;
21  
22  import org.apache.commons.lang.StringUtils;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.extremecomponents.table.cell.BaseCell;
28  
29  import org.extremecomponents.table.model.ParameterRegistry;
30  import org.extremecomponents.table.model.TableModelUtils;
31  
32  import org.extremecomponents.tree.bean.TreeNode;
33  
34  import org.extremecomponents.tree.model.TreeModel;
35  import org.extremecomponents.tree.model.TreeModelUtils;
36  
37  import org.extremecomponents.util.HtmlBuilder;
38  
39  
40  /***
41   * Will generate a simple cell to display.
42   *
43   * @author Jeff Johnston
44   */
45  public class TreeCell extends BaseCell
46  {
47  	private static Log logger = LogFactory.getLog(TreeCell.class);
48  	public static final String PLUS_IMAGE = "plus";
49  	public static final String MINUS_IMAGE = "minus";
50  	String plus;
51  	String minus;
52  
53  	public String html()
54  	{
55  		HtmlBuilder html = new HtmlBuilder();
56  
57  		html.append(startTD());
58  
59  		Object value = column.getValue();
60  
61  		if ((value != null) && StringUtils.isNotEmpty(value.toString()))
62  		{
63  			try
64  			{
65  				TreeModel treeModel = (TreeModel) model;
66  				TreeNode node = (TreeNode) value;
67  				html.append(buildNodeCell(treeModel, node));
68  			}
69  			catch (Exception e)
70  			{
71  				logger.error("TreeCell.html() Problem", e);
72  			}
73  		}
74  		else
75  		{
76  			html.append(" ");
77  		}
78  
79  		html.append(endTD());
80  
81  		return html.toString();
82  	}
83  
84  	public String value()
85  	{
86  		Object obj = column.getValue();
87  
88  		if ((obj != null) && StringUtils.isNotBlank(obj.toString()))
89  		{
90  			return TableModelUtils.parseCell(obj.toString());
91  		}
92  
93  		return "";
94  	}
95  
96  	private String buildNodeCell(TreeModel model, TreeNode node) throws Exception
97  	{
98  		HtmlBuilder html = new HtmlBuilder();
99  
100 		String val = null;
101 
102 		if (StringUtils.isNotBlank(model.getExtendedValue()))
103 		{
104 			val = model.getExtendedValue();
105 		}
106 		else if (StringUtils.isNotBlank(column.getProperty()))
107 		{
108 			val = BeanUtils.getProperty(node, column.getProperty());
109 		}
110 
111 		if (StringUtils.isBlank(val))
112 		{
113 			val = "";
114 		}
115 
116 		plus = model.getTable().getImage(PLUS_IMAGE);
117 		minus = model.getTable().getImage(MINUS_IMAGE);
118 
119 		html.table(0).cellPadding("0").cellSpacing("0").border("0").close().tr(1).close();
120 
121 		for (int i = 0; i < node.getDepth(); i++)
122 		{
123 			html.td(2).close().nbsp().nbsp().nbsp().tdEnd();
124 		}
125 
126 		if ((node.getChildren() != null) && (node.getChildren().size() > 0))
127 		{
128 			buildLink(model, node, val, html);
129 		}
130 		else
131 		{
132 			html.td(2).close().nbsp().nbsp().nbsp().tdEnd(); //Image buffer
133 			html.td(2).close().nbsp().append(val).tdEnd();
134 		}
135 
136 		html.trEnd(1).tableEnd(0);
137 
138 		return html.toString();
139 	}
140 
141 	private void buildLink(TreeModel model, TreeNode node, String val, HtmlBuilder html) throws Exception
142 	{
143 		String id = BeanUtils.getProperty(node, model.getIdentifier());
144 
145 		html.td(2).close();
146 
147 		html.a().quote();
148 
149 		String action = model.getTable().getAction();
150 
151 		if (StringUtils.isNotEmpty(action))
152 		{
153 			html.append(action);
154 		}
155 
156 		html.question().append(model.getRegistry().getURLParameterString(true, false, false));
157 
158 		Map openNodes = model.getOpenNodes();
159 		Object[] keys = openNodes.keySet().toArray();
160 		String openKey = TreeModelUtils.getNodeKey(model, id);
161 
162 		for (int i = 0; i < keys.length; i++)
163 		{
164 			if (keys[i].equals(openKey))
165 			{
166 				continue;
167 			}
168 
169 			html.ampersand().append(keys[i]).equals().append("true");
170 		}
171 
172 		html.append(model.getRegistry().getParameterString(ParameterRegistry.SORT));
173 
174 		if (node.isOpen())
175 		{
176 			html.quote().title("Close").close();
177 			html.img(minus).aEnd();
178 		}
179 		else
180 		{
181 			html.ampersand().append(openKey).equals().append("true");
182 			html.quote().title("Open").close();
183 			html.img(plus).aEnd();
184 		}
185 
186 		html.tdEnd().td(2).close().nbsp().append(val).tdEnd();
187 	}
188 }