comparison dbus-d-javatests/jsrc/filetree/FileTreeLabelProvider.java @ 0:a5576806d36d

recreate repository without any libs for lightweight repository
author Frank Benoit <benoit@tionex.de>
date Sat, 20 Oct 2007 18:07:18 +0200
parents
children 65fb7ef02c50
comparison
equal deleted inserted replaced
-1:000000000000 0:a5576806d36d
1 package filetree;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.eclipse.jface.viewers.ILabelProvider;
10 import org.eclipse.jface.viewers.ILabelProviderListener;
11 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
12 import org.eclipse.swt.graphics.Image;
13 import org.eclipse.swt.program.Program;
14
15 /**
16 * This class provides the labels for the file tree
17 */
18
19 class FileTreeLabelProvider implements ILabelProvider {
20 // The listeners
21 private List<ILabelProviderListener> listeners;
22
23 // Images for tree nodes
24 private Image file;
25
26 private Image dir;
27
28 // Label provider state: preserve case of file names/directories
29 boolean preserveCase;
30
31 /**
32 * Constructs a FileTreeLabelProvider
33 */
34 public FileTreeLabelProvider() {
35 // Create the list to hold the listeners
36 listeners = new ArrayList<ILabelProviderListener>();
37
38 // Create the images
39 try {
40 file = new Image(null, new FileInputStream("icons/file_obj.gif"));
41 dir = new Image(null, new FileInputStream("icons/fldr_obj.gif"));
42 } catch (FileNotFoundException e) {
43 // Swallow it; we'll do without images
44 }
45 }
46
47 /**
48 * Sets the preserve case attribute
49 *
50 * @param preserveCase
51 * the preserve case attribute
52 */
53 public void setPreserveCase(boolean preserveCase) {
54 this.preserveCase = preserveCase;
55
56 // Since this attribute affects how the labels are computed,
57 // notify all the listeners of the change.
58 LabelProviderChangedEvent event = new LabelProviderChangedEvent(this);
59 for (int i = 0, n = listeners.size(); i < n; i++) {
60 ILabelProviderListener ilpl = (ILabelProviderListener) listeners
61 .get(i);
62 ilpl.labelProviderChanged(event);
63 }
64 }
65
66 /**
67 * Gets the image to display for a node in the tree
68 *
69 * @param arg0
70 * the node
71 * @return Image
72 */
73 public Image getImage(Object arg0) {
74 // If the node represents a directory, return the directory image.
75 // Otherwise, return the file image.
76 // Program.findProgram(".txt" ).getImageData();
77 return ((DataItem) arg0).isFolder() ? dir : file;
78 }
79
80 /**
81 * Gets the text to display for a node in the tree
82 *
83 * @param arg0
84 * the node
85 * @return String
86 */
87 public String getText(Object arg0) {
88 // Get the name of the file
89 String text = ((DataItem) arg0).getName();
90
91 // If name is blank, get the path
92 if (text.length() == 0) {
93 //text = ((DataItem) arg0).getPath();
94 }
95 System.out.println( "Text: "+text );
96 // Check the case settings before returning the text
97 return preserveCase ? text : text.toUpperCase();
98 }
99
100 /**
101 * Adds a listener to this label provider
102 *
103 * @param arg0
104 * the listener
105 */
106 public void addListener(ILabelProviderListener arg0) {
107 listeners.add(arg0);
108 }
109
110 /**
111 * Called when this LabelProvider is being disposed
112 */
113 public void dispose() {
114 // Dispose the images
115 if (dir != null)
116 dir.dispose();
117 if (file != null)
118 file.dispose();
119 }
120
121 /**
122 * Returns whether changes to the specified property on the specified
123 * element would affect the label for the element
124 *
125 * @param arg0
126 * the element
127 * @param arg1
128 * the property
129 * @return boolean
130 */
131 public boolean isLabelProperty(Object arg0, String arg1) {
132 return false;
133 }
134
135 /**
136 * Removes the listener
137 *
138 * @param arg0
139 * the listener to remove
140 */
141 public void removeListener(ILabelProviderListener arg0) {
142 listeners.remove(arg0);
143 }
144 }
145