|
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Vector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.TreeAdapter;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
/**
* File Viewer example
*/
public class SWTFileViewerDemo {
private final static String DRIVE_A = "a:" + File.separator;
private final static String DRIVE_B = "b:" + File.separator;
/* UI elements */
private Display display;
private Shell shell;
private ToolBar toolBar;
private Label numObjectsLabel;
private Label diskSpaceLabel;
private File currentDirectory = null;
private boolean initial = true;
/* Drag and drop optimizations */
private boolean isDragging = false; // if this app is dragging
private boolean isDropping = false; // if this app is dropping
private File[] processedDropFiles = null; // so Drag only deletes what it
// needs to
private File[] deferredRefreshFiles = null; // to defer notifyRefreshFiles
// while we do DND
private boolean deferredRefreshRequested = false; // to defer
// notifyRefreshFiles
// while we do DND
private ProgressDialog progressDialog = null; // progress dialog for
// locally-initiated
// operations
/* Combo view */
private static final String COMBODATA_ROOTS = "Combo.roots";
// File[]: Array of files whose paths are currently displayed in the combo
private static final String COMBODATA_LASTTEXT = "Combo.lastText";
// String: Previous selection text string
private Combo combo;
/* Tree view */
private IconCache iconCache = new IconCache();
private static final String TREEITEMDATA_FILE = "TreeItem.file";
// File: File associated with tree item
private static final String TREEITEMDATA_IMAGEEXPANDED = "TreeItem.imageExpanded";
// Image: shown when item is expanded
private static final String TREEITEMDATA_IMAGECOLLAPSED = "TreeItem.imageCollapsed";
// Image: shown when item is collapsed
private static final String TREEITEMDATA_STUB = "TreeItem.stub";
// Object: if not present or null then the item has not been populated
private Tree tree;
private Label treeScopeLabel;
/* Table view */
private static final DateFormat dateFormat = DateFormat
.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
private static final String TABLEITEMDATA_FILE = "TableItem.file";
// File: File associated with table row
private static final String TABLEDATA_DIR = "Table.dir";
// File: Currently visible directory
private static final int[] tableWidths = new int[] { 150, 60, 75, 150 };
private final String[] tableTitles = new String[] {
SWTFileViewerDemo.getResourceString("table.Name.title"),
SWTFileViewerDemo.getResourceString("table.Size.title"),
SWTFileViewerDemo.getResourceString("table.Type.title"),
SWTFileViewerDemo.getResourceString("table.Modified.title") };
private Table table;
private Label tableContentsOfLabel;
/* Table update worker */
// Control data
private final Object workerLock = new Object();
// Lock for all worker control data and state
private volatile Thread workerThread = null;
// The worker's thread
private volatile boolean workerStopped = false;
// True if the worker must exit on completion of the current cycle
private volatile boolean workerCancelled = false;
// True if the worker must cancel its operations prematurely perhaps due to
// a state update
// Worker state information -- this is what gets synchronized by an update
private volatile File workerStateDir = null;
// State information to use for the next cycle
private volatile File workerNextDir = null;
/* Simulate only flag */
// when true, disables actual filesystem manipulations and outputs results
// to standard out
private boolean simulateOnly = true;
/**
* Runs main program.
*/
public static void main(String[] args) {
Display display = new Display();
SWTFileViewerDemo application = new SWTFileViewerDemo();
Shell shell = application.open(display);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
application.close();
display.dispose();
}
/**
* Opens the main program.
*/
public Shell open(Display display) {
// Create the window
this.display = display;
iconCache.initResources(display);
shell = new Shell();
createShellContents();
notifyRefreshFiles(null);
shell.open();
return shell;
}
/**
* Closes the main program.
*/
void close() {
workerStop();
iconCache.freeResources();
}
/**
* Returns a string from the resource bundle. We don't want to crash because
* of a missing String. Returns the key if not found.
*/
static String getResourceString(String key) {
return key;
}
/**
* Returns a string from the resource bundle and binds it with the given
* arguments. If the key is not found, return the key.
*/
static String getResourceString(String key, Object[] args) {
try {
return MessageFormat.format(getResourceString(key), args);
} catch (MissingResourceException e) {
return key;
} catch (NullPointerException e) {
return "!" + key + "!";
}
}
/**
* Construct the UI
*
* @param container
* the ShellContainer managing the Shell we are rendering inside
*/
private void createShellContents() {
shell.setText(getResourceString("Title", new Object[] { "" }));
shell.setImage(iconCache.stockImages[iconCache.shellIcon]);
Menu bar = new Menu(shell, SWT.BAR);
shell.setMenuBar(bar);
createFileMenu(bar);
createHelpMenu(bar);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
gridLayout.marginHeight = gridLayout.marginWidth = 0;
shell.setLayout(gridLayout);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.widthHint = 185;
createComboView(shell, gridData);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
createToolBar(shell, gridData);
SashForm sashForm = new SashForm(shell, SWT.NONE);
sashForm.setOrientation(SWT.HORIZONTAL);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL);
gridData.horizontalSpan = 3;
sashForm.setLayoutData(gridData);
createTreeView(sashForm);
createTableView(sashForm);
sashForm.setWeights(new int[] { 2, 5 });
numObjectsLabel = new Label(shell, SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_FILL);
gridData.widthHint = 185;
numObjectsLabel.setLayoutData(gridData);
diskSpaceLabel = new Label(shell, SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
diskSpaceLabel.setLayoutData(gridData);
}
/**
* Creates the File Menu.
*
* @param parent
* the parent menu
*/
private void createFileMenu(Menu parent) {
Menu menu = new Menu(parent);
MenuItem header = new MenuItem(parent, SWT.CASCADE);
header.setText(getResourceString("menu.File.text"));
header.setMenu(menu);
final MenuItem simulateItem = new MenuItem(menu, SWT.CHECK);
simulateItem.setText(getResourceString("menu.File.SimulateOnly.text"));
simulateItem.setSelection(simulateOnly);
simulateItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
simulateOnly = simulateItem.getSelection();
}
});
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(getResourceString("menu.File.Close.text"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
}
/**
* Creates the Help Menu.
*
* @param parent
* the parent menu
*/
private void createHelpMenu(Menu parent) {
Menu menu = new Menu(parent);
MenuItem header = new MenuItem(parent, SWT.CASCADE);
header.setText(getResourceString("menu.Help.text"));
header.setMenu(menu);
MenuItem item = new MenuItem(menu, SWT.PUSH);
item.setText(getResourceString("menu.Help.About.text"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
MessageBox box = new MessageBox(shell, SWT.ICON_INFORMATION
| SWT.OK);
box.setText(getResourceString("dialog.About.title"));
box.setMessage(getResourceString("dialog.About.description",
new Object[] { System.getProperty("os.name") }));
box.open();
}
});
}
/**
* Creates the toolbar
*
* @param shell
* the shell on which to attach the toolbar
* @param layoutData
* the layout data
*/
private void createToolBar(final Shell shell, Object layoutData) {
toolBar = new ToolBar(shell, SWT.NULL);
toolBar.setLayoutData(layoutData);
ToolItem item = new ToolItem(toolBar, SWT.SEPARATOR);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdParent]);
item.setToolTipText(getResourceString("tool.Parent.tiptext"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
doParent();
}
});
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdRefresh]);
item.setToolTipText(getResourceString("tool.Refresh.tiptext"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
doRefresh();
}
});
SelectionAdapter unimplementedListener = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
MessageBox box = new MessageBox(shell, SWT.ICON_INFORMATION
| SWT.OK);
box.setText(getResourceString("dialog.NotImplemented.title"));
box
.setMessage(getResourceString("dialog.ActionNotImplemented.description"));
box.open();
}
};
item = new ToolItem(toolBar, SWT.SEPARATOR);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdCut]);
item.setToolTipText(getResourceString("tool.Cut.tiptext"));
item.addSelectionListener(unimplementedListener);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdCopy]);
item.setToolTipText(getResourceString("tool.Copy.tiptext"));
item.addSelectionListener(unimplementedListener);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdPaste]);
item.setToolTipText(getResourceString("tool.Paste.tiptext"));
item.addSelectionListener(unimplementedListener);
item = new ToolItem(toolBar, SWT.SEPARATOR);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdDelete]);
item.setToolTipText(getResourceString("tool.Delete.tiptext"));
item.addSelectionListener(unimplementedListener);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdRename]);
item.setToolTipText(getResourceString("tool.Rename.tiptext"));
item.addSelectionListener(unimplementedListener);
item = new ToolItem(toolBar, SWT.SEPARATOR);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdSearch]);
item.setToolTipText(getResourceString("tool.Search.tiptext"));
item.addSelectionListener(unimplementedListener);
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(iconCache.stockImages[iconCache.cmdPrint]);
item.setToolTipText(getResourceString("tool.Print.tiptext"));
item.addSelectionListener(unimplementedListener);
}
/**
* Creates the combo box view.
*
* @param parent
* the parent control
*/
private void createComboView(Composite parent, Object layoutData) {
combo = new Combo(parent, SWT.NONE);
combo.setLayoutData(layoutData);
combo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
final File[] roots = (File[]) combo.getData(COMBODATA_ROOTS);
if (roots == null)
return;
int selection = combo.getSelectionIndex();
if (selection >= 0 && selection < roots.length) {
notifySelectedDirectory(roots[selection]);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
final String lastText = (String) combo
.getData(COMBODATA_LASTTEXT);
String text = combo.getText();
if (text == null)
return;
if (lastText != null && lastText.equals(text))
return;
combo.setData(COMBODATA_LASTTEXT, text);
notifySelectedDirectory(new File(text));
}
});
}
/**
* Creates the file tree view.
*
* @param parent
* the parent control
*/
private void createTreeView(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
gridLayout.marginHeight = gridLayout.marginWidth = 2;
gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;
composite.setLayout(gridLayout);
treeScopeLabel = new Label(composite, SWT.BORDER);
treeScopeLabel.setText(SWTFileViewerDemo
.getResourceString("details.AllFolders.text"));
treeScopeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_FILL));
tree = new Tree(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
| SWT.SINGLE);
tree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.FILL_VERTICAL));
tree.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
final TreeItem[] selection = tree.getSelection();
if (selection != null && selection.length != 0) {
TreeItem item = selection[0];
File file = (File) item.getData(TREEITEMDATA_FILE);
notifySelectedDirectory(file);
}
}
public void widgetDefaultSelected(SelectionEvent event) {
final TreeItem[] selection = tree.getSelection();
if (selection != null && selection.length != 0) {
TreeItem item = selection[0];
item.setExpanded(true);
treeExpandItem(item);
}
}
});
tree.addTreeListener(new TreeAdapter() {
public void treeExpanded(TreeEvent event) {
final TreeItem item = (TreeItem) event.item;
final Image image = (Image) item
.getData(TREEITEMDATA_IMAGEEXPANDED);
if (image != null)
item.setImage(image);
treeExpandItem(item);
}
public void treeCollapsed(TreeEvent event) {
final TreeItem item = (TreeItem) event.item;
final Image image = (Image) item
.getData(TREEITEMDATA_IMAGECOLLAPSED);
if (image != null)
item.setImage(image);
}
});
createTreeDragSource(tree);
createTreeDropTarget(tree);
}
/**
* Creates the Drag & Drop DragSource for items being dragged from the tree.
*
* @return the DragSource for the tree
*/
private DragSource createTreeDragSource(final Tree tree) {
DragSource dragSource = new DragSource(tree, DND.DROP_MOVE
| DND.DROP_COPY);
dragSource.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dragSource.addDragListener(new DragSourceListener() {
TreeItem[] dndSelection = null;
String[] sourceNames = null;
public void dragStart(DragSourceEvent event) {
dndSelection = tree.getSelection();
sourceNames = null;
event.doit = dndSelection.length > 0;
isDragging = true;
processedDropFiles = null;
}
public void dragFinished(DragSourceEvent event) {
dragSourceHandleDragFinished(event, sourceNames);
dndSelection = null;
sourceNames = null;
isDragging = false;
processedDropFiles = null;
handleDeferredRefresh();
}
public void dragSetData(DragSourceEvent event) {
if (dndSelection == null || dndSelection.length == 0)
return;
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
sourceNames = new String[dndSelection.length];
for (int i = 0; i < dndSelection.length; i++) {
File file = (File) dndSelection[i]
.getData(TREEITEMDATA_FILE);
sourceNames[i] = file.getAbsolutePath();
}
event.data = sourceNames;
}
});
return dragSource;
}
/**
* Creates the Drag & Drop DropTarget for items being dropped onto the tree.
*
* @return the DropTarget for the tree
*/
private DropTarget createTreeDropTarget(final Tree tree) {
DropTarget dropTarget = new DropTarget(tree, DND.DROP_MOVE
| DND.DROP_COPY);
dropTarget.setTransfer(new Transfer[] { FileTransfer.getInstance() });
dropTarget.addDropListener(new DropTargetAdapter() {
public void dragEnter(DropTargetEvent event) {
isDropping = true;
}
public void dragLeave(DropTargetEvent event) {
isDropping = false;
handleDeferredRefresh();
}
public void dragOver(DropTargetEvent event) {
dropTargetValidate(event, getTargetFile(event));
event.feedback |= DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
}
public void drop(DropTargetEvent event) {
File targetFile = getTargetFile(event);
if (dropTargetValidate(event, targetFile))
dropTargetHandleDrop(event, targetFile);
}
private File getTargetFile(DropTargetEvent event) {
// Determine the target File for the drop
TreeItem item = tree.getItem(tree.toControl(new Point(event.x,
event.y)));
File targetFile = null;
if (item != null) {
// We are over a particular item in the tree, use the item's
// file
targetFile = (File) item.getData(TREEITEMDATA_FILE);
}
&nbs
|