Example usage for org.eclipse.jface.viewers TreeViewer setContentProvider

List of usage examples for org.eclipse.jface.viewers TreeViewer setContentProvider

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers TreeViewer setContentProvider.

Prototype

@Override
public void setContentProvider(IContentProvider provider) 

Source Link

Document

Sets the content provider used by this TreeViewer.

Usage

From source file:org.eclipse.ice.client.common.TreeCompositeViewer.java

License:Open Source License

/**
 * Creates the {@link #treeViewer} used to display the {@link #inputTree}.
 * /*  w  w w  .  jav  a 2 s .  c  o m*/
 * @param parent
 *            The container for the <code>TreeViewer</code>.
 * @return A new <code>TreeViewer</code>.
 */
protected TreeViewer createViewer(Composite parent) {

    TreeViewer treeViewer = null;

    if (parent != null) {
        // Initialize the TreeViewer.
        treeViewer = new TreeViewer(parent,
                SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);

        // Set and configure the content and label providers
        treeViewer.setContentProvider(new TreeCompositeContentProvider(this, parentMap));
        treeViewer.setLabelProvider(new TreeCompositeLabelProvider());
    }

    return treeViewer;
}

From source file:org.eclipse.ice.client.widgets.ICEResourceView.java

License:Open Source License

/**
 * This operation creates content and label providers for a TreeViewer.
 * //  w  ww  . ja va  2s.  com
 * @param inputTreeViewer
 *            The TreeViewer to have the providers added to.
 */
private void initializeTreeViewer(TreeViewer inputTreeViewer) {

    // Create a content provider that will show the name and its path and
    // edit date as children
    inputTreeViewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            // Don't handle input changes
            return;
        }

        @Override
        public Object[] getElements(Object inputElement) {
            // Cast the input and return it as an array
            return ((List<?>) inputElement).toArray();
        }

        @Override
        public Object[] getChildren(Object parentElement) {
            // If the element is a PropertySource
            if (parentElement instanceof PropertySource) {
                PropertySource source = (PropertySource) parentElement;
                ICEResource resource = (ICEResource) source.getWrappedData();
                // Load the path and modification date as children
                String[] children = { "Path: " + resource.getPath().toASCIIString(),
                        "Date: " + resource.getLastModificationDate() };

                // Map the children to their parent resource for appropriate
                // selection behavior
                resourceChildMap.put("Path: " + resource.getPath().toASCIIString(), resource);
                resourceChildMap.put("Date: " + resource.getLastModificationDate(), resource);

                return children;
            }
            return null;
        }

        @Override
        public Object getParent(Object element) {
            // Don't identify parents
            return null;
        }

        @Override
        public boolean hasChildren(Object element) {
            // Only PropertySources will have children
            if (element instanceof PropertySource) {
                return true;
            }
            return false;
        }

    });
    // Add a label provider to properly label the resources
    inputTreeViewer.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            // Only set a label if it is an ICEResource. Otherwise we
            // shouldn't need it.... I think.
            if (element instanceof PropertySource) {
                PropertySource source = (PropertySource) element;
                ICEResource resource = (ICEResource) source.getWrappedData();
                return resource.getName();
            }
            return (String) element;
        }

        @Override
        public Image getImage(Object element) {
            // If it is a resource, set a "folder" picture
            if (element instanceof PropertySource) {
                return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
            }
            return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
        }

    });

    return;
}

From source file:org.eclipse.ice.client.widgets.MeshElementTreeView.java

License:Open Source License

/**
 * This operation creates content and label providers for a TreeViewer.
 * //  w  w  w  .j a  va 2s  .  c o  m
 * @param inputTreeViewer
 *            The TreeViewer to have the providers added to.
 */
