Advertisement
Guest User

X

a guest
Dec 2nd, 2010
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.87 KB | None | 0 0
  1. import org.eclipse.swt.SWT;
  2. import org.eclipse.swt.widgets.Display;
  3. import org.eclipse.swt.widgets.Shell;
  4. import org.eclipse.swt.widgets.Text;
  5. import org.eclipse.swt.widgets.ProgressBar;
  6. import org.eclipse.swt.widgets.Label;
  7. import org.eclipse.swt.graphics.Rectangle;
  8. import org.eclipse.swt.widgets.Monitor;
  9. import org.eclipse.swt.widgets.Button;
  10. import org.eclipse.swt.widgets.Listener;
  11. import org.eclipse.swt.widgets.Control;
  12. import org.eclipse.swt.widgets.Event;
  13. import org.eclipse.swt.layout.GridData;
  14. import org.eclipse.swt.layout.GridLayout;
  15. import org.eclipse.swt.graphics.Image;
  16.  
  17. public class SWTHelloWorld {
  18.  
  19.     private Shell shell;
  20.     private Text txtConsole;
  21.  
  22.     public SWTHelloWorld (Display display) {
  23.         shell = new Shell(display, SWT.TITLE | SWT.CLOSE | SWT.RESIZE);
  24.  
  25.         initShell();
  26.         addIcon();
  27.         addLabelInfo();
  28.         addProgressBar();
  29.         addLabelStatus();
  30.         addTextboxConsole();
  31.         addSeparator();
  32.         addCheckBox();
  33.         addButtonClear();
  34.  
  35.         shell.pack();
  36.         shell.open ();
  37.         while (!shell.isDisposed ()) {
  38.            if (!display.readAndDispatch ()) display.sleep ();
  39.         }
  40.         display.dispose ();
  41.     }
  42.  
  43.    /**
  44.     * Validates a chess move.
  45.     *
  46.     * @return
  47.     */
  48.     private void initShell() {
  49.  
  50.         shell.setText("Operation In Progress");
  51.         //shell.setSize(515, 600);
  52.  
  53.         Image small = new Image(shell.getDisplay(), "Vuze.ico");
  54.  
  55.         GridLayout formGrid = new GridLayout(4, false);
  56.         formGrid.horizontalSpacing = 4;
  57.         formGrid.verticalSpacing = 4;
  58.         formGrid.marginBottom = 5;
  59.         formGrid.marginTop = 20;
  60.         shell.setLayout(formGrid);
  61.  
  62.         shell.setImage(small);
  63.  
  64.         Rectangle bounds = shell.getDisplay().getPrimaryMonitor().getBounds();
  65.         Rectangle rect = shell.getBounds();
  66.         int x = bounds.x + (bounds.width - rect.width) / 2;
  67.         int y = bounds.y + (bounds.height - rect.height) / 2;
  68.  
  69.         shell.setLocation(x, y);
  70.     }
  71.  
  72.    /**
  73.     * Validates a chess move.
  74.     *
  75.     * @return
  76.     */
  77.     private void addIcon() {
  78.         Button button3 = new Button(shell, SWT.PUSH);
  79.         GridData gridData = new GridData();
  80.         gridData.widthHint = 32;
  81.         gridData.heightHint = 32;
  82.         gridData.verticalSpan = 4;
  83.         gridData.horizontalIndent = 25;
  84.         gridData.verticalAlignment = SWT.TOP;
  85.         button3.setLayoutData(gridData);
  86.     }
  87.  
  88.    /**
  89.     * Validates a chess move.
  90.     *
  91.     * @return
  92.     */
  93.     private void addProgressBar() {
  94.         ProgressBar barProgress = new ProgressBar (shell, SWT.INDETERMINATE);
  95.         GridData gridData = new GridData();
  96.         gridData.horizontalSpan = 3;
  97.         gridData.horizontalAlignment = GridData.FILL;
  98.         gridData.widthHint = 385;
  99.         gridData.grabExcessHorizontalSpace = true;
  100.         barProgress.setLayoutData(gridData);
  101.     }
  102.  
  103.    /**
  104.     * Validates a chess move.
  105.     *
  106.     * @return
  107.     */
  108.     private void addLabelInfo() {
  109.         Label lblInfo = new Label(shell, SWT.LEFT);
  110.         GridData gridData = new GridData();
  111.         gridData.horizontalSpan = 3;
  112.         gridData.horizontalAlignment = GridData.FILL;
  113.         lblInfo.setText("Greetings from SWT");
  114.         lblInfo.setLayoutData(gridData);
  115.         lblInfo.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
  116.     }
  117.  
  118.    /**
  119.     * Validates a chess move.
  120.     *
  121.     * @return
  122.     */
  123.     private void addLabelStatus() {
  124.         Label lblStatus = new Label(shell, SWT.LEFT);
  125.         GridData gridData = new GridData();
  126.         gridData.horizontalSpan = 3;
  127.         gridData.horizontalAlignment = GridData.FILL;
  128.         lblStatus.setText("Something");
  129.         lblStatus.setLayoutData(gridData);
  130.         lblStatus.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
  131.     }
  132.  
  133.    /**
  134.     * Validates a chess move.
  135.     *
  136.     * @return
  137.     */
  138.     private void addTextboxConsole() {
  139.         txtConsole = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
  140.         GridData gridData = new GridData();
  141.         gridData.horizontalSpan = 3;
  142.         gridData.horizontalAlignment = SWT.FILL;
  143.         gridData.verticalAlignment =  SWT.FILL;
  144.         gridData.heightHint = 480;
  145.         gridData.grabExcessHorizontalSpace = true;
  146.         gridData.grabExcessVerticalSpace = true;
  147.         gridData.exclude = true;
  148.         txtConsole.setLayoutData(gridData);
  149.     }
  150.  
  151.    /**
  152.     * Validates a chess move.
  153.     *
  154.     * @return
  155.     */
  156.     private void addSeparator() {
  157.         Label rulSeparator = new Label(shell, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
  158.         GridData gridData = new GridData();
  159.         gridData.horizontalSpan = 4;
  160.         gridData.horizontalAlignment = SWT.FILL;
  161.         gridData.verticalAlignment = SWT.END;
  162.         gridData.verticalIndent = 5;
  163.         gridData.grabExcessVerticalSpace = true;
  164.         rulSeparator.setLayoutData(gridData);
  165.     }
  166.  
  167.    /**
  168.     * Validates a chess move.
  169.     *
  170.     * @return
  171.     */
  172.     private void addCheckBox() {
  173.         final Button chkDetails = new Button(shell, SWT.CHECK);
  174.         chkDetails.setText("Show Console");
  175.         GridData gridData = new GridData();
  176.         gridData.horizontalSpan = 2;
  177.         gridData.horizontalAlignment = SWT.LEFT;
  178.         gridData.verticalAlignment = SWT.END;
  179.         chkDetails.setLayoutData(gridData);
  180.  
  181.         chkDetails.addListener(SWT.Selection, new Listener() {
  182.             public void handleEvent(Event event) {
  183.                 if (chkDetails.getSelection()) {
  184.                     GridData data = (GridData) txtConsole.getLayoutData();
  185.                     data.exclude = false;
  186.                     txtConsole.setVisible(!data.exclude);
  187.                     shell.layout(false);
  188.                     //shell.setSize(515, 800);
  189.                 }
  190.                 else {
  191.                     GridData data = (GridData) txtConsole.getLayoutData();
  192.                     data.exclude = true;
  193.                     txtConsole.setVisible(!data.exclude);
  194.                     shell.layout(false);
  195.                     //shell.setSize(515, 200);
  196.                 }
  197.             }
  198.         });
  199.     }
  200.  
  201.    /**
  202.     * Validates a chess move.
  203.     *
  204.     * @return
  205.     */
  206.     private void addButtonClear() {
  207.         Button btnClear = new Button(shell, SWT.PUSH);
  208.         GridData gridData = new GridData();
  209.         gridData.horizontalSpan = 2;
  210.         gridData.widthHint = 130;
  211.         gridData.horizontalAlignment = SWT.RIGHT;
  212.         gridData.verticalAlignment = SWT.END;
  213.         btnClear.setText("Cancel");
  214.         btnClear.setLayoutData(gridData);
  215.     }
  216.  
  217.    /**
  218.     * Validates a chess move.
  219.     *
  220.     * @return
  221.     */
  222.     public static void main(String[] args) {
  223.         Display display = new Display();
  224.         SWTHelloWorld x = new SWTHelloWorld(display);
  225.         display.dispose();
  226.     }
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement