001: /*
002: * FileCellRenderer.java - renders table cells for the VFS browser
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999 Jason Ginchereau
007: * Portions copyright (C) 2001, 2003 Slava Pestov
008: * Portions copyright (C) 2007 Matthieu Casanova
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: */
024:
025: package org.gjt.sp.jedit.browser;
026:
027: //{{{ Imports
028: import java.awt.*;
029: import java.awt.font.*;
030: import javax.swing.*;
031: import javax.swing.border.*;
032: import javax.swing.table.*;
033: import org.gjt.sp.jedit.io.VFSFile;
034: import org.gjt.sp.jedit.*;
035:
036: //}}}
037:
038: /**
039: * Local filesystem VFS.
040: * @version $Id: FileCellRenderer.java 8581 2007-01-13 18:42:18Z kpouer $
041: */
042: public class FileCellRenderer extends DefaultTableCellRenderer {
043: public static Icon fileIcon = GUIUtilities.loadIcon("File.png");
044: public static Icon openFileIcon = GUIUtilities
045: .loadIcon("OpenFile.png");
046: public static Icon dirIcon = GUIUtilities.loadIcon("Folder.png");
047: public static Icon openDirIcon = GUIUtilities
048: .loadIcon("OpenFolder.png");
049: public static Icon filesystemIcon = GUIUtilities
050: .loadIcon("DriveSmall.png");
051: public static Icon loadingIcon = GUIUtilities
052: .loadIcon("ReloadSmall.png");
053:
054: //{{{ FileCellRenderer constructor
055: public FileCellRenderer() {
056: plainFont = UIManager.getFont("Tree.font");
057: if (plainFont == null)
058: plainFont = jEdit.getFontProperty("metal.secondary.font");
059: boldFont = plainFont.deriveFont(Font.BOLD);
060: } //}}}
061:
062: //{{{ getTableCellRendererComponent() method
063: public Component getTableCellRendererComponent(JTable table,
064: Object value, boolean isSelected, boolean hasFocus,
065: int row, int column) {
066: super .getTableCellRendererComponent(table, value, isSelected,
067: hasFocus, row, column);
068:
069: if (value instanceof VFSDirectoryEntryTableModel.Entry) {
070: VFSDirectoryEntryTableModel.Entry entry = (VFSDirectoryEntryTableModel.Entry) value;
071: VFSFile file = entry.dirEntry;
072:
073: setFont(file.getType() == VFSFile.FILE ? plainFont
074: : boldFont);
075:
076: this .isSelected = isSelected;
077: this .file = file;
078:
079: if (column == 0) {
080: // while its broken to have a null
081: // symlinkPath, some older plugins
082: // might...
083: String path;
084: if (file.getSymlinkPath() == null)
085: path = file.getPath();
086: else
087: path = file.getSymlinkPath();
088: openBuffer = jEdit._getBuffer(path) != null;
089:
090: setIcon(showIcons ? getIconForFile(file,
091: entry.expanded, openBuffer) : null);
092: setText(file.getName());
093:
094: int state;
095: if (file.getType() == VFSFile.FILE)
096: state = ExpansionToggleBorder.STATE_NONE;
097: else if (entry.expanded)
098: state = ExpansionToggleBorder.STATE_EXPANDED;
099: else
100: state = ExpansionToggleBorder.STATE_COLLAPSED;
101:
102: setBorder(new ExpansionToggleBorder(state, entry.level));
103: } else {
104: VFSDirectoryEntryTableModel model = (VFSDirectoryEntryTableModel) table
105: .getModel();
106: String extAttr = model.getExtendedAttribute(column);
107:
108: openBuffer = false;
109: setIcon(null);
110: setText(file.getExtendedAttribute(extAttr));
111: setBorder(new EmptyBorder(1, 1, 1, 1));
112: }
113: }
114:
115: return this ;
116: } //}}}
117:
118: //{{{ paintComponent() method
119: public void paintComponent(Graphics g) {
120: if (!isSelected) {
121: Color color = file.getColor();
122:
123: setForeground(color == null ? UIManager
124: .getColor("Tree.foreground") : color);
125: }
126:
127: super .paintComponent(g);
128:
129: if (openBuffer) {
130: Font font = getFont();
131:
132: FontMetrics fm = getFontMetrics(font);
133: int x, y;
134: if (getIcon() == null) {
135: x = 0;
136: y = fm.getAscent() + 2;
137: } else {
138: x = getIcon().getIconWidth() + getIconTextGap();
139: y = Math.max(fm.getAscent() + 2, 16);
140: }
141:
142: Insets border = getBorder().getBorderInsets(this );
143: x += border.left;
144:
145: g.setColor(getForeground());
146: g.drawLine(x, y, x + fm.stringWidth(getText()), y);
147: }
148: } //}}}
149:
150: //{{{ getIconForFile() method
151: /**
152: * @since jEdit 4.3pre2
153: */
154: public static Icon getIconForFile(VFSFile file, boolean expanded) {
155: return getIconForFile(file, expanded, jEdit._getBuffer(file
156: .getSymlinkPath()) != null);
157: } //}}}
158:
159: //{{{ getIconForFile() method
160: public static Icon getIconForFile(VFSFile file, boolean expanded,
161: boolean openBuffer) {
162: if (defaultIcons)
163: return file.getDefaultIcon(expanded, openBuffer);
164: return file.getIcon(expanded, openBuffer);
165: } //}}}
166:
167: //{{{ Package-private members
168: Font plainFont;
169: Font boldFont;
170: boolean showIcons;
171: private static boolean defaultIcons = true;
172:
173: //{{{ propertiesChanged() method
174: void propertiesChanged() {
175: showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons");
176: defaultIcons = jEdit
177: .getBooleanProperty("vfs.browser.useDefaultIcons");
178: } //}}}
179:
180: //{{{ getEntryWidth() method
181: int getEntryWidth(VFSDirectoryEntryTableModel.Entry entry,
182: Font font, FontRenderContext fontRenderContext) {
183: String name = entry.dirEntry.getName();
184: int width = (int) font.getStringBounds(name, fontRenderContext)
185: .getWidth();
186: width += ExpansionToggleBorder.ICON_WIDTH + entry.level
187: * ExpansionToggleBorder.LEVEL_WIDTH + 3;
188: if (showIcons) {
189: width += fileIcon.getIconWidth();
190: width += getIconTextGap();
191: }
192: return width;
193: } //}}}
194:
195: //}}}
196:
197: //{{{ Private members
198: private boolean openBuffer;
199: private boolean isSelected;
200: private VFSFile file;
201:
202: //}}}
203:
204: //{{{ ExpansionToggleBorder class
205: static class ExpansionToggleBorder implements Border {
206: static final Icon COLLAPSED_ICON;
207: static final Icon EXPANDED_ICON;
208: static final int ICON_WIDTH;
209:
210: static final int LEVEL_WIDTH = 15;
211:
212: static final int STATE_NONE = 0;
213: static final int STATE_COLLAPSED = 1;
214: static final int STATE_EXPANDED = 2;
215:
216: //{{{ ExpansionToggleBorder constructor
217: ExpansionToggleBorder(int state, int level) {
218: this .state = state;
219: this .level = level;
220: } //}}}
221:
222: //{{{ paintBorder() method
223: public void paintBorder(Component c, Graphics g, int x, int y,
224: int width, int height) {
225: switch (state) {
226: case STATE_COLLAPSED:
227: COLLAPSED_ICON
228: .paintIcon(c, g, x + level * LEVEL_WIDTH + 2, y
229: + (height - COLLAPSED_ICON
230: .getIconHeight()) / 2);
231: break;
232: case STATE_EXPANDED:
233: EXPANDED_ICON.paintIcon(c, g, x + level * LEVEL_WIDTH
234: + 2, y + 2
235: + (height - EXPANDED_ICON.getIconHeight()) / 2);
236: break;
237: }
238: } //}}}
239:
240: //{{{ getBorderInsets() method
241: public Insets getBorderInsets(Component c) {
242: return new Insets(1, level * LEVEL_WIDTH + ICON_WIDTH + 4,
243: 1, 1);
244: } //}}}
245:
246: //{{{ isBorderOpaque() method
247: public boolean isBorderOpaque() {
248: return false;
249: } //}}}
250:
251: //{{{ isExpansionToggle() method
252: public static boolean isExpansionToggle(int level, int x) {
253: return (x >= level * LEVEL_WIDTH)
254: && (x <= level * LEVEL_WIDTH + ICON_WIDTH);
255: } //}}}
256:
257: //{{{ Private members
258: private int state;
259: private int level;
260:
261: static {
262: COLLAPSED_ICON = GUIUtilities.loadIcon("arrow1.png");
263: EXPANDED_ICON = GUIUtilities.loadIcon("arrow2.png");
264: ICON_WIDTH = Math.max(COLLAPSED_ICON.getIconWidth(),
265: EXPANDED_ICON.getIconWidth());
266: } //}}}
267: } //}}}
268: }
|