[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-swt-dev] questions on TableTree

The following worked for me:


import java.io.*;
import java.util.*;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.program.*;
import org.eclipse.swt.widgets.*;
public class TreeViewerTest {

        class TreeViewerTestLabelProvider implements ITableLabelProvider, 
ITableColorProvider, ITableFontProvider {
                Font font = null;
                public String getColumnText(Object element, int 
columnIndex) {
                        if (!(element instanceof File)) return null;
                        File file = (File)element;
                        switch (columnIndex) {
                        case 0:
                                return file.getName();
                        case 1:
                                return ""+new Date(file.lastModified());
                        case 2:
                                return ""+file.isHidden();
                        }
                        return null;
                }
                public Image getColumnImage(Object element, int 
columnIndex) {
                        if (columnIndex != 0) return null;
                        if (!(element instanceof File)) return null;
                        File file = (File)element;
                        Display display = Display.getCurrent();
                        if (file.isDirectory()) return 
display.getSystemImage(SWT.ICON_WARNING);
                        return display.getSystemImage(SWT.ICON_QUESTION);
                }
                public void dispose() {
                        if (font != null) font.dispose();
                        font = null;
                }
                public boolean isLabelProperty(Object element, String 
property) {
                        return false;
                }
                public void addListener(ILabelProviderListener listener) {
                }
                public void removeListener(ILabelProviderListener 
listener) {
                }
                public Color getForeground(Object element, int 
columnIndex) {
                        if (columnIndex == 1) {
                                Display display = Display.getCurrent();
                                return display.getSystemColor(SWT.
COLOR_RED);
                        }
                        return null;
                }
                public Color getBackground(Object element, int 
columnIndex) {
                        if (columnIndex == 0) {
                                File file = (File)element;
                                Display display = Display.getCurrent();
                                if (file.isDirectory()) return 
display.getSystemColor(SWT.COLOR_CYAN);
                                return display.getSystemColor(SWT.
COLOR_MAGENTA);
                        }
                        return null;
                }
                public Font getFont(Object element, int columnIndex) {
                        if (columnIndex == 2) {
                                if (font == null) {
                                        Display display = Display.
getCurrent();
                                        font = new Font(display, "Times", 
12, SWT.ITALIC);
                                }
                                return font;
                        }
                        return null;
                }
        }
public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FormLayout());
 
        TreeViewerTestLabelProvider labelProvider1 = new TreeViewerTest().
new TreeViewerTestLabelProvider();
        ITreeContentProvider contentProvider = new ITreeContentProvider() 
{
                public Object[] getChildren(Object parentElement) {
                        if (!(parentElement instanceof File)) return null;
                        File file = (File)parentElement;
                        if (file.isDirectory()) return file.listFiles();
                        return null;
                }

                public Object getParent(Object element) {
                        if (!(element instanceof  File)) return null;
                        File file = (File)element;
                        return file.getParentFile();
                }

                public boolean hasChildren(Object element) {
                        if (!(element instanceof  File)) return false;
                        File file = (File)element;
                        return file.isDirectory();
                }

                public Object[] getElements(Object inputElement) {
                        return getChildren(inputElement);
                }

                public void dispose() {
                }

                public void inputChanged(Viewer viewer, Object oldInput, 
Object newInput) {
                }
        };
 
        ILabelProvider labelProvider2 = new ILabelProvider() {
                public void dispose() {
                }
                public boolean isLabelProperty(Object element, String 
property) {
                        return false;
                }
                public void addListener(ILabelProviderListener listener) {
                }
                public void removeListener(ILabelProviderListener 
listener) {
                }
                public Image getImage(Object element) {
                        if (!(element instanceof File)) return null;
                        File file = (File)element;
                        if (file.isDirectory()) return 
display.getSystemImage(SWT.ICON_WARNING);
                        return display.getSystemImage(SWT.ICON_QUESTION);
                }
                public String getText(Object element) {
                        if (!(element instanceof File)) return null;
                        File file = (File)element;
                        return file.getName();
                }
 
        };
 
        final TreeViewer treeViewer1 = new TreeViewer(shell, SWT.
FULL_SELECTION | SWT.CHECK);
        Tree tree1 = treeViewer1.getTree();
        tree1.setHeaderVisible(true);
        TreeColumn column1 = new TreeColumn(tree1, SWT.LEFT);
        column1.setText("Name");
        column1.setImage(display.getSystemImage(SWT.ICON_WORKING));
        column1.setWidth(200);
        TreeColumn column2 = new TreeColumn(tree1, SWT.CENTER);
        column2.setText("Last Modified");
        column2.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
        column2.setWidth(200);
        TreeColumn column3 = new TreeColumn(tree1, SWT.CENTER);
        column3.setText("Hidden");
        column3.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
        column3.setWidth(100);
        treeViewer1.setLabelProvider(labelProvider1);
        treeViewer1.setContentProvider(contentProvider);
 
        final TreeViewer treeViewer2 = new TreeViewer(shell);
        Tree tree2 = treeViewer2.getTree();
        treeViewer2.setLabelProvider(labelProvider2);
        treeViewer2.setContentProvider(contentProvider);
 
        Button b = new Button(shell, SWT.PUSH);
        b.setText("choose root folder");
        b.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event e) {
                        DirectoryDialog f = new DirectoryDialog(shell, 
SWT.OPEN);
                        String root = f.open();
                        if (root != null) {
                                File file = new File(root);
                                if (file.exists()) {
                                        treeViewer1.setInput(file);
                                        treeViewer1.refresh();
                                        treeViewer2.setInput(file);
                                        treeViewer2.refresh();
                                }
                        }
                }
        });
 
        FormData data = new FormData();
        data.left = new FormAttachment(0, 10);
        data.top = new FormAttachment(0, 10);
        data.right = new FormAttachment(50, -5);
        data.bottom = new FormAttachment(b, -10);
        tree1.setLayoutData(data);
 
        data = new FormData();
        data.left = new FormAttachment(50, 5);
        data.top = new FormAttachment(0, 10);
        data.right = new FormAttachment(100, -10);
        data.bottom = new FormAttachment(b, -10);
        tree2.setLayoutData(data);
 
        data = new FormData();
        data.left = new FormAttachment(0, 10);
        data.bottom = new FormAttachment(100, -10);
        b.setLayoutData(data);
 
        shell.open();
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                        display.sleep();
        }
        display.dispose();
}
}




