?? fileviewer.java
字號(hào):
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); } return targetFile; } }); return dropTarget; } /** * Handles expand events on a tree item. * * @param item the TreeItem to fill in */ private void treeExpandItem(TreeItem item) { shell.setCursor(iconCache.stockCursors[iconCache.cursorWait]); final Object stub = item.getData(TREEITEMDATA_STUB); if (stub == null) treeRefreshItem(item, true); shell.setCursor(iconCache.stockCursors[iconCache.cursorDefault]); } /** * Traverse the entire tree and update only what has changed. * * @param roots the root directory listing */ private void treeRefresh(File[] masterFiles) { TreeItem[] items = tree.getItems(); int masterIndex = 0; int itemIndex = 0; for (int i = 0; i < items.length; ++i) { final TreeItem item = items[i]; final File itemFile = (File) item.getData(TREEITEMDATA_FILE); if ((itemFile == null) || (masterIndex == masterFiles.length)) { // remove bad item or placeholder item.dispose(); continue; } final File masterFile = masterFiles[masterIndex]; int compare = compareFiles(masterFile, itemFile); if (compare == 0) { // same file, update it treeRefreshItem(item, false); ++itemIndex; ++masterIndex; } else if (compare < 0) { // should appear before file, insert it TreeItem newItem = new TreeItem(tree, SWT.NULL, itemIndex); treeInitVolume(newItem, masterFile); new TreeItem(newItem, SWT.NULL); // placeholder child item to get "expand" button ++itemIndex; ++masterIndex; --i; } else { // should appear after file, delete stale item item.dispose(); } } for (;masterIndex < masterFiles.length; ++masterIndex) { final File masterFile = masterFiles[masterIndex]; TreeItem newItem = new TreeItem(tree, SWT.NULL); treeInitVolume(newItem, masterFile); new TreeItem(newItem, SWT.NULL); // placeholder child item to get "expand" button } } /** * Traverse an item in the tree and update only what has changed. * * @param dirItem the tree item of the directory * @param forcePopulate true iff we should populate non-expanded items as well */ private void treeRefreshItem(TreeItem dirItem, boolean forcePopulate) { final File dir = (File) dirItem.getData(TREEITEMDATA_FILE); if (! forcePopulate && ! dirItem.getExpanded()) { // Refresh non-expanded item if (dirItem.getData(TREEITEMDATA_STUB) != null) { treeItemRemoveAll(dirItem); new TreeItem(dirItem, SWT.NULL); // placeholder child item to get "expand" button dirItem.setData(TREEITEMDATA_STUB, null); } return; } // Refresh expanded item dirItem.setData(TREEITEMDATA_STUB, this); // clear stub flag /* Get directory listing */ File[] subFiles = (dir != null) ? FileViewer.getDirectoryList(dir) : null; if (subFiles == null || subFiles.length == 0) { /* Error or no contents */ treeItemRemoveAll(dirItem); dirItem.setExpanded(false); return; } /* Refresh sub-items */ TreeItem[] items = dirItem.getItems(); final File[] masterFiles = subFiles; int masterIndex = 0; int itemIndex = 0; File masterFile = null; for (int i = 0; i < items.length; ++i) { while ((masterFile == null) && (masterIndex < masterFiles.length)) { masterFile = masterFiles[masterIndex++]; if (! masterFile.isDirectory()) masterFile = null; } final TreeItem item = items[i]; final File itemFile = (File) item.getData(TREEITEMDATA_FILE); if ((itemFile == null) || (masterFile == null)) { // remove bad item or placeholder item.dispose(); continue; } int compare = compareFiles(masterFile, itemFile); if (compare == 0) { // same file, update it treeRefreshItem(item, false); masterFile = null; ++itemIndex; } else if (compare < 0) { // should appear before file, insert it TreeItem newItem = new TreeItem(dirItem, SWT.NULL, itemIndex); treeInitFolder(newItem, masterFile); new TreeItem(newItem, SWT.NULL); // add a placeholder child item so we get the "expand" button masterFile = null; ++itemIndex; --i; } else { // should appear after file, delete stale item item.dispose(); } } while ((masterFile != null) || (masterIndex < masterFiles.length)) { if (masterFile != null) { TreeItem newItem = new TreeItem(dirItem, SWT.NULL); treeInitFolder(newItem, masterFile); new TreeItem(newItem, SWT.NULL); // add a placeholder child item so we get the "expand" button if (masterIndex == masterFiles.length) break; } masterFile = masterFiles[masterIndex++]; if (! masterFile.isDirectory()) masterFile = null; } } /** * Foreign method: removes all children of a TreeItem. * @param treeItem the TreeItem */ private static void treeItemRemoveAll(TreeItem treeItem) { final TreeItem[] children = treeItem.getItems(); for (int i = 0; i < children.length; ++i) { children[i].dispose(); } } /** * Initializes a folder item. * * @param item the TreeItem to initialize * @param folder the File associated with this TreeItem */ private void treeInitFolder(TreeItem item, File folder) { item.setText(folder.getName()); item.setImage(iconCache.stockImages[iconCache.iconClosedFolder]); item.setData(TREEITEMDATA_FILE, folder); item.setData(TREEITEMDATA_IMAGEEXPANDED, iconCache.stockImages[iconCache.iconOpenFolder]); item.setData(TREEITEMDATA_IMAGECOLLAPSED, iconCache.stockImages[iconCache.iconClosedFolder]); } /** * Initializes a volume item. * * @param item the TreeItem to initialize * @param volume the File associated with this TreeItem */ private void treeInitVolume(TreeItem item, File volume) { item.setText(volume.getPath()); item.setImage(iconCache.stockImages[iconCache.iconClosedDrive]); item.setData(TREEITEMDATA_FILE, volume); item.setData(TREEITEMDATA_IMAGEEXPANDED, iconCache.stockImages[iconCache.iconOpenDrive]); item.setData(TREEITEMDATA_IMAGECOLLAPSED, iconCache.stockImages[iconCache.iconClosedDrive]); } /** * Creates the file details table. * * @param parent the parent control */ private void createTableView(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); tableContentsOfLabel = new Label(composite, SWT.BORDER); tableContentsOfLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); table = new Table(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION); table.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL)); for (int i = 0; i < tableTitles.length; ++i) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(tableTitles[i]); column.setWidth(tableWidths[i]); } table.setHeaderVisible(true); table.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { notifySelectedFiles(getSelectedFiles()); } public void widgetDefaultSelected(SelectionEvent event) { doDefaultFileAction(getSelectedFiles()); } private File[] getSelectedFiles() { final TableItem[] items = table.getSelection(); final File[] files = new File[items.length]; for (int i = 0; i < items.length; ++i) { files[i] = (File) items[i].getData(TABLEITEMDATA_FILE); } return files; } }); createTableDragSource(table); createTableDropTarget(table); } /** * Creates the Drag & Drop DragSource for items being dragged from the table. * * @return the DragSource for the table */ private DragSource createTableDragSource(final Table table) { DragSource dragSource = new DragSource(table, DND.DROP_MOVE | DND.DROP_COPY); dragSource.setTransfer(new Transfer[] { FileTransfer.getInstance() }); dragSource.addDragListener(new DragSourceListener() { TableItem[] dndSelection = null; String[] sourceNames = null; public void dragStart(DragSourceEvent event){ dndSelection = table.getSelection(); sourceNames = null; event.doit = dndSelection.length > 0; isDragging = true; } public void dragFinished(DragSourceEvent event){ dragSourceHandleDragFinished(event, sourceNames); dndSelection = null; sourceNames = null; isDragging = false; 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(TABLEITEMDATA_FILE); sourceNames[i] = file.getAbsolutePath(); } event.data = sourceNames; } }); return dragSource; } /** * Creates the Drag & Drop DropTarget for items being dropped onto the table. * * @return the DropTarget for the table */ private DropTarget createTableDropTarget(final Table table){ DropTarget dropTarget = new DropTarget(table, 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 TableItem item = table.getItem(table.toControl(new Point(event.x, event.y))); File targetFile = null; if (item == null) { // We are over an unoccupied area of the table. // If it is a COPY, we can use the table's root file. if (event.detail == DND.DROP_COPY) { targetFile = (File) table.getData(TABLEDATA_DIR); } } else { // We are over a particular item in the table, use the item's file targetFile = (File) item.getData(TABLEITEMDATA_FILE); } return targetFile; } }); return dropTarget; } /** * Notifies the application components that a new current directory has been selected * * @param dir the directory that was selected, null is ignored */ void notifySelectedDirectory(File dir) { if (dir == null) return; if (currentDirectory != null && dir.equals(currentDirectory)) return; currentDirectory = dir; notifySelectedFiles(null); /* Shell: * Sets the title to indicate the selected directory */ shell.setText(getResourceString("Title", new Object[] { currentDirectory.getPath() })); /* Table view: * Displays the contents of the selected directory. */ workerUpdate(dir, false); /* Combo view: * Sets the combo box to point to the selected directory. */ final File[] comboRoots = (File[]) combo.getData(COMBODATA_ROOTS); int comboEntry = -1; if (comboRoots != null) { for (int i = 0; i < comboRoots.length; ++i) { if (dir.equals(comboRoots[i])) { comboEntry = i; break; } } } if (comboEntry == -1) combo.setText(dir.getPath()); else combo.select(comboEntry); /* Tree view: * If not already expanded, recursively expands the parents of the specified * directory until it is visible.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -