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.awt.*;
022: import java.awt.event.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027:
028: import org.openharmonise.swing.FontManager;
029: import org.openharmonise.vfs.*;
030: import org.openharmonise.vfs.metadata.*;
031: import org.openharmonise.vfs.metadata.range.*;
032: import org.openharmonise.vfs.metadata.value.*;
033:
034: /**
035: * Cell for {@link org.openharmonise.him.swing.resourcetree.formresourcetree.FormResourceTree}.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.4 $
039: *
040: */
041: public class FormTreeCell extends JPanel implements MouseListener,
042: LayoutManager {
043:
044: /**
045: * Icon for resource.
046: */
047: protected Icon m_icon = null;
048:
049: /**
050: * Name of resource.
051: */
052: protected String m_sText = null;
053:
054: /**
055: * Icon label.
056: */
057: protected JLabel m_iconLabel = new JLabel();
058:
059: /**
060: * Name label.
061: */
062: protected JLabel m_textLabel = new JLabel();
063:
064: /**
065: * Checkbox/radio button.
066: */
067: private JToggleButton m_checkBox = null;
068:
069: /**
070: * Tree this cell is part of.
071: */
072: private FormResourceTree m_tree = null;
073:
074: /**
075: * Full path to resource.
076: */
077: private String m_sFullPath = null;
078:
079: /**
080: * True if this cell is for a leaf node.
081: */
082: private boolean m_bIsLeaf = false;
083:
084: /**
085: * True if the children of this node are populated.
086: */
087: private boolean m_bIsChildrenPopulated = false;
088:
089: /**
090: * Constructs a new form tree cell.
091: *
092: */
093: public FormTreeCell() {
094: super ();
095: }
096:
097: /**
098: * Constructs a new form tree cell.
099: *
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 sFullPath,
109: Icon icon, String sText, JToggleButton checkBox,
110: boolean bIsLeaf, boolean bIsChildrenPopulated) {
111: super ();
112: this .m_sFullPath = sFullPath;
113: this .m_tree = tree;
114: this .m_icon = icon;
115: this .m_sText = sText;
116: this .m_checkBox = checkBox;
117: this .m_bIsLeaf = bIsLeaf;
118: this .m_bIsChildrenPopulated = bIsChildrenPopulated;
119: this .setup();
120: }
121:
122: /**
123: * @param arg0
124: */
125: private FormTreeCell(boolean arg0) {
126: super (arg0);
127: }
128:
129: /**
130: * @param arg0
131: */
132: private FormTreeCell(LayoutManager arg0) {
133: super (arg0);
134: }
135:
136: /**
137: * @param arg0
138: * @param arg1
139: */
140: private FormTreeCell(LayoutManager arg0, boolean arg1) {
141: super (arg0, arg1);
142: }
143:
144: /**
145: * Initialises this component.
146: *
147: */
148: protected void setup() {
149:
150: VirtualFile vfFile = this .m_tree.getVFS().getVirtualFile(
151: m_sFullPath).getResource();
152: if (vfFile.isVirtualDirectory()
153: && !this .m_tree.isAllowVirtualDirectorySelect()) {
154: this .m_checkBox = null;
155: }
156:
157: boolean bBoldText = false;
158:
159: if (this .m_bIsLeaf || !this .m_tree.isShowLeafNodes()) {
160: if (this .m_tree.getPathValues().contains(this .m_sFullPath)) {
161: this .m_checkBox.setSelected(true);
162: bBoldText = true;
163: }
164: }
165:
166: if (!this .m_bIsLeaf) {
167: Iterator itor = this .m_tree.getPathValues().iterator();
168: while (itor.hasNext()) {
169: String sPath = (String) itor.next();
170: if (sPath.startsWith(this .m_sFullPath)
171: && sPath.replaceFirst(this .m_sFullPath, "")
172: .indexOf("/") == 0) {
173: bBoldText = true;
174: break;
175: }
176: }
177: }
178:
179: boolean bChecked = false;
180:
181: if (this .m_checkBox != null) {
182: this .addMouseListener(this );
183: }
184:
185: this .m_tree.addCell(this );
186:
187: this .setLayout(this );
188: this .setBackground(Color.WHITE);
189:
190: if (bBoldText) {
191: //Font font = UIManager.getFont("Tree.font").deriveFont(Font.BOLD);
192: //this.m_textLabel.setFont( font );
193: this .m_textLabel.setFont(FontManager.getInstance().getFont(
194: FontManager.FONT_RESOURCE_TITLE_BOLD));
195: } else {
196: //this.m_textLabel.setFont( UIManager.getFont("Tree.font") );
197: this .m_textLabel.setFont(FontManager.getInstance().getFont(
198: FontManager.FONT_RESOURCE_TITLE));
199: }
200:
201: this .m_textLabel.setEnabled(this .m_tree.isEnabled());
202:
203: this .m_iconLabel.setIcon(this .m_icon);
204: this .m_iconLabel.setPreferredSize(new Dimension(20, 20));
205: this .add(this .m_iconLabel);
206: this .m_textLabel.setText(this .m_sText);
207: this .m_textLabel.setPreferredSize(new Dimension(
208: this .m_textLabel.getPreferredSize().width, 20));
209: this .add(this .m_textLabel);
210: if (this .m_checkBox != null) {
211: this .m_checkBox.setPreferredSize(new Dimension(17, 20));
212:
213: this .m_checkBox.setBackground(Color.WHITE);
214: this .add(this .m_checkBox);
215: }
216: }
217:
218: /* (non-Javadoc)
219: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
220: */
221: public void removeLayoutComponent(Component arg0) {
222: }
223:
224: /* (non-Javadoc)
225: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
226: */
227: public void layoutContainer(Container arg0) {
228:
229: this .m_iconLabel.setSize(this .m_iconLabel.getPreferredSize());
230: this .m_iconLabel.setLocation(0, 0);
231:
232: int xPos = 18;
233: if (this .m_checkBox != null) {
234: this .m_checkBox.setSize(20, 20);
235: this .m_checkBox.setLocation(16, 0);
236: xPos = 38;
237: }
238:
239: this .m_textLabel.setSize(this .m_textLabel.getPreferredSize());
240: this .m_textLabel.setLocation(xPos, 0);
241: }
242:
243: /* (non-Javadoc)
244: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
245: */
246: public void addLayoutComponent(String arg0, Component arg1) {
247: }
248:
249: /* (non-Javadoc)
250: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
251: */
252: public Dimension minimumLayoutSize(Container arg0) {
253: return this .getPreferredSize();
254: }
255:
256: /* (non-Javadoc)
257: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
258: */
259: public Dimension preferredLayoutSize(Container arg0) {
260: return this .getPreferredSize();
261: }
262:
263: /* (non-Javadoc)
264: * @see java.awt.Component#getPreferredSize()
265: */
266: public Dimension getPreferredSize() {
267: int nWidth = 3;
268: if (this .m_checkBox != null) {
269: nWidth = nWidth + this .m_checkBox.getPreferredSize().width;
270: }
271: nWidth = nWidth + this .m_iconLabel.getPreferredSize().width
272: + this .m_textLabel.getPreferredSize().width;
273: return new Dimension(nWidth, 20);
274: }
275:
276: /* (non-Javadoc)
277: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
278: */
279: public void mouseClicked(MouseEvent me) {
280: if (this .m_checkBox != null) {
281: VirtualFile vfFile = this .m_tree.getVFS().getVirtualFile(
282: m_sFullPath).getResource();
283: if (this .m_tree.isAllowVirtualDirectorySelect()
284: || !vfFile.isVirtualDirectory()) {
285: if (this .m_checkBox.isSelected()) {
286: this .m_checkBox.setSelected(false);
287: this .m_tree.cellUnSelected(m_sFullPath);
288: } else {
289: this .m_checkBox.setSelected(true);
290: this .m_tree.cellSelected(m_sFullPath);
291: }
292: m_tree.revalidate();
293: m_tree.repaint();
294: }
295: }
296: }
297:
298: /* (non-Javadoc)
299: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
300: */
301: public void mouseEntered(MouseEvent arg0) {
302: }
303:
304: /* (non-Javadoc)
305: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
306: */
307: public void mouseExited(MouseEvent arg0) {
308: }
309:
310: /* (non-Javadoc)
311: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
312: */
313: public void mousePressed(MouseEvent arg0) {
314: }
315:
316: /* (non-Javadoc)
317: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
318: */
319: public void mouseReleased(MouseEvent arg0) {
320: }
321:
322: /**
323: * Gets the full path of the resource.
324: *
325: * @return full path to the resource.
326: */
327: public String getFullPath() {
328: return this .m_sFullPath;
329: }
330:
331: /**
332: * Sets whether this cell is selected.
333: *
334: * @param bSelected true if this cell is selected.
335: */
336: public void setSelected(boolean bSelected) {
337: if (this .m_checkBox != null) {
338: this .m_checkBox.setSelected(bSelected);
339: }
340: }
341:
342: /**
343: * Checks if the children of this node are populated.
344: *
345: * @return true if the children of this node are populated.
346: */
347: protected boolean isChildrenPopulated() {
348: return this .m_bIsChildrenPopulated;
349: }
350:
351: /**
352: * Checks if this is leaf node.
353: *
354: * @return true if this is a leaf node.
355: */
356: protected boolean isLeaf() {
357: return this.m_bIsLeaf;
358: }
359: }
|