001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.swing.resourcetree.formresourcetree;
020:
021: import java.util.*;
022:
023: import org.openharmonise.him.swing.resourcetree.*;
024: import org.openharmonise.vfs.*;
025:
026: /**
027: * A resource tree that can be used as a form component. Each node in the
028: * tree can have either a checkbox or a radio button as part of the cell.
029: *
030: * @author Matthew Large
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class FormResourceTree extends ResourceTree {
035:
036: /**
037: * True if multiple resources can be selected. If true checkboxes are used, if false radion buttons.
038: */
039: private boolean m_bIsMultiSelect = true;
040:
041: /**
042: * List of full paths to selected resources.
043: */
044: private List m_pathValues = new ArrayList();
045:
046: /**
047: * Ordered map of full paths to cell objects.
048: */
049: private Map m_cells = new TreeMap();
050:
051: /**
052: * List of {@link FormResourceTreeListener} listeners.
053: */
054: private List m_formResoruceTreeListeners = new ArrayList();
055:
056: /**
057: * True if virtual collections are allowed to be selected.
058: */
059: private boolean m_bAllowVirtualDirectorySelect = false;
060:
061: /**
062: * Constructs a new form resource tree.
063: *
064: * @param pathValues list of full paths of already selected values.
065: */
066: public FormResourceTree(List pathValues) {
067: super ();
068: this .m_pathValues.addAll(pathValues);
069: }
070:
071: /**
072: * Constructs a new form resource tree.
073: *
074: * @param bUseScrollPane true if the tree is to be enclosed in a scroll pane.
075: * @param pathValues list of full paths of already selected values.
076: */
077: public FormResourceTree(boolean bUseScrollPane, List pathValues) {
078: super (bUseScrollPane);
079: this .m_pathValues.addAll(pathValues);
080: }
081:
082: /**
083: * Initialises this component.
084: */
085: protected void setup() {
086: super .setup();
087:
088: super .m_tree.setCellRenderer(new FormTreeCellRenderer(this ));
089:
090: FormTreeMouseListener listener = new FormTreeMouseListener(this );
091: super .m_tree.addTreeSelectionListener(listener);
092: super .m_tree.setSelectionModel(listener);
093: }
094:
095: /**
096: * Checks if multiple resources can be selected.
097: *
098: * @return true if multiple resources can be selected.
099: */
100: public boolean isMultiSelect() {
101: return this .m_bIsMultiSelect;
102: }
103:
104: /**
105: * Sets whether multiple resources can be selected.
106: *
107: * @param bIsMultiSelect true if multiple resources can be selected.
108: */
109: public void setIsMultiSelect(boolean bIsMultiSelect) {
110: this .m_bIsMultiSelect = bIsMultiSelect;
111: super .m_tree.setCellRenderer(new FormTreeCellRenderer(this ));
112: }
113:
114: /**
115: * Gets a list of full paths to selected resources.
116: *
117: * @return list of full paths to selected resources.
118: */
119: public List getPathValues() {
120: return this .m_pathValues;
121: }
122:
123: /**
124: * Adds a selected resource.
125: *
126: * @param sFullPath full path to selected resource to add.
127: */
128: public void addPathValue(String sFullPath) {
129: this .m_pathValues.add(sFullPath);
130: this .repaint();
131: }
132:
133: /**
134: * Removes a selected resource.
135: *
136: * @param sFullPath full path to selected resource to remove.
137: */
138: public void removePathValue(String sFullPath) {
139: this .m_pathValues.remove(sFullPath);
140: this .repaint();
141: }
142:
143: /**
144: * Adds a cell to the fuall path to cell map.
145: *
146: * @param cell cell to add.
147: */
148: protected void addCell(FormTreeCell cell) {
149: this .m_cells.put(cell.getFullPath(), cell);
150: }
151:
152: /**
153: * Gets a list of ordered full paths to all the cells.
154: *
155: * @return ordered list of full paths.
156: */
157: protected List getCellPaths() {
158: return new ArrayList(this .m_cells.keySet());
159: }
160:
161: /**
162: * Selects a cell.
163: *
164: * @param sFullPath full path to cell to selected.
165: */
166: protected void cellSelected(String sFullPath) {
167:
168: Iterator itorTemp = this .m_cells.keySet().iterator();
169: while (itorTemp.hasNext()) {
170: String sPath = (String) itorTemp.next();
171: }
172:
173: if (!this .isMultiSelect()) {
174: this .m_pathValues.clear();
175: }
176: if (!this .m_pathValues.contains(sFullPath)) {
177: this .m_pathValues.add(sFullPath);
178: this .firePathValuesChanged();
179: }
180:
181: this .m_tree.revalidate();
182: this .m_tree.repaint();
183: }
184:
185: /**
186: * Deselects a cell.
187: *
188: * @param sFullPath full path to cell to deselect.
189: */
190: protected void cellUnSelected(String sFullPath) {
191: if (this .m_pathValues.contains(sFullPath)) {
192: this .m_pathValues.remove(sFullPath);
193: this .firePathValuesChanged();
194: }
195: }
196:
197: /**
198: * Gets a cell for a given full path.
199: *
200: * @param sFullPath full path of cell to get.
201: * @return cell for given full path.
202: */
203: protected FormTreeCell getCellForPath(String sFullPath) {
204: return (FormTreeCell) this .m_cells.get(sFullPath);
205: }
206:
207: /**
208: * Adds a form resource tree listener.
209: *
210: * @param listener listener to add.
211: */
212: public void addFormResourceTreeListener(
213: FormResourceTreeListener listener) {
214: this .m_formResoruceTreeListeners.add(listener);
215: }
216:
217: /**
218: * Removes a form resource tree listener.
219: *
220: * @param listener listener to remove.
221: */
222: public void removeFormResourceTreeListener(
223: FormResourceTreeListener listener) {
224: this .m_formResoruceTreeListeners.remove(listener);
225: }
226:
227: /**
228: * Fires a path values changed event to all the {@link FormResourceTreeListener}
229: * listeners.
230: *
231: */
232: private void firePathValuesChanged() {
233: Iterator itor = this .m_formResoruceTreeListeners.iterator();
234: while (itor.hasNext()) {
235: FormResourceTreeListener listener = (FormResourceTreeListener) itor
236: .next();
237: listener
238: .pathValuesChanged(new ArrayList(this .m_pathValues));
239: }
240: }
241:
242: /**
243: * Sets whether virtual collections can be selected.
244: *
245: * @param bAllow true to allow virtual collections to be selected.
246: */
247: public void setAllowVirtualDirectorySelect(boolean bAllow) {
248: this .m_bAllowVirtualDirectorySelect = bAllow;
249: }
250:
251: /**
252: * Checks if virtual collections can be selected.
253: *
254: * @return true if virtual collections can be selected.
255: */
256: public boolean isAllowVirtualDirectorySelect() {
257: return this .m_bAllowVirtualDirectorySelect;
258: }
259:
260: /* (non-Javadoc)
261: * @see com.simulacramedia.contentmanager.swing.resourcetree.ResourceTree#getVFS()
262: */
263: protected AbstractVirtualFileSystem getVFS() {
264: return super.getVFS();
265: }
266: }
|