View Javadoc

1   /***
2    * Copyright (C) 2008 Jeremy Thomerson (jthomerson@users.sourceforge.net)
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  
17  package net.sf.vcaperture.web.models;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.wicket.model.IModel;
23  
24  public class AppendingStringModel implements IModel {
25  
26  	private static final long serialVersionUID = 1L;
27  
28  	private final List<IModel> mModels = new ArrayList<IModel>();
29  	private final String mSeparator;
30  
31  	public AppendingStringModel(IModel/* <String> */model1, IModel/* <String> */model2, String separator) {
32  		this(separator);
33  		mModels.add(model1);
34  		mModels.add(model2);
35  	}
36  
37  	private AppendingStringModel(List<IModel> models, String separator) {
38  		this(separator);
39  		for (IModel model : models) {
40  			addModel(model);
41  		}
42      }
43  
44  	public AppendingStringModel(String separator) {
45  		mSeparator = separator;
46      }
47  
48  	public Object getObject() {
49  		StringBuffer sb = new StringBuffer();
50  		for (IModel/* <String> */model : mModels) {
51  			sb.append(model.getObject());
52  			sb.append(mSeparator);
53  		}
54  		return sb.length() == 0 ? "" : sb.substring(0, sb.length() - mSeparator.length()).toString();
55  	}
56  
57  	public void setObject(Object object) {
58  		throw new UnsupportedOperationException("not supported");
59  	}
60  
61  	public void addModel(IModel model) {
62  		mModels.add(model);
63  	}
64  
65  	public void detach() {
66  		for (IModel/* <String> */model : mModels) {
67  			model.detach();
68  		}
69  	}
70  
71  	public AppendingStringModel copy() {
72  	    return new AppendingStringModel(mModels, mSeparator);
73      }
74  
75  }