?? fileviewer.java
字號:
/******************************************************************************* * Copyright (c) 2000, 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.swt.examples.fileviewer;import org.eclipse.swt.*;import org.eclipse.swt.custom.*;import org.eclipse.swt.dnd.*;import org.eclipse.swt.events.*;import org.eclipse.swt.graphics.*;import org.eclipse.swt.layout.*;import org.eclipse.swt.program.*;import org.eclipse.swt.widgets.*;import java.io.*;import java.text.*;import java.util.*;/** * File Viewer example */public class FileViewer { private static ResourceBundle resourceBundle = ResourceBundle.getBundle("examples_fileviewer"); 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 [] { FileViewer.getResourceString("table.Name.title"), FileViewer.getResourceString("table.Size.title"), FileViewer.getResourceString("table.Type.title"), FileViewer.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 (); FileViewer application = new FileViewer(); Shell shell = application.open(display); while (! shell.isDisposed()) { if (! display.readAndDispatch()) display.sleep(); } application.close(); display.dispose(); System.out.println("OS:"+System.getProperty("os.name")); } /** * 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) { try { return resourceBundle.getString(key); } catch (MissingResourceException e) { return key; } catch (NullPointerException e) { 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(FileViewer.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); } });
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -