KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.swing.FontManager;
29 import org.openharmonise.vfs.*;
30 import org.openharmonise.vfs.metadata.*;
31 import org.openharmonise.vfs.metadata.range.*;
32 import org.openharmonise.vfs.metadata.value.*;
33
34
35 /**
36  * Cell for {@link org.openharmonise.him.swing.resourcetree.formresourcetree.FormResourceTree}.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.4 $
40  *
41  */

42 public class FormTreeCell extends JPanel implements MouseListener, LayoutManager {
43
44     /**
45      * Icon for resource.
46      */

47     protected Icon m_icon = null;
48     
49     /**
50      * Name of resource.
51      */

52     protected String JavaDoc m_sText = null;
53     
54     /**
55      * Icon label.
56      */

57     protected JLabel m_iconLabel = new JLabel();
58     
59     /**
60      * Name label.
61      */

62     protected JLabel m_textLabel = new JLabel();
63     
64     /**
65      * Checkbox/radio button.
66      */

67     private JToggleButton m_checkBox = null;
68     
69     /**
70      * Tree this cell is part of.
71      */

72     private FormResourceTree m_tree = null;
73     
74     /**
75      * Full path to resource.
76      */

77     private String JavaDoc m_sFullPath = null;
78     
79     /**
80      * True if this cell is for a leaf node.
81      */

82     private boolean m_bIsLeaf = false;
83     
84     /**
85      * True if the children of this node are populated.
86      */

87     private boolean m_bIsChildrenPopulated = false;
88
89     /**
90      * Constructs a new form tree cell.
91      *
92      */

93     public FormTreeCell() {
94         super();
95     }
96
97     /**
98      * Constructs a new form tree cell.
99      *
100      * @param tree tree this cell is for.
101      * @param sFullPath full path to resource.
102      * @param icon icon for resource.
103      * @param sText name of resource.
104      * @param checkBox checkbox/radion button for resource.
105      * @param bIsLeaf true if this cell is for a lead node.
106      * @param bIsChildrenPopulated true if the children of this node are populated.
107      */

108     public FormTreeCell(FormResourceTree tree, String JavaDoc sFullPath, Icon icon, String JavaDoc sText, JToggleButton checkBox, boolean bIsLeaf, boolean bIsChildrenPopulated) {
109         super();
110         this.m_sFullPath = sFullPath;
111         this.m_tree = tree;
112         this.m_icon = icon;
113         this.m_sText = sText;
114         this.m_checkBox = checkBox;
115         this.m_bIsLeaf = bIsLeaf;
116         this.m_bIsChildrenPopulated = bIsChildrenPopulated;
117         this.setup();
118     }
119
120     /**
121      * @param arg0
122      */

123     private FormTreeCell(boolean arg0) {
124         super(arg0);
125     }
126
127     /**
128      * @param arg0
129      */

130     private FormTreeCell(LayoutManager arg0) {
131         super(arg0);
132     }
133
134     /**
135      * @param arg0
136      * @param arg1
137      */

138     private FormTreeCell(LayoutManager arg0, boolean arg1) {
139         super(arg0, arg1);
140     }
141     
142     /**
143      * Initialises this component.
144      *
145      */

146     protected void setup() {
147         
148         VirtualFile vfFile = this.m_tree.getVFS().getVirtualFile(m_sFullPath).getResource();
149         if(vfFile.isVirtualDirectory() && !this.m_tree.isAllowVirtualDirectorySelect()) {
150             this.m_checkBox=null;
151         }
152         
153         boolean bBoldText = false;
154         
155         if(this.m_bIsLeaf || !this.m_tree.isShowLeafNodes()) {
156             if(this.m_tree.getPathValues().contains(this.m_sFullPath)) {
157                 this.m_checkBox.setSelected(true);
158                 bBoldText = true;
159             }
160         }
161         
162         if(!this.m_bIsLeaf) {
163             Iterator itor = this.m_tree.getPathValues().iterator();
164             while (itor.hasNext()) {
165                 String JavaDoc sPath = (String JavaDoc) itor.next();
166                 if(sPath.startsWith(this.m_sFullPath) && sPath.replaceFirst(this.m_sFullPath, "").indexOf("/")==0) {
167                     bBoldText=true;
168                     break;
169                 }
170             }
171         }
172         
173         boolean bChecked = false;
174         
175         if(this.m_checkBox!=null) {
176             this.addMouseListener(this);
177         }
178         
179         this.m_tree.addCell(this);
180         
181         this.setLayout(this);
182         this.setBackground(Color.WHITE);
183         
184         if(bBoldText) {
185             //Font font = UIManager.getFont("Tree.font").deriveFont(Font.BOLD);
186
//this.m_textLabel.setFont( font );
187
this.m_textLabel.setFont( FontManager.getInstance().getFont( FontManager.FONT_RESOURCE_TITLE_BOLD ) );
188         } else {
189             //this.m_textLabel.setFont( UIManager.getFont("Tree.font") );
190
this.m_textLabel.setFont( FontManager.getInstance().getFont( FontManager.FONT_RESOURCE_TITLE ) );
191         }
192         
193         this.m_textLabel.setEnabled(this.m_tree.isEnabled());
194         
195         this.m_iconLabel.setIcon(this.m_icon);
196         this.m_iconLabel.setPreferredSize(new Dimension(20,20));
197         this.add(this.m_iconLabel);
198         this.m_textLabel.setText(this.m_sText);
199         this.m_textLabel.setPreferredSize(new Dimension(this.m_textLabel.getPreferredSize().width,20));
200         this.add(this.m_textLabel);
201         if(this.m_checkBox!=null) {
202             this.m_checkBox.setPreferredSize(new Dimension(17,20));
203
204             this.m_checkBox.setBackground(Color.WHITE);
205             this.add(this.m_checkBox);
206         }
207     }
208
209     /* (non-Javadoc)
210      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
211      */

212     public void removeLayoutComponent(Component arg0) {
213     }
214
215     /* (non-Javadoc)
216      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
217      */

218     public void layoutContainer(Container arg0) {
219         
220         this.m_iconLabel.setSize( this.m_iconLabel.getPreferredSize() );
221         this.m_iconLabel.setLocation(0,0);
222         
223         int xPos = 18;
224         if(this.m_checkBox!=null) {
225             this.m_checkBox.setSize( 20,20 );
226             this.m_checkBox.setLocation(16,0);
227             xPos = 38;
228         }
229         
230         this.m_textLabel.setSize( this.m_textLabel.getPreferredSize() );
231         this.m_textLabel.setLocation(xPos,0);
232     }
233
234     /* (non-Javadoc)
235      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
236      */

237     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
238     }
239
240     /* (non-Javadoc)
241      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
242      */