private void initializeTreeViewer(TreeViewer inputTreeViewer) {

    // Set the tree's content provider
    inputTreeViewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        /**
         * This function retrieves the elements of the tree.
         * 
         * @param inputElement
         *            The structure containing all of the elements
         * 
         * @see ITreeContentProvider#getElements(Object)
         */
        @Override
        public Object[] getElements(Object inputElement) {

            // Local Declaration
            ArrayList<BasicController> allElements = (ArrayList<BasicController>) inputElement;
            ArrayList<MeshSelection> contents = new ArrayList<MeshSelection>();

            // Wrap the Polygons into PropertySources and add them to
            // the array
            for (BasicController i : allElements) {
                contents.add(new MeshSelection(meshComponent.getMesh(), i));
            }

            return contents.toArray();
        }

        /**
         * This function retrieves the children of a given tree element.
         * 
         * @param parentElement
         *            The tree element for which children are being
         *            requested
         * 
         * @see ITreeContentProvider#getChildren(Object)
         */
        @Override
        public Object[] getChildren(Object parentElement) {

            // If the element is a PropertySource
            if (parentElement instanceof MeshSelection) {

                MeshSelection selection = (MeshSelection) parentElement;

                // Load edges and vertices as children of polygons
                ArrayList<MeshSelection> children = new ArrayList<MeshSelection>();

                // An array of every unique vertex from the selection
                ArrayList<IController> vertices = new ArrayList<IController>();

                if (selection.selectedMeshPart instanceof FaceController) {
                    FaceController polygon = (FaceController) selection.selectedMeshPart;
                    // Add new MeshSelections for the edges.
                    for (IController e : polygon.getEntitiesFromCategory(MeshCategory.EDGES)) {
                        children.add(new MeshSelection(meshComponent.getMesh(), e));

                        // Add each of the edge's vertices to the list if
                        // they are nto already present
                        for (IController v : e.getEntitiesFromCategory(MeshCategory.VERTICES)) {
                            if (!vertices.contains(v)) {
                                vertices.add(v);
                            }
                        }
                    }

                    // Add new MeshSelections for the vertices.
                    for (IController v : vertices) {
                        children.add(new MeshSelection(meshComponent.getMesh(), v));
                    }
                }

                return children.toArray();
            }

            return null;
        }

        @Override
        public Object getParent(Object element) {
            return null;
        }

        /**
         * This function checks whether or not the tree element has any
         * children. This should only return true for Polygons.
         * 
         * @param element
         *            The tree element to investigate
         * 
         * @return True if this is of type Polygon. False otherwise.
         * 
         * @see ITreeContentProvider#hasChildren(Object)
         */
        @Override
        public boolean hasChildren(Object element) {

            // Only selected Polygons will have children.
            return (element instanceof MeshSelection
                    && ((MeshSelection) element).selectedMeshPart instanceof FaceController);
        }
    });

    // Add a label provider to properly label the mesh elements
    inputTreeViewer.setLabelProvider(new LabelProvider() {

        /**
         * The String to contain the text of the label.
         */
        String label = "";

        /**
         * A function to create text labels for elements in the
         * MeshElementTreeView.
         * 
         * @param element
         *            The tree element to be labeled
         * 
         * @return The String to serve as the tree element label
         * 
         * @see LabelProvider#getText(Object)
         */
        @Override
        public String getText(Object element) {

            // Only set a label if it is a PropertySource. Otherwise we
            // shouldn't need it.... I think.
            if (element instanceof MeshSelection) {

                // Get the wrapped IMeshPart.
                IController meshPart = ((MeshSelection) element).selectedMeshPart;

                // Cast the IMeshPart to an ICEObject and set the label text
                // from its name and ID.
                label = meshPart.getProperty(MeshProperty.NAME) + " " + meshPart.getProperty(MeshProperty.ID);

                return label;
            }
            return element.toString();
        }
    });

    return;
}

From source file:org.eclipse.ice.client.widgets.moose.MOOSETreeCompositeView.java

License:Open Source License

/**
 * Overrides the default viewer to add an additional feature: When a parent
 * node is unchecked, all of its child nodes are unchecked.
 *//*from   w w w .java 2 s  .  c om*/
@Override
protected TreeViewer createViewer(Composite parent) {
    TreeViewer treeViewer = null;

    if (parent != null) {
        // Initialize the TreeViewer. Note: We create a CheckboxTreeViewer!
        final CheckboxTreeViewer checkboxTreeViewer = new CheckboxTreeViewer(parent,
                SWT.VIRTUAL | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
        treeViewer = checkboxTreeViewer;

        // Set and configure the content and label providers
        treeViewer.setContentProvider(new TreeCompositeContentProvider(this, parentMap));
        treeViewer.setLabelProvider(new TreeCompositeLabelProvider());

        // Add a provider to tell the viewer when elements should be
        // checked. This is NOT default behavior.
        MOOSETreeCheckStateManager checkManager;
        checkManager = new MOOSETreeCheckStateManager();
        checkboxTreeViewer.setCheckStateProvider(checkManager);
        checkboxTreeViewer.addCheckStateListener(checkManager);
    }

    return treeViewer;
}

From source file:org.eclipse.ice.viz.csv.viewer.CSVPlotViewer.java

License:Open Source License

/**
 * Initializes the provided TreeViewer based on the current VizResource for
 * this PlotViewer./* w w  w .j  a  va2s  . c o m*/
 * 
 * @param inputTreeViewer
 *            The TreeViewer that should be configured to display the
 *            currently selected plots for a CSV file.
 */
private void initializeTreeViewer(TreeViewer inputTreeViewer) {

    // Set the content provider, which determines how the input should
    // produce elements in the TreeViewer.

    inputTreeViewer.setContentProvider(new PlotTreeContentProvider());

    // Set up the label provider, which determines what string is displayed
    // for each element in the tree. Currently, this only needs to produce
    // a string for each PlotProvider.
    inputTreeViewer.setLabelProvider(new PlotTreeLabelProvider());

    return;
}

From source file:org.eclipse.ice.viz.visit.VisitPlotViewer.java

License:Open Source License

/**
 * Initializes the provided TreeViewer based on the current ICEResource for
 * this PlotViewer.//w  w w. j a  va 2  s .c  o  m
 * 
 * @param inputTreeViewer
 *            The TreeViewer that should be configured to display the
 *            currently selected plots for a VisIt-compatible ICEResource.
 */
private void initializeTreeViewer(TreeViewer inputTreeViewer) {

    // Set up the content provider and label provider for the TreeViewer.
    // The input should be of the type Entry[]. Elements should be the
    // entries themselves.

    // Set the content provider, which determines how the input (an Entry[])
    // should produce elements in the TreeViewer.
    inputTreeViewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            return;
        }

        @Override
        public void dispose() {
            // No image descriptors or non-textual resources to dispose.
            return;
        }

        @Override
        public boolean hasChildren(Object element) {
            // Currently, we do not have nested elements in the tree.
            return false;
        }

        @Override
        public Object getParent(Object element) {
            // Currently, we do not have nested elements in the tree.
            return null;
        }

        @Override
        public Object[] getElements(Object inputElement) {
            Object[] elements;
            if (inputElement instanceof Object[]) {
                elements = (Object[]) inputElement;
            } else {
                elements = new Object[] {};
            }

            return elements;
        }

        @Override
        public Object[] getChildren(Object parentElement) {
            // Currently, we do not have nested elements in the tree.
            return null;
        }
    });

    // Set up the label provider, which determines what string is displayed
    // for each element in the tree. Currently, this only needs to produce
    // a string for each Entry.
    inputTreeViewer.setLabelProvider(new StyledCellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            Object element = cell.getElement();

            // Get a String from the Entry if possible.
            StyledString styledStr = new StyledString();
            if (element instanceof Entry) {
                Entry entry = (Entry) element;
                // Get the name from the resource
                styledStr.append(entry.getName());
                // Append the path stored as the Entry description
                styledStr.append(" [" + entry.getParent(), StyledString.QUALIFIER_STYLER);
                // Append the data type stored as the Entry parent
                styledStr.append("-" + entry.getDescription() + "]", StyledString.QUALIFIER_STYLER);
            }
            // If the element isn't an Entry, convert it to a String.
            else {
                styledStr.append(element.toString());
            }

            // Set the text for the cell and call
            // StyledCellLabelProvider#update()
            cell.setText(styledStr.toString());
            cell.setStyleRanges(styledStr.getStyleRanges());
            super.update(cell);
        }
    });

    return;
}

