Sample ListViewer : List Viewer « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » List ViewerScreenshots 
Sample ListViewer
Sample ListViewer



/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on Feb 15, 2004 10:57:32 AM by JACK
 * $Id$
 
 *****************************************************************************/



import java.util.Iterator;
import java.util.Vector;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
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.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SampleListViewer {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  ListViewer listViewer;

  /**
   * Represents programming languages. 
   *
   */
  public static class Language {
    public String genre;
    public boolean isObjectOriented;
    
    public Language() { }
    public Language(String genre, boolean isObjectOriented) {
      this.genre = genre;
      this.isObjectOriented = isObjectOriented;
    }
    
    public String toString() {
      return "Lang: " + genre + " [" (isObjectOriented ? "Object-oriented" "Procedural""]";
    }
  }
  
  Vector languages = new Vector();
  
  private void init() {
    languages.add(new Language("Java"true));
    languages.add(new Language("C"false));
    languages.add(new Language("C++"true));
    languages.add(new Language("SmallTalk"true));
    
    listViewer = new ListViewer(shell);
    
    
    listViewer.setContentProvider(new IStructuredContentProvider() {
      public Object[] getElements(Object inputElement) {
        Vector v = (Vector)inputElement;
        return v.toArray();
      }
      
      public void dispose() {
        System.out.println("Disposing ...");
      }

      public void inputChanged(
        Viewer viewer,
        Object oldInput,
        Object newInput) {
        System.out.println("Input changed: old=" + oldInput + ", new=" + newInput);
      }
    });
    
    //listViewer.setContentProvider(new ArrayContentProvider());
    
    listViewer.setInput(languages);
    
    listViewer.setLabelProvider(new LabelProvider() {
      public Image getImage(Object element) {
        return null;
      }

      public String getText(Object element) {
        return ((Language)element).genre;
      }
    });
    
    listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
      public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = (IStructuredSelection)event.getSelection();
        StringBuffer sb = new StringBuffer("Selection - ");
        sb.append("tatal " + selection.size() " items selected: ");
        for(Iterator iterator = selection.iterator(); iterator.hasNext()) {
          sb.append(iterator.next() ", ");
        }
        System.out.println(sb);
      }
    });
    
    listViewer.addFilter(new ViewerFilter() {
      public boolean select(
        Viewer viewer,
        Object parentElement,
        Object element) {
        
        if(((Language)element).isObjectOriented)      
          return true;
        else
          return false;
      }
    });
    

    listViewer.setSorter(new ViewerSorter(){
      public int compare(Viewer viewer, Object e1, Object e2) {
        return ((Language)e1).genre.compareTo(((Language)e2).genre);
      }

    });
    
//    Object[] toBeSelectedItems = new Object[2];
//    toBeSelectedItems[0] = languages.elementAt(languages.size()-1);
//    toBeSelectedItems[1] = languages.elementAt(languages.size()-2);
//    IStructuredSelection selection = new StructuredSelection(toBeSelectedItems);
//    
//    listViewer.setSelection(selection);
    
    
//    
//    Vector v = new Vector();
//    v.addElement(new Language("VB", true));
//    
//    listViewer.setInput(v);
//    
//    listViewer.add(new Language("C#", true));
  }
  
  Button buttonAdd;
  Button buttonRemove;
  Button buttonModify;
  
  private void addButtons() {
    Composite composite = new Composite(shell, SWT.NULL);
    FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
    fillLayout.spacing = 2;
    
    composite.setLayout(fillLayout);
    
    buttonAdd = new Button(composite, SWT.PUSH);
    buttonAdd.setText("Add");
    
    buttonModify = new Button(composite, SWT.PUSH);
    buttonModify.setText("Modify");
    
    buttonRemove = new Button(composite, SWT.PUSH);
    buttonRemove.setText("Remove");
    
    buttonAdd.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
//        String text = OptionPane.showInputDialog(shell, "New language genre: ", "Add new element", null);
  //      if(text != null) {
    //      languages.add(new Language(text, true));
      //  }
        
        listViewer.refresh(false);
      }
    });

    buttonModify.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
        Language language = (Language)selection.getFirstElement();
        if(language == null) {
          System.out.println("Please select a language first.");
          return;
        }
        
//        String text = OptionPane.showInputDialog(shell, "Rename: ", "Modify genre", language.genre);
  //      if(text != null) {
    //      language.genre = text;
      //  }

        listViewer.update(language, null);
      }
    });    
    
    buttonRemove.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
        Language language = (Language)selection.getFirstElement();
        if(language == null) {
          System.out.println("Please select a language first.");
          return;
        }
        
        languages.remove(language);
        System.out.println("Removed: " + language);
        
        listViewer.refresh(false);
      }
    });
  }
    
  
  public SampleListViewer() {
    RowLayout rowLayout = new RowLayout();
    shell.setLayout(rowLayout);
    
    (new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
    
    init();
    
    addButtons();

    shell.pack();
    shell.open();
    //textUser.forceFocus();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }
  public static void main(String[] args) {
    new SampleListViewer();
  }
}

           
       
Related examples in the same category
1. Demonstrates ListViewerDemonstrates ListViewer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.