1 11 package org.eclipse.ui.dialogs; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.ui.ISharedImages; 17 import org.eclipse.ui.internal.WorkbenchImages; 18 import org.eclipse.ui.internal.WorkbenchPlugin; 19 import org.eclipse.ui.model.AdaptableList; 20 import org.eclipse.ui.model.IWorkbenchAdapter; 21 22 36 public class FileSystemElement implements IAdaptable { 37 private String name; 38 39 private Object fileSystemObject; 40 41 46 private AdaptableList folders = null; 47 48 private AdaptableList files = null; 49 50 private boolean isDirectory = false; 51 52 private FileSystemElement parent; 53 54 private IWorkbenchAdapter workbenchAdapter = new IWorkbenchAdapter() { 55 58 public Object [] getChildren(Object o) { 59 return getFolders().getChildren(o); 60 } 61 62 65 public Object getParent(Object o) { 66 return parent; 67 } 68 69 72 public String getLabel(Object o) { 73 return name; 74 } 75 76 79 public ImageDescriptor getImageDescriptor(Object object) { 80 if (isDirectory()) { 81 return WorkbenchImages 82 .getImageDescriptor(ISharedImages.IMG_OBJ_FOLDER); 83 } else { 84 return WorkbenchPlugin.getDefault().getEditorRegistry() 85 .getImageDescriptor(name); 86 } 88 } 89 }; 90 91 103 public FileSystemElement(String name, FileSystemElement parent, 104 boolean isDirectory) { 105 this.name = name; 106 this.parent = parent; 107 this.isDirectory = isDirectory; 108 if (parent != null) { 109 parent.addChild(this); 110 } 111 } 112 113 119 public void addChild(FileSystemElement child) { 120 if (child.isDirectory()) { 121 if (folders == null) { 122 folders = new AdaptableList(1); 123 } 124 folders.add(child); 125 } else { 126 if (files == null) { 127 files = new AdaptableList(1); 128 } 129 files.add(child); 130 } 131 } 132 133 136 public Object getAdapter(Class adapter) { 137 if (adapter == IWorkbenchAdapter.class) { 138 return workbenchAdapter; 139 } 140 return Platform.getAdapterManager().getAdapter(this, adapter); 142 } 143 144 149 public String getFileNameExtension() { 150 int lastDot = name.lastIndexOf('.'); 151 return lastDot < 0 ? "" : name.substring(lastDot + 1); } 153 154 161 public AdaptableList getFiles() { 162 if (files == null) { 163 files = new AdaptableList(0); 165 } 166 return files; 167 } 168 169 174 public Object getFileSystemObject() { 175 return fileSystemObject; 176 } 177 178 186 public AdaptableList getFolders() { 187 if (folders == null) { 188 folders = new AdaptableList(0); 190 } 191 return folders; 192 } 193 194 200 public FileSystemElement getParent() { 201 return this.parent; 202 } 203 204 208 public boolean isDirectory() { 209 return isDirectory; 210 } 211 212 216 public void removeFolder(FileSystemElement child) { 217 if (folders == null) { 218 return; 219 } 220 folders.remove(child); 221 child.setParent(null); 222 } 223 224 230 public void setFileSystemObject(Object value) { 231 fileSystemObject = value; 232 } 233 234 238 public void setParent(FileSystemElement element) { 239 parent = element; 240 } 241 242 245 public String toString() { 246 StringBuffer buf = new StringBuffer (); 247 if (isDirectory()) { 248 buf.append("Folder("); } else { 250 buf.append("File("); } 252 buf.append(name).append(")"); if (!isDirectory()) { 254 return buf.toString(); 255 } 256 buf.append(" folders: "); buf.append(folders); 258 buf.append(" files: "); buf.append(files); 260 return buf.toString(); 261 } 262 } 263 | Popular Tags |