"Carmelo Scala" <carmeloscala@xxxxxxxxxxx> 
Sent by: platform-swt-dev-admin@xxxxxxxxxxx
03/15/2005 11:32 AM
Please respond to
platform-swt-dev


To
<platform-swt-dev@xxxxxxxxxxx>
cc

Subject
Re: [platform-swt-dev] questions on TableTree






Hi Veronika ... sorry to keep asking ...



One more question ...

you say it's possible to use a TreeViewer ... but what will be the right 
way 
to mix viewer / contentProvider / LabelProvider and/or TreeItem ?



Previously I could have used a LabelProvider (implements 
ITableLableProvider) ... but now using just a LabelProvider this will 
supply 
only information about the tree node ... am i right ?





Thanks again

Car


----- Original Message ----- 
From: "Veronika Irvine" <Veronika_Irvine@xxxxxxxxxx>
To: <platform-swt-dev@xxxxxxxxxxx>
Sent: Tuesday, March 15, 2005 3:03 PM
Subject: Re: [platform-swt-dev] questions on TableTree


> TableTree will never support an icon in the first column.,  The first
> column image is used to show the +/-.  Using TableTreeViewer will not 
make
> any difference because it uses TableTree underneath.
>
> In 3.1, instead of using TableTree use Tree with TreeColumn to create a
> multi column Tree.  This will support an icon in the first column.  You
> can also use TreeViewer with TreeColumns in 3.1.
>
> See:
>
> 
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet170.java?rev=HEAD&content-type=text/vnd.viewcvs-markup

>
>
>
>
> "Carmelo Scala" <carmeloscala@xxxxxxxxxxx>
> Sent by: platform-swt-dev-admin@xxxxxxxxxxx
> 03/15/2005 05:25 AM
> Please respond to
> platform-swt-dev
>
>
> To
> <platform-swt-dev@xxxxxxxxxxx>
> cc
>
> Subject
> [platform-swt-dev] questions on TableTree
>
>
>
>
>
>
> Hello there,
> I'm having problem trying to use icon for the first column in a 
TableTree
> (... I would like to have icon + text for the tree node items).
>
> I have used a normal TableTree ->
> --- TableTreeItem .setText (0, ...)
> --- TableTreeItem .setImage (0, ...)
> with no result ...
>
> I have seen a TableTreeViewer ... would it be the right thing to use ...
> and
> how to specify the LabelProvider ?
>
> Thanks for any help
> Carmelo
> _______________________________________________
> platform-swt-dev mailing list
> platform-swt-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/platform-swt-dev
>
>
> _______________________________________________
> platform-swt-dev mailing list
> platform-swt-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/platform-swt-dev
> 
_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/platform-swt-dev