A progress bar dialog : ProgressBar « 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 » ProgressBarScreenshots 
A progress bar dialog
A progress bar dialog



import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.dnd.Clipboard;
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.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

/**
 * progress bar dialog.
 * the first, you must know your app execute times,
 * you need implement two method: 
 
 * process(int times);
 * initGuage();
 
 * you can implements method:
 *  
 * cleanUp()
 * doBefore()
 * doAfter() 
 @author yin_zhiguo
 *         yin_zhiguo@hotmail.com
 
 */
public abstract class ProgressBarDialog extends Dialog {

    private Label processMessageLabel; //info of process finish 
    private Button cancelButton; //cancel button
    private Composite cancelComposite;
    private Label lineLabel;//
    private Composite progressBarComposite;//
    private CLabel message;//
    private ProgressBar progressBar = null//

    private Object result; //
    private Shell shell; //
    private Display display = null
    
    protected volatile boolean isClosed = false;//closed state
    
    protected int executeTime = 50;//process times
    protected String processMessage = "process......";//procress info
    protected String shellTitle = "Progress..."//
    protected Image processImage = SWTUtil.getImageOfMessage();//image
    protected boolean mayCancel = true//cancel
    protected int processBarStyle = SWT.SMOOTH; //process bar style

  public void setMayCancel(boolean mayCancel) {
    this.mayCancel = mayCancel;
  }

    public void setExecuteTime(int executeTime) {
        this.executeTime = executeTime;
    }

    public void setProcessImage(Image processImage) {
    this.processImage = processImage;
  }

  public void setProcessMessage(String processInfo) {
    this.processMessage = processInfo;
  }

  public ProgressBarDialog(Shell parent) {
        super(parent);
    }
    
  
    public abstract void initGuage();

    public Object open() {
        createContents()//create window
        shell.open();
        shell.layout();
        
        //start work
        new ProcessThread(executeTime).start();

        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        return result;
    }

    protected void createContents() {
        shell = new Shell(getParent(), SWT.TITLE | SWT.PRIMARY_MODAL);
        display = shell.getDisplay();
        final GridLayout gridLayout = new GridLayout();
        gridLayout.verticalSpacing = 10;

        shell.setLayout(gridLayout);
        shell.setSize(483181);
        shell.setText(shellTitle);

        final Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        composite.setLayout(new GridLayout());

        message = new CLabel(composite, SWT.NONE);
        message.setImage(processImage);
        message.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
        message.setText(processMessage);

        progressBarComposite = new Composite(shell, SWT.NONE);
        progressBarComposite.setLayoutData(new GridData(GridData.FILL,
                GridData.CENTER, false, false));
        progressBarComposite.setLayout(new FillLayout());

        progressBar = new ProgressBar(progressBarComposite, processBarStyle);
        progressBar.setMaximum(executeTime);

        processMessageLabel = new Label(shell, SWT.NONE);
        processMessageLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));
        lineLabel = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
        lineLabel.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false));

        cancelComposite = new Composite(shell, SWT.NONE);
        cancelComposite.setLayoutData(new GridData(GridData.END,
                GridData.CENTER, false, false));
        final GridLayout gridLayout_1 = new GridLayout();
        gridLayout_1.numColumns = 2;
        cancelComposite.setLayout(gridLayout_1);

        cancelButton = new Button(cancelComposite, SWT.NONE);
        cancelButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                isClosed = true;
                //System.out.println(isClosed);
            }
        });
        cancelButton.setLayoutData(new GridData(78, SWT.DEFAULT));
        cancelButton.setText("cancel");
        cancelButton.setEnabled(this.mayCancel);

    }

    protected abstract String process(int times);


    protected void cleanUp()
    {
      
    }
    

    protected void doBefore()
    {
      
    }
    
    protected void doAfter()
    {
      
    }

    class ProcessThread extends Thread {
        private int max = 0;
        private volatile boolean shouldStop = false;

        ProcessThread(int max) {
            this.max = max;
        }

        public void run() {
          doBefore();
            for (final int[] i = new int[] {1}; i[0<= max; i[0]++
            {
                //
                final String info = process(i[0]);
                if (display.isDisposed()) {
                    return;
                }
                display.syncExec(new Runnable() {
                    public void run() {
                        if (progressBar.isDisposed()) {
                            return;
                        }
                        //
                        processMessageLabel.setText(info);
                        //
                        progressBar.setSelection(i[0]);
                        //
                        if (i[0== max || isClosed) {
                            if (isClosed) {
                                shouldStop = true;//
                                cleanUp();//
                            }
                            shell.close();//
                        }
                    }
                });

                if (shouldStop) {
                    break;
                }
            }
            doAfter();
        }
    }

  public void setShellTitle(String shellTitle) {
    this.shellTitle = shellTitle;
  }

  public void setProcessBarStyle(boolean pStyle) {
    if(pStyle)
      this.processBarStyle = SWT.SMOOTH;
    else
      this.processBarStyle = SWT.NONE;
      
  }
}


class DemoProgressBar extends ProgressBarDialog {
  
  private String[] info = null;
  
  public DemoProgressBar(Shell parent) {
       super(parent);
   }
  

  @Override
  public void initGuage() {
    info = new String[100];
    for (int i = 0; i < info.length; i++) {
      info[i"process task " + i + ".";
    }
    this.setExecuteTime(100);
    this.setMayCancel(true);
    this.setProcessMessage("please waiting....");
    this.setShellTitle("Demo");

  }

  @Override
  protected String process(int arg0) {
    try{
      Thread.sleep((long)(Math.random() 300));
    }catch(Exception e){e.printStackTrace();}
    
    return info[arg0 - 1];
  }

}


class SWTUtil {


    private static ImageRegistry imageRegistry = new ImageRegistry();

    private static Clipboard clipboard = new Clipboard(Display.getCurrent());
    
    private static final String ___IMAGE_Of_MESSAGE = "";
    
    static{
      imageRegistry.put(___IMAGE_Of_MESSAGE, ImageDescriptor.createFromURL(newURL("file:icons/info2.gif")));
    }


    private SWTUtil(){}

    
    public static URL newURL(String url_name) {
        try {
            return new URL(url_name);
        catch (MalformedURLException e) {
            throw new RuntimeException("Malformed URL " + url_name, e);
        }
    }
    
    
    public static void registryImage(String id, String urlName)
    {
      imageRegistry.put(id, ImageDescriptor.createFromURL(newURL(urlName)));
    }
    
    
    public static Image getImage(String id)
    {
      return imageRegistry.get(id);
    }

    
    public static Clipboard getClipboard() {
        return clipboard;
    }
    
    
    public static Image getImageOfMessage()
    {
      return imageRegistry.get(___IMAGE_Of_MESSAGE);
    }
}

class PBDialogDemo extends Shell {

  public static void main(String args[]) {
    try {
      Display display = Display.getDefault();
      PBDialogDemo shell = new PBDialogDemo(display, SWT.SHELL_TRIM);
      shell.open();
      shell.layout();
      while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
          display.sleep();
      }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public PBDialogDemo(Display display, int style) {
    super(display, style);
    createContents();
  }

  protected void createContents() {
    setText("ProgressBar Dialog");
    setSize(21898);
    setLayout(new FillLayout());

    final Button openProgressbarDialogButton = new Button(this, SWT.NONE);
    openProgressbarDialogButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        DemoProgressBar dpb = new DemoProgressBar(PBDialogDemo.this);
        dpb.initGuage();
        dpb.open();
      }
    });
    openProgressbarDialogButton.setText("Open ProgressBar Dialog");
    
  }

  protected void checkSubclass() {
  }

}

           
       
ProgressBarDialog_src.zip( 20 k)
Related examples in the same category
1. Update a SWT progress bar (from another thread)Update a SWT progress bar (from another thread)
2. Update a progress bar (from the UI thread)Update a progress bar (from the UI thread)
3. Count NumbersCount Numbers
4. ProgressBar ExamplesProgressBar Examples
5. Demonstrates ProgressBarDemonstrates ProgressBar
6. ProgressBar ExampleProgressBar Example
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.