comparison dbus-d-javatests/jsrc/testapp/FileBrowserSample.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 /*******************************************************************************
2 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
3 *
4 * Created on Mar 9, 2004 7:07:16 PM by JACK $Id$
5 *
6 ******************************************************************************/
7
8 package testapp;
9
10 import java.io.File;
11
12 import org.eclipse.jface.action.Action;
13 import org.eclipse.jface.action.ToolBarManager;
14 import org.eclipse.jface.resource.ImageRegistry;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.TreeEditor;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.events.SelectionListener;
19 import org.eclipse.swt.events.TreeEvent;
20 import org.eclipse.swt.events.TreeListener;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.ImageData;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.program.Program;
27 import org.eclipse.swt.widgets.DirectoryDialog;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Listener;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.swt.widgets.ToolBar;
34 import org.eclipse.swt.widgets.Tree;
35 import org.eclipse.swt.widgets.TreeItem;
36
37 public class FileBrowserSample {
38 Display display = new Display();
39 Shell shell = new Shell(display);
40
41 Tree tree;
42
43 File rootDir;
44
45 public FileBrowserSample() {
46 Action actionSetRootDir = new Action("Set Root Dir") {
47 public void run() {
48 DirectoryDialog dialog = new DirectoryDialog(shell);
49 String path = dialog.open();
50 if (path != null) {
51 setRootDir(new File(path));
52 }
53 }
54 };
55
56
57 ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
58 ToolBarManager manager = new ToolBarManager(toolBar);
59 manager.add(actionSetRootDir);
60
61 manager.update(true);
62
63 shell.setLayout(new GridLayout());
64
65 tree = new Tree(shell, SWT.BORDER);
66 tree.setLayoutData(new GridData(GridData.FILL_BOTH));
67
68 setRootDir(new File("C:/"));
69
70 tree.addTreeListener(new TreeListener() {
71 public void treeCollapsed(TreeEvent e) {
72 }
73
74 public void treeExpanded(TreeEvent e) {
75 TreeItem item = (TreeItem) e.item;
76 TreeItem[] children = item.getItems();
77
78 for (int i = 0; i < children.length; i++)
79 if (children[i].getData() == null)
80 children[i].dispose();
81 else // Child files already added to the tree.
82 return;
83
84 File[] files = ((File) item.getData()).listFiles();
85 for (int i = 0; files != null && i < files.length; i++)
86 addFileToTree(item, files[i]);
87 }
88 });
89
90 tree.addSelectionListener(new SelectionListener() {
91 public void widgetSelected(SelectionEvent e) {
92 }
93 // Gets called when a tree item is double-clicked.
94 public void widgetDefaultSelected(SelectionEvent e) {
95 TreeItem item = (TreeItem) e.item;
96 File file = (File) item.getData();
97 if (Program.launch(file.getAbsolutePath())) {
98 System.out.println("File has been launched: " + file);
99 } else {
100 System.out.println("Unable to launch file: " + file);
101 }
102 }
103 });
104
105 shell.setSize(400, 260);
106 shell.open();
107 //textUser.forceFocus();
108
109 // Set up the event loop.
110 while (!shell.isDisposed()) {
111 if (!display.readAndDispatch()) {
112 // If no more entries in event queue
113 display.sleep();
114 }
115 }
116
117 display.dispose();
118 }
119
120 /**
121 * Sets the root directory to browse from.
122 *
123 * @param root
124 */
125 private void setRootDir(File root) {
126 // validates the root first.
127 if( (!root.isDirectory()) || (!root.exists()))
128 throw new IllegalArgumentException("Invalid root: " + root);
129
130 this.rootDir = root;
131 shell.setText("Now browsing: " + root.getAbsolutePath());
132
133 // Remove all the items in the tree.
134 if (tree.getItemCount() > 0) {
135 TreeItem[] items = tree.getItems();
136 for (int i = 0; i < items.length; i++) {
137 items[i].dispose();
138 // Dispose itself and all of its descendants.
139 }
140 }
141
142 // Adds files under the root to the tree.
143 File[] files = root.listFiles();
144 for(int i=0; files != null && i < files.length; i++)
145 addFileToTree(tree, files[i]);
146 }
147
148 /**
149 * Adds the given file to the tree.
150 *
151 * @param parent
152 * @param file
153 */
154 private void addFileToTree(Object parent, File file) {
155 TreeItem item = null;
156
157 if (parent instanceof Tree)
158 item = new TreeItem((Tree) parent, SWT.NULL);
159 else if (parent instanceof TreeItem)
160 item = new TreeItem((TreeItem) parent, SWT.NULL);
161 else
162 throw new IllegalArgumentException(
163 "parent should be a tree or a tree item: " + parent);
164
165 item.setText(file.getName());
166 item.setImage(getIcon(file));
167
168 item.setData(file);
169
170 if (file.isDirectory()) {
171 // // recursively adds all the children of this file.
172 // File[] files = file.listFiles();
173 // for(int i=0; i<files.length; i++)
174 // buildTree(item, files[i]);
175 if (file.list() != null && file.list().length > 0)
176 new TreeItem(item, SWT.NULL);
177 }
178 }
179
180 private ImageRegistry imageRegistry;
181 Image iconFolder = new Image(shell.getDisplay(), "icons/java2s.gif");
182 Image iconFile = new Image(shell.getDisplay(), "icons/java2s.gif");
183
184 /**
185 * Returns an icon representing the specified file.
186 *
187 * @param file
188 * @return
189 */
190 private Image getIcon(File file) {
191 if (file.isDirectory())
192 return iconFolder;
193
194 int lastDotPos = file.getName().indexOf('.');
195 if (lastDotPos == -1)
196 return iconFile;
197
198 Image image = getIcon(file.getName().substring(lastDotPos + 1));
199 return image == null ? iconFile : image;
200 }
201
202 /**
203 * Returns the icon for the file type with the specified extension.
204 *
205 * @param extension
206 * @return
207 */
208 private Image getIcon(String extension) {
209 if (imageRegistry == null)
210 imageRegistry = new ImageRegistry();
211 Image image = imageRegistry.get(extension);
212 if (image != null)
213 return image;
214
215 Program program = Program.findProgram(extension);
216 ImageData imageData = (program == null ? null : program.getImageData());
217 if (imageData != null) {
218 image = new Image(shell.getDisplay(), imageData);
219 imageRegistry.put(extension, image);
220 } else {
221 image = iconFile;
222 }
223
224 return image;
225 }
226
227 public static void main(String[] args) {
228 new FileBrowserSample();
229 }
230 }
231