`
Arizona
  • 浏览: 32258 次
  • 性别: Icon_minigender_2
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Check File Tree

阅读更多
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * This class demonstrates the CheckboxTreeViewer
 */
public class CheckFileTree extends FileTree {
  /**
   * Configures the shell
   * 
   * @param shell
   *            the shell
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText("Check File Tree");
  }

  /**
   * Creates the main window's contents
   * 
   * @param parent
   *            the main window
   * @return Control
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));

    // Add a checkbox to toggle whether the labels preserve case
    Button preserveCase = new Button(composite, SWT.CHECK);
    preserveCase.setText("&Preserve case");

    // Create the tree viewer to display the file tree
    final CheckboxTreeViewer tv = new CheckboxTreeViewer(composite);
    tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
    tv.setContentProvider(new FileTreeContentProvider());
    tv.setLabelProvider(new FileTreeLabelProvider());
    tv.setInput("root"); // pass a non-null that will be ignored

    // When user checks the checkbox, toggle the preserve case attribute
    // of the label provider
    preserveCase.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        boolean preserveCase = ((Button) event.widget).getSelection();
        FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv
            .getLabelProvider();
        ftlp.setPreserveCase(preserveCase);
      }
    });

    // When user checks a checkbox in the tree, check all its children
    tv.addCheckStateListener(new ICheckStateListener() {
      public void checkStateChanged(CheckStateChangedEvent event) {
        // If the item is checked . . .
        if (event.getChecked()) {
          // . . . check all its children
          tv.setSubtreeChecked(event.getElement(), true);
        }
      }
    });
    return composite;
  }

  /**
   * The application entry point
   * 
   * @param args
   *            the command line arguments
   */
  public static void main(String[] args) {
    new CheckFileTree().run();
  }
}

/**
 * This class demonstrates TreeViewer. It shows the drives, directories, and
 * files on the system.
 */

class FileTree extends ApplicationWindow {
  /**
   * FileTree constructor
   */
  public FileTree() {
    super(null);
  }

  /**
   * Runs the application
   */
  public void run() {
    // Don't return from open() until window closes
    setBlockOnOpen(true);

    // Open the main window
    open();

    // Dispose the display
    Display.getCurrent().dispose();
  }

  /**
   * Configures the shell
   * 
   * @param shell
   *            the shell
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);

    // Set the title bar text and the size
    shell.setText("File Tree");
    shell.setSize(400, 400);
  }

  /**
   * Creates the main window's contents
   * 
   * @param parent
   *            the main window
   * @return Control
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));

    // Add a checkbox to toggle whether the labels preserve case
    Button preserveCase = new Button(composite, SWT.CHECK);
    preserveCase.setText("&Preserve case");

    // Create the tree viewer to display the file tree
    final TreeViewer tv = new TreeViewer(composite);
    tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
    tv.setContentProvider(new FileTreeContentProvider());
    tv.setLabelProvider(new FileTreeLabelProvider());
    tv.setInput("root"); // pass a non-null that will be ignored

    // When user checks the checkbox, toggle the preserve case attribute
    // of the label provider
    preserveCase.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        boolean preserveCase = ((Button) event.widget).getSelection();
        FileTreeLabelProvider ftlp = (FileTreeLabelProvider) tv
            .getLabelProvider();
        ftlp.setPreserveCase(preserveCase);
      }
    });
    return composite;
  }

}

/**
 * This class provides the content for the tree in FileTree
 */

class FileTreeContentProvider implements ITreeContentProvider {
  /**
   * Gets the children of the specified object
   * 
   * @param arg0
   *            the parent object
   * @return Object[]
   */
  public Object[] getChildren(Object arg0) {
    // Return the files and subdirectories in this directory
    return ((File) arg0).listFiles();
  }

  /**
   * Gets the parent of the specified object
   * 
   * @param arg0
   *            the object
   * @return Object
   */
  public Object getParent(Object arg0) {
    // Return this file's parent file
    return ((File) arg0).getParentFile();
  }

  /**
   * Returns whether the passed object has children
   * 
   * @param arg0
   *            the parent object
   * @return boolean
   */
  public boolean hasChildren(Object arg0) {
    // Get the children
    Object[] obj = getChildren(arg0);

    // Return whether the parent has children
    return obj == null ? false : obj.length > 0;
  }

  /**
   * Gets the root element(s) of the tree
   * 
   * @param arg0
   *            the input data
   * @return Object[]
   */
  public Object[] getElements(Object arg0) {
    // These are the root elements of the tree
    // We don't care what arg0 is, because we just want all
    // the root nodes in the file system
    return File.listRoots();
  }

  /**
   * Disposes any created resources
   */
  public void dispose() {
    // Nothing to dispose
  }

  /**
   * Called when the input changes
   * 
   * @param arg0
   *            the viewer
   * @param arg1
   *            the old input
   * @param arg2
   *            the new input
   */
  public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
    // Nothing to change
  }
}

/**
 * This class provides the labels for the file tree
 */

class FileTreeLabelProvider implements ILabelProvider {
  // The listeners
  private List listeners;

  // Images for tree nodes
  private Image file;

  private Image dir;

  // Label provider state: preserve case of file names/directories
  boolean preserveCase;

  /**
   * Constructs a FileTreeLabelProvider
   */
  public FileTreeLabelProvider() {
    // Create the list to hold the listeners
    listeners = new ArrayList();

    // Create the images
    try {
      file = new Image(null, new FileInputStream("images/file.gif"));
      dir = new Image(null, new FileInputStream("images/directory.gif"));
    } catch (FileNotFoundException e) {
      // Swallow it; we'll do without images
    }
  }

  /**
   * Sets the preserve case attribute
   * 
   * @param preserveCase
   *            the preserve case attribute
   */
  public void setPreserveCase(boolean preserveCase) {
    this.preserveCase = preserveCase;

    // Since this attribute affects how the labels are computed,
    // notify all the listeners of the change.
    LabelProviderChangedEvent event = new LabelProviderChangedEvent(this);
    for (int i = 0, n = listeners.size(); i < n; i++) {
      ILabelProviderListener ilpl = (ILabelProviderListener) listeners
          .get(i);
      ilpl.labelProviderChanged(event);
    }
  }

  /**
   * Gets the image to display for a node in the tree
   * 
   * @param arg0
   *            the node
   * @return Image
   */
  public Image getImage(Object arg0) {
    // If the node represents a directory, return the directory image.
    // Otherwise, return the file image.
    return ((File) arg0).isDirectory() ? dir : file;
  }

  /**
   * Gets the text to display for a node in the tree
   * 
   * @param arg0
   *            the node
   * @return String
   */
  public String getText(Object arg0) {
    // Get the name of the file
    String text = ((File) arg0).getName();

    // If name is blank, get the path
    if (text.length() == 0) {
      text = ((File) arg0).getPath();
    }

    // Check the case settings before returning the text
    return preserveCase ? text : text.toUpperCase();
  }

  /**
   * Adds a listener to this label provider
   * 
   * @param arg0
   *            the listener
   */
  public void addListener(ILabelProviderListener arg0) {
    listeners.add(arg0);
  }

  /**
   * Called when this LabelProvider is being disposed
   */
  public void dispose() {
    // Dispose the images
    if (dir != null)
      dir.dispose();
    if (file != null)
      file.dispose();
  }

  /**
   * Returns whether changes to the specified property on the specified
   * element would affect the label for the element
   * 
   * @param arg0
   *            the element
   * @param arg1
   *            the property
   * @return boolean
   */
  public boolean isLabelProperty(Object arg0, String arg1) {
    return false;
  }

  /**
   * Removes the listener
   * 
   * @param arg0
   *            the listener to remove
   */
  public void removeListener(ILabelProviderListener arg0) {
    listeners.remove(arg0);
  }
}

 

分享到:
评论

相关推荐

    perl-File-CheckTree-4.42-3.el7.noarch.rpm

    离线安装包,亲测可用

    checktree例子

    checktree("/path/to/directory", ["file1.txt", "file2.txt"], ["subdir1", "subdir2"]) ``` 4. **checktree的用途** - **备份验证**:在备份操作后,可以使用`checktree`确保备份的文件系统与原始文件系统一致。...

    perl-File-CheckTree-4.42-303.el8.noarch(1).rpm

    离线安装包,亲测可用

    27款jQuery Tree 树形结构插件

    3. **jQuery File Tree** - 这是一个可配置的Ajax文件浏览器插件,允许通过CSS定制外观。 - 通过Ajax获取文件信息,可自定义展开/收缩事件和速度。 - 开源项目。 4. **CheckTree** - CheckTree 是带有复选框的...

    7-1 Build A Binary Search Tree.zip_13MV_6ST_Case File_Datastrctu

    Each input file contains one test case. Each case starts with a line containing two positive integers N (≤100), the number of activity check points (hence it is assumed that the check points are ...

    基于Merkle树的移动平台文件完整性校验_张晓燕1

    【基于Merkle树的移动平台文件完整性校验】是一种针对移动设备下载业务的重要安全机制。在移动平台的下载业务中,确保用户接收到的数据完整无损是至关重要的,因为任何数据损坏都可能导致应用程序无法正常运行或者...

    基于jstree使用AJAX请求获取数据形成树

    `"themes"`设置了主题的响应式,`"check_callback" : true`允许用户对树进行操作,如移动节点或改变层级关系。`'data'`函数是一个回调,用于异步加载数据。在这个例子中,它使用`$.ajax`发送GET请求到`"/demo/...

    [加密软件].NCH.Meo.Encryption.v2.15.Incl.Keygen-BRD

    MEO File Encryption Software Encrypt and decrypt files and keep your data secure ...Create and verify checksums (MD5, SHA-1, Tiger Tree) to confirm file integrity Simple and intuitive interface

    'FrontEnd Plus' The GUI for the fast JAva Decompiler.

    In a case you want to check the accuracy of the decompilation or just curious, there is an option -a which tells Jad to annotate the output with JAVA Virtual Machine bytecodes. Jad supports the inner...

    Java反编译软件JAD1

    -nodos - do not check for class files in DOS mode (default: check) -nocast - don't generate auxiliary casts -nocode - don't generate the source code for methods -noconv - don't convert Java ...

    安卓学习者百度地图定位sdk环境配置安卓实现定位功能实例

    implementation fileTree(dir: 'libs', include: ['*.jar']) // 其他依赖... } ``` 3. **权限设置**:在AndroidManifest.xml文件中,必须添加以下权限来允许应用访问网络和位置服务: ```xml ``` 4. *...

    HM-10.0.tar.bz2(JCT-VC HEVC)

    Step 1: Download the source tree Get the latest version of HM. % svn co https://hevc.hhi.fraunhofer.de/svn/svn_HEVCSoftware/tags/HM-10.0 Step 2: Build it! Start Visual Studio command line prompt ...

    vc环境实现sift算子

    the maximum number of keypoint NN candidates to check during BBF search #define KDTREE BBF MAX NN CHKS 200 threshold on squared ratio of distances between NN and 2nd NN #define NN SQ DIST RATIO ...

    VB编程资源大全(英文源码 文件)

    &lt;END&gt;&lt;br&gt;40,chkdir.zip Check for a Directory &lt;END&gt;&lt;br&gt;41,getext.zip Get a File's Extension&lt;END&gt;&lt;br&gt;42,geticon.zip Get a File's Icon&lt;END&gt;&lt;br&gt;43,parseint.zip Parse an Internet Filename&lt;END&gt;...

    dos的基本命令,dir,cd

    - **语法**:`CHKDSK [drive][file] [/F][/V]` - `/F` 修复错误。 - `/V` 显示文件名。 - **示例**: - `CHKDSK C: /F`:检查并修复C盘上的错误。 - `CHKDSK C: /V`:显示C盘上的文件名。 #### DISKCOPY (Copy ...

    pywinauto使用

    checkbox.check() ``` **对话框和消息框** pywinauto还支持处理对话框和消息框。例如,等待并关闭一个对话框: ```python dialog = app.wait('visible', timeout=10) dialog.close() ``` **遍历控件树** 为了...

    java反编译工具jad 1.5.8g(可以反编译jdk1.5,1.6)

    For example, if file 'tree/a/b/c.class' contains class 'c' from package 'a.b', then output file will have a name 'src/a/b/c.java'. &lt;br&gt;Note the use of the "two stars" wildcard ('**') in ...

Global site tag (gtag.js) - Google Analytics