KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > swing > resourcetree > formresourcetree > FormResourceTree


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.swing.resourcetree.formresourcetree;
20
21 import java.util.*;
22
23 import org.openharmonise.him.swing.resourcetree.*;
24 import org.openharmonise.vfs.*;
25
26
27 /**
28  * A resource tree that can be used as a form component. Each node in the
29  * tree can have either a checkbox or a radio button as part of the cell.
30  *
31  * @author Matthew Large
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class FormResourceTree extends ResourceTree {
36
37     /**
38      * True if multiple resources can be selected. If true checkboxes are used, if false radion buttons.
39      */

40     private boolean m_bIsMultiSelect = true;
41     
42     /**
43      * List of full paths to selected resources.
44      */

45     private List m_pathValues = new ArrayList();
46     
47     /**
48      * Ordered map of full paths to cell objects.
49      */

50     private Map m_cells = new TreeMap();
51     
52     /**
53      * List of {@link FormResourceTreeListener} listeners.
54      */

55     private List m_formResoruceTreeListeners = new ArrayList();
56     
57     /**
58      * True if virtual collections are allowed to be selected.
59      */

60     private boolean m_bAllowVirtualDirectorySelect = false;
61     
62     /**
63      * Constructs a new form resource tree.
64      *
65      * @param pathValues list of full paths of already selected values.
66      */

67     public FormResourceTree(List pathValues) {
68         super();
69         this.m_pathValues.addAll(pathValues);
70     }
71     
72     /**
73      * Constructs a new form resource tree.
74      *
75      * @param bUseScrollPane true if the tree is to be enclosed in a scroll pane.
76      * @param pathValues list of full paths of already selected values.
77      */

78     public FormResourceTree(boolean bUseScrollPane, List pathValues) {
79         super(bUseScrollPane);
80         this.m_pathValues.addAll(pathValues);
81     }
82     
83     /**
84      * Initialises this component.
85      */

86     protected void setup() {
87         super.setup();
88         
89         super.m_tree.setCellRenderer( new FormTreeCellRenderer(this) );
90         
91         FormTreeMouseListener listener = new FormTreeMouseListener(this);
92         super.m_tree.addTreeSelectionListener(listener);
93         super.m_tree.setSelectionModel(listener);
94     }
95     
96     /**
97      * Checks if multiple resources can be selected.
98      *
99      * @return true if multiple resources can be selected.
100      */

101     public boolean isMultiSelect() {
102         return this.m_bIsMultiSelect;
103     }
104     
105     /**
106      * Sets whether multiple resources can be selected.
107      *
108      * @param bIsMultiSelect true if multiple resources can be selected.
109      */

110     public void setIsMultiSelect(boolean bIsMultiSelect) {
111         this.m_bIsMultiSelect = bIsMultiSelect;
112         super.m_tree.setCellRenderer( new FormTreeCellRenderer(this) );
113     }
114     
115     /**
116      * Gets a list of full paths to selected resources.
117      *
118      * @return list of full paths to selected resources.
119      */

120     public List getPathValues() {
121         return this.m_pathValues;
122     }
123     
124     /**
125      * Adds a selected resource.
126      *
127      * @param sFullPath full path to selected resource to add.
128      */

129     public void addPathValue(String JavaDoc sFullPath) {
130         this.m_pathValues.add(sFullPath);
131         this.repaint();
132     }
133     
134     /**
135      * Removes a selected resource.
136      *
137      * @param sFullPath full path to selected resource to remove.
138      */

139     public void removePathValue(String JavaDoc sFullPath) {
140         this.m_pathValues.remove(sFullPath);
141         this.repaint();
142     }
143     
144     /**
145      * Adds a cell to the fuall path to cell map.
146      *
147      * @param cell cell to add.
148      */

149     protected void addCell(FormTreeCell cell) {
150         this.m_cells.put(cell.getFullPath(), cell);
151     }
152     
153     /**
154      * Gets a list of ordered full paths to all the cells.
155      *
156      * @return ordered list of full paths.
157      */

158     protected List getCellPaths() {
159         return new ArrayList(this.m_cells.keySet());
160     }
161     
162     /**
163      * Selects a cell.
164      *
165      * @param sFullPath full path to cell to selected.
166      */

167     protected void cellSelected(String JavaDoc sFullPath) {
168         
169         Iterator itorTemp = this.m_cells.keySet().iterator();
170         while (itorTemp.hasNext()) {
171             String JavaDoc sPath = (String JavaDoc) itorTemp.next();
172         }
173         
174         if(!this.isMultiSelect()) {
175             this.m_pathValues.clear();
176         }
177         if(!this.m_pathValues.contains(sFullPath)) {
178             this.m_pathValues.add(sFullPath);
179             this.firePathValuesChanged();
180         }
181
182         this.m_tree.revalidate();
183         this.m_tree.repaint();
184     }
185     
186     /**
187      * Deselects a cell.
188      *
189      * @param sFullPath full path to cell to deselect.
190      */

191     protected void cellUnSelected(String JavaDoc sFullPath) {
192         if(this.m_pathValues.contains(sFullPath)) {
193             this.m_pathValues.remove(sFullPath);
194             this.firePathValuesChanged();
195         }
196     }
197     
198     /**
199      * Gets a cell for a given full path.
200      *
201      * @param sFullPath full path of cell to get.
202      * @return cell for given full path.
203      */

204     protected FormTreeCell getCellForPath(String JavaDoc sFullPath) {
205         return (FormTreeCell) this.m_cells.get(sFullPath);
206     }
207
208     /**
209      * Adds a form resource tree listener.
210      *
211      * @param listener listener to add.
212      */

213     public void addFormResourceTreeListener(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(FormResourceTreeListener listener) {
223         this.m_formResoruceTreeListeners.remove(listener);
224     }
225     
226     /**
227      * Fires a path values changed event to all the {@link FormResourceTreeListener}
228      * listeners.
229      *
230      */

231     private void firePathValuesChanged() {
232         Iterator itor = this.m_formResoruceTreeListeners.iterator();
233         while (itor.hasNext()) {
234             FormResourceTreeListener listener = (FormResourceTreeListener) itor.next();
235             listener.pathValuesChanged(new ArrayList(this.m_pathValues));
236         }
237     }
238     
239     /**
240      * Sets whether virtual collections can be selected.
241      *
242      * @param bAllow true to allow virtual collections to be selected.
243      */

244     public void setAllowVirtualDirectorySelect(boolean bAllow) {
245         this.m_bAllowVirtualDirectorySelect = bAllow;
246     }
247     
248     /**
249      * Checks if virtual collections can be selected.
250      *
251      * @return true if virtual collections can be selected.
252      */

253     public boolean isAllowVirtualDirectorySelect() {
254         return this.m_bAllowVirtualDirectorySelect;
255     }
256     
257     /* (non-Javadoc)
258      * @see com.simulacramedia.contentmanager.swing.resourcetree.ResourceTree#getVFS()
259      */

260     protected AbstractVirtualFileSystem getVFS() {
261         return super.getVFS();
262     }
263 }
264
Popular Tags