1 24 25 package org.gjt.sp.jedit.browser; 26 27 import java.awt.*; 29 import java.awt.font.*; 30 import javax.swing.*; 31 import javax.swing.border.*; 32 import javax.swing.table.*; 33 import org.gjt.sp.jedit.io.VFSFile; 34 import org.gjt.sp.jedit.*; 35 37 41 public class FileCellRenderer extends DefaultTableCellRenderer 42 { 43 public static Icon fileIcon = GUIUtilities.loadIcon("File.png"); 44 public static Icon openFileIcon = GUIUtilities.loadIcon("OpenFile.png"); 45 public static Icon dirIcon = GUIUtilities.loadIcon("Folder.png"); 46 public static Icon openDirIcon = GUIUtilities.loadIcon("OpenFolder.png"); 47 public static Icon filesystemIcon = GUIUtilities.loadIcon("DriveSmall.png"); 48 public static Icon loadingIcon = GUIUtilities.loadIcon("ReloadSmall.png"); 49 50 public FileCellRenderer() 52 { 53 plainFont = UIManager.getFont("Tree.font"); 54 if(plainFont == null) 55 plainFont = jEdit.getFontProperty("metal.secondary.font"); 56 boldFont = plainFont.deriveFont(Font.BOLD); 57 } 59 public Component getTableCellRendererComponent(JTable table, 61 Object value, boolean isSelected, boolean hasFocus, 62 int row, int column) 63 { 64 super.getTableCellRendererComponent(table,value,isSelected, 65 hasFocus,row,column); 66 67 if(value instanceof VFSDirectoryEntryTableModel.Entry) 68 { 69 VFSDirectoryEntryTableModel.Entry entry = 70 (VFSDirectoryEntryTableModel.Entry)value; 71 VFSFile file = entry.dirEntry; 72 73 setFont(file.getType() == VFSFile.FILE 74 ? plainFont : boldFont); 75 76 this.isSelected = isSelected; 77 this.file = file; 78 79 if(column == 0) 80 { 81 String path; 85 if(file.getSymlinkPath() == null) 86 path = file.getPath(); 87 else 88 path = file.getSymlinkPath(); 89 openBuffer = jEdit._getBuffer(path) != null; 90 91 setIcon(showIcons 92 ? getIconForFile(file,entry.expanded, 93 openBuffer) : null); 94 setText(file.getName()); 95 96 int state; 97 if(file.getType() == VFSFile.FILE) 98 state = ExpansionToggleBorder.STATE_NONE; 99 else if(entry.expanded) 100 state = ExpansionToggleBorder.STATE_EXPANDED; 101 else 102 state = ExpansionToggleBorder.STATE_COLLAPSED; 103 104 setBorder(new ExpansionToggleBorder( 105 state,entry.level)); 106 } 107 else 108 { 109 VFSDirectoryEntryTableModel model = (VFSDirectoryEntryTableModel)table.getModel(); 110 String extAttr = model.getExtendedAttribute(column); 111 112 openBuffer = false; 113 setIcon(null); 114 setText(file.getExtendedAttribute(extAttr)); 115 setBorder(new EmptyBorder(1,1,1,1)); 116 } 117 } 118 119 return this; 120 } 122 public void paintComponent(Graphics g) 124 { 125 if(!isSelected) 126 { 127 Color color = file.getColor(); 128 129 setForeground(color == null 130 ? UIManager.getColor("Tree.foreground") 131 : color); 132 } 133 134 super.paintComponent(g); 135 136 if(openBuffer) 137 { 138 Font font = getFont(); 139 140 FontMetrics fm = getFontMetrics(font); 141 int x, y; 142 if(getIcon() == null) 143 { 144 x = 0; 145 y = fm.getAscent() + 2; 146 } 147 else 148 { 149 x = getIcon().getIconWidth() + getIconTextGap(); 150 y = Math.max(fm.getAscent() + 2,16); 151 } 152 153 Insets border = getBorder().getBorderInsets(this); 154 x += border.left; 155 156 g.setColor(getForeground()); 157 g.drawLine(x,y,x + fm.stringWidth(getText()),y); 158 } 159 } 161 165 public static Icon getIconForFile(VFSFile file, 166 boolean expanded) 167 { 168 return getIconForFile(file,expanded, 169 jEdit._getBuffer(file.getSymlinkPath()) != null); 170 } 172 public static Icon getIconForFile(VFSFile file, 174 boolean expanded, boolean openBuffer) 175 { 176 if (defaultIcons) 177 return file.getDefaultIcon(expanded, openBuffer); 178 return file.getIcon(expanded, openBuffer); 179 } 181 Font plainFont; 183 Font boldFont; 184 boolean showIcons; 185 private static boolean defaultIcons = true; 186 187 void propertiesChanged() 189 { 190 showIcons = jEdit.getBooleanProperty("vfs.browser.showIcons"); 191 defaultIcons = jEdit.getBooleanProperty("vfs.browser.useDefaultIcons"); 192 } 194 int getEntryWidth(VFSDirectoryEntryTableModel.Entry entry, 196 Font font, FontRenderContext fontRenderContext) 197 { 198 String name = entry.dirEntry.getName(); 199 int width = (int)font.getStringBounds(name,fontRenderContext) 200 .getWidth(); 201 width += ExpansionToggleBorder.ICON_WIDTH 202 + entry.level * ExpansionToggleBorder.LEVEL_WIDTH 203 + 3; 204 if(showIcons) 205 { 206 width += fileIcon.getIconWidth(); 207 width += getIconTextGap(); 208 } 209 return width; 210 } 212 214 private boolean openBuffer; 216 private boolean isSelected; 217 private VFSFile file; 218 220 static class ExpansionToggleBorder implements Border 222 { 223 static final Icon COLLAPSED_ICON; 224 static final Icon EXPANDED_ICON; 225 static final int ICON_WIDTH; 226 227 static final int LEVEL_WIDTH = 15; 228 229 static final int STATE_NONE = 0; 230 static final int STATE_COLLAPSED = 1; 231 static final int STATE_EXPANDED = 2; 232 233 ExpansionToggleBorder(int state, int level) 235 { 236 this.state = state; 237 this.level = level; 238 } 240 public void paintBorder(Component c, Graphics g, 242 int x, int y, int width, int height) 243 { 244 switch(state) 245 { 246 case STATE_COLLAPSED: 247 COLLAPSED_ICON.paintIcon(c,g, 248 x + level * LEVEL_WIDTH + 2, 249 y + (height - COLLAPSED_ICON.getIconHeight()) / 2); 250 break; 251 case STATE_EXPANDED: 252 EXPANDED_ICON.paintIcon(c,g, 253 x + level * LEVEL_WIDTH + 2, 254 y + 2 + (height - EXPANDED_ICON.getIconHeight()) / 2); 255 break; 256 } 257 } 259 public Insets getBorderInsets(Component c) 261 { 262 return new Insets(1,level * LEVEL_WIDTH 263 + ICON_WIDTH + 4,1,1); 264 } 266 public boolean isBorderOpaque() 268 { 269 return false; 270 } 272 public static boolean isExpansionToggle(int level, int x) 274 { 275 return (x >= level * LEVEL_WIDTH) 276 && (x <= level * LEVEL_WIDTH + ICON_WIDTH); 277 } 279 private int state; 281 private int level; 282 283 static 284 { 285 COLLAPSED_ICON = GUIUtilities.loadIcon("arrow1.png"); 286 EXPANDED_ICON = GUIUtilities.loadIcon("arrow2.png"); 287 ICON_WIDTH = Math.max(COLLAPSED_ICON.getIconWidth(), 288 EXPANDED_ICON.getIconWidth()); 289 } } } 292 | Popular Tags |