View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: PropertyTreeEntry.java
13   * Created: 2009-08-04
14   * Author: kpietak
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.ui.dialogs.property;
19  
20  import java.util.Collection;
21  import java.util.Comparator;
22  import java.util.Map;
23  import java.util.SortedMap;
24  import java.util.TreeMap;
25  
26  import pl.edu.agh.cast.data.model.property.MetaProperty;
27  import pl.edu.agh.cast.data.model.property.Property;
28  
29  /**
30   * Class which describes single entry in properties tree. It contains one of the two following fields:
31   * <ul>
32   * <li><code>label</code> when describe category entry such as 'visual data', etc.; such entries are not editable,
33   * <li><code>property</code> when describe element's property with value; such entries types depends on property type
34   * </ul>
35   * One and the only one field from above list is not null.
36   *
37   * @author AGH CAST Team
38   */
39  public final class PropertyTreeEntry {
40  
41  	private String id;
42  
43  	private PropertyEditType type;
44  
45  	private String label;
46  
47  	private boolean isMarkAsLabel;
48  
49  	private Property<? extends MetaProperty> property;
50  
51  	private PropertyTreeEntry parent;
52  
53  	private SortedMap<String, PropertyTreeEntry> children;
54  
55  	/**
56  	 * Enumeration of properties entries edition types.
57  	 *
58  	 * @author AGH CAST Team
59  	 */
60  	public enum PropertyEditType {
61  		CUSTOM_EDITABLE, CUSTOM_NOT_EDITABLE, EDITABLE, NOT_EDITABLE, CATEGORY
62  	}
63  
64  	/**
65  	 * Alphabetic string comparator.
66  	 *
67  	 * @author AGH CAST Team
68  	 */
69  	class AlphabeticComparator implements Comparator<String> {
70  		public int compare(String o1, String o2) {
71  			return o1.toLowerCase().compareTo(o2.toLowerCase());
72  		}
73  
74  	}
75  
76  	// BEGIN Constructors
77  
78  	/**
79  	 * Constructor for entry with label. The type is set to {@link PropertyEditType#CATEGORY}.
80  	 *
81  	 * @param id
82  	 *            entry id
83  	 * @param label
84  	 *            entry label
85  	 */
86  	public PropertyTreeEntry(String id, String label) {
87  		this(id, PropertyEditType.CATEGORY);
88  		if (label == null) {
89  			throw new IllegalArgumentException();
90  		}
91  		this.label = label;
92  		this.property = null;
93  	}
94  
95  	/**
96  	 * Constructor for entry with property. The type is based on property type.
97  	 *
98  	 * @param id
99  	 *            entry id
100 	 * @param property
101 	 *            property in this entry
102 	 * @param type
103 	 *            entry type based on property type
104 	 */
105 	public PropertyTreeEntry(String id, Property<? extends MetaProperty> property, PropertyEditType type) {
106 		this(id, type);
107 		if (property == null) {
108 			throw new IllegalArgumentException();
109 		}
110 		this.label = null;
111 		this.property = property;
112 	}
113 
114 	/**
115 	 * Constructor for entry with label which sets also parent entry.
116 	 *
117 	 * @param id
118 	 *            entry id
119 	 * @param label
120 	 *            entry label
121 	 * @param parent
122 	 *            parent entry
123 	 * @see PropertyTreeEntry#PropertyTreeEntry(String, String)
124 	 */
125 	public PropertyTreeEntry(String id, String label, PropertyTreeEntry parent) {
126 		this(id, label);
127 		setParent(parent);
128 	}
129 
130 	/**
131 	 * Constructor for entry with property which sets also parent entry.
132 	 *
133 	 * @param id
134 	 *            entry id
135 	 * @param property
136 	 *            property in this entry
137 	 * @param type
138 	 *            entry type based on property type
139 	 * @param parent
140 	 *            parent entry
141 	 * @see PropertyTreeEntry#PropertyTreeEntry(String, Property, PropertyEditType)
142 	 */
143 	public PropertyTreeEntry(String id, Property<? extends MetaProperty> property, PropertyEditType type,
144 	        PropertyTreeEntry parent) {
145 		this(id, property, type);
146 		setParent(parent);
147 	}
148 
149 	private PropertyTreeEntry(String id, PropertyEditType type) {
150 		if (id == null || type == null) {
151 			throw new IllegalArgumentException();
152 		}
153 		this.id = id;
154 		this.type = type;
155 		children = new TreeMap<String, PropertyTreeEntry>(new AlphabeticComparator());
156 		isMarkAsLabel = false;
157 	}
158 
159 	// END Constructors
160 
161 	// BEGIN Getters & setters
162 
163 	public String getId() {
164 		return id;
165 	}
166 
167 	public PropertyEditType getType() {
168 		return type;
169 	}
170 
171 	public String getLabel() {
172 		return label;
173 	}
174 
175 	public boolean isMarkAsLabel() {
176 		return isMarkAsLabel;
177 	}
178 
179 	public void setIsMarkAsLabel(boolean isMarkAsLabel) {
180 		this.isMarkAsLabel = isMarkAsLabel;
181 	}
182 
183 	public Property<? extends MetaProperty> getProperty() {
184 		return property;
185 	}
186 
187 	public void setParent(PropertyTreeEntry parent) {
188 		this.parent = parent;
189 	}
190 
191 	public PropertyTreeEntry getParent() {
192 		return parent;
193 	}
194 
195 	public Collection<PropertyTreeEntry> getChildren() {
196 		return children.values();
197 	}
198 
199 	// END Getters & setters
200 
201 	// BEGIN Children management methods
202 
203 	/**
204 	 * Add child {@link PropertyTreeEntry}. Sets parent in child entry as this.
205 	 *
206 	 * @param entry
207 	 *            child entry to add
208 	 */
209 	public void addChild(PropertyTreeEntry entry) {
210 		if (entry == null) {
211 			throw new IllegalArgumentException();
212 		}
213 		children.put(entry.getId(), entry);
214 		entry.setParent(this);
215 	}
216 
217 	/**
218 	 * Adds children {@link PropertyTreeEntry} entries. Sets parent in all children as this.
219 	 *
220 	 * @param childrenToAdd
221 	 *            children entries to add
222 	 */
223 	public void addAllChildren(Map<String, PropertyTreeEntry> childrenToAdd) {
224 		if (childrenToAdd == null) {
225 			throw new IllegalArgumentException();
226 		}
227 		this.children.putAll(childrenToAdd);
228 		for (PropertyTreeEntry entry : childrenToAdd.values()) {
229 			entry.setParent(this);
230 		}
231 	}
232 
233 	/**
234 	 * Removes given child.
235 	 *
236 	 * @param entry
237 	 *            entry to remove
238 	 * @return removed entry
239 	 */
240 	public PropertyTreeEntry removeChild(PropertyTreeEntry entry) {
241 		if (entry == null) {
242 			throw new IllegalArgumentException();
243 		}
244 		return children.remove(entry.getId());
245 	}
246 
247 	/**
248 	 * Checks if entry has children.
249 	 *
250 	 * @return true if entry has at least one child, false otherwise
251 	 */
252 	public boolean hasChildren() {
253 		return !children.isEmpty();
254 	}
255 
256 	/**
257 	 * Gets child by given id.
258 	 *
259 	 * @param childId
260 	 *            entry id
261 	 * @return child entry
262 	 */
263 	public PropertyTreeEntry getChild(String childId) {
264 		return children.get(childId);
265 	}
266 
267 	/**
268 	 * Gets maps of children.
269 	 *
270 	 * @return maps of children entries
271 	 */
272 	public Map<String, PropertyTreeEntry> getChildrenMap() {
273 		return children;
274 	}
275 
276 	// END Children management methods
277 
278 }