From source file:org.eclipse.ice.viz.VizFileViewer.java

License:Open Source License

/**
 * Initializes the provided TreeViewer based on the ResourceComponent for
 * this VizFileViewer.//from w  w  w.  j a va 2  s . c o  m
 * 
 * @param inputTreeViewer
 *            The TreeViewer that should be configured to display the
 *            ICEResources in a ResourceComponent.
 */
private void initializeTreeViewer(TreeViewer inputTreeViewer) {

    inputTreeViewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            return;
        }

        @Override
        public void dispose() {
            return;
        }

        @Override
        public boolean hasChildren(Object element) {

            // Make sure we have a VizResource
            if (element instanceof VizResource) {
                // Cast it to make life easier
                VizResource resource = (VizResource) element;

                // If this resource has a valid fileset...
                if (resource.getFileSet() != null) {
                    // Then return true if it is not empty
                    return resource.getFileSet().length != 0;

                    // If the fileset was null, but we have children
                    // resources...
                } else if (resource.getChildrenResources() != null) {
                    // then return true if the children list is not empty
                    return !resource.getChildrenResources().isEmpty();
                }
            }

            // All else, we have no children
            return false;
        }

        @Override
        public Object getParent(Object element) {
            return null;
        }

        @Override
        public Object[] getElements(Object inputElement) {

            // Local Declaration
            Object[] elements;

            // Convert the element to an ArrayList (it should be an
            // ArrayList<ICEResource>) and then get an Object array.
            if (inputElement instanceof ArrayList<?>) {
                elements = ((ArrayList<?>) inputElement).toArray();
            } else {
                elements = new Object[] {};
            }

            return elements;
        }

        @Override
        public Object[] getChildren(Object parentElement) {

            // Make sure this is a VizResource
            if (parentElement instanceof VizResource) {
                // Cast to make life easier
                VizResource resource = (VizResource) parentElement;

                // If we have children...
                if (resource.getChildrenResources() != null && !resource.getChildrenResources().isEmpty()) {
                    // Return all Child resources
                    return resource.getChildrenResources().toArray();

                } else if (resource.getFileSet() != null && resource.getFileSet().length != 0) {
                    // If we didn't have VizResourc children, then check
                    // that we
                    // have file names to return
                    return resource.getFileSet();
                }
            }

            // Otherwise return null
            return null;

        }
    });

    inputTreeViewer.setLabelProvider(new StyledCellLabelProvider() {
        @Override
        public void update(ViewerCell cell) {
            Object element = cell.getElement();

            // Get a String from the VizResource if possible.
            StyledString styledStr = new StyledString();
            if (element instanceof VizResource) {
                VizResource resource = (VizResource) element;
                // Get the name from the resource
                styledStr.append(resource.getName());

                String host = resource.getHost();
                String path = resource.getPath().getPath();

                // Get the host from the resource and append it to the tree
                // entry String grayed-out and enclosed in square brackets.
                if (host != null && !host.isEmpty()) {
                    styledStr.append(" [" + host, StyledString.QUALIFIER_STYLER);
                    // Include the path with the host if available
                    if (path != null && !path.isEmpty() && !resource.isRemote()) {
                        styledStr.append(":" + path, StyledString.QUALIFIER_STYLER);
                    }
                    styledStr.append("]", StyledString.QUALIFIER_STYLER);
                }
            }

            // If the element isn't an VizResource, convert it to a String.
            else {
                styledStr.append(element.toString());
            }

            // Set the text for the cell and call
            // StyledCellLabelProvider#update()
            cell.setText(styledStr.toString());
            cell.setStyleRanges(styledStr.getStyleRanges());
            super.update(cell);
        }
    });

    return;
}

From source file:org.eclipse.imp.lpg.search.LPGSearchResultPage.java

License:Open Source License

protected void configureTreeViewer(TreeViewer viewer) {
    //        PostfixLabelProvider postfixLabelProvider= new PostfixLabelProvider(this);
    viewer.setUseHashlookup(true);/*from   ww  w. java2s  .c  o m*/
    //        viewer.setSorter(new DecoratorIgnoringViewerSorter(postfixLabelProvider));
    //        viewer.setLabelProvider(new ColorDecoratingLabelProvider(postfixLabelProvider, PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()));
    viewer.setLabelProvider(new LPGLabelProvider());
    fContentProvider = new LPGSearchTreeContentProvider(this);
    viewer.setContentProvider(fContentProvider);
    //        addDragAdapters(viewer);
}

From source file:org.eclipse.jdt.internal.debug.ui.jres.VMDetailsDialog.java

License:Open Source License

@Override
protected Control createDialogArea(Composite ancestor) {
    Composite parent = (Composite) super.createDialogArea(ancestor);
    GridLayout layout = new GridLayout(2, false);
    layout.makeColumnsEqualWidth = false;
    parent.setLayout(layout);// ww w .  jav a  2 s .com

    // type
    createLabel(parent, JREMessages.addVMDialog_jreType);
    createLabel(parent, fVM.getVMInstallType().getName());

    // name
    createLabel(parent, JREMessages.addVMDialog_jreName);
    createLabel(parent, fVM.getName());

    // home
    createLabel(parent, JREMessages.addVMDialog_jreHome);
    createLabel(parent, fVM.getInstallLocation().getAbsolutePath());

    // vm args
    SWTFactory.createLabel(parent, JREMessages.AddVMDialog_23, 2);
    String text = null;
    if (fVM instanceof IVMInstall2) {
        text = ((IVMInstall2) fVM).getVMArgs();
    } else {
        String[] args = fVM.getVMArguments();
        if (args != null) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < args.length; i++) {
                buf.append(args[i]);
                if (i < (args.length - 1)) {
                    buf.append(" "); //$NON-NLS-1$
                }
            }
            text = buf.toString();
        }
    }
    if (text == null) {
        text = ""; //$NON-NLS-1$
    }
    Text argText = SWTFactory.createText(parent, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL, 2, text);
    GridData gd = (GridData) argText.getLayoutData();
    gd.heightHint = 75;
    gd.widthHint = 300;

    // libraries
    SWTFactory.createLabel(parent, JREMessages.AddVMDialog_JRE_system_libraries__1, 2);
    TreeViewer libraryViewer = new TreeViewer(parent);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    gd.heightHint = 100;
    libraryViewer.getControl().setLayoutData(gd);
    LibraryContentProvider provider = new LibraryContentProvider();
    libraryViewer.setContentProvider(provider);
    libraryViewer.setLabelProvider(new LibraryLabelProvider());
    libraryViewer.setInput(this);
    provider.setLibraries(JavaRuntime.getLibraryLocations(fVM));

    applyDialogFont(parent);
    return parent;
}

From source file:org.eclipse.jface.snippets.layout.Snippet027TreeLayout.java

License:Open Source License

public Snippet027TreeLayout(Shell shell) {
    final TreeViewer v = new TreeViewer(shell);
    v.getTree().setHeaderVisible(true);// w w w. jav a2s.c  om
    v.getTree().setLinesVisible(true);

    TreeColumnLayout ad = new TreeColumnLayout();
    shell.setLayout(ad);

    TreeColumn column1 = createTreeColumn(v.getTree(), "Column 1");
    TreeColumn column2 = createTreeColumn(v.getTree(), "Column 2");

    ad.setColumnData(column1, new ColumnWeightData(50, 100));
    ad.setColumnData(column2, new ColumnWeightData(50, 100));

    v.setLabelProvider(new MyLabelProvider());
    v.setContentProvider(new MyContentProvider());
    v.setInput(createModel());
}