001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016:
017: package org.columba.mail.gui.tree.util;
018:
019: import java.awt.Component;
020: import java.awt.Font;
021:
022: import javax.swing.Icon;
023: import javax.swing.JTree;
024: import javax.swing.UIManager;
025: import javax.swing.tree.DefaultTreeCellRenderer;
026:
027: import org.columba.core.resourceloader.ImageLoader;
028: import org.columba.mail.config.IFolderItem;
029: import org.columba.mail.folder.IMailFolder;
030: import org.columba.mail.folder.IMailbox;
031: import org.columba.mail.folder.IMailboxInfo;
032: import org.columba.mail.folder.LocalRootFolder;
033: import org.columba.mail.folder.imap.IMAPRootFolder;
034: import org.columba.mail.folder.virtual.VirtualFolder;
035: import org.columba.mail.resourceloader.MailImageLoader;
036:
037: /**
038: * This class is used for the mail folder tree.
039: */
040: public class FolderTreeCellRenderer extends DefaultTreeCellRenderer {
041: private static Icon expandedFolderIcon;
042:
043: private static Icon collapsedFolderIcon;
044:
045: private static Icon virtualFolderIcon;
046:
047: private static Icon localRootFolderIcon;
048:
049: private static Icon imapRootFolderIcon;
050:
051: private static Icon inboxIcon;
052:
053: private static Icon outboxIcon;
054:
055: private static Icon trashIcon;
056:
057: private static Icon templateIcon;
058:
059: private static Icon sentIcon;
060:
061: static {
062: collapsedFolderIcon = ImageLoader.getSmallIcon("folder.png");
063: expandedFolderIcon = ImageLoader
064: .getSmallIcon("folder-open.png");
065: virtualFolderIcon = ImageLoader
066: .getSmallIcon("folder-saved-search.png");
067: localRootFolderIcon = ImageLoader.getSmallIcon("computer.png");
068: imapRootFolderIcon = ImageLoader
069: .getSmallIcon("network-server.png");
070: inboxIcon = MailImageLoader
071: .getSmallIcon("folder-inbox-new.png");
072: outboxIcon = MailImageLoader.getSmallIcon("folder-outbox.png");
073:
074: templateIcon = MailImageLoader
075: .getSmallIcon("folder-template.png");
076: sentIcon = MailImageLoader.getSmallIcon("folder-sent.png");
077: trashIcon = ImageLoader.getSmallIcon("user-trash.png");
078: }
079:
080: private Font plainFont;
081:
082: private Font boldFont;
083:
084: private Font italicFont;
085:
086: /**
087: * Generates a new CellRenderer. In this contructor font and images are set
088: * to local variables. The fonts are depended on the current UIManager.
089: */
090: public FolderTreeCellRenderer() {
091: super ();
092:
093: boldFont = UIManager.getFont("Tree.font");
094: boldFont = boldFont.deriveFont(Font.BOLD);
095: italicFont = UIManager.getFont("Tree.font");
096: italicFont = italicFont.deriveFont(Font.ITALIC);
097: plainFont = UIManager.getFont("Tree.font");
098: }
099:
100: /**
101: * The tooltip text and unseen counter for the current folder component are
102: * set. If the folder has unseen Messages the folder self is show as bold
103: * and the unseen message counter is added to the folder label. The folder
104: * gets a tooltip where infos (unseen, recent, total) are set. If the folder
105: * is an IMAP folder and not selectable the folder is set to italic with a
106: * darkgrey background.
107: */
108: public Component getTreeCellRendererComponent(JTree tree,
109: Object value, boolean isSelected, boolean expanded,
110: boolean leaf, int row, boolean hasFocusVar) {
111: /*
112: * RIYAD: Even though we don't do anything with this value, what it is
113: * doing is setting up the selection colors and such as implemented per
114: * the default cell rendered.
115: */
116: super .getTreeCellRendererComponent(tree, value, isSelected,
117: expanded, leaf, row, hasFocusVar);
118:
119: // setting default Values
120: // setFont(plainFont);
121: // setToolTipText("");
122:
123: IMailFolder treeNode = (IMailFolder) value;
124: // setText(treeNode.getName());
125: // setIcon(getFolderIcon(treeNode, expanded));
126:
127: if (value instanceof IMailbox) {
128: IMailbox folder = (IMailbox) value;
129:
130: // getting folder info
131: IMailboxInfo info = folder.getMessageFolderInfo();
132:
133: // get folder item configuration
134: IFolderItem item = folder.getConfiguration();
135:
136: if (item == null)
137: return this ;
138:
139: // set tooltip text
140: setToolTipText(createTooltipText(info));
141:
142: // set label text
143: setText(createLabelText(info, item));
144:
145: } else {
146: setText(treeNode.getName());
147: }
148:
149: setIcon(getFolderIcon(treeNode, expanded));
150: return this ;
151: }
152:
153: /**
154: * Create HTML label text
155: *
156: * @param info
157: * mailbox info (total/unread/recent count)
158: * @param item
159: * folderitem containing xml-configuration
160: * @return label
161: */
162: private String createLabelText(IMailboxInfo info, IFolderItem item) {
163:
164: // name of folder
165: String name = item.getString("property", "name");
166: // IMAP folder specific
167: // - Is this folder selectable (does it contain messages?)
168: boolean not_selectable = item.getBooleanWithDefault(
169: "selectable", true) == false;
170:
171: if (not_selectable || info.getUnseen() > 0
172: || info.getRecent() > 0) {
173:
174: StringBuffer buf;
175: // set label text
176: buf = new StringBuffer("<html><body>");
177:
178: // if IMAP selectable folder
179: // -> lightgray font color and italic
180: if (not_selectable)
181: buf.append("<i><font color='#333333'>");
182:
183: if (info.getUnseen() > 0) {
184: // draw folder name and unseen count
185: // -> using bold font
186: buf.append("<b>");
187:
188: // append unseen count to folder name
189: name = name + " (" + info.getUnseen() + ")";
190: buf.append(name);
191:
192: buf.append("</b>");
193: } else
194: buf.append(name);
195:
196: // if IMAP selectable folder
197: if (not_selectable)
198: buf.append("</i>");
199:
200: if (info.getRecent() > 0) {
201: // append recent message count
202: // -> using blue font
203: buf.append("<font color='blue'> (");
204: buf.append(info.getRecent());
205: buf.append(")");
206: }
207:
208: buf.append("</body></html>");
209:
210: return buf.toString();
211: } else {
212: return name;
213: }
214: }
215:
216: /**
217: * Create HTML tooltip text.
218: *
219: * @param info
220: * mailboxinfo (total/unread/recent count)
221: * @return tooltip text
222: */
223: private String createTooltipText(IMailboxInfo info) {
224: StringBuffer buf = new StringBuffer();
225: buf.append("<html><body> Total: " + info.getExists());
226: buf.append("<br> Unseen: " + info.getUnseen());
227: buf.append("<br> Recent: " + info.getRecent());
228: buf.append("</body></html>");
229: return buf.toString();
230: }
231:
232: /**
233: * Returns an icon suitable for the given folder.
234: */
235: public static Icon getFolderIcon(IMailFolder node, boolean expanded) {
236: if (node instanceof LocalRootFolder) {
237: return localRootFolderIcon;
238: } else if (node instanceof IMAPRootFolder) {
239: return imapRootFolderIcon;
240: } else if (node instanceof VirtualFolder) {
241: return virtualFolderIcon;
242: } else if (node instanceof IMailbox) {
243: IMailbox folder = (IMailbox) node;
244: if (folder.isInboxFolder()) {
245: return inboxIcon;
246: } else if (folder.getId().equals("103")) {
247:
248: return outboxIcon;
249: } else if (folder.getId().equals("107")) {
250: return templateIcon;
251: } else if (folder.getId().equals("104")) {
252: return sentIcon;
253: } else if (folder.isTrashFolder()) {
254: return trashIcon;
255: }
256: }
257: return expanded ? expandedFolderIcon : collapsedFolderIcon;
258: }
259: }
|