CoolBar Examples : CoolBar « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » SWT JFace Eclipse » CoolBarScreenshots 
CoolBar Examples
CoolBar Examples



/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on Feb 27, 2004 12:17:28 AM by JACK $Id$
 *  
 ******************************************************************************/



import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
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.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CoolBarExamples {
  Display display = new Display();
  Shell shell = new Shell(display);

  public CoolBarExamples() {
    shell.setLayout(new GridLayout());

    final CoolBar coolBar = new CoolBar(shell, SWT.NONE);

    coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // cool item with a text field.
    CoolItem textItem = new CoolItem(coolBar, SWT.NONE);

    Text text = new Text(coolBar, SWT.BORDER | SWT.DROP_DOWN);
    text.setText("TEXT");
    text.pack();

    Point size = text.getSize();
    textItem.setControl(text);
    textItem.setSize(textItem.computeSize(size.x, size.y));

    // cool item with a label.
    CoolItem labelItem = new CoolItem(coolBar, SWT.NONE);

    Label label = new Label(coolBar, SWT.NONE);
    label.setText("LABEL");
    label.pack();

    size = label.getSize();
    labelItem.setControl(label);
    labelItem.setSize(textItem.computeSize(size.x, size.y));

    // cool item with a button.
    CoolItem buttonItem = new CoolItem(coolBar, SWT.NONE | SWT.DROP_DOWN);

    Composite composite = new Composite(coolBar, SWT.NONE);
    composite.setLayout(new GridLayout(2true));

    Button button1 = new Button(composite, SWT.PUSH);
    button1.setText("Button 1");
    button1.pack();

    Button button2 = new Button(composite, SWT.PUSH);
    button2.setText("Button 2");
    button2.pack();

    composite.pack();

    size = composite.getSize();
    buttonItem.setControl(composite);
    buttonItem.setSize(buttonItem.computeSize(size.x, size.y));

//    // Test cool item adding method.
//    Label label2 = new Label(coolBar, SWT.NONE);
//    label2.setText("label2");
//    addControlToCoolBar(label2, SWT.DROP_DOWN, coolBar);

    try {
      setState(coolBar, new File("coolbar.state"));
    catch (IOException e1) {
      e1.printStackTrace();
    }
    
    shell.addListener(SWT.Close, new Listener() {
      public void handleEvent(Event event) {
        try {
          saveState(coolBar, new File("coolbar.state") );
        catch (IOException e) {
          e.printStackTrace();
        }
      }
    });

    shell.setSize(300120);
    // shell.pack();
    shell.open();

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

    display.dispose();
  }

  /**
   * Creates a cool item with the given control and add the cool item to the
   * specified cool bar.
   
   @param control
   @param coolItemStyle -
   *            should be SWT.NONE or SWT.DROP_DOWN.
   @param coolBar
   @return the cool item created.
   */
  public static CoolItem addControlToCoolBar(
    Control control,
    int coolItemStyle,
    CoolBar coolBar) {
    CoolItem coolItem = new CoolItem(coolBar, coolItemStyle);
    Point size = control.getSize();
    if (size.x == && size.y == 0) {
      // The control size has not been set yet.
      // Pack the control and recalculate its size.
      control.pack();
      size = control.getSize();
    }

    coolItem.setControl(control);
    coolItem.setSize(coolItem.computeSize(size.x, size.y));

    return coolItem;
  }
  
  // Save the display state of the given cool bar in the specified file. 
  private void saveState(CoolBar coolBar, File filethrows IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    
    try {
      // Orders of items. 
      System.out.println("Item order: " + intArrayToString(coolBar.getItemOrder()));
      int[] order = coolBar.getItemOrder();
      out.writeInt(order.length);
      for(int i=0; i<order.length; i++)
        out.writeInt(order[i]);
      
      // Wrap indices.
      System.out.println("Wrap indices: " + intArrayToString(coolBar.getWrapIndices()));
      int[] wrapIndices = coolBar.getWrapIndices();
      out.writeInt(wrapIndices.length);
      for(int i=0; i<wrapIndices.length; i++)
        out.writeInt(wrapIndices[i]);
      
      // Sizes. 
      Point[] sizes = coolBar.getItemSizes();
      out.writeInt(sizes.length);
      for(int i=0; i<sizes.length; i++) {
        out.writeInt(sizes[i].x);
        out.writeInt(sizes[i].y);
      }
    finally {
      out.close();
    }
    
  }
  
  // Sets the display state for a cool bar, using the saved information in the given file. 
  private void setState(CoolBar coolBar, File filethrows IOException {
    if(! file.exists())
      throw new IOException("File does not exist: " + file);
    
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    
    try {
      // Order
      int size = in.readInt();
      int[] order = new int[size];
      for(int i=0; i<order.length; i++)
        order[i= in.readInt();
      
      // Wrap indices.
      size = in.readInt();
      int[] wrapIndices = new int[size];
      for(int i=0; i<wrapIndices.length; i++)
        wrapIndices[i= in.readInt();
      
      // Sizes.
      size = in.readInt();
      Point[] sizes = new Point[size];
      for(int i=0; i<sizes.length; i++
        sizes[inew Point(in.readInt(), in.readInt());
      
      coolBar.setItemLayout(order, wrapIndices, sizes);
    finally {
      in.close();
    }
    
  }

  public static String intArrayToString(int values[]) {
    StringBuffer sb = new StringBuffer();
    sb.append("{");
    for (int i = 0; values != null && i < values.length; i++) {
      sb.append(values[i]);
      if (i != values.length - 1)
        sb.append(", ");
    }
    sb.append("}");
    return sb.toString();
  }

  public static void main(String[] args) {
    new CoolBarExamples();
  }
}

           
       
Related examples in the same category
1. SWT CoolBar Test DemoSWT CoolBar Test Demo
2. CoolBarTestCoolBarTest
3. Coolbar Example 2
4. Coolbar Example
5. SWT ToolBar DemoSWT ToolBar Demo
6. SWY CoolBarClassSWY CoolBarClass
7. Drop-down a chevron menu containing hidden tool itemsDrop-down a chevron menu containing hidden tool items
8. Create a coolbar (relayout when resized)Create a coolbar (relayout when resized)
9. CoolBar example snippet: create a cool barCoolBar example snippet: create a cool bar
10. Control example snippet: print mouse state and button (down, move, up)Control example snippet: print mouse state and button (down, move, up)
11. Control example snippet: print key state, code and characterControl example snippet: print key state, code and character
ww_w_.___j__av_a__2s__.co___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.