Demonstrates TreeViewer

 

FileTree.png

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

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.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 TreeViewer. It shows the drives, directories, and * files on the system. */
public 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;
  }

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

/** * 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);
  }
}
           
      
 

 

[출처] http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DemonstratesTreeViewer.htm

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
27 [java][maven] jar 파일 의존성 한번에 다운로드 maven 사용 졸리운_곰 2023.08.24 192
26 Prometheus + Grafana로 Java 애플리케이션 모니터링하기 file 졸리운_곰 2020.12.17 279
25 Blockchain Implementation With Java Code file 졸리운_곰 2019.06.16 338
24 Java 코드로 이해하는 블록체인(Blockchain) 졸리운_곰 2019.06.16 377
23 순수 Java Application 코드로 Restful api 호출 졸리운_곰 2018.10.10 415
22 WebDAV 구현을 위한 환경 설정 file 졸리운_곰 2017.09.24 268
21 [Java] Apache Commons HttpClient로 SSL 통신하기 졸리운_곰 2017.03.27 784
20 JSoup를 이용한 HTML 파싱 졸리운_곰 2017.03.04 320
19 jsoup을 활용해서 Java에서 HTML 파싱하는 방법 정리 file 졸리운_곰 2017.03.04 579
18 NSA의 Dataflow 엔진 Apache NiFi 소개와 설치 file 졸리운_곰 2017.01.23 630
17 wordpress-java-integration 자바와 워드프레스 통합 졸리운_곰 2016.12.30 298
16 Create New Posts in Wordpress using Java and XMLRpc 졸리운_곰 2016.11.14 268
15 자바로 POST 방식으로 통신하기, java httppost 클래스를 활용한 예제 졸리운_곰 2016.11.14 647
14 [Java]아파치 HttpClient사용하기 file 졸리운_곰 2016.11.14 301
13 Building a Search Engine With Nutch Solr And Hadoop file 졸리운_곰 2016.04.21 435
12 Nutch and Hadoop Tutorial file 졸리운_곰 2016.04.21 391
11 Latest step by Step Installation guide for dummies: Nutch 0. file 졸리운_곰 2016.04.21 305
10 Nutch 초간단 빌드와 실행 졸리운_곰 2016.04.21 681
9 Nutch로 알아보는 Crawling 구조 - Joinc 졸리운_곰 2016.04.21 536
8 A tiny bittorrent library Java: 자바로 만든 작은 bittorrent 라이브러리 file 졸리운_곰 2016.04.20 418
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED