001: /*
002: * FolderTree.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: FolderTree.java,v 1.2 2003/07/23 15:57:52 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Graphics;
027: import java.awt.Point;
028: import java.awt.event.InputEvent;
029: import java.awt.event.MouseEvent;
030: import java.awt.event.MouseListener;
031: import java.util.Enumeration;
032: import javax.swing.JTree;
033: import javax.swing.tree.DefaultMutableTreeNode;
034: import javax.swing.tree.DefaultTreeCellRenderer;
035: import javax.swing.tree.TreePath;
036: import javax.swing.tree.TreeSelectionModel;
037: import org.armedbear.j.Buffer;
038: import org.armedbear.j.Display;
039: import org.armedbear.j.Editor;
040: import org.armedbear.j.Frame;
041: import org.armedbear.j.NavigationComponent;
042: import org.armedbear.j.Utilities;
043:
044: public final class FolderTree extends JTree implements
045: NavigationComponent, MouseListener {
046: private final Frame frame;
047:
048: private FolderTree(Frame frame) {
049: super (FolderTreeModel.getDefaultModel());
050: this .frame = frame;
051: getSelectionModel().setSelectionMode(
052: TreeSelectionModel.SINGLE_TREE_SELECTION);
053: setRootVisible(false);
054: toggleClickCount = 1;
055: setCellRenderer(new TreeCellRenderer(this ));
056: addMouseListener(this );
057: }
058:
059: public static FolderTree getInstance(Frame frame) {
060: Object obj = frame.getFolderTree();
061: if (obj instanceof FolderTree)
062: return (FolderTree) obj;
063: FolderTree tree = new FolderTree(frame);
064: frame.setFolderTree(tree);
065: return tree;
066: }
067:
068: public void refresh() {
069: }
070:
071: public void updatePosition() {
072: int row = -1;
073: final Buffer buf = frame.getCurrentEditor().getBuffer();
074: MailboxURL url = null;
075: if (buf instanceof Mailbox)
076: url = ((Mailbox) buf).getUrl();
077: else if (buf instanceof MessageBuffer) {
078: Mailbox mailbox = ((MessageBuffer) buf).getMailbox();
079: if (mailbox != null)
080: url = mailbox.getUrl();
081: }
082: if (url != null) {
083: DefaultMutableTreeNode root = (DefaultMutableTreeNode) getModel()
084: .getRoot();
085: if (root != null) {
086: Enumeration nodes = root.depthFirstEnumeration();
087: while (nodes.hasMoreElements()) {
088: DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes
089: .nextElement();
090: if (node.getUserObject() instanceof Folder) {
091: Folder folder = (Folder) node.getUserObject();
092: if (url.equals(folder.getUrl())) {
093: scrollPathToVisible(new TreePath(node
094: .getPath()));
095: final int limit = getRowCount();
096: for (int i = 0; i < limit; i++) {
097: TreePath treepath = getPathForRow(i);
098: if (treepath != null
099: && treepath
100: .getLastPathComponent() == node) {
101: row = i;
102: break;
103: }
104: }
105: }
106: }
107: }
108: }
109: }
110: if (row >= 0)
111: setSelectionRow(row);
112: else
113: clearSelection();
114: repaint();
115: }
116:
117: public final String getLabelText() {
118: return "Folders";
119: }
120:
121: public void mousePressed(MouseEvent e) {
122: }
123:
124: public void mouseReleased(MouseEvent e) {
125: }
126:
127: public void mouseClicked(MouseEvent e) {
128: final Editor editor = frame.getCurrentEditor();
129: final int modifiers = e.getModifiers();
130: if (modifiers != InputEvent.BUTTON1_MASK
131: && modifiers != InputEvent.BUTTON2_MASK) {
132: e.consume();
133: editor.setFocusToDisplay();
134: return;
135: }
136: Point point = e.getPoint();
137: TreePath treePath = getPathForLocation(point.x, point.y);
138: if (treePath != null) {
139: DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath
140: .getLastPathComponent();
141: Object object = node.getUserObject();
142: if (object instanceof Folder) {
143: Folder folder = (Folder) object;
144: MailboxURL url = folder.getUrl();
145: editor.setWaitCursor();
146: MailCommands.openMailbox(editor, url);
147: editor.updateDisplay();
148: editor.setDefaultCursor();
149: }
150: }
151: editor.setFocusToDisplay();
152: }
153:
154: public void mouseEntered(MouseEvent e) {
155: }
156:
157: public void mouseExited(MouseEvent e) {
158: updatePosition();
159: frame.getCurrentEditor().setFocusToDisplay();
160: }
161:
162: private static class TreeCellRenderer extends
163: DefaultTreeCellRenderer {
164: private static final Color noFocusSelectionBackground = new Color(
165: 208, 208, 208);
166:
167: private JTree tree;
168: private Color oldBackgroundSelectionColor;
169:
170: TreeCellRenderer(JTree tree) {
171: super ();
172: this .tree = tree;
173: oldBackgroundSelectionColor = getBackgroundSelectionColor();
174: setOpenIcon(Utilities.getIconFromFile("dir_open.png"));
175: setClosedIcon(Utilities.getIconFromFile("dir_close.png"));
176: setLeafIcon(Utilities.getIconFromFile("mailbox.png"));
177: }
178:
179: public Component getTreeCellRendererComponent(JTree tree,
180: Object value, boolean selected, boolean expanded,
181: boolean leaf, int row, boolean hasFocus) {
182: super .getTreeCellRendererComponent(tree, value, selected,
183: expanded, leaf, row, hasFocus);
184: if (selected)
185: super .setForeground(getTextSelectionColor());
186: else
187: super .setForeground(getTextNonSelectionColor());
188: if (Editor.getCurrentFrame().getFocusedComponent() == tree)
189: setBackgroundSelectionColor(oldBackgroundSelectionColor);
190: else
191: setBackgroundSelectionColor(noFocusSelectionBackground);
192: return this ;
193: }
194:
195: public void paintComponent(Graphics g) {
196: Display.setRenderingHints(g);
197: super.paintComponent(g);
198: }
199: }
200: }
|