243     public Dimension minimumLayoutSize(Container arg0) {
244         return this.getPreferredSize();
245     }
246
247     /* (non-Javadoc)
248      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
249      */

250     public Dimension preferredLayoutSize(Container arg0) {
251         return this.getPreferredSize();
252     }
253
254     /* (non-Javadoc)
255      * @see java.awt.Component#getPreferredSize()
256      */

257     public Dimension getPreferredSize() {
258         int nWidth = 3;
259         if(this.m_checkBox!=null) {
260             nWidth = nWidth + this.m_checkBox.getPreferredSize().width;
261         }
262         nWidth = nWidth + this.m_iconLabel.getPreferredSize().width + this.m_textLabel.getPreferredSize().width;
263         return new Dimension(nWidth, 20);
264     }
265
266     /* (non-Javadoc)
267      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
268      */

269     public void mouseClicked(MouseEvent me) {
270         if(this.m_checkBox!=null) {
271             VirtualFile vfFile = this.m_tree.getVFS().getVirtualFile(m_sFullPath).getResource();
272             if(this.m_tree.isAllowVirtualDirectorySelect() || !vfFile.isVirtualDirectory()) {
273                 if(this.m_checkBox.isSelected()) {
274                     this.m_checkBox.setSelected(false);
275                     this.m_tree.cellUnSelected(m_sFullPath);
276                 } else {
277                     this.m_checkBox.setSelected(true);
278                     this.m_tree.cellSelected(m_sFullPath);
279                 }
280                 m_tree.revalidate();
281                 m_tree.repaint();
282             }
283         }
284     }
285
286     /* (non-Javadoc)
287      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
288      */

289     public void mouseEntered(MouseEvent arg0) {
290     }
291
292     /* (non-Javadoc)
293      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
294      */

295     public void mouseExited(MouseEvent arg0) {
296     }
297
298     /* (non-Javadoc)
299      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
300      */

301     public void mousePressed(MouseEvent arg0) {
302     }
303
304     /* (non-Javadoc)
305      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
306      */

307     public void mouseReleased(MouseEvent arg0) {
308     }
309     
310     /**
311      * Gets the full path of the resource.
312      *
313      * @return full path to the resource.
314      */

315     public String JavaDoc getFullPath() {
316         return this.m_sFullPath;
317     }
318     
319     /**
320      * Sets whether this cell is selected.
321      *
322      * @param bSelected true if this cell is selected.
323      */

324     public void setSelected(boolean bSelected) {
325         if(this.m_checkBox!=null) {
326             this.m_checkBox.setSelected(bSelected);
327         }
328     }
329     
330     /**
331      * Checks if the children of this node are populated.
332      *
333      * @return true if the children of this node are populated.
334      */

335     protected boolean isChildrenPopulated() {
336         return this.m_bIsChildrenPopulated;
337     }
338     
339     /**
340      * Checks if this is leaf node.
341      *
342      * @return true if this is a leaf node.
343      */

344     protected boolean isLeaf() {
345         return this.m_bIsLeaf;
346     }
347 }
348
Popular Tags