Monday 23 January 2017

SWT: Checkbox tree viewer example

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

public class CustomTreeLabelProvider implements ILabelProvider {

 private Image dir;
 private Image file;

 private static String dirLocation = "C:\\Users\\I335077\\Downloads\\icons\\folder.jpg";
 private static String fileLocation = "C:\\Users\\I335077\\Downloads\\icons\\document.png";

 public CustomTreeLabelProvider() {
  // Create the images
  try {
   dir = new Image(null, new FileInputStream(dirLocation));
   file = new Image(null, new FileInputStream(fileLocation));
  } catch (FileNotFoundException e) {
   // Swallow it; we'll do without images
  }
 }

 @Override
 public void addListener(ILabelProviderListener arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void dispose() {
  if (dir != null)
   dir.dispose();
 }

 @Override
 public boolean isLabelProperty(Object arg0, String arg1) {
  // TODO Auto-generated method stub
  return false;
 }

 @Override
 public void removeListener(ILabelProviderListener arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public Image getImage(Object arg0) {
  return ((File) arg0).isDirectory() ? dir : file;
 }

 @Override
 public String getText(Object arg0) {
  return arg0.toString();
 }

}

package test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class CustomTreeContentProvider implements ITreeContentProvider {
 private Map<String, String[]> contentMap = new HashMap<>();

 public CustomTreeContentProvider() {
  initMap();
 }

 private void initMap() {
  String[] roots = { "root1", "root2" };

  contentMap.put("roots", roots);

  contentMap.put("root1", new String[] { "root1_child1", "root1_child2", "root1_child3" });
  contentMap.put("root1_child1", new String[] { "root1_child1_child1", "root1_child1_child2" });
 }

 @Override
 public Object[] getElements(Object arg0) {
  return File.listRoots();
 }

 @Override
 public void dispose() {
  // TODO Auto-generated method stub

 }

 @Override
 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
  // TODO Auto-generated method stub

 }

 @Override
 public Object[] getChildren(Object arg0) {
  return ((File) arg0).listFiles();
 }

 @Override
 public Object getParent(Object arg0) {
  return ((File) arg0).getParentFile();
 }

 @Override
 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;
 }

 private String getParentOfEle(Object arg0) {
  Set<String> keys = contentMap.keySet();

  for (String key : keys) {
   String[] values = contentMap.get(key);

   for (String val : values) {
    if (val.equals(arg0)) {
     return key;
    }
   }
  }
  return null;
 }

}


package test;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;

public class TreeViewerDemo extends ApplicationWindow {

 public TreeViewerDemo() {
  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();
 }

 protected void configureShell(Shell shell) {
  super.configureShell(shell);

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

 protected Control createContents(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new GridLayout(1, false));

  final CheckboxTreeViewer tv = new CheckboxTreeViewer(composite);
  tv.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
  tv.setContentProvider(new CustomTreeContentProvider());
  tv.setLabelProvider(new CustomTreeLabelProvider());

  tv.setInput("root");

  /* 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, check all its children */
    if (event.getChecked()) {
     tv.setSubtreeChecked(event.getElement(), true);

    }

    /*
     * If user unchecks a checkbox in the tree, uncheck all its
     * children
     */
    if (!event.getChecked()) {
     tv.setSubtreeChecked(event.getElement(), false);
    }
   }
  });

  /* Add Selection Listener to the tree */
  tv.getTree().addListener(SWT.Selection, new Listener() {
   public void handleEvent(Event e) {
    String string = "";
    TreeItem[] selection = tv.getTree().getSelection();
    for (int i = 0; i < selection.length; i++)
     string += selection[i] + " ";
    System.out.println("Selection={" + string + "}");
   }
  });
  
  TreeItem[] items = tv.getTree().getItems();
  
  for(TreeItem item: items){
   System.out.println("Item is : " + item);
   item.setChecked(false);
  }
  return composite;
 }

}

package test;

public class TreeViewerDemoTest {
 public static void main(String args[]) {
  TreeViewerDemo treeViewer = new TreeViewerDemo();
  treeViewer.run();
 }
}
Previous                                                 Next                                                 Home

No comments:

Post a Comment