diff options
author | Silenio Quarti <silenio> | 2004-08-17 15:28:45 +0000 |
---|---|---|
committer | Silenio Quarti <silenio> | 2004-08-17 15:28:45 +0000 |
commit | f131078b265d59ff026a88fc18755e5cfff9696f (patch) | |
tree | ce5d4b71ccf4be6da465eddd8b32a8228ca3a4a0 /examples/org.eclipse.swt.snippets/src | |
parent | c119dc519b42fe7320eceecb25f228ec605a17ce (diff) | |
download | eclipse.platform.swt-f131078b265d59ff026a88fc18755e5cfff9696f.tar.gz eclipse.platform.swt-f131078b265d59ff026a88fc18755e5cfff9696f.tar.xz eclipse.platform.swt-f131078b265d59ff026a88fc18755e5cfff9696f.zip |
swt snippets
Diffstat (limited to 'examples/org.eclipse.swt.snippets/src')
155 files changed, 10390 insertions, 0 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java new file mode 100644 index 0000000000..7205af668e --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet10.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +public class Snippet10 { + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet100.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet100.java new file mode 100644 index 0000000000..bbb1c4d92c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet100.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Font example snippet: create a large font for use by a text widget + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet100 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setBounds(10, 10, 200, 200); + Text text = new Text(shell, SWT.MULTI); + text.setBounds(10, 10, 150, 150); + Font initialFont = text.getFont(); + FontData[] fontData = initialFont.getFontData(); + for (int i = 0; i < fontData.length; i++) { + fontData[i].setHeight(24); + } + Font newFont = new Font(display, fontData); + text.setFont(newFont); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + newFont.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet101.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet101.java new file mode 100644 index 0000000000..6a94d7de5c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet101.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: insert a table item (at an index) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet101 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setSize (200, 200); + for (int i=0; i<12; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("Item " + i); + } + TableItem item = new TableItem (table, SWT.NONE, 1); + item.setText ("*** New Item " + table.indexOf (item) + " ***"); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} + diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet102.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet102.java new file mode 100644 index 0000000000..881a72a031 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet102.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: insert a tree item (at an index) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet102 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI); + tree.setSize (200, 200); + for (int i=0; i<12; i++) { + TreeItem item = new TreeItem (tree, SWT.NONE); + item.setText ("Item " + i); + } + TreeItem item = new TreeItem (tree, SWT.NONE, 1); + TreeItem [] items = tree.getItems (); + int index = 0; + while (index < items.length && items [index] != item) index++; + item.setText ("*** New Item " + index + " ***"); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet103.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet103.java new file mode 100644 index 0000000000..a75a7c4858 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet103.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: update table item text + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet103 { + +static char content = 'a'; +public static void main(String[] args) { + final Display display = new Display(); + Shell shell = new Shell(display); + shell.setBounds(10, 10, 200, 240); + Table table = new Table(shell, SWT.NONE); + table.setBounds(10, 10, 160, 160); + + final TableItem[] items = new TableItem[4]; + for (int i = 0; i < 4; i++) { + new TableColumn(table, SWT.NONE).setWidth(40); + } + for (int i = 0; i < 4; i++) { + items[i] = new TableItem(table, SWT.NONE); + populateItem(items[i]); + } + + Button button = new Button(shell, SWT.PUSH); + button.setBounds(10, 180, 50, 30); + button.setText("Change"); + button.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + for (int i = 0; i < 4; i++) { + populateItem(items[i]); + } + } + }); + + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} + +static void populateItem(TableItem item) { + String stringContent = String.valueOf(content); + item.setText( + new String[] { + stringContent, + stringContent, + stringContent, + stringContent }); + content++; + if (content > 'z') content = 'a'; +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java new file mode 100644 index 0000000000..e40d1c59e6 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet104.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: create a splash screen + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet104 { + +public static void main(String[] args) { + final Display display = new Display(); + final int [] count = new int [] {4}; + final Image image = new Image(display, 300, 300); + final Shell splash = new Shell(SWT.ON_TOP); + final ProgressBar bar = new ProgressBar(splash, SWT.NONE); + bar.setMaximum(count[0]); + Label label = new Label(splash, SWT.NONE); + label.setImage(image); + FormLayout layout = new FormLayout(); + splash.setLayout(layout); + FormData labelData = new FormData (); + labelData.right = new FormAttachment (100, 0); + labelData.bottom = new FormAttachment (100, 0); + label.setLayoutData(labelData); + FormData progressData = new FormData (); + progressData.left = new FormAttachment (0, 5); + progressData.right = new FormAttachment (100, -5); + progressData.bottom = new FormAttachment (100, -5); + bar.setLayoutData(progressData); + splash.pack(); + Rectangle splashRect = splash.getBounds(); + Rectangle displayRect = display.getBounds(); + int x = (displayRect.width - splashRect.width) / 2; + int y = (displayRect.height - splashRect.height) / 2; + splash.setLocation(x, y); + splash.open(); + display.asyncExec(new Runnable() { + public void run() { + Shell [] shells = new Shell[count[0]]; + for (int i=0; i<count[0]; i++) { + shells [i] = new Shell(display); + shells [i].setSize (300, 300); + shells [i].addListener(SWT.Close, new Listener() { + public void handleEvent (Event e) { + --count[0]; + } + }); + bar.setSelection(i+1); + try {Thread.sleep(1000);} catch (Throwable e) {} + } + splash.close(); + image.dispose(); + for (int i=0; i<count[0]; i++) { + shells [i].open(); + } + } + }); + while (count [0] != 0) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose(); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet105.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet105.java new file mode 100644 index 0000000000..c22fa12335 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet105.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Program example snippet: invoke an external batch file + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.program.*; +import org.eclipse.swt.widgets.*; + +public class Snippet105 { + +public static void main(String[] args) { + Display display = new Display (); + Program.launch("c:\\cygwin\\cygwin.bat"); + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet106.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet106.java new file mode 100644 index 0000000000..fe1cecc3ee --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet106.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: insert a table column (at an index) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet106 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new RowLayout (SWT.VERTICAL)); + final Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setHeaderVisible (true); + for (int i=0; i<4; i++) { + TableColumn column = new TableColumn (table, SWT.NONE); + column.setText ("Column " + i); + } + final TableColumn [] columns = table.getColumns (); + for (int i=0; i<12; i++) { + TableItem item = new TableItem (table, SWT.NONE); + for (int j=0; j<columns.length; j++) { + item.setText (j, "Item " + i); + } + } + for (int i=0; i<columns.length; i++) columns [i].pack (); + Button button = new Button (shell, SWT.PUSH); + final int index = 1; + button.setText ("Insert Column " + index + "a"); + button.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + TableColumn column = new TableColumn (table, SWT.NONE, index); + column.setText ("Column " + index + "a"); + TableItem [] items = table.getItems (); + for (int i=0; i<items.length; i++) { + items [i].setText (index, "Item " + i + "a"); + } + column.pack (); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet107.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet107.java new file mode 100644 index 0000000000..4a511697da --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet107.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; +/* + * Sash example snippet: implement a simple splitter (with a 20 pixel limit) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet107 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + Button button1 = new Button (shell, SWT.PUSH); + button1.setText ("Button 1"); + final Sash sash = new Sash (shell, SWT.VERTICAL); + Button button2 = new Button (shell, SWT.PUSH); + button2.setText ("Button 2"); + + final FormLayout form = new FormLayout (); + shell.setLayout (form); + + FormData button1Data = new FormData (); + button1Data.left = new FormAttachment (0, 0); + button1Data.right = new FormAttachment (sash, 0); + button1Data.top = new FormAttachment (0, 0); + button1Data.bottom = new FormAttachment (100, 0); + button1.setLayoutData (button1Data); + + final int limit = 20, percent = 50; + final FormData sashData = new FormData (); + sashData.left = new FormAttachment (percent, 0); + sashData.top = new FormAttachment (0, 0); + sashData.bottom = new FormAttachment (100, 0); + sash.setLayoutData (sashData); + sash.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + Rectangle sashRect = sash.getBounds (); + Rectangle shellRect = shell.getClientArea (); + int right = shellRect.width - sashRect.width - limit; + e.x = Math.max (Math.min (e.x, right), limit); + if (e.x != sashRect.x) { + sashData.left = new FormAttachment (0, e.x); + shell.layout (); + } + } + }); + + FormData button2Data = new FormData (); + button2Data.left = new FormAttachment (sash, 0); + button2Data.right = new FormAttachment (100, 0); + button2Data.top = new FormAttachment (0, 0); + button2Data.bottom = new FormAttachment (100, 0); + button2.setLayoutData (button2Data); + + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet108.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet108.java new file mode 100644 index 0000000000..1bb0d314f2 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet108.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Button example snippet: set the default button + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet108 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Label label = new Label (shell, SWT.NONE); + label.setText ("Enter your name:"); + Text text = new Text (shell, SWT.BORDER); + text.setLayoutData (new RowData (100, SWT.DEFAULT)); + Button ok = new Button (shell, SWT.PUSH); + ok.setText ("Ok"); + Button cancel = new Button (shell, SWT.PUSH); + cancel.setText ("Cancel"); + shell.setDefaultButton (cancel); + shell.setLayout (new RowLayout ()); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet109.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet109.java new file mode 100644 index 0000000000..7e8d35cf68 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet109.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * SashForm example snippet: create a sash form with three children + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.custom.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet109 { + +public static void main (String [] args) { + final Display display = new Display (); + Shell shell = new Shell(display); + shell.setLayout (new FillLayout()); + + SashForm form = new SashForm(shell,SWT.HORIZONTAL); + form.setLayout(new FillLayout()); + + Composite child1 = new Composite(form,SWT.NONE); + child1.setLayout(new FillLayout()); + new Label(child1,SWT.NONE).setText("Label in pane 1"); + + Composite child2 = new Composite(form,SWT.NONE); + child2.setLayout(new FillLayout()); + new Button(child2,SWT.PUSH).setText("Button in pane2"); + + Composite child3 = new Composite(form,SWT.NONE); + child3.setLayout(new FillLayout()); + new Label(child3,SWT.PUSH).setText("Label in pane3"); + + form.setWeights(new int[] {30,40,30}); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet11.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet11.java new file mode 100644 index 0000000000..11137b0204 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet11.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: set the selection (i-beam) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet11 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Text text = new Text (shell, SWT.BORDER | SWT.V_SCROLL); + text.setBounds (10, 10, 100, 100); + for (int i=0; i<16; i++) { + text.append ("Line " + i + "\n"); + } + shell.open (); + text.setSelection (30); + System.out.println (text.getCaretLocation ()); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet110.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet110.java new file mode 100644 index 0000000000..714a111e10 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet110.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: find a table cell from mouse down (works for any table style) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.*; + +public class Snippet110 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Table table = new Table (shell, SWT.BORDER | SWT.V_SCROLL); + table.setHeaderVisible (true); + table.setLinesVisible (true); + final int rowCount = 64, columnCount = 4; + for (int i=0; i < columnCount; i++) { + TableColumn column = new TableColumn (table, SWT.NULL); + column.setText ("Column " + i); + } + for (int i=0; i < rowCount; i++) { + TableItem item = new TableItem (table, SWT.NULL); + for (int j=0; j < columnCount; j++) { + item.setText (j, "Item " + i + "-" + j); + } + } + for (int i=0; i < columnCount; i++) { + table.getColumn (i).pack (); + } + Point size = table.computeSize (SWT.DEFAULT, 200); + table.setSize (size); + shell.pack (); + table.addListener (SWT.MouseDown, new Listener () { + public void handleEvent (Event event) { + Rectangle clientArea = table.getClientArea (); + Point pt = new Point (event.x, event.y); + int index = table.getTopIndex (); + while (index < table.getItemCount ()) { + boolean visible = false; + TableItem item = table.getItem (index); + for (int i=0; i < columnCount; i++) { + Rectangle rect = item.getBounds (i); + if (rect.contains (pt)) { + System.out.println ("Item " + index + "-" + i); + } + if (!visible && rect.intersects (clientArea)) { + visible = true; + } + } + if (!visible) return; + index++; + } + } + }); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet111.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet111.java new file mode 100644 index 0000000000..20d1c5e77a --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet111.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TreeEditor example snippet: edit the text of a tree item (in place, fancy) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.custom.*; + +public class Snippet111 { + +public static void main (String [] args) { + final Display display = new Display (); + final Color black = display.getSystemColor (SWT.COLOR_BLACK); + Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + final Tree tree = new Tree (shell, SWT.BORDER); + for (int i=0; i<16; i++) { + TreeItem itemI = new TreeItem (tree, SWT.NONE); + itemI.setText ("Item " + i); + for (int j=0; j<16; j++) { + TreeItem itemJ = new TreeItem (itemI, SWT.NONE); + itemJ.setText ("Item " + j); + } + } + final TreeItem [] lastItem = new TreeItem [1]; + final TreeEditor editor = new TreeEditor (tree); + tree.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + final TreeItem item = (TreeItem) e.item; + if (item != null && item == lastItem [0]) { + final Composite composite = new Composite (tree, SWT.NONE); + composite.setBackground (black); + final Text text = new Text (composite, SWT.NONE); + composite.addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Rectangle rect = composite.getClientArea (); + text.setBounds (rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2); + } + }); + Listener textListener = new Listener () { + public void handleEvent (final Event e) { + switch (e.type) { + case SWT.FocusOut: + item.setText (text.getText ()); + composite.dispose (); + break; + case SWT.Verify: + String newText = text.getText (); + String leftText = newText.substring (0, e.start); + String rightText = newText.substring (e.end, newText.length ()); + GC gc = new GC (text); + Point size = gc.textExtent (leftText + e.text + rightText); + gc.dispose (); + size = text.computeSize (size.x, SWT.DEFAULT); + editor.horizontalAlignment = SWT.LEFT; + Rectangle itemRect = item.getBounds (), rect = tree.getClientArea (); + editor.minimumWidth = Math.max (size.x, itemRect.width) + 2; + int left = itemRect.x, right = rect.x + rect.width; + editor.minimumWidth = Math.min (editor.minimumWidth, right - left); + editor.layout (); + break; + case SWT.Traverse: + switch (e.detail) { + case SWT.TRAVERSE_RETURN: + item.setText (text.getText ()); + //FALL THROUGH + case SWT.TRAVERSE_ESCAPE: + composite.dispose (); + e.doit = false; + } + break; + } + } + }; + text.addListener (SWT.FocusOut, textListener); + text.addListener (SWT.Traverse, textListener); + text.addListener (SWT.Verify, textListener); + editor.setEditor (composite, item); + text.setText (item.getText ()); + text.selectAll (); + text.setFocus (); + } + lastItem [0] = item; + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java new file mode 100644 index 0000000000..41eedd5e3d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet112.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Image example snippet: display an image in a group + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet112 { + +public static void main (String [] args) { + Display display = new Display (); + final Image image = new Image (display, 20, 20); + Color color = display.getSystemColor (SWT.COLOR_RED); + GC gc = new GC (image); + gc.setBackground (color); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + color.dispose (); + + Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + Group group = new Group (shell, SWT.NONE); + group.setLayout (new FillLayout ()); + group.setText ("a square"); + Canvas canvas = new Canvas (group, SWT.NONE); + canvas.addPaintListener (new PaintListener () { + public void paintControl (PaintEvent e) { + e.gc.drawImage (image, 0, 0); + } + }); + + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) + display.sleep (); + } + image.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet113.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet113.java new file mode 100644 index 0000000000..a997987e55 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet113.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: detect a selection or check event in a table (SWT.CHECK) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet113 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Table table = new Table (shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + for (int i=0; i<12; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("Item " + i); + } + table.setSize (100, 100); + table.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + String string = event.detail == SWT.CHECK ? "Checked" : "Selected"; + System.out.println (event.item + " " + string); + } + }); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet114.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet114.java new file mode 100644 index 0000000000..a7284fb24d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet114.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: detect a selection or check event in a tree (SWT.CHECK) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet114 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Tree tree = new Tree (shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + for (int i=0; i<12; i++) { + TreeItem item = new TreeItem (tree, SWT.NONE); + item.setText ("Item " + i); + } + tree.setSize (100, 100); + tree.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + String string = event.detail == SWT.CHECK ? "Checked" : "Selected"; + System.out.println (event.item + " " + string); + } + }); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet115.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet115.java new file mode 100644 index 0000000000..118ea2d5d6 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet115.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Composite example snippet: force radio behavior on two different composites + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet115 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new RowLayout (SWT.VERTICAL)); + Composite c1 = new Composite (shell, SWT.BORDER | SWT.NO_RADIO_GROUP); + c1.setLayout (new RowLayout ()); + Composite c2 = new Composite (shell, SWT.BORDER | SWT.NO_RADIO_GROUP); + c2.setLayout (new RowLayout ()); + final Composite [] composites = new Composite [] {c1, c2}; + Listener radioGroup = new Listener () { + public void handleEvent (Event event) { + for (int i=0; i<composites.length; i++) { + Composite composite = composites [i]; + Control [] children = composite.getChildren (); + for (int j=0; j<children.length; j++) { + Control child = children [j]; + if (child instanceof Button) { + Button button = (Button) child; + if ((button.getStyle () & SWT.RADIO) != 0) button.setSelection (false); + } + } + } + Button button = (Button) event.widget; + button.setSelection (true); + } + }; + for (int i=0; i<4; i++) { + Button button = new Button (c1, SWT.RADIO); + button.setText ("Button " + i); + button.addListener (SWT.Selection, radioGroup); + } + for (int i=0; i<4; i++) { + Button button = new Button (c2, SWT.RADIO); + button.setText ("Button " + (i + 4)); + button.addListener (SWT.Selection, radioGroup); + } + shell.pack (); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet116.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet116.java new file mode 100644 index 0000000000..9263b53348 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet116.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: stop CR from going to the default button + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet116 { +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + Text text = new Text(shell, SWT.SINGLE | SWT.BORDER); + text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + text.setText("Here is some text"); + text.addSelectionListener(new SelectionAdapter() { + public void widgetDefaultSelected(SelectionEvent e) { + System.out.println("Text default selected (overrides default button)"); + } + }); + text.addTraverseListener(new TraverseListener() { + public void keyTraversed(TraverseEvent e) { + if (e.detail == SWT.TRAVERSE_RETURN) { + e.doit = false; + e.detail = SWT.TRAVERSE_NONE; + } + } + }); + Button button = new Button(shell, SWT.PUSH); + button.setText("Ok"); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + System.out.println("Button selected"); + } + }); + shell.setDefaultButton(button); + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet117.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet117.java new file mode 100644 index 0000000000..d49fced851 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet117.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: add a select all menu item to the control + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet117 { +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + final Text t = new Text(shell, SWT.BORDER | SWT.MULTI); + + Menu bar = new Menu (shell, SWT.BAR); + shell.setMenuBar (bar); + MenuItem editItem = new MenuItem (bar, SWT.CASCADE); + editItem.setText ("Edit"); + Menu submenu = new Menu (shell, SWT.DROP_DOWN); + editItem.setMenu (submenu); + + MenuItem item = new MenuItem (submenu, SWT.PUSH); + item.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + t.selectAll(); + } + }); + item.setText ("Select &All\tCtrl+A"); + item.setAccelerator (SWT.CTRL + 'A'); + + // Note that as long as your application has not overridden + // the global accelerators for copy, paste, and cut + //(CTRL+C or CTRL+INSERT, CTRL+V or SHIFT+INSERT, and CTRL+X or SHIFT+DELETE) + // these behaviours are already available by default. + // If your application overrides these accelerators, + // you will need to call Text.copy(), Text.paste() and Text.cut() + // from the Selection callback for the accelerator when the + // text widget has focus. + + shell.setSize(200, 200); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java new file mode 100644 index 0000000000..899faa9bd2 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet118.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Cursor example snippet: create a color cursor from an image file + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet118 { + +public static void main (String [] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setSize(150, 150); + final Cursor[] cursor = new Cursor[1]; + Button button = new Button(shell, SWT.PUSH); + button.setText("Change cursor"); + Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT); + button.setSize(size); + button.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + FileDialog dialog = new FileDialog(shell); + dialog.setFilterExtensions(new String[] {"*.ico", "*.gif", "*.*"}); + String name = dialog.open(); + if (name == null) return; + ImageData image = new ImageData(name); + Cursor oldCursor = cursor[0]; + cursor[0] = new Cursor(display, image, 0, 0); + shell.setCursor(cursor[0]); + if (oldCursor != null) oldCursor.dispose(); + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + if (cursor[0] != null) cursor[0].dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet119.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet119.java new file mode 100644 index 0000000000..ec981025f3 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet119.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Cursor example snippet: create a color cursor from a source and a mask + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet119 { + +static byte[] source = { + (byte)0x11, (byte)0x11, (byte)0x11, (byte)0x00, (byte)0x00, (byte)0x11, (byte)0x11, (byte)0x11, + (byte)0x11, (byte)0x10, (byte)0x00, (byte)0x01, (byte)0x10, (byte)0x00, (byte)0x01, (byte)0x11, + (byte)0x11, (byte)0x00, (byte)0x22, (byte)0x01, (byte)0x10, (byte)0x33, (byte)0x00, (byte)0x11, + (byte)0x10, (byte)0x02, (byte)0x22, (byte)0x01, (byte)0x10, (byte)0x33, (byte)0x30, (byte)0x01, + (byte)0x10, (byte)0x22, (byte)0x22, (byte)0x01, (byte)0x10, (byte)0x33, (byte)0x33, (byte)0x01, + (byte)0x10, (byte)0x22, (byte)0x22, (byte)0x01, (byte)0x10, (byte)0x33, (byte)0x33, (byte)0x01, + (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, + (byte)0x01, (byte)0x11, (byte)0x11, (byte)0x01, (byte)0x10, (byte)0x11, (byte)0x11, (byte)0x10, + (byte)0x01, (byte)0x11, (byte)0x11, (byte)0x01, (byte)0x10, (byte)0x11, (byte)0x11, (byte)0x10, + (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, + (byte)0x10, (byte)0x44, (byte)0x44, (byte)0x01, (byte)0x10, (byte)0x55, (byte)0x55, (byte)0x01, + (byte)0x10, (byte)0x44, (byte)0x44, (byte)0x01, (byte)0x10, (byte)0x55, (byte)0x55, (byte)0x01, + (byte)0x10, (byte)0x04, (byte)0x44, (byte)0x01, (byte)0x10, (byte)0x55, (byte)0x50, (byte)0x01, + (byte)0x11, (byte)0x00, (byte)0x44, (byte)0x01, (byte)0x10, (byte)0x55, (byte)0x00, (byte)0x11, + (byte)0x11, (byte)0x10, (byte)0x00, (byte)0x01, (byte)0x10, (byte)0x00, (byte)0x01, (byte)0x11, + (byte)0x11, (byte)0x11, (byte)0x11, (byte)0x00, (byte)0x00, (byte)0x11, (byte)0x11, (byte)0x11, +}; + +static byte[] mask = { + (byte)0x03, (byte)0xc0, + (byte)0x1f, (byte)0xf8, + (byte)0x3f, (byte)0xfc, + (byte)0x7f, (byte)0xfe, + (byte)0x7f, (byte)0xfe, + (byte)0x7f, (byte)0xfe, + (byte)0xff, (byte)0xff, + (byte)0xfe, (byte)0x7f, + (byte)0xfe, (byte)0x7f, + (byte)0xff, (byte)0xff, + (byte)0x7f, (byte)0xfe, + (byte)0x7f, (byte)0xfe, + (byte)0x7f, (byte)0xfe, + (byte)0x3f, (byte)0xfc, + (byte)0x1f, (byte)0xf8, + (byte)0x03, (byte)0xc0 +}; + +public static void main (String [] args) { + Display display = new Display(); + Color white = display.getSystemColor (SWT.COLOR_WHITE); + Color black = display.getSystemColor (SWT.COLOR_BLACK); + Color yellow = display.getSystemColor (SWT.COLOR_YELLOW); + Color red = display.getSystemColor (SWT.COLOR_RED); + Color green = display.getSystemColor (SWT.COLOR_GREEN); + Color blue = display.getSystemColor (SWT.COLOR_BLUE); + + //Create a source ImageData of depth 4 + PaletteData palette = new PaletteData (new RGB[] { + black.getRGB(), white.getRGB(), yellow.getRGB(), + red.getRGB(), blue.getRGB(), green.getRGB()}); + ImageData sourceData = new ImageData (16, 16, 4, palette, 1, source); + + //Create a mask ImageData of depth 1 (monochrome) + palette = new PaletteData (new RGB [] {black.getRGB(), white.getRGB(),}); + ImageData maskData = new ImageData (16, 16, 1, palette, 1, mask); + + //Set mask + sourceData.maskData = maskData.data; + sourceData.maskPad = maskData.scanlinePad; + + //Create cursor + Cursor cursor = new Cursor(display, sourceData, 10, 10); + + //Remove mask to draw them separately just to show what they look like + sourceData.maskData = null; + sourceData.maskPad = -1; + + Shell shell = new Shell(display); + final Image source = new Image (display,sourceData); + final Image mask = new Image (display, maskData); + shell.addPaintListener(new PaintListener() { + public void paintControl(PaintEvent e) { + GC gc = e.gc; + int x = 10, y = 10; + String stringSource = "source: "; + String stringMask = "mask: "; + gc.drawString(stringSource, x, y); + gc.drawString(stringMask, x, y + 30); + x += Math.max(gc.stringExtent(stringSource).x, gc.stringExtent(stringMask).x); + gc.drawImage(source, x, y); + gc.drawImage(mask, x, y + 30); + } + }); + shell.setSize(150, 150); + shell.open(); + shell.setCursor(cursor); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + cursor.dispose(); + source.dispose(); + mask.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet12.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet12.java new file mode 100644 index 0000000000..be408691c7 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet12.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: set the selection (start, end) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet12 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Text text = new Text (shell, SWT.BORDER | SWT.V_SCROLL); + text.setBounds (10, 10, 100, 100); + for (int i=0; i<16; i++) { + text.append ("Line " + i + "\n"); + } + shell.open (); + text.setSelection (30, 32); + System.out.println (text.getCaretLocation ()); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet120.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet120.java new file mode 100644 index 0000000000..01b31a98c1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet120.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Monitor example snippet: center a shell on the primary monitor + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet120 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setSize (200, 200); + Monitor primary = display.getPrimaryMonitor (); + Rectangle bounds = primary.getBounds (); + Rectangle rect = shell.getBounds (); + int x = bounds.x + (bounds.width - rect.width) / 2; + int y = bounds.y + (bounds.height - rect.height) / 2; + shell.setLocation (x, y); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet121.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet121.java new file mode 100644 index 0000000000..ecba3c80ba --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet121.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: prompt for a password (set the echo character) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet121 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + Text text = new Text (shell, SWT.SINGLE | SWT.BORDER); + text.setEchoChar ('*'); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java new file mode 100644 index 0000000000..d2a3c3023f --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Clipboard example snippet: enable/disable menu depending on clipboard content availability + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.dnd.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet122 { + +public static void main(String[] args) { + Display display = new Display(); + final Clipboard cb = new Clipboard(display); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP); + Menu menu = new Menu(shell, SWT.POP_UP); + final MenuItem copyItem = new MenuItem(menu, SWT.PUSH); + copyItem.setText("Copy"); + copyItem.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e) { + String selection = text.getSelectionText(); + if (selection == null) return; + Object[] data = new Object[]{selection}; + Transfer[] types = new Transfer[] {TextTransfer.getInstance()}; + cb.setContents(data, types); + } + }); + final MenuItem pasteItem = new MenuItem(menu, SWT.PUSH); + pasteItem.setText ("Paste"); + pasteItem.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e) { + String string = (String)(cb.getContents(TextTransfer.getInstance())); + if (string != null) { + text.insert(string); + } + } + }); + menu.addMenuListener(new MenuAdapter() { + public void menuShown(MenuEvent e) { + TransferData[] available = cb.getAvailableTypes(); + boolean enabled = false; + for (int i = 0; i < available.length; i++) { + if (TextTransfer.getInstance().isSupportedType(available[i])) { + enabled = true; + break; + } + } + pasteItem.setEnabled(enabled); + } + }); + text.setMenu (menu); + + shell.setSize(200, 200); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + cb.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet123.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet123.java new file mode 100644 index 0000000000..b606063ffc --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet123.java @@ -0,0 +1,189 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * OLE and ActiveX example snippet: get events from IE control + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.ole.win32.*; +import org.eclipse.swt.internal.ole.win32.*; + +public class Snippet123 { + +public static void main(String[] args) { + final Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + OleControlSite controlSite; + try { + OleFrame frame = new OleFrame(shell, SWT.NONE); + controlSite = new OleControlSite(frame, SWT.NONE, "Shell.Explorer"); + controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); + } catch (SWTError e) { + System.out.println("Unable to open activeX control"); + return; + } + shell.open(); + + // IWebBrowser + final OleAutomation webBrowser = new OleAutomation(controlSite); + + // When a new document is loaded, access the document object for the new page. + int DownloadComplete = 104; + controlSite.addEventListener(DownloadComplete, new OleListener() { + public void handleEvent(OleEvent event) { + int[] htmlDocumentID = webBrowser.getIDsOfNames(new String[]{"Document"}); + if (htmlDocumentID == null) return; + Variant pVarResult = webBrowser.getProperty(htmlDocumentID[0]); + if (pVarResult == null || pVarResult.getType() == 0) return; + //IHTMLDocument2 + OleAutomation htmlDocument = pVarResult.getAutomation(); + + // Request to be notified of click, double click and key down events + EventDispatch myDispatch = new EventDispatch(EventDispatch.onclick); + IDispatch idispatch = new IDispatch(myDispatch.getAddress()); + Variant dispatch = new Variant(idispatch); + htmlDocument.setProperty(EventDispatch.onclick, dispatch); + + myDispatch = new EventDispatch(EventDispatch.ondblclick); + idispatch = new IDispatch(myDispatch.getAddress()); + dispatch = new Variant(idispatch); + htmlDocument.setProperty(EventDispatch.ondblclick, dispatch); + + myDispatch = new EventDispatch(EventDispatch.onkeydown); + idispatch = new IDispatch(myDispatch.getAddress()); + dispatch = new Variant(idispatch); + htmlDocument.setProperty(EventDispatch.onkeydown, dispatch); + + //Remember to release OleAutomation Object + htmlDocument.dispose(); + } + }); + + // Navigate to a web site + int[] ids = webBrowser.getIDsOfNames(new String[]{"Navigate", "URL"}); + Variant[] rgvarg = new Variant[] {new Variant("http://www.google.com")}; + int[] rgdispidNamedArgs = new int[]{ids[1]}; + webBrowser.invoke(ids[0], rgvarg, rgdispidNamedArgs); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + //Remember to release OleAutomation Object + webBrowser.dispose(); + display.dispose(); + +} +} +// EventDispatch implements a simple IDispatch interface which will be called +// when the event is fired. +class EventDispatch { + private COMObject iDispatch; + private int refCount = 0; + private int eventID; + + final static int onhelp = 0x8001177d; + final static int onclick = 0x80011778; + final static int ondblclick = 0x80011779; + final static int onkeyup = 0x80011776; + final static int onkeydown = 0x80011775; + final static int onkeypress = 0x80011777; + final static int onmouseup = 0x80011773; + final static int onmousedown = 0x80011772; + final static int onmousemove = 0x80011774; + final static int onmouseout = 0x80011771; + final static int onmouseover = 0x80011770; + final static int onreadystatechange = 0x80011789; + final static int onafterupdate = 0x80011786; + final static int onrowexit= 0x80011782; + final static int onrowenter = 0x80011783; + final static int ondragstart = 0x80011793; + final static int onselectstart = 0x80011795; + + EventDispatch(int eventID) { + this.eventID = eventID; + createCOMInterfaces(); + } + int getAddress() { + return iDispatch.getAddress(); + } + private void createCOMInterfaces() { + iDispatch = new COMObject(new int[]{2, 0, 0, 1, 3, 4, 8}){ + public int method0(int[] args) {return QueryInterface(args[0], args[1]);} + public int method1(int[] args) {return AddRef();} + public int method2(int[] args) {return Release();} + // method3 GetTypeInfoCount - not implemented + // method4 GetTypeInfo - not implemented + // method5 GetIDsOfNames - not implemented + public int method6(int[] args) {return Invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);} + }; + } + private void disposeCOMInterfaces() { + if (iDispatch != null) + iDispatch.dispose(); + iDispatch = null; + + } + private int AddRef() { + refCount++; + return refCount; + } + private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams, int pVarResult, int pExcepInfo, int pArgErr) { + switch (eventID) { + case onhelp: System.out.println("onhelp"); break; + case onclick: System.out.println("onclick"); break; + case ondblclick: System.out.println("ondblclick"); break; + case onkeyup: System.out.println("onkeyup"); break; + case onkeydown: System.out.println("onkeydown"); break; + case onkeypress: System.out.println("onkeypress"); break; + case onmouseup: System.out.println("onmouseup"); break; + case onmousedown: System.out.println("onmousedown"); break; + case onmousemove: System.out.println("onmousemove"); break; + case onmouseout: System.out.println("onmouseout"); break; + case onmouseover: System.out.println("onmouseover"); break; + case onreadystatechange: System.out.println("onreadystatechange"); break; + case onafterupdate: System.out.println("onafterupdate"); break; + case onrowexit: System.out.println("onrowexit"); break; + case onrowenter: System.out.println("onrowenter"); break; + case ondragstart: System.out.println("ondragstart"); break; + case onselectstart: System.out.println("onselectstart"); break; + } + return COM.S_OK; + } + private int QueryInterface(int riid, int ppvObject) { + if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG; + GUID guid = new GUID(); + COM.MoveMemory(guid, riid, GUID.sizeof); + + if ( COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDispatch)) { + COM.MoveMemory(ppvObject, new int[] {iDispatch.getAddress()}, 4); + AddRef(); + return COM.S_OK; + } + COM.MoveMemory(ppvObject, new int[] {0}, 4); + return COM.E_NOINTERFACE; + } + int Release() { + refCount--; + if (refCount == 0) { + disposeCOMInterfaces(); + } + return refCount; + } +} + diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet124.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet124.java new file mode 100644 index 0000000000..bd863ac4ce --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet124.java @@ -0,0 +1,100 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TableEditor example snippet: edit a cell in a table (in place, fancy) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.custom.*; + +public class Snippet124 { +public static void main (String[] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + final Table table = new Table(shell, SWT.BORDER | SWT.MULTI); + table.setLinesVisible (true); + for (int i=0; i<3; i++) { + TableColumn column = new TableColumn (table, SWT.NONE); + column.setWidth(100); + } + for (int i=0; i<3; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText(new String [] {"" + i, "" + i , "" + i}); + } + final TableEditor editor = new TableEditor (table); + editor.horizontalAlignment = SWT.LEFT; + editor.grabHorizontal = true; + table.addListener (SWT.MouseDown, new Listener () { + public void handleEvent (Event event) { + Rectangle clientArea = table.getClientArea (); + Point pt = new Point (event.x, event.y); + int index = table.getTopIndex (); + while (index < table.getItemCount ()) { + boolean visible = false; + final TableItem item = table.getItem (index); + for (int i=0; i<table.getColumnCount (); i++) { + Rectangle rect = item.getBounds (i); + if (rect.contains (pt)) { + final int column = i; + final Text text = new Text (table, SWT.NONE); + Listener textListener = new Listener () { + public void handleEvent (final Event e) { + switch (e.type) { + case SWT.FocusOut: + item.setText (column, text.getText ()); + text.dispose (); + break; + case SWT.Traverse: + switch (e.detail) { + case SWT.TRAVERSE_RETURN: + item.setText (column, text.getText ()); + //FALL THROUGH + case SWT.TRAVERSE_ESCAPE: + text.dispose (); + e.doit = false; + } + break; + } + } + }; + text.addListener (SWT.FocusOut, textListener); + text.addListener (SWT.Traverse, textListener); + editor.setEditor (text, item, i); + text.setText (item.getText (i)); + text.selectAll (); + text.setFocus (); + return; + } + if (!visible && rect.intersects (clientArea)) { + visible = true; + } + } + if (!visible) return; + index++; + } + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet125.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet125.java new file mode 100644 index 0000000000..55e035f8f9 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet125.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tool Tips example snippet: create fake tool tips for items in a table + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet125 { + +public static void main (String[] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + final Table table = new Table (shell, SWT.BORDER); + for (int i = 0; i < 20; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("item " + i); + } + // Disable native tooltip + table.setToolTipText (""); + + // Implement a "fake" tooltip + final Listener labelListener = new Listener () { + public void handleEvent (Event event) { + Label label = (Label)event.widget; + Shell shell = label.getShell (); + switch (event.type) { + case SWT.MouseDown: + Event e = new Event (); + e.item = (TableItem) label.getData ("_TABLEITEM"); + // Assuming table is single select, set the selection as if + // the mouse down event went through to the table + table.setSelection (new TableItem [] {(TableItem) e.item}); + table.notifyListeners (SWT.Selection, e); + // fall through + case SWT.MouseExit: + shell.dispose (); + break; + } + } + }; + + Listener tableListener = new Listener () { + Shell tip = null; + Label label = null; + public void handleEvent (Event event) { + switch (event.type) { + case SWT.Dispose: + case SWT.KeyDown: + case SWT.MouseMove: { + if (tip == null) break; + tip.dispose (); + tip = null; + label = null; + break; + } + case SWT.MouseHover: { + TableItem item = table.getItem (new Point (event.x, event.y)); + if (item != null) { + if (tip != null && !tip.isDisposed ()) tip.dispose (); + tip = new Shell (shell, SWT.ON_TOP); + tip.setLayout (new FillLayout ()); + label = new Label (tip, SWT.NONE); + label.setForeground (display.getSystemColor (SWT.COLOR_INFO_FOREGROUND)); + label.setBackground (display.getSystemColor (SWT.COLOR_INFO_BACKGROUND)); + label.setData ("_TABLEITEM", item); + label.setText ("tooltip "+item.getText ()); + label.addListener (SWT.MouseExit, labelListener); + label.addListener (SWT.MouseDown, labelListener); + Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT); + Rectangle rect = item.getBounds (0); + Point pt = table.toDisplay (rect.x, rect.y); + tip.setBounds (pt.x, pt.y, size.x, size.y); + tip.setVisible (true); + } + } + } + } + }; + table.addListener (SWT.Dispose, tableListener); + table.addListener (SWT.KeyDown, tableListener); + table.addListener (SWT.MouseMove, tableListener); + table.addListener (SWT.MouseHover, tableListener); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet126.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet126.java new file mode 100644 index 0000000000..aa565bacc5 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet126.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: place arbitrary controls in a table + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.custom.*; + +public class Snippet126 { +public static void main(String[] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setLinesVisible (true); + for (int i=0; i<3; i++) { + TableColumn column = new TableColumn(table, SWT.NONE); + column.setWidth (100); + } + for (int i=0; i<12; i++) { + TableItem item = new TableItem (table, SWT.NONE); + } + TableItem [] items = table.getItems (); + for (int i=0; i<items.length; i++) { + TableEditor editor = new TableEditor (table); + CCombo combo = new CCombo (table, SWT.NONE); + editor.grabHorizontal = true; + editor.setEditor(combo, items[i], 0); + editor = new TableEditor (table); + Text text = new Text (table, SWT.NONE); + editor.grabHorizontal = true; + editor.setEditor(text, items[i], 1); + editor = new TableEditor (table); + Button button = new Button (table, SWT.CHECK); + button.pack (); + editor.minimumWidth = button.getSize ().x; + editor.horizontalAlignment = SWT.LEFT; + editor.setEditor (button, items[i], 2); + } + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet127.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet127.java new file mode 100644 index 0000000000..f54d712ed1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet127.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Control example snippet: prevent Tab from traversing out of a control + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.widgets.*; + +public class Snippet127 { +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Button button1 = new Button(shell, SWT.PUSH); + button1.setBounds(10,10,80,30); + button1.setText("no traverse"); + button1.addTraverseListener(new TraverseListener () { + public void keyTraversed(TraverseEvent e) { + switch (e.detail) { + case SWT.TRAVERSE_TAB_NEXT: + case SWT.TRAVERSE_TAB_PREVIOUS: { + e.doit = false; + } + } + } + }); + Button button2 = new Button (shell, SWT.PUSH); + button2.setBounds(100,10,80,30); + button2.setText("can traverse"); + shell.pack (); + shell.open(); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet128.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet128.java new file mode 100644 index 0000000000..26f2894ade --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet128.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Browser example snippet: bring up a browser + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.browser.*; + +public class Snippet128 { + public static void main(String [] args) { + Display display = new Display(); + final Shell shell = new Shell(display); + GridLayout gridLayout = new GridLayout(); + gridLayout.numColumns = 3; + shell.setLayout(gridLayout); + ToolBar toolbar = new ToolBar(shell, SWT.NONE); + ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH); + itemBack.setText("Back"); + ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH); + itemForward.setText("Forward"); + ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH); + itemStop.setText("Stop"); + ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH); + itemRefresh.setText("Refresh"); + ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH); + itemGo.setText("Go"); + + GridData data = new GridData(); + data.horizontalSpan = 3; + toolbar.setLayoutData(data); + + Label labelAddress = new Label(shell, SWT.NONE); + labelAddress.setText("Address"); + + final Text location = new Text(shell, SWT.BORDER); + data = new GridData(); + data.horizontalAlignment = GridData.FILL; + data.horizontalSpan = 2; + data.grabExcessHorizontalSpace = true; + location.setLayoutData(data); + + final Browser browser = new Browser(shell, SWT.NONE); + data = new GridData(); + data.horizontalAlignment = GridData.FILL; + data.verticalAlignment = GridData.FILL; + data.horizontalSpan = 3; + data.grabExcessHorizontalSpace = true; + data.grabExcessVerticalSpace = true; + browser.setLayoutData(data); + + final Label status = new Label(shell, SWT.NONE); + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 2; + status.setLayoutData(data); + + final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE); + data = new GridData(); + data.horizontalAlignment = GridData.END; + progressBar.setLayoutData(data); + + /* event handling */ + Listener listener = new Listener() { + public void handleEvent(Event event) { + ToolItem item = (ToolItem)event.widget; + String string = item.getText(); + if (string.equals("Back")) browser.back(); + else if (string.equals("Forward")) browser.forward(); + else if (string.equals("Stop")) browser.stop(); + else if (string.equals("Refresh")) browser.refresh(); + else if (string.equals("Go")) browser.setUrl(location.getText()); + } + }; + browser.addProgressListener(new ProgressListener() { + public void changed(ProgressEvent event) { + if (event.total == 0) return; + int ratio = event.current * 100 / event.total; + progressBar.setSelection(ratio); + } + public void completed(ProgressEvent event) { + progressBar.setSelection(0); + } + }); + browser.addStatusTextListener(new StatusTextListener() { + public void changed(StatusTextEvent event) { + status.setText(event.text); + } + }); + browser.addLocationListener(new LocationListener() { + public void changed(LocationEvent event) { + if (event.top) location.setText(event.location); + } + public void changing(LocationEvent event) { + } + }); + itemBack.addListener(SWT.Selection, listener); + itemForward.addListener(SWT.Selection, listener); + itemStop.addListener(SWT.Selection, listener); + itemRefresh.addListener(SWT.Selection, listener); + itemGo.addListener(SWT.Selection, listener); + location.addListener(SWT.DefaultSelection, new Listener() { + public void handleEvent(Event e) { + browser.setUrl(location.getText()); + } + }); + + shell.open(); + browser.setUrl("http://eclipse.org"); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet129.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet129.java new file mode 100644 index 0000000000..c6f3b05123 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet129.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: color cells and rows in table + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet129 { + +public static void main(String[] args) { + Display display = new Display(); + Color red = display.getSystemColor(SWT.COLOR_RED); + Color blue = display.getSystemColor(SWT.COLOR_BLUE); + Color white = display.getSystemColor(SWT.COLOR_WHITE); + Color gray = display.getSystemColor(SWT.COLOR_GRAY); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Table table = new Table(shell, SWT.BORDER); + table.setBackground(gray); + TableColumn column1 = new TableColumn(table, SWT.NONE); + TableColumn column2 = new TableColumn(table, SWT.NONE); + TableColumn column3 = new TableColumn(table, SWT.NONE); + TableItem item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"entire","row","red foreground"}); + item.setForeground(red); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"entire","row","red background"}); + item.setBackground(red); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"entire","row","white fore/red back"}); + item.setForeground(white); + item.setBackground(red); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"normal","blue foreground","red foreground"}); + item.setForeground(1, blue); + item.setForeground(2, red); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"normal","blue background","red background"}); + item.setBackground(1, blue); + item.setBackground(2, red); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"white fore/blue back","normal","white fore/red back"}); + item.setForeground(0, white); + item.setBackground(0, blue); + item.setForeground(2, white); + item.setBackground(2, red); + + column1.pack(); + column2.pack(); + column3.pack(); + + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet13.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet13.java new file mode 100644 index 0000000000..4a87ca1564 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet13.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * GC example snippet: draw a thick line + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet13 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.open (); + GC gc = new GC (shell); + gc.setLineWidth (4); + gc.drawRectangle (20, 20, 100, 100); + gc.dispose (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet130.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet130.java new file mode 100644 index 0000000000..d7a7060b6b --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet130.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * BusyIndicator example snippet: display busy cursor during long running task + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.custom.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet130 { + + public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL); + text.setLayoutData(new GridData(GridData.FILL_BOTH)); + final int[] nextId = new int[1]; + Button b = new Button(shell, SWT.PUSH); + b.setText("invoke long running job"); + b.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + Runnable longJob = new Runnable() { + boolean done = false; + int id; + public void run() { + Thread thread = new Thread(new Runnable() { + public void run() { + id = nextId[0]++; + display.syncExec(new Runnable() { + public void run() { + if (text.isDisposed()) return; + text.append("\nStart long running task "+id); + } + }); + for (int i = 0; i < 100000; i++) { + if (display.isDisposed()) return; + System.out.println("do task that takes a long time in a separate thread "+id); + } + if (display.isDisposed()) return; + display.syncExec(new Runnable() { + public void run() { + if (text.isDisposed()) return; + text.append("\nCompleted long running task "+id); + } + }); + done = true; + display.wake(); + } + }); + thread.start(); + while (!done && !shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + } + }; + BusyIndicator.showWhile(display, longJob); + } + }); + shell.setSize(250, 150); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet131.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet131.java new file mode 100644 index 0000000000..0fc210e020 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet131.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Menu example snippet: show a popup menu (wait for it to close) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet131 { +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + shell.addListener (SWT.MenuDetect, new Listener () { + public void handleEvent (Event event) { + Menu menu = new Menu (shell, SWT.POP_UP); + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Menu Item"); + item.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + System.out.println ("Item Selected"); + } + }); + menu.setLocation (event.x, event.y); + menu.setVisible (true); + while (!menu.isDisposed () && menu.isVisible ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + menu.dispose (); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet132.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet132.java new file mode 100644 index 0000000000..92f871e0d6 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet132.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Printing example snippet: print "Hello World!" in black, outlined in red, to default printer + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.printing.*; + +public class Snippet132 { + +public static void main (String [] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.open (); + PrinterData data = Printer.getDefaultPrinterData(); + if (data == null) { + System.out.println("Warning: No default printer."); + return; + } + Printer printer = new Printer(data); + if (printer.startJob("SWT Printing Snippet")) { + Color black = printer.getSystemColor(SWT.COLOR_BLACK); + Color white = printer.getSystemColor(SWT.COLOR_WHITE); + Color red = printer.getSystemColor(SWT.COLOR_RED); + Rectangle trim = printer.computeTrim(0, 0, 0, 0); + Point dpi = printer.getDPI(); + int leftMargin = dpi.x + trim.x; // one inch from left side of paper + int topMargin = dpi.y / 2 + trim.y; // one-half inch from top edge of paper + GC gc = new GC(printer); + Font font = gc.getFont(); // example just uses printer's default font + if (printer.startPage()) { + gc.setBackground(white); + gc.setForeground(black); + String testString = "Hello World!"; + Point extent = gc.stringExtent(testString); + gc.drawString(testString, leftMargin, topMargin + font.getFontData()[0].getHeight()); + gc.setForeground(red); + gc.drawRectangle(leftMargin, topMargin, extent.x, extent.y); + printer.endPage(); + } + gc.dispose(); + printer.endJob(); + } + printer.dispose(); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java new file mode 100644 index 0000000000..c1b5bf834e --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet133.java @@ -0,0 +1,323 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Printing example snippet: print text to printer, with word wrap and pagination + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.io.*; +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.printing.*; + +public class Snippet133 { + Display display; + Shell shell; + Text text; + Font font; + Color foregroundColor, backgroundColor; + + Printer printer; + GC gc; + Font printerFont; + Color printerForegroundColor, printerBackgroundColor; + + int lineHeight = 0; + int tabWidth = 0; + int leftMargin, rightMargin, topMargin, bottomMargin; + int x, y; + int index, end; + String textToPrint; + String tabs; + StringBuffer wordBuffer; + + public static void main(String[] args) { + new Snippet133().open(); + } + + void open() { + display = new Display(); + font = new Font(display, "Courier", 10, SWT.NORMAL); + foregroundColor = display.getSystemColor(SWT.COLOR_BLACK); + backgroundColor = display.getSystemColor(SWT.COLOR_WHITE); + shell = new Shell(display); + shell.setLayout(new FillLayout()); + shell.setText("Print Text"); + shell.setMaximized(true); + text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); + text.setFont(font); + text.setForeground(foregroundColor); + text.setBackground(backgroundColor); + + Menu menuBar = new Menu(shell, SWT.BAR); + shell.setMenuBar(menuBar); + MenuItem item = new MenuItem(menuBar, SWT.CASCADE); + item.setText("&File"); + Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); + item.setMenu(fileMenu); + item = new MenuItem(fileMenu, SWT.PUSH); + item.setText("&Open..."); + item.setAccelerator(SWT.CTRL + 'O'); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + menuOpen(); + } + }); + item = new MenuItem(fileMenu, SWT.PUSH); + item.setText("Font..."); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + menuFont(); + } + }); + item = new MenuItem(fileMenu, SWT.PUSH); + item.setText("Foreground Color..."); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + menuForegroundColor(); + } + }); + item = new MenuItem(fileMenu, SWT.PUSH); + item.setText("Background Color..."); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + menuBackgroundColor(); + } + }); + item = new MenuItem(fileMenu, SWT.PUSH); + item.setText("&Print..."); + item.setAccelerator(SWT.CTRL + 'P'); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + menuPrint(); + } + }); + new MenuItem(fileMenu, SWT.SEPARATOR); + item = new MenuItem(fileMenu, SWT.PUSH); + item.setText("E&xit"); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + System.exit(0); + } + }); + + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + if (font != null) font.dispose(); + if (foregroundColor != null) foregroundColor.dispose(); + if (backgroundColor != null) backgroundColor.dispose(); + } + + void menuOpen() { + final String textString; + FileDialog dialog = new FileDialog(shell, SWT.OPEN); + dialog.setFilterExtensions(new String[] {"*.java", "*.*"}); + String name = dialog.open(); + if ((name == null) || (name.length() == 0)) return; + + try { + File file = new File(name); + FileInputStream stream= new FileInputStream(file.getPath()); + try { + Reader in = new BufferedReader(new InputStreamReader(stream)); + char[] readBuffer= new char[2048]; + StringBuffer buffer= new StringBuffer((int) file.length()); + int n; + while ((n = in.read(readBuffer)) > 0) { + buffer.append(readBuffer, 0, n); + } + textString = buffer.toString(); + stream.close(); + } catch (IOException e) { + MessageBox box = new MessageBox(shell, SWT.ICON_ERROR); + box.setMessage("Error reading file:\n" + name); + box.open(); + return; + } + } catch (FileNotFoundException e) { + MessageBox box = new MessageBox(shell, SWT.ICON_ERROR); + box.setMessage("File not found:\n" + name); + box.open(); + return; + } + text.setText(textString); + } + + void menuFont() { + FontDialog fontDialog = new FontDialog(shell); + fontDialog.setFontList(font.getFontData()); + FontData fontData = fontDialog.open(); + if (fontData != null) { + if (font != null) font.dispose(); + font = new Font(display, fontData); + text.setFont(font); + } + } + + void menuForegroundColor() { + ColorDialog colorDialog = new ColorDialog(shell); + colorDialog.setRGB(foregroundColor.getRGB()); + RGB rgb = colorDialog.open(); + if (rgb != null) { + if (foregroundColor != null) foregroundColor.dispose(); + foregroundColor = new Color(display, rgb); + text.setForeground(foregroundColor); + } + } + + void menuBackgroundColor() { + ColorDialog colorDialog = new ColorDialog(shell); + colorDialog.setRGB(backgroundColor.getRGB()); + RGB rgb = colorDialog.open(); + if (rgb != null) { + if (backgroundColor != null) backgroundColor.dispose(); + backgroundColor = new Color(display, rgb); + text.setBackground(backgroundColor); + } + } + + void menuPrint() { + PrintDialog dialog = new PrintDialog(shell, SWT.NULL); + PrinterData data = dialog.open(); + if (data == null) return; + if (data.printToFile) { + data.fileName = "print.out"; // you probably want to ask the user for a filename + } + + /* Get the text to print from the Text widget (you could get it from anywhere, i.e. your java model) */ + textToPrint = text.getText(); + + /* Do the printing in a background thread so that spooling does not freeze the UI. */ + printer = new Printer(data); + Thread printingThread = new Thread("Printing") { + public void run() { + print(printer); + printer.dispose(); + } + }; + printingThread.start(); + } + + void print(Printer printer) { + if (printer.startJob("Text")) { // the string is the job name - shows up in the printer's job list + Rectangle clientArea = printer.getClientArea(); + Rectangle trim = printer.computeTrim(0, 0, 0, 0); + Point dpi = printer.getDPI(); + leftMargin = dpi.x + trim.x; // one inch from left side of paper + rightMargin = clientArea.width - dpi.x + trim.x + trim.width; // one inch from right side of paper + topMargin = dpi.y + trim.y; // one inch from top edge of paper + bottomMargin = clientArea.height - dpi.y + trim.y + trim.height; // one inch from bottom edge of paper + + /* Create a buffer for computing tab width. */ + int tabSize = 4; // is tab width a user setting in your UI? + StringBuffer tabBuffer = new StringBuffer(tabSize); + for (int i = 0; i < tabSize; i++) tabBuffer.append(' '); + tabs = tabBuffer.toString(); + + /* Create printer GC, and create and set the printer font & foreground color. */ + gc = new GC(printer); + + FontData fontData = font.getFontData()[0]; + printerFont = new Font(printer, fontData.getName(), fontData.getHeight(), fontData.getStyle()); + gc.setFont(printerFont); + tabWidth = gc.stringExtent(tabs).x; + lineHeight = gc.getFontMetrics().getHeight(); + + RGB rgb = foregroundColor.getRGB(); + printerForegroundColor = new Color(printer, rgb); + gc.setForeground(printerForegroundColor); + + rgb = backgroundColor.getRGB(); + printerBackgroundColor = new Color(printer, rgb); + gc.setBackground(printerBackgroundColor); + + /* Print text to current gc using word wrap */ + printText(); + printer.endJob(); + + /* Cleanup graphics resources used in printing */ + printerFont.dispose(); + printerForegroundColor.dispose(); + printerBackgroundColor.dispose(); + gc.dispose(); + } + } + + void printText() { + printer.startPage(); + wordBuffer = new StringBuffer(); + x = leftMargin; + y = topMargin; + index = 0; + end = textToPrint.length(); + while (index < end) { + char c = textToPrint.charAt(index); + index++; + if (c != 0) { + if (c == 0x0a || c == 0x0d) { + if (c == 0x0d && index < end && textToPrint.charAt(index) == 0x0a) { + index++; // if this is cr-lf, skip the lf + } + printWordBuffer(); + newline(); + } else { + if (c != '\t') { + wordBuffer.append(c); + } + if (Character.isWhitespace(c)) { + printWordBuffer(); + if (c == '\t') { + x += tabWidth; + } + } + } + } + } + if (y + lineHeight <= bottomMargin) { + printer.endPage(); + } + } + + void printWordBuffer() { + if (wordBuffer.length() > 0) { + String word = wordBuffer.toString(); + int wordWidth = gc.stringExtent(word).x; + if (x + wordWidth > rightMargin) { + /* word doesn't fit on current line, so wrap */ + newline(); + } + gc.drawString(word, x, y, false); + x += wordWidth; + wordBuffer = new StringBuffer(); + } + } + + void newline() { + x = leftMargin; + y += lineHeight; + if (y + lineHeight > bottomMargin) { + printer.endPage(); + if (index + 1 < end) { + y = topMargin; + printer.startPage(); + } + } + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet134.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet134.java new file mode 100644 index 0000000000..cfeeade297 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet134.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: create a non-rectangular window + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet134 { + +static int[] circle(int r, int offsetX, int offsetY) { + int[] polygon = new int[8 * r + 4]; + //x^2 + y^2 = r^2 + for (int i = 0; i < 2 * r + 1; i++) { + int x = i - r; + int y = (int)Math.sqrt(r*r - x*x); + polygon[2*i] = offsetX + x; + polygon[2*i+1] = offsetY + y; + polygon[8*r - 2*i - 2] = offsetX + x; + polygon[8*r - 2*i - 1] = offsetY - y; + } + return polygon; +} + +public static void main(String[] args) { + final Display display = new Display(); + //Shell must be created with style SWT.NO_TRIM + final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); + shell.setBackground(display.getSystemColor(SWT.COLOR_RED)); + //define a region that looks like a key hole + Region region = new Region(); + region.add(circle(67, 67, 67)); + region.subtract(circle(20, 67, 50)); + region.subtract(new int[]{67, 50, 55, 105, 79, 105}); + //define the shape of the shell using setRegion + shell.setRegion(region); + Rectangle size = region.getBounds(); + shell.setSize(size.width, size.height); + //add ability to move shell around + Listener l = new Listener() { + Point origin; + public void handleEvent(Event e) { + switch (e.type) { + case SWT.MouseDown: + origin = new Point(e.x, e.y); + break; + case SWT.MouseUp: + origin = null; + break; + case SWT.MouseMove: + if (origin != null) { + Point p = display.map(shell, null, e.x, e.y); + shell.setLocation(p.x - origin.x, p.y - origin.y); + } + break; + } + } + }; + shell.addListener(SWT.MouseDown, l); + shell.addListener(SWT.MouseUp, l); + shell.addListener(SWT.MouseMove, l); + //add ability to close shell + Button b = new Button(shell, SWT.PUSH); + b.setBackground(shell.getBackground()); + b.setText("close"); + b.pack(); + b.setLocation(10, 68); + b.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + shell.close(); + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + region.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet135.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet135.java new file mode 100644 index 0000000000..7f8e7a56e2 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet135.java @@ -0,0 +1,274 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: embed Swing/AWT in SWT + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.awt.EventQueue; +import java.io.*; +import java.util.*; +import javax.swing.*; +import javax.swing.table.*; + +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.awt.SWT_AWT; + +public class Snippet135 { + + static class FileTableModel extends AbstractTableModel { + File[] files; + String[] columnsName = {"Name", "Size", "Date Modified"}; + + public FileTableModel (File[] files) { + this.files = files; + } + public int getColumnCount () { + return columnsName.length; + } + public Class getColumnClass (int col) { + if (col == 1) return Long.class; + if (col == 2) return Date.class; + return String.class; + } + public int getRowCount () { + return files == null ? 0 : files.length; + } + public Object getValueAt (int row, int col) { + if (col == 0) return files[row].getName(); + if (col == 1) return new Long(files[row].length()); + if (col == 2) return new Date(files[row].lastModified()); + return ""; + } + public String getColumnName (int col) { + return columnsName[col]; + } + } + + public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setText("SWT and Swing/AWT Example"); + + Listener exitListener = new Listener() { + public void handleEvent(Event e) { + MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION); + dialog.setText("Question"); + dialog.setMessage("Exit?"); + if (e.type == SWT.Close) e.doit = false; + if (dialog.open() != SWT.OK) return; + shell.dispose(); + } + }; + Listener aboutListener = new Listener() { + public void handleEvent(Event e) { + final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); + s.setText("About"); + GridLayout layout = new GridLayout(1, false); + layout.verticalSpacing = 20; + layout.marginHeight = layout.marginWidth = 10; + s.setLayout(layout); + Label label = new Label(s, SWT.NONE); + label.setText("SWT and AWT Example."); + Button button = new Button(s, SWT.PUSH); + button.setText("OK"); + GridData data = new GridData(); + data.horizontalAlignment = GridData.CENTER; + button.setLayoutData(data); + button.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + s.dispose(); + } + }); + s.pack(); + Rectangle parentBounds = shell.getBounds(); + Rectangle bounds = s.getBounds(); + int x = parentBounds.x + (parentBounds.width - bounds.width) / 2; + int y = parentBounds.y + (parentBounds.height - bounds.height) / 2; + s.setLocation(x, y); + s.open(); + while (!s.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + } + }; + shell.addListener(SWT.Close, exitListener); + Menu mb = new Menu(shell, SWT.BAR); + MenuItem fileItem = new MenuItem(mb, SWT.CASCADE); + fileItem.setText("&File"); + Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); + fileItem.setMenu(fileMenu); + MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH); + exitItem.setText("&Exit\tCtrl+X"); + exitItem.setAccelerator(SWT.CONTROL + 'X'); + exitItem.addListener(SWT.Selection, exitListener); + MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH); + aboutItem.setText("&About\tCtrl+A"); + aboutItem.setAccelerator(SWT.CONTROL + 'A'); + aboutItem.addListener(SWT.Selection, aboutListener); + shell.setMenuBar(mb); + + RGB color = shell.getBackground().getRGB(); + Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); + Label locationLb = new Label(shell, SWT.NONE); + locationLb.setText("Location:"); + Composite locationComp = new Composite(shell, SWT.EMBEDDED); + ToolBar toolBar = new ToolBar(shell, SWT.FLAT); + ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH); + exitToolItem.setText("&Exit"); + exitToolItem.addListener(SWT.Selection, exitListener); + ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH); + aboutToolItem.setText("&About"); + aboutToolItem.addListener(SWT.Selection, aboutListener); + Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); + final Composite comp = new Composite(shell, SWT.NONE); + final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER); + Sash sash = new Sash(comp, SWT.VERTICAL); + Composite tableComp = new Composite(comp, SWT.EMBEDDED); + Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); + Composite statusComp = new Composite(shell, SWT.EMBEDDED); + + java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp); + final java.awt.TextField locationText = new java.awt.TextField(); + locationFrame.add(locationText); + + java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp); + java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout()); + fileTableFrame.add(panel); + final JTable fileTable = new JTable(new FileTableModel(null)); + fileTable.setDoubleBuffered(true); + fileTable.setShowGrid(false); + fileTable.createDefaultColumnsFromModel(); + JScrollPane scrollPane = new JScrollPane(fileTable); + panel.add(scrollPane); + + java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp); + statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue)); + final java.awt.Label statusLabel = new java.awt.Label(); + statusFrame.add(statusLabel); + statusLabel.setText("Select a file"); + + sash.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + if (e.detail == SWT.DRAG) return; + GridData data = (GridData)fileTree.getLayoutData(); + Rectangle trim = fileTree.computeTrim(0, 0, 0, 0); + data.widthHint = e.x - trim.width; + comp.layout(); + } + }); + + File[] roots = File.listRoots(); + for (int i = 0; i < roots.length; i++) { + File file = roots[i]; + TreeItem treeItem = new TreeItem(fileTree, SWT.NONE); + treeItem.setText(file.getAbsolutePath()); + treeItem.setData(file); + TreeItem dumyItem = new TreeItem(treeItem, SWT.NONE); + } + fileTree.addListener(SWT.Expand, new Listener() { + public void handleEvent(Event e) { + TreeItem item = (TreeItem)e.item; + if (item == null) return; + if (item.getItemCount() == 1) { + TreeItem firstItem = item.getItems()[0]; + if (firstItem.getData() != null) return; + firstItem.dispose(); + } else { + return; + } + File root = (File)item.getData(); + File[] files = root.listFiles(); + if (files == null) return; + for (int i = 0; i < files.length; i++) { + File file = files[i]; + if (file.isDirectory()) { + TreeItem treeItem = new TreeItem(item, SWT.NONE); + treeItem.setText(file.getName()); + treeItem.setData(file); + TreeItem dumyItem = new TreeItem(treeItem, SWT.NONE); + } + } + } + }); + fileTree.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + TreeItem item = (TreeItem)e.item; + if (item == null) return; + final File root = (File)item.getData(); + EventQueue.invokeLater(new Runnable() { + public void run() { + statusLabel.setText(root.getAbsolutePath()); + locationText.setText(root.getAbsolutePath()); + fileTable.setModel(new FileTableModel(root.listFiles())); + } + }); + } + }); + + GridLayout layout = new GridLayout(4, false); + layout.marginWidth = layout.marginHeight = 0; + layout.horizontalSpacing = layout.verticalSpacing = 1; + shell.setLayout(layout); + GridData data; + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 4; + separator1.setLayoutData(data); + data = new GridData(); + data.horizontalSpan = 1; + data.horizontalIndent = 10; + locationLb.setLayoutData(data); + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 2; + data.heightHint = locationText.getPreferredSize().height; + locationComp.setLayoutData(data); + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 1; + toolBar.setLayoutData(data); + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 4; + separator2.setLayoutData(data); + data = new GridData(GridData.FILL_BOTH); + data.horizontalSpan = 4; + comp.setLayoutData(data); + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 4; + separator3.setLayoutData(data); + data = new GridData(GridData.FILL_HORIZONTAL); + data.horizontalSpan = 4; + data.heightHint = statusLabel.getPreferredSize().height; + statusComp.setLayoutData(data); + + layout = new GridLayout(3, false); + layout.marginWidth = layout.marginHeight = 0; + layout.horizontalSpacing = layout.verticalSpacing = 1; + comp.setLayout(layout); + data = new GridData(GridData.FILL_VERTICAL); + data.widthHint = 200; + fileTree.setLayoutData(data); + data = new GridData(GridData.FILL_VERTICAL); + sash.setLayoutData(data); + data = new GridData(GridData.FILL_BOTH); + tableComp.setLayoutData(data); + + shell.open(); + while(!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet136.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet136.java new file mode 100644 index 0000000000..99b6510ac1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet136.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Browser example snippet: render HTML from memory + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.browser.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet136 { + public static void main(String [] args) { + String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>"; + for (int i = 0; i < 100; i++) html += "<P>This is line "+i+"</P>"; + html += "</BODY></HTML>"; + + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Browser browser = new Browser(shell, SWT.NONE); + browser.setText(html); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} + + diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet137.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet137.java new file mode 100644 index 0000000000..06b54feeca --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet137.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Browser example snippet: render HTML that includes relative links from memory + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.browser.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet137 { + +public static void main(String [] args) { + /* Relative links: use the HTML base tag */ + String html = "<html><head>"+ + "<base href=\"http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/\" >"+ + "<title>HTML Test</title></head>"+ + "<body><a href=\"dev.html\">local link</a></body></html>"; + + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Browser browser = new Browser(shell, SWT.NONE); + browser.setText(html); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java new file mode 100644 index 0000000000..841dc592aa --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet138.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: set icons with different resolutions + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet138 { + public static void main(String[] args) { + Display display = new Display(); + + Image small = new Image(display, 16, 16); + GC gc = new GC(small); + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillArc(0, 0, 16, 16, 45, 270); + gc.dispose(); + + Image large = new Image(display, 32, 32); + gc = new GC(large); + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillArc(0, 0, 32, 32, 45, 270); + gc.dispose(); + + /* Provide different resolutions for icons to get + * high quality rendering wherever the OS needs + * large icons. For example, the ALT+TAB window + * on certain systems uses a larger icon. + */ + Shell shell = new Shell(display); + shell.setText("Small and Large icons"); + shell.setImages(new Image[] {small, large}); + + /* No large icon: the OS will scale up the + * small icon when it needs a large one. + */ + Shell shell2 = new Shell(display); + shell2.setText("Small icon"); + shell2.setImage(small); + + shell.open(); + shell2.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java new file mode 100644 index 0000000000..028e580d86 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet139.java @@ -0,0 +1,137 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * This snippet was adapted from org.eclipse.draw2d.ImageUtilities in + * http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.draw2d/?cvsroot=Tools_Project + * by Pratik Shah. + * + * example snippet: rotate and flip an image + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet139 { + +static ImageData rotate(ImageData srcData, int direction) { + int bytesPerPixel = srcData.bytesPerLine / srcData.width; + int destBytesPerLine = (direction == SWT.DOWN)? srcData.width * bytesPerPixel : srcData.height * bytesPerPixel; + byte[] newData = new byte[srcData.data.length]; + int width = 0, height = 0; + for (int srcY = 0; srcY < srcData.height; srcY++) { + for (int srcX = 0; srcX < srcData.width; srcX++) { + int destX = 0, destY = 0, destIndex = 0, srcIndex = 0; + switch (direction){ + case SWT.LEFT: // left 90 degrees + destX = srcY; + destY = srcData.width - srcX - 1; + width = srcData.height; + height = srcData.width; + break; + case SWT.RIGHT: // right 90 degrees + destX = srcData.height - srcY - 1; + destY = srcX; + width = srcData.height; + height = srcData.width; + break; + case SWT.DOWN: // 180 degrees + destX = srcData.width - srcX - 1; + destY = srcData.height - srcY - 1; + width = srcData.width; + height = srcData.height; + break; + } + destIndex = (destY * destBytesPerLine) + (destX * bytesPerPixel); + srcIndex = (srcY * srcData.bytesPerLine) + (srcX * bytesPerPixel); + System.arraycopy(srcData.data, srcIndex, newData, destIndex, bytesPerPixel); + } + } + // destBytesPerLine is used as scanlinePad to ensure that no padding is required + return new ImageData(width, height, srcData.depth, srcData.palette, destBytesPerLine, newData); +} +static ImageData flip(ImageData srcData, boolean vertical) { + int bytesPerPixel = srcData.bytesPerLine / srcData.width; + int destBytesPerLine = srcData.width * bytesPerPixel; + byte[] newData = new byte[srcData.data.length]; + for (int srcY = 0; srcY < srcData.height; srcY++) { + for (int srcX = 0; srcX < srcData.width; srcX++) { + int destX = 0, destY = 0, destIndex = 0, srcIndex = 0; + if (vertical){ + destX = srcX; + destY = srcData.height - srcY - 1; + } else { + destX = srcData.width - srcX - 1; + destY = srcY; + } + destIndex = (destY * destBytesPerLine) + (destX * bytesPerPixel); + srcIndex = (srcY * srcData.bytesPerLine) + (srcX * bytesPerPixel); + System.arraycopy(srcData.data, srcIndex, newData, destIndex, bytesPerPixel); + } + } + // destBytesPerLine is used as scanlinePad to ensure that no padding is required + return new ImageData(srcData.width, srcData.height, srcData.depth, srcData.palette, destBytesPerLine, newData); +} + +public static void main(String[] args) { + Display display = new Display(); + + // create an image with the word "hello" on it + final Image image0 = new Image(display, 50, 30); + GC gc = new GC(image0); + gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); + gc.fillRectangle(image0.getBounds()); + gc.drawString("hello", 5, 5, true); + gc.dispose(); + + ImageData data = image0.getImageData(); + // rotate and flip this image + final Image image1 = new Image(display, rotate(data, SWT.LEFT)); + final Image image2 = new Image(display, rotate(data, SWT.RIGHT)); + final Image image3 = new Image(display, rotate(data, SWT.DOWN)); + final Image image4 = new Image(display, flip(data, true)); + final Image image5 = new Image(display, flip(data, false)); + + Shell shell = new Shell(display); + // draw the results on the shell + shell.addPaintListener(new PaintListener(){ + public void paintControl(PaintEvent e) { + e.gc.drawText("Original Image:", 10, 10, true); + e.gc.drawImage(image0, 10, 40); + e.gc.drawText("Left, Right, 180:", 10, 80, true); + e.gc.drawImage(image1, 10, 110); + e.gc.drawImage(image2, 50, 110); + e.gc.drawImage(image3, 90, 110); + e.gc.drawText("Flipped Vertical, Horizontal:", 10, 170, true); + e.gc.drawImage(image4, 10, 200); + e.gc.drawImage(image5, 70, 200); + } + }); + shell.setSize(300, 300); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + image0.dispose(); + image1.dispose(); + image2.dispose(); + image3.dispose(); + image4.dispose(); + image5.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet14.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet14.java new file mode 100644 index 0000000000..ff0109bfbc --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet14.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Control example snippet: detect mouse enter, exit and hover events + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet14 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setSize (100, 100); + shell.addListener (SWT.MouseEnter, new Listener () { + public void handleEvent (Event e) { + System.out.println ("ENTER"); + } + }); + shell.addListener (SWT.MouseExit, new Listener () { + public void handleEvent (Event e) { + System.out.println ("EXIT"); + } + }); + shell.addListener (SWT.MouseHover, new Listener () { + public void handleEvent (Event e) { + System.out.println ("HOVER"); + } + }); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet140.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet140.java new file mode 100644 index 0000000000..40c8b27c81 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet140.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * CoolBar example snippet: drop-down a chevron menu containing hidden tool items + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; + +public class Snippet140 { + static Display display; + static Shell shell; + static CoolBar coolBar; + static Menu chevronMenu = null; + +public static void main (String [] args) { + display = new Display (); + shell = new Shell (display); + shell.setLayout(new GridLayout()); + coolBar = new CoolBar(shell, SWT.FLAT | SWT.BORDER); + coolBar.setLayoutData(new GridData(GridData.FILL_BOTH)); + ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT | SWT.WRAP); + int minWidth = 0; + for (int j = 0; j < 5; j++) { + int width = 0; + ToolItem item = new ToolItem(toolBar, SWT.PUSH); + item.setText("B" + j); + width = item.getWidth(); + /* find the width of the widest tool */ + if (width > minWidth) minWidth = width; + } + CoolItem coolItem = new CoolItem(coolBar, SWT.DROP_DOWN); + coolItem.setControl(toolBar); + Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT); + Point coolSize = coolItem.computeSize (size.x, size.y); + coolItem.setMinimumSize(minWidth, coolSize.y); + coolItem.setPreferredSize(coolSize); + coolItem.setSize(coolSize); + coolItem.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent event) { + if (event.detail == SWT.ARROW) { + CoolItem item = (CoolItem) event.widget; + Rectangle itemBounds = item.getBounds (); + Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y)); + itemBounds.x = pt.x; + itemBounds.y = pt.y; + ToolBar toolBar = (ToolBar) item.getControl (); + ToolItem[] tools = toolBar.getItems (); + + int i = 0; + while (i < tools.length) { + Rectangle toolBounds = tools[i].getBounds (); + pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y)); + toolBounds.x = pt.x; + toolBounds.y = pt.y; + + /* Figure out the visible portion of the tool by looking at the + * intersection of the tool bounds with the cool item bounds. */ + Rectangle intersection = itemBounds.intersection (toolBounds); + + /* If the tool is not completely within the cool item bounds, then it + * is partially hidden, and all remaining tools are completely hidden. */ + if (!intersection.equals (toolBounds)) break; + i++; + } + + /* Create a menu with items for each of the completely hidden buttons. */ + if (chevronMenu != null) chevronMenu.dispose(); + chevronMenu = new Menu (coolBar); + for (int j = i; j < tools.length; j++) { + MenuItem menuItem = new MenuItem (chevronMenu, SWT.PUSH); + menuItem.setText (tools[j].getText()); + } + + /* Drop down the menu below the chevron, with the left edges aligned. */ + pt = coolBar.toDisplay(new Point(event.x, event.y)); + chevronMenu.setLocation (pt.x, pt.y); + chevronMenu.setVisible (true); + } + } + }); + + shell.pack(); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java new file mode 100644 index 0000000000..476ccb6414 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java @@ -0,0 +1,157 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Image example snippet: display an animated GIF + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet141 { + static Display display; + static Shell shell; + static GC shellGC; + static Color shellBackground; + static ImageLoader loader; + static ImageData[] imageDataArray; + static Thread animateThread; + static Image image; + static final boolean useGIFBackground = false; + + public static void main(String[] args) { + display = new Display(); + shell = new Shell(display); + shell.setSize(300, 300); + shell.open(); + shellGC = new GC(shell); + shellBackground = shell.getBackground(); + + FileDialog dialog = new FileDialog(shell); + dialog.setFilterExtensions(new String[] {"*.gif"}); + String fileName = dialog.open(); + if (fileName != null) { + loader = new ImageLoader(); + try { + imageDataArray = loader.load(fileName); + if (imageDataArray.length > 1) { + animateThread = new Thread("Animation") { + public void run() { + /* Create an off-screen image to draw on, and fill it with the shell background. */ + Image offScreenImage = new Image(display, loader.logicalScreenWidth, loader.logicalScreenHeight); + GC offScreenImageGC = new GC(offScreenImage); + offScreenImageGC.setBackground(shellBackground); + offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth, loader.logicalScreenHeight); + + try { + /* Create the first image and draw it on the off-screen image. */ + int imageDataIndex = 0; + ImageData imageData = imageDataArray[imageDataIndex]; + if (image != null && !image.isDisposed()) image.dispose(); + image = new Image(display, imageData); + offScreenImageGC.drawImage( + image, + 0, + 0, + imageData.width, + imageData.height, + imageData.x, + imageData.y, + imageData.width, + imageData.height); + + /* Now loop through the images, creating and drawing each one + * on the off-screen image before drawing it on the shell. */ + int repeatCount = loader.repeatCount; + while (loader.repeatCount == 0 || repeatCount > 0) { + switch (imageData.disposalMethod) { + case SWT.DM_FILL_BACKGROUND: + /* Fill with the background color before drawing. */ + Color bgColor = null; + if (useGIFBackground && loader.backgroundPixel != -1) { + bgColor = new Color(display, imageData.palette.getRGB(loader.backgroundPixel)); + } + offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground); + offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); + if (bgColor != null) bgColor.dispose(); + break; + case SWT.DM_FILL_PREVIOUS: + /* Restore the previous image before drawing. */ + offScreenImageGC.drawImage( + image, + 0, + 0, + imageData.width, + imageData.height, + imageData.x, + imageData.y, + imageData.width, + imageData.height); + break; + } + + imageDataIndex = (imageDataIndex + 1) % imageDataArray.length; + imageData = imageDataArray[imageDataIndex]; + image.dispose(); + image = new Image(display, imageData); + offScreenImageGC.drawImage( + image, + 0, + 0, + imageData.width, + imageData.height, + imageData.x, + imageData.y, + imageData.width, + imageData.height); + + /* Draw the off-screen image to the shell. */ + shellGC.drawImage(offScreenImage, 0, 0); + + /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */ + try { + int ms = imageData.delayTime * 10; + if (ms < 20) ms += 30; + if (ms < 30) ms += 10; + Thread.sleep(ms); + } catch (InterruptedException e) { + } + + /* If we have just drawn the last image, decrement the repeat count and start again. */ + if (imageDataIndex == imageDataArray.length - 1) repeatCount--; + } + } catch (SWTException ex) { + System.out.println("There was an error animating the GIF"); + } finally { + if (offScreenImage != null && !offScreenImage.isDisposed()) offScreenImage.dispose(); + if (offScreenImageGC != null && !offScreenImageGC.isDisposed()) offScreenImageGC.dispose(); + if (image != null && !image.isDisposed()) image.dispose(); + } + } + }; + animateThread.setDaemon(true); + animateThread.start(); + } + } catch (SWTException ex) { + System.out.println("There was an error loading the GIF"); + } + } + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet142.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet142.java new file mode 100644 index 0000000000..90cd4125f2 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet142.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * UI Automation (for testing tools) snippet: post mouse events + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet142 { + +public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + final Button button = new Button(shell,SWT.NONE); + button.setSize(100,100); + button.setText("Click"); + shell.pack(); + shell.open(); + button.addListener(SWT.MouseDown, new Listener() { + public void handleEvent(Event e){ + System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")"); + } + }); + final Point pt = display.map(shell, null, 50, 50); + new Thread(){ + Event event; + public void run(){ + try { + Thread.sleep(300); + } catch (InterruptedException e) {} + event = new Event(); + event.type = SWT.MouseMove; + event.x = pt.x; + event.y = pt.y; + display.post(event); + try { + Thread.sleep(300); + } catch (InterruptedException e) {} + event.type = SWT.MouseDown; + event.button = 1; + display.post(event); + try { + Thread.sleep(300); + } catch (InterruptedException e) {} + event.type = SWT.MouseUp; + display.post(event); + } + }.start(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java new file mode 100644 index 0000000000..d513439c01 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet143.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tray example snippet: place an icon with a popup menu on the system tray + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet143 { + +public static void main(String[] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Image image = new Image (display, 16, 16); + final Tray tray = display.getSystemTray (); + final TrayItem item = new TrayItem (tray, SWT.NONE); + item.setToolTipText("SWT TrayItem"); + item.addListener (SWT.Show, new Listener () { + public void handleEvent (Event event) { + System.out.println("show"); + } + }); + item.addListener (SWT.Hide, new Listener () { + public void handleEvent (Event event) { + System.out.println("hide"); + } + }); + item.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + System.out.println("selection"); + } + }); + item.addListener (SWT.DefaultSelection, new Listener () { + public void handleEvent (Event event) { + System.out.println("default selection"); + } + }); + final Menu menu = new Menu (shell, SWT.POP_UP); + for (int i = 0; i < 8; i++) { + MenuItem mi = new MenuItem (menu, SWT.PUSH); + mi.setText ("Item" + i); + } + item.addListener (SWT.MenuDetect, new Listener () { + public void handleEvent (Event event) { + menu.setVisible (true); + } + }); + item.setImage (image); + shell.setBounds(50, 50, 300, 200); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet144.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet144.java new file mode 100644 index 0000000000..4970babf4c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet144.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Virtual Table example snippet: create a table with 1,000,000 items + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet144 { + +static final int COUNT = 1000000; + +public static void main(String[] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + shell.setLayout (new RowLayout (SWT.VERTICAL)); + final Table table = new Table (shell, SWT.VIRTUAL | SWT.BORDER); + table.addListener (SWT.SetData, new Listener () { + public void handleEvent (Event event) { + TableItem item = (TableItem) event.item; + int index = table.indexOf (item); + item.setText ("Item " + index); + System.out.println (item.getText ()); + } + }); + table.setLayoutData (new RowData (200, 200)); + Button button = new Button (shell, SWT.PUSH); + button.setText ("Add Items"); + final Label label = new Label(shell, SWT.NONE); + button.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + long t1 = System.currentTimeMillis (); + table.setItemCount (COUNT); + long t2 = System.currentTimeMillis (); + label.setText ("Items: " + COUNT + ", Time: " + (t2 - t1) + " (ms)"); + shell.layout (); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet145.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet145.java new file mode 100644 index 0000000000..eee2f9770a --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet145.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TextLayout example snippet: draw internationalized styled text on a shell + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.*; + +public class Snippet145 { + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell (display); + + Font font1 = new Font(display, "Tahoma", 14, SWT.BOLD); + Font font2 = new Font(display, "MS Mincho", 20, SWT.ITALIC); + Font font3 = new Font(display, "Arabic Transparent", 18, SWT.NORMAL); + + Color blue = display.getSystemColor(SWT.COLOR_BLUE); + Color green = display.getSystemColor(SWT.COLOR_GREEN); + Color yellow = display.getSystemColor(SWT.COLOR_YELLOW); + Color gray = display.getSystemColor(SWT.COLOR_GRAY); + + final TextLayout layout = new TextLayout(display); + TextStyle style1 = new TextStyle(font1, yellow, blue); + TextStyle style2 = new TextStyle(font2, green, null); + TextStyle style3 = new TextStyle(font3, blue, gray); + + layout.setText("English \u65E5\u672C\u8A9E \u0627\u0644\u0639\u0631\u0628\u064A\u0651\u0629"); + layout.setStyle(style1, 0, 6); + layout.setStyle(style2, 8, 10); + layout.setStyle(style3, 13, 21); + + shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); + shell.addListener(SWT.Paint, new Listener() { + public void handleEvent (Event event) { + layout.draw(event.gc, 10, 10); + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + font1.dispose(); + font2.dispose(); + font3.dispose(); + layout.dispose(); + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet146.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet146.java new file mode 100644 index 0000000000..f461762676 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet146.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * UI Automation (for testing tools) snippet: post key events + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet146 { + +public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + final Text text = new Text(shell, SWT.BORDER); + text.setSize(text.computeSize(150, SWT.DEFAULT)); + shell.pack(); + shell.open(); + new Thread(){ + public void run(){ + String string = "Love the method."; + for (int i = 0; i < string.length(); i++) { + char ch = string.charAt(i); + boolean shift = Character.isUpperCase(ch); + ch = Character.toLowerCase(ch); + if (shift) { + Event event = new Event(); + event.type = SWT.KeyDown; + event.keyCode = SWT.SHIFT; + display.post(event); + } + Event event = new Event(); + event.type = SWT.KeyDown; + event.character = ch; + display.post(event); + try { + Thread.sleep(10); + } catch (InterruptedException e) {} + event.type = SWT.KeyUp; + display.post(event); + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + if (shift) { + event = new Event(); + event.type = SWT.KeyUp; + event.keyCode = SWT.SHIFT; + display.post(event); + } + } + } + }.start(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet147.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet147.java new file mode 100644 index 0000000000..57857b0520 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet147.java @@ -0,0 +1,61 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: stop CR from going to the default button + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet147 { +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + Combo combo = new Combo(shell, SWT.NONE); + combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + combo.setText("Here is some text"); + combo.addSelectionListener(new SelectionAdapter() { + public void widgetDefaultSelected(SelectionEvent e) { + System.out.println("Combo default selected (overrides default button)"); + } + }); + combo.addTraverseListener(new TraverseListener() { + public void keyTraversed(TraverseEvent e) { + if (e.detail == SWT.TRAVERSE_RETURN) { + e.doit = false; + e.detail = SWT.TRAVERSE_NONE; + } + } + }); + Button button = new Button(shell, SWT.PUSH); + button.setText("Ok"); + button.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + System.out.println("Button selected"); + } + }); + shell.setDefaultButton(button); + shell.pack(); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet148.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet148.java new file mode 100644 index 0000000000..0de0f3388c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet148.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: check if the browser is available or not + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.browser.*; + +public class Snippet148 { + + public static void main(String[] args) { + final Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Browser browser = null; + try { + browser = new Browser(shell, SWT.NONE); + } catch (SWTError e) { + /* The Browser widget throws an SWTError if it fails to + * instantiate properly. Application code should catch + * this SWTError and disable any feature requiring the + * Browser widget. + * Platform requirements for the SWT Browser widget are available + * from the SWT FAQ website. + */ + } + if (browser != null) { + /* The Browser widget can be used */ + browser.setUrl("http://www.eclipse.org"); + } + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} + + diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet149.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet149.java new file mode 100644 index 0000000000..a79726b944 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet149.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TableEditor example snippet: place a progress bar in a table + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.custom.*; + +public class Snippet149 { + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout (new FillLayout ()); + Table table = new Table (shell, SWT.BORDER); + table.setHeaderVisible (true); + for (int i=0; i<2; i++) { + TableColumn column = new TableColumn (table, SWT.NONE); + } + table.getColumn (0).setText ("Task"); + table.getColumn (1).setText ("Progress"); + for (int i=0; i<4; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("Task " + i); + ProgressBar bar = new ProgressBar (table, SWT.NONE); + bar.setSelection (i * 25); + TableEditor editor = new TableEditor (table); + editor.grabHorizontal = editor.grabVertical = true; + editor.setEditor (bar, item, 1); + } + table.getColumn (0).pack (); + table.getColumn (1).setWidth (128); + shell.pack (); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet15.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet15.java new file mode 100644 index 0000000000..5328052fc7 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet15.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: create a tree + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet15 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Tree tree = new Tree (shell, SWT.BORDER); + tree.setSize (100, 100); + shell.setSize (200, 200); + for (int i=0; i<4; i++) { + TreeItem iItem = new TreeItem (tree, 0); + iItem.setText ("TreeItem (0) -" + i); + for (int j=0; j<4; j++) { + TreeItem jItem = new TreeItem (iItem, 0); + jItem.setText ("TreeItem (1) -" + j); + for (int k=0; k<4; k++) { + TreeItem kItem = new TreeItem (jItem, 0); + kItem.setText ("TreeItem (2) -" + k); + for (int l=0; l<4; l++) { + TreeItem lItem = new TreeItem (kItem, 0); + lItem.setText ("TreeItem (3) -" + l); + } + } + } + } + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet150.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet150.java new file mode 100644 index 0000000000..54fb72092b --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet150.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * CoolBar example snippet: create a coolbar (relayout when resized) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet150 { + +static int itemCount; +static CoolItem createItem(CoolBar coolBar, int count) { + ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT); + for (int i = 0; i < count; i++) { + ToolItem item = new ToolItem(toolBar, SWT.PUSH); + item.setText(itemCount++ +""); + } + toolBar.pack(); + Point size = toolBar.getSize(); + CoolItem item = new CoolItem(coolBar, SWT.NONE); + item.setControl(toolBar); + Point preferred = item.computeSize(size.x, size.y); + item.setPreferredSize(preferred); + return item; +} + +public static void main(String[] args) { + Display display = new Display(); + final Shell shell = new Shell(display); + CoolBar coolBar = new CoolBar(shell, SWT.NONE); + createItem(coolBar, 3); + createItem(coolBar, 2); + createItem(coolBar, 3); + createItem(coolBar, 4); + int style = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL; + Text text = new Text(shell, style); + FormLayout layout = new FormLayout(); + shell.setLayout(layout); + FormData coolData = new FormData(); + coolData.left = new FormAttachment(0); + coolData.right = new FormAttachment(100); + coolData.top = new FormAttachment(0); + coolBar.setLayoutData(coolData); + coolBar.addListener(SWT.Resize, new Listener() { + public void handleEvent(Event event) { + shell.layout(); + } + }); + FormData textData = new FormData(); + textData.left = new FormAttachment(0); + textData.right = new FormAttachment(100); + textData.top = new FormAttachment(coolBar); + textData.bottom = new FormAttachment(100); + text.setLayoutData(textData); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java new file mode 100644 index 0000000000..41017c7568 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Create a virtual table and add 1000 entries to it every 500 ms. + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.util.*; +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet151 { + +static int[] values = new int[0]; +static Thread thread; + +public static void main (String [] args) { + final Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + final Table table = new Table(shell, SWT.BORDER | SWT.VIRTUAL); + table.addListener(SWT.SetData, new Listener() { + public void handleEvent(Event e) { + TableItem item = (TableItem)e.item; + int index = table.indexOf(item); + item.setText("item "+values[index]); + } + }); + final Random random = new Random(); + thread = new Thread() { + public void run() { + int count = 0; + while (count++ < 2000) { + if (table.isDisposed()) return; + // add 1000 random numbers to a sorted array + for (int j = 0; j < 1000; j++) { + int next = random.nextInt(); + int[] newItems = new int[values.length + 1]; + int index = 0; + for (int i = 0; i < values.length; i++) { + if (values[i] > next) break; + index++; + } + System.arraycopy(values, 0, newItems, 0, index); + newItems[index] = next; + System.arraycopy(values, index, newItems, index+1, values.length-index); + values = newItems; + } + display.syncExec(new Runnable() { + public void run() { + if (table.isDisposed()) return; + table.setItemCount(values.length); + table.clearAll(); + // bug 69398 on Windows + table.redraw(); + } + }); + try {Thread.sleep(500);} catch (Throwable t) {} + } + } + }; + thread.start(); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet152.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet152.java new file mode 100644 index 0000000000..92ef6cc11c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet152.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Control example snippet: update a status line when an item is armed + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ + +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet152 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + FormLayout layout = new FormLayout(); + shell.setLayout(layout); + final Label label = new Label(shell, SWT.BORDER); + Listener armListener = new Listener() { + public void handleEvent(Event event) { + MenuItem item = (MenuItem) event.widget; + label.setText(item.getText()); + label.update(); + } + }; + Listener showListener = new Listener() { + public void handleEvent(Event event) { + Menu menu = (Menu) event.widget; + MenuItem item = menu.getParentItem(); + if (item != null) { + label.setText(item.getText()); + label.update(); + } + } + }; + Listener hideListener = new Listener() { + public void handleEvent(Event event) { + label.setText(""); + label.update(); + } + }; + FormData labelData = new FormData(); + labelData.left = new FormAttachment(0); + labelData.right = new FormAttachment(100); + labelData.bottom = new FormAttachment(100); + label.setLayoutData(labelData); + Menu menuBar = new Menu(shell, SWT.BAR); + shell.setMenuBar(menuBar); + MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE); + fileItem.setText("File"); + fileItem.addListener(SWT.Arm, armListener); + MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE); + editItem.setText("Edit"); + editItem.addListener(SWT.Arm, armListener); + Menu fileMenu = new Menu(shell, SWT.DROP_DOWN); + fileMenu.addListener(SWT.Hide, hideListener); + fileMenu.addListener(SWT.Show, showListener); + fileItem.setMenu(fileMenu); + String[] fileStrings = { "New", "Close", "Exit" }; + for (int i = 0; i < fileStrings.length; i++) { + MenuItem item = new MenuItem(fileMenu, SWT.PUSH); + item.setText(fileStrings[i]); + item.addListener(SWT.Arm, armListener); + } + Menu editMenu = new Menu(shell, SWT.DROP_DOWN); + editMenu.addListener(SWT.Hide, hideListener); + editMenu.addListener(SWT.Show, showListener); + String[] editStrings = { "Cut", "Copy", "Paste" }; + editItem.setMenu(editMenu); + for (int i = 0; i < editStrings.length; i++) { + MenuItem item = new MenuItem(editMenu, SWT.PUSH); + item.setText(editStrings[i]); + item.addListener(SWT.Arm, armListener); + } + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet153.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet153.java new file mode 100644 index 0000000000..39818d0915 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet153.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: update a status line when the pointer enters a ToolItem + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet153 { + +static String statusText = ""; +public static void main(String[] args) { + final Display display = new Display(); + Shell shell = new Shell(display); + shell.setBounds(10, 10, 200, 200); + final ToolBar bar = new ToolBar(shell, SWT.BORDER); + bar.setBounds(10, 10, 150, 50); + final Label statusLine = new Label(shell, SWT.BORDER); + statusLine.setBounds(10, 90, 150, 30); + new ToolItem(bar, SWT.NONE).setText("item 1"); + new ToolItem(bar, SWT.NONE).setText("item 2"); + new ToolItem(bar, SWT.NONE).setText("item 3"); + bar.addMouseMoveListener(new MouseMoveListener() { + public void mouseMove(MouseEvent e) { + ToolItem item = bar.getItem(new Point(e.x, e.y)); + String name = ""; + if (item != null) { + name = item.getText(); + } + if (!statusText.equals(name)) { + statusLine.setText(name); + statusText = name; + } + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet154.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet154.java new file mode 100644 index 0000000000..2f9cb186c8 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet154.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: embed a JTable in SWT (no flicker) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.awt.BorderLayout; +import java.awt.Frame; +import java.awt.Panel; +import javax.swing.*; +import java.util.Vector; + +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.awt.SWT_AWT; + +public class Snippet154 { + + public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + + Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED); + + /* Create and setting up frame */ + Frame frame = SWT_AWT.new_Frame(composite); + Panel panel = new Panel(new BorderLayout()); + frame.add(panel); + JPanel root = new JPanel(); + root.setLayout(new BorderLayout()); + panel.add(root); + + /* Creating components */ + int nrows = 1000, ncolumns = 10; + Vector rows = new Vector(); + for (int i = 0; i < nrows; i++) { + Vector row = new Vector(); + for (int j = 0; j < ncolumns; j++) { + row.addElement("Item " + i + "-" + j); + } + rows.addElement(row); + } + Vector columns = new Vector(); + for (int i = 0; i < ncolumns; i++) { + columns.addElement("Column " + i); + } + JTable table = new JTable(rows, columns); + table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); + table.createDefaultColumnsFromModel(); + JScrollPane scrollPane = new JScrollPane(table); + root.add(scrollPane); + + shell.open(); + while(!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet155.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet155.java new file mode 100644 index 0000000000..09f7aebbf0 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet155.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: draw an X using AWT Graphics + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.awt.Frame; +import java.awt.Canvas; +import java.awt.Graphics; +import java.awt.Dimension; + +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.awt.SWT_AWT; + +public class Snippet155 { + + public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Composite composite = new Composite(shell, SWT.EMBEDDED); + + /* Draw an X using AWT */ + Frame frame = SWT_AWT.new_Frame(composite); + Canvas canvas = new Canvas() { + public void paint (Graphics g) { + Dimension d = getSize(); + g.drawLine(0, 0, d.width, d.height); + g.drawLine(d.width, 0, 0, d.height); + } + }; + frame.add(canvas); + + shell.open(); + while(!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); + } +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java new file mode 100644 index 0000000000..8698e33e20 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java @@ -0,0 +1,197 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: convert between SWT Image and AWT BufferedImage + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.Insets; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DirectColorModel; +import java.awt.image.IndexColorModel; +import java.awt.image.WritableRaster; + +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.*; + +public class Snippet156 { + +static BufferedImage convertToAWT(ImageData data) { + ColorModel colorModel = null; + PaletteData palette = data.palette; + if (palette.isDirect) { + colorModel = new DirectColorModel(data.depth, palette.redMask, palette.greenMask, palette.blueMask); + BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); + WritableRaster raster = bufferedImage.getRaster(); + int[] pixelArray = new int[3]; + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + int pixel = data.getPixel(x, y); + RGB rgb = palette.getRGB(pixel); + pixelArray[0] = rgb.red; + pixelArray[1] = rgb.green; + pixelArray[2] = rgb.blue; + raster.setPixels(x, y, 1, 1, pixelArray); + } + } + return bufferedImage; + } else { + RGB[] rgbs = palette.getRGBs(); + byte[] red = new byte[rgbs.length]; + byte[] green = new byte[rgbs.length]; + byte[] blue = new byte[rgbs.length]; + for (int i = 0; i < rgbs.length; i++) { + RGB rgb = rgbs[i]; + red[i] = (byte)rgb.red; + green[i] = (byte)rgb.green; + blue[i] = (byte)rgb.blue; + } + if (data.transparentPixel != -1) { + colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue, data.transparentPixel); + } else { + colorModel = new IndexColorModel(data.depth, rgbs.length, red, green, blue); + } + BufferedImage bufferedImage = new BufferedImage(colorModel, colorModel.createCompatibleWritableRaster(data.width, data.height), false, null); + WritableRaster raster = bufferedImage.getRaster(); + int[] pixelArray = new int[1]; + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + int pixel = data.getPixel(x, y); + pixelArray[0] = pixel; + raster.setPixel(x, y, pixelArray); + } + } + return bufferedImage; + } +} + +static ImageData convertToSWT(BufferedImage bufferedImage) { + if (bufferedImage.getColorModel() instanceof DirectColorModel) { + DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel(); + PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask()); + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); + WritableRaster raster = bufferedImage.getRaster(); + int[] pixelArray = new int[3]; + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + raster.getPixel(x, y, pixelArray); + int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2])); + data.setPixel(x, y, pixel); + } + } + return data; + } else if (bufferedImage.getColorModel() instanceof IndexColorModel) { + IndexColorModel colorModel = (IndexColorModel)bufferedImage.getColorModel(); + int size = colorModel.getMapSize(); + byte[] reds = new byte[size]; + byte[] greens = new byte[size]; + byte[] blues = new byte[size]; + colorModel.getReds(reds); + colorModel.getGreens(greens); + colorModel.getBlues(blues); + RGB[] rgbs = new RGB[size]; + for (int i = 0; i < rgbs.length; i++) { + rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); + } + PaletteData palette = new PaletteData(rgbs); + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette); + data.transparentPixel = colorModel.getTransparentPixel(); + WritableRaster raster = bufferedImage.getRaster(); + int[] pixelArray = new int[1]; + for (int y = 0; y < data.height; y++) { + for (int x = 0; x < data.width; x++) { + raster.getPixel(x, y, pixelArray); + data.setPixel(x, y, pixelArray[0]); + } + } + return data; + } + return null; +} + +static ImageData createSampleImage(Display display) { + Image image = new Image(display, 100, 100); + Rectangle bounds = image.getBounds(); + GC gc = new GC(image); + gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE)); + gc.fillRectangle(bounds); + gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN)); + gc.fillOval(0, 0, bounds.width, bounds.height); + gc.setForeground(display.getSystemColor(SWT.COLOR_RED)); + gc.drawLine(0, 0, bounds.width, bounds.height); + gc.drawLine(bounds.width, 0, 0, bounds.height); + gc.dispose(); + ImageData data = image.getImageData(); + image.dispose(); + return data; +} + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setText("SWT Image"); + ImageData data; + if (args.length > 0) { + String fileName = args[0]; + data = new ImageData(fileName); + } else { + data = createSampleImage(display); + } + final Image swtImage = new Image(display, data); + final BufferedImage awtImage = convertToAWT(data); + final Image swtImage2 = new Image(display, convertToSWT(awtImage)); + shell.addListener(SWT.Paint, new Listener() { + public void handleEvent(Event e) { + int y = 10; + if (swtImage != null) { + e.gc.drawImage(swtImage, 10, y); + y += swtImage.getBounds().height + 10; + } + if (swtImage2 != null) { + e.gc.drawImage(swtImage2, 10, y); + } + } + }); + Frame frame = new Frame() { + public void paint(Graphics g) { + Insets insets = getInsets(); + if (awtImage != null) { + g.drawImage(awtImage, 10 + insets.left, 10 + insets.top, null); + } + } + }; + frame.setTitle("AWT Image"); + shell.setLocation(50, 50); + Rectangle bounds = swtImage.getBounds(); + shell.setSize(bounds.width + 50, bounds.height * 2 + 100); + Point size = shell.getSize(); + Point location = shell.getLocation(); + Insets insets = frame.getInsets(); + frame.setLocation(location.x + size.x + 10, location.y); + frame.setSize(size.x - (insets.left + insets.right), size.y - (insets.top + insets.bottom)); + frame.setVisible(true); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + if (swtImage != null) swtImage.dispose(); + if (swtImage2 != null) swtImage.dispose(); + frame.dispose(); + System.exit(0); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet16.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet16.java new file mode 100644 index 0000000000..aeaf9bf1f8 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet16.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Display example snippet: create one repeating timer (every 500 ms) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet16 { + +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + final int time = 500; + Runnable timer = new Runnable () { + public void run () { + Point point = display.getCursorLocation (); + Rectangle rect = shell.getBounds (); + if (rect.contains (point)) { + System.out.println ("In"); + } else { + System.out.println ("Out"); + } + display.timerExec (time, this); + } + }; + display.timerExec (time, timer); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet17.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet17.java new file mode 100644 index 0000000000..8d94ec5518 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet17.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Slider example snippet: print scroll event details + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet17 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Slider slider = new Slider (shell, SWT.HORIZONTAL); + slider.setBounds (10, 10, 200, 32); + slider.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + String string = "SWT.NONE"; + switch (event.detail) { + case SWT.DRAG: string = "SWT.DRAG"; break; + case SWT.HOME: string = "SWT.HOME"; break; + case SWT.END: string = "SWT.END"; break; + case SWT.ARROW_DOWN: string = "SWT.ARROW_DOWN"; break; + case SWT.ARROW_UP: string = "SWT.ARROW_UP"; break; + case SWT.PAGE_DOWN: string = "SWT.PAGE_DOWN"; break; + case SWT.PAGE_UP: string = "SWT.PAGE_UP"; break; + } + System.out.println ("Scroll detail -> " + string); + } + }); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet18.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet18.java new file mode 100644 index 0000000000..f82cd1b106 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet18.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: create a tool bar (text) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet18 { + +public static void main (String [] args) { + Shell shell = new Shell (); + ToolBar bar = new ToolBar (shell, SWT.BORDER); + for (int i=0; i<8; i++) { + ToolItem item = new ToolItem (bar, SWT.PUSH); + item.setText ("Item " + i); + } + bar.pack (); + shell.open (); + Display display = shell.getDisplay (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet19.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet19.java new file mode 100644 index 0000000000..54c34e6a46 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet19.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: verify input (only allow digits) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet19 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Text text = new Text (shell, SWT.BORDER | SWT.V_SCROLL); + text.setBounds (10, 10, 200, 200); + text.addListener (SWT.Verify, new Listener () { + public void handleEvent (Event e) { + String text = e.text; + char [] chars = new char [text.length ()]; + text.getChars (0, chars.length, chars, 0); + for (int i=0; i<chars.length; i++) { + if (!('0' <= chars [i] && chars [i] <= '9')) { + e.doit = false; + return; + } + } + } + }); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet2.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet2.java new file mode 100644 index 0000000000..4bb8d964fe --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet2.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: sort a table by column + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import java.text.*; +import java.util.*; + +public class Snippet2 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + final Table table = new Table(shell, SWT.BORDER); + table.setHeaderVisible(true); + TableColumn column1 = new TableColumn(table, SWT.NONE); + column1.setText("Column 1"); + TableColumn column2 = new TableColumn(table, SWT.NONE); + column2.setText("Column 2"); + TableItem item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"a", "3"}); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"b", "2"}); + item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"c", "1"}); + column1.pack(); + column2.pack(); + column1.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + // sort column 1 + TableItem[] items = table.getItems(); + Collator collator = Collator.getInstance(Locale.getDefault()); + for (int i = 1; i < items.length; i++) { + String value1 = items[i].getText(0); + for (int j = 0; j < i; j++){ + String value2 = items[j].getText(0); + if (collator.compare(value1, value2) < 0) { + String[] values = {items[i].getText(0), items[i].getText(1)}; + items[i].dispose(); + TableItem item = new TableItem(table, SWT.NONE, j); + item.setText(values); + break; + } + } + } + } + }); + column2.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + // sort column 2 + TableItem[] items = table.getItems(); + Collator collator = Collator.getInstance(Locale.getDefault()); + for (int i = 1; i < items.length; i++) { + String value1 = items[i].getText(1); + for (int j = 0; j < i; j++){ + String value2 = items[j].getText(1); + if (collator.compare(value1, value2) < 0) { + String[] values = {items[i].getText(0), items[i].getText(1)}; + items[i].dispose(); + TableItem item = new TableItem(table, SWT.NONE, j); + item.setText(values); + break; + } + } + } + } + }); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet20.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet20.java new file mode 100644 index 0000000000..17655ec941 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet20.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * CoolBar example snippet: create a cool bar + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet20 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + CoolBar bar = new CoolBar (shell, SWT.BORDER); + for (int i=0; i<2; i++) { + CoolItem item = new CoolItem (bar, SWT.NONE); + Button button = new Button (bar, SWT.PUSH); + button.setText ("Button " + i); + Point size = button.computeSize (SWT.DEFAULT, SWT.DEFAULT); + item.setPreferredSize (item.computeSize (size.x, size.y)); + item.setControl (button); + } + bar.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet21.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet21.java new file mode 100644 index 0000000000..af11694424 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet21.java @@ -0,0 +1,89 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Canvas example snippet: implement tab traversal (behave like a tab group) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet21 { + +public static void main (String [] args) { + Display display = new Display (); + final Color red = display.getSystemColor (SWT.COLOR_RED); + final Color blue = display.getSystemColor (SWT.COLOR_BLUE); + Shell shell = new Shell (display); + Button b = new Button (shell, SWT.PUSH); + b.setBounds (10, 10, 100, 32); + b.setText ("Button"); + shell.setDefaultButton (b); + final Canvas c = new Canvas (shell, SWT.BORDER); + c.setBounds (10, 50, 100, 32); + c.addListener (SWT.Traverse, new Listener () { + public void handleEvent (Event e) { + switch (e.detail) { + /* Do tab group traversal */ + case SWT.TRAVERSE_ESCAPE: + case SWT.TRAVERSE_RETURN: + case SWT.TRAVERSE_TAB_NEXT: + case SWT.TRAVERSE_TAB_PREVIOUS: + case SWT.TRAVERSE_PAGE_NEXT: + case SWT.TRAVERSE_PAGE_PREVIOUS: + e.doit = true; + break; + } + } + }); + c.addListener (SWT.FocusIn, new Listener () { + public void handleEvent (Event e) { + c.setBackground (red); + } + }); + c.addListener (SWT.FocusOut, new Listener () { + public void handleEvent (Event e) { + c.setBackground (blue); + } + }); + c.addListener (SWT.KeyDown, new Listener () { + public void handleEvent (Event e) { + System.out.println ("KEY"); + for (int i=0; i<64; i++) { + Color c1 = red, c2 = blue; + if (c.isFocusControl ()) { + c1 = blue; c2 = red; + } + c.setBackground (c1); + c.update (); + c.setBackground (c2); + } + } + }); + Text t = new Text (shell, SWT.SINGLE | SWT.BORDER); + t.setBounds (10, 85, 100, 32); + + Text r = new Text (shell, SWT.MULTI | SWT.BORDER); + r.setBounds (10, 120, 100, 32); + + c.setFocus (); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet22.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet22.java new file mode 100644 index 0000000000..04bf92f0cd --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet22.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: select all the text in the control + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet22 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Text text = new Text (shell, 0); + text.setText ("ASDF"); + text.setSize (64, 32); + text.selectAll (); + shell.pack (); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet23.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet23.java new file mode 100644 index 0000000000..795a339a5d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet23.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tracker example snippet: create a tracker (drag on mouse down) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet23 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + shell.open (); + shell.addListener (SWT.MouseDown, new Listener () { + public void handleEvent (Event e) { + Tracker tracker = new Tracker (shell, 0); + tracker.setRectangles (new Rectangle [] { + new Rectangle (e.x, e.y, 100, 100), + }); + tracker.open (); + } + }); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet24.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet24.java new file mode 100644 index 0000000000..1cdba3d925 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet24.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: detect CR in a text control (default selelection) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet24 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new RowLayout ()); + Combo combo = new Combo (shell, SWT.NULL); + combo.setItems (new String [] {"A-1", "B-1", "C-1"}); + Text text = new Text (shell, SWT.SINGLE | SWT.BORDER); + text.setText ("some text"); + combo.addListener (SWT.DefaultSelection, new Listener () { + public void handleEvent (Event e) { + System.out.println (e.widget + " - Default Selection"); + } + }); + text.addListener (SWT.DefaultSelection, new Listener () { + public void handleEvent (Event e) { + System.out.println (e.widget + " - Default Selection"); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet25.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet25.java new file mode 100644 index 0000000000..f1dfc297bb --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet25.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Control example snippet: print key state, code and character + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet25 { + +static String stateMask (int stateMask) { + String string = ""; + if ((stateMask & SWT.CTRL) != 0) string += " CTRL"; + if ((stateMask & SWT.ALT) != 0) string += " ALT"; + if ((stateMask & SWT.SHIFT) != 0) string += " SHIFT"; + if ((stateMask & SWT.COMMAND) != 0) string += " COMMAND"; + return string; +} + +static String character (char character) { + switch (character) { + case 0: return "'\\0'"; + case SWT.BS: return "'\\b'"; + case SWT.CR: return "'\\r'"; + case SWT.DEL: return "DEL"; + case SWT.ESC: return "ESC"; + case SWT.LF: return "'\\n'"; + case SWT.TAB: return "'\\t'"; + } + return "'" + character +"'"; +} + +static String keyCode (int keyCode) { + switch (keyCode) { + + /* Keyboard and Mouse Masks */ + case SWT.ALT: return "ALT"; + case SWT.SHIFT: return "SHIFT"; + case SWT.CONTROL: return "CONTROL"; + case SWT.COMMAND: return "COMMAND"; + + /* Non-Numeric Keypad Keys */ + case SWT.ARROW_UP: return "ARROW_UP"; + case SWT.ARROW_DOWN: return "ARROW_DOWN"; + case SWT.ARROW_LEFT: return "ARROW_LEFT"; + case SWT.ARROW_RIGHT: return "ARROW_RIGHT"; + case SWT.PAGE_UP: return "PAGE_UP"; + case SWT.PAGE_DOWN: return "PAGE_DOWN"; + case SWT.HOME: return "HOME"; + case SWT.END: return "END"; + case SWT.INSERT: return "INSERT"; + + /* Virtual and Ascii Keys */ + case SWT.BS: return "BS"; + case SWT.CR: return "CR"; + case SWT.DEL: return "DEL"; + case SWT.ESC: return "ESC"; + case SWT.LF: return "LF"; + case SWT.TAB: return "TAB"; + + /* Functions Keys */ + case SWT.F1: return "F1"; + case SWT.F2: return "F2"; + case SWT.F3: return "F3"; + case SWT.F4: return "F4"; + case SWT.F5: return "F5"; + case SWT.F6: return "F6"; + case SWT.F7: return "F7"; + case SWT.F8: return "F8"; + case SWT.F9: return "F9"; + case SWT.F10: return "F10"; + case SWT.F11: return "F11"; + case SWT.F12: return "F12"; + case SWT.F13: return "F13"; + case SWT.F14: return "F14"; + case SWT.F15: return "F15"; + + /* Numeric Keypad Keys */ + case SWT.KEYPAD_ADD: return "KEYPAD_ADD"; + case SWT.KEYPAD_SUBTRACT: return "KEYPAD_SUBTRACT"; + case SWT.KEYPAD_MULTIPLY: return "KEYPAD_MULTIPLY"; + case SWT.KEYPAD_DIVIDE: return "KEYPAD_DIVIDE"; + case SWT.KEYPAD_DECIMAL: return "KEYPAD_DECIMAL"; + case SWT.KEYPAD_CR: return "KEYPAD_CR"; + case SWT.KEYPAD_0: return "KEYPAD_0"; + case SWT.KEYPAD_1: return "KEYPAD_1"; + case SWT.KEYPAD_2: return "KEYPAD_2"; + case SWT.KEYPAD_3: return "KEYPAD_3"; + case SWT.KEYPAD_4: return "KEYPAD_4"; + case SWT.KEYPAD_5: return "KEYPAD_5"; + case SWT.KEYPAD_6: return "KEYPAD_6"; + case SWT.KEYPAD_7: return "KEYPAD_7"; + case SWT.KEYPAD_8: return "KEYPAD_8"; + case SWT.KEYPAD_9: return "KEYPAD_9"; + case SWT.KEYPAD_EQUAL: return "KEYPAD_EQUAL"; + + /* Other keys */ + case SWT.CAPS_LOCK: return "CAPS_LOCK"; + case SWT.NUM_LOCK: return "NUM_LOCK"; + case SWT.SCROLL_LOCK: return "SCROLL_LOCK"; + case SWT.PAUSE: return "PAUSE"; + case SWT.BREAK: return "BREAK"; + case SWT.PRINT_SCREEN: return "PRINT_SCREEN"; + case SWT.HELP: return "HELP"; + } + return character ((char) keyCode); +} + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Listener listener = new Listener () { + public void handleEvent (Event e) { + String string = e.type == SWT.KeyDown ? "DOWN:" : "UP :"; + string += " stateMask=0x" + Integer.toHexString (e.stateMask) + stateMask (e.stateMask) + ","; + string += " keyCode=0x" + Integer.toHexString (e.keyCode) + " " + keyCode (e.keyCode) + ","; + string += " character=0x" + Integer.toHexString (e.character) + " " + character (e.character); + System.out.println (string); + } + }; + shell.addListener (SWT.KeyDown, listener); + shell.addListener (SWT.KeyUp, listener); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet26.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet26.java new file mode 100644 index 0000000000..fa74413580 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet26.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Combo example snippet: create a combo box (non-editable) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet26 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Combo combo = new Combo (shell, SWT.READ_ONLY); + combo.setItems (new String [] {"A", "B", "C"}); + combo.setSize (200, 200); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet27.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet27.java new file mode 100644 index 0000000000..f701a4fc2d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet27.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: open a shell minimized (iconified) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet27 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setMinimized (true); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet28.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet28.java new file mode 100644 index 0000000000..b7c2767fc3 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet28.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: open a shell maximized (full screen) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet28 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setMaximized (true); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet29.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet29.java new file mode 100644 index 0000000000..e6d9bfd129 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet29.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Menu example snippet: create a bar and pull down menu (accelerators, mnemonics) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet29 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Menu bar = new Menu (shell, SWT.BAR); + shell.setMenuBar (bar); + MenuItem fileItem = new MenuItem (bar, SWT.CASCADE); + fileItem.setText ("File"); + Menu submenu = new Menu (shell, SWT.DROP_DOWN); + fileItem.setMenu (submenu); + MenuItem item = new MenuItem (submenu, SWT.PUSH); + item.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + System.out.println ("Select All"); + } + }); + item.setText ("Select &All\tCtrl+A"); + item.setAccelerator (SWT.CTRL + 'A'); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet3.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet3.java new file mode 100644 index 0000000000..de21522081 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet3.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: find a table cell from mouse down (SWT.FULL_SELECTION) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.graphics.*; + +public class Snippet3 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Table table = new Table (shell, SWT.BORDER | SWT.V_SCROLL | SWT.FULL_SELECTION); + table.setHeaderVisible (true); + table.setLinesVisible (true); + final int rowCount = 64, columnCount = 4; + for (int i=0; i < columnCount; i++) { + TableColumn column = new TableColumn (table, SWT.NULL); + column.setText ("Column " + i); + } + for (int i=0; i < rowCount; i++) { + TableItem item = new TableItem (table, SWT.NULL); + for (int j=0; j < columnCount; j++) { + item.setText (j, "Item " + i + "-" + j); + } + } + for (int i=0; i < columnCount; i++) { + table.getColumn (i).pack (); + } + Point size = table.computeSize (SWT.DEFAULT, 200); + table.setSize (size); + shell.pack (); + table.addListener (SWT.MouseDown, new Listener () { + public void handleEvent (Event event) { + Point pt = new Point (event.x, event.y); + TableItem item = table.getItem (pt); + if (item == null) return; + for (int i=0; i < columnCount; i++) { + Rectangle rect = item.getBounds (i); + if (rect.contains (pt)) { + int index = table.indexOf (item); + System.out.println ("Item " + index + "-" + i); + } + } + } + }); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet30.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet30.java new file mode 100644 index 0000000000..47c72f4279 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet30.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Program example snippet: invoke the system text editor on autoexec.bat + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.program.*; +import org.eclipse.swt.widgets.*; + +public class Snippet30 { + +public static void main (String [] args) { + Display display = new Display (); + Program p = Program.findProgram (".txt"); + if (p != null) p.execute ("c:\\autoexec.bat"); + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet31.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet31.java new file mode 100644 index 0000000000..0f856921ac --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet31.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tracker example snippet: create a tracker (drag when "torn off") + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet31 { + +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + shell.setSize (200, 200); + shell.open (); + Listener listener = new Listener () { + Point point = null; + static final int JITTER = 8; + public void handleEvent (Event event) { + switch (event.type) { + case SWT.MouseDown: + point = new Point (event.x, event.y); + break; + case SWT.MouseMove: + if (point == null) return; + int deltaX = point.x - event.x, deltaY = point.y - event.y; + if (Math.abs (deltaX) < JITTER && Math.abs (deltaY) < JITTER) { + return; + } + Tracker tracker = new Tracker (display, SWT.NONE); + Rectangle rect = display.map (shell, null, shell.getClientArea ()); + rect.x -= deltaX; + rect.y -= deltaY; + tracker.setRectangles (new Rectangle [] {rect}); + tracker.open (); + //FALL THROUGH + case SWT.MouseUp: + point = null; + break; + } + } + }; + shell.addListener (SWT.MouseDown, listener); + shell.addListener (SWT.MouseMove, listener); + shell.addListener (SWT.MouseUp, listener); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet32.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet32.java new file mode 100644 index 0000000000..8eb0cf3f41 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet32.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Program example snippet: find the icon of the program that edits .bmp files + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.program.*; + +public class Snippet32 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Label label = new Label (shell, SWT.NONE); + label.setText ("Can't find icon for .bmp"); + Image image = null; + Program p = Program.findProgram (".bmp"); + if (p != null) { + ImageData data = p.getImageData (); + if (data != null) { + image = new Image (display, data); + label.setImage (image); + } + } + label.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + if (image != null) image.dispose (); + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet33.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet33.java new file mode 100644 index 0000000000..d47b098174 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet33.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * DirectoryDialog example snippet: prompt for a directory + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet33 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.open (); + DirectoryDialog dialog = new DirectoryDialog (shell); + dialog.setFilterPath ("c:\\"); //Windows specific + System.out.println ("RESULT=" + dialog.open ()); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java new file mode 100644 index 0000000000..67a60896d4 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet34.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Label example snippet: create a label (with an image) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet34 { + +public static void main (String[] args) { + Display display = new Display(); + Image image = new Image (display, 16, 16); + Color color = display.getSystemColor (SWT.COLOR_RED); + GC gc = new GC (image); + gc.setBackground (color); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + Shell shell = new Shell (display); + Label label = new Label (shell, SWT.BORDER); + label.setImage (image); + label.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet35.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet35.java new file mode 100644 index 0000000000..eba3ed9505 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet35.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: create a table (no columns, no headers) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet35 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Table table = new Table (shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); + for (int i=0; i<12; i++) { + TableItem item = new TableItem (table, 0); + item.setText ("Item " + i); + } + table.setSize (100, 100); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java new file mode 100644 index 0000000000..df6cc629ee --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet36.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: create a flat tool bar (images) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet36 { + +public static void main (String [] args) { + Display display = new Display(); + Image image = new Image (display, 16, 16); + Color color = display.getSystemColor (SWT.COLOR_RED); + GC gc = new GC (image); + gc.setBackground (color); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + Shell shell = new Shell (display); + ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER); + for (int i=0; i<12; i++) { + ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN); + item.setImage (image); + } + toolBar.pack (); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet37.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet37.java new file mode 100644 index 0000000000..a680deb58f --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet37.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Label example snippet: create a label (a separator) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet37 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new FillLayout ()); + Label label1 = new Label (shell, SWT.SEPARATOR | SWT.HORIZONTAL); + Label label2 = new Label (shell, SWT.SEPARATOR | SWT.VERTICAL); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet38.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet38.java new file mode 100644 index 0000000000..83eb2fc046 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet38.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: create a table (columns, headers, lines) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet38 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Table table = new Table (shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); + table.setLinesVisible (true); + table.setHeaderVisible (true); + String[] titles = {" ", "C", "!", "Description", "Resource", "In Folder", "Location"}; + for (int i=0; i<titles.length; i++) { + TableColumn column = new TableColumn (table, SWT.NULL); + column.setText (titles [i]); + } + int count = 128; + for (int i=0; i<count; i++) { + TableItem item = new TableItem (table, SWT.NULL); + item.setText (0, "x"); + item.setText (1, "y"); + item.setText (2, "!"); + item.setText (3, "this stuff behaves the way I expect"); + item.setText (4, "almost everywhere"); + item.setText (5, "some.folder"); + item.setText (6, "line " + i + " in nowhere"); + } + for (int i=0; i<titles.length; i++) { + table.getColumn (i).pack (); + } + table.setSize (table.computeSize (SWT.DEFAULT, 200)); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet39.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet39.java new file mode 100644 index 0000000000..c8475b28f4 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet39.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +public class Snippet39 { + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet4.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet4.java new file mode 100644 index 0000000000..6a6c762cf4 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet4.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: prevent escape from closing a dialog + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.widgets.*; + +public class Snippet4 { + + public static void main(String[] args) { + Display display = new Display(); + final Shell shell = new Shell(display); + Button b = new Button(shell, SWT.PUSH); + b.setText("Open Dialog ..."); + b.pack(); + b.setLocation(10, 10); + b.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + Shell dialog = new Shell(shell, SWT.DIALOG_TRIM); + dialog.addListener(SWT.Traverse, new Listener() { + public void handleEvent(Event e) { + if (e.detail == SWT.TRAVERSE_ESCAPE) { + e.doit = false; + } + } + }); + dialog.open(); + } + }); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet40.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet40.java new file mode 100644 index 0000000000..eb25de03c7 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet40.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Menu example snippet: create a popup menu (set in multiple controls) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet40 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Composite c1 = new Composite (shell, SWT.BORDER); + c1.setSize (100, 100); + Composite c2 = new Composite (shell, SWT.BORDER); + c2.setBounds (100, 0, 100, 100); + Menu menu = new Menu (shell, SWT.POP_UP); + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Popup"); + c1.setMenu (menu); + c2.setMenu (menu); + shell.setMenu (menu); + shell.setSize (300, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet41.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet41.java new file mode 100644 index 0000000000..47e0cf794f --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet41.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tool Tips example snippet: create tool tips for a tab folder, tool bar and control + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet41 { + +public static void main (String [] args) { + String string = "This is a string\nwith a new line."; + Display display = new Display (); + Shell shell = new Shell (display); + shell.open (); + TabFolder folder = new TabFolder (shell, SWT.BORDER); + folder.setSize (200, 200); + TabItem item0 = new TabItem (folder, 0); + item0.setToolTipText (string); + ToolBar bar = new ToolBar (shell, SWT.BORDER); + bar.setBounds (0, 200, 200, 64); + ToolItem item1 = new ToolItem (bar, 0); + item1.setToolTipText (string); + shell.setToolTipText (string); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet42.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet42.java new file mode 100644 index 0000000000..ba6ca8fb0d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet42.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Display example snippet: get the bounds and client area of a display + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet42 { + +public static void main (String [] args) { + Display display = new Display (); + System.out.println ("BOUNDS=" + display.getBounds () + " CLIENT=" + display.getClientArea ()); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java new file mode 100644 index 0000000000..6f4f24597e --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet43.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Caret example snippet: create a caret (using an image) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet43 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.open (); + Caret caret = new Caret (shell, SWT.NONE); + Color white = display.getSystemColor (SWT.COLOR_WHITE); + Color black = display.getSystemColor (SWT.COLOR_BLACK); + Color red = display.getSystemColor (SWT.COLOR_RED); + Image image = new Image (display, 20, 20); + GC gc = new GC (image); + gc.setBackground (black); + gc.fillRectangle (0, 0, 20, 20); + gc.setForeground (white); + gc.drawLine (0, 0, 19, 19); + gc.drawLine (19, 0, 0, 19); + gc.dispose (); + caret.setLocation (10, 10); + caret.setImage (image); + gc = new GC (shell); + gc.drawImage (image, 10, 64); + caret.setVisible (false); + gc.drawString ("Test", 12, 12); + caret.setVisible (true); + gc.dispose (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet44.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet44.java new file mode 100644 index 0000000000..a67c591d54 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet44.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Cursor example snippet: set the hand cursor into a control + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet44 { + +public static void main (String [] args) { + Display display = new Display (); + final Cursor cursor = new Cursor (display, SWT.CURSOR_HAND); + Shell shell = new Shell (display); + shell.open (); + final Button b = new Button (shell, 0); + b.setBounds (10, 10, 200, 200); + b.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + b.setCursor (cursor); + } + }); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + cursor.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet45.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet45.java new file mode 100644 index 0000000000..307361e239 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet45.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Scale example snippet: create a scale (maximum 40, page increment 5) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet45 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Scale scale = new Scale (shell, SWT.BORDER); + scale.setSize (200, 64); + scale.setMaximum (40); + scale.setPageIncrement (5); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet46.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet46.java new file mode 100644 index 0000000000..4d01ff25c1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet46.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Composite example snippet: intercept mouse events (drag a button with the mouse) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet46 { +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + final Composite composite = new Composite (shell, SWT.NULL); + composite.setEnabled (false); + composite.setLayout (new FillLayout ()); + Button button = new Button (composite, SWT.PUSH); + button.setText ("Button"); + composite.pack (); + composite.setLocation (10, 10); + final Point [] offset = new Point [1]; + Listener listener = new Listener () { + public void handleEvent (Event event) { + switch (event.type) { + case SWT.MouseDown: + Rectangle rect = composite.getBounds (); + if (rect.contains (event.x, event.y)) { + Point pt1 = composite.toDisplay (0, 0); + Point pt2 = shell.toDisplay (event.x, event.y); + offset [0] = new Point (pt2.x - pt1.x, pt2.y - pt1.y); + } + break; + case SWT.MouseMove: + if (offset [0] != null) { + Point pt = offset [0]; + composite.setLocation (event.x - pt.x, event.y - pt.y); + } + break; + case SWT.MouseUp: + offset [0] = null; + break; + } + } + }; + shell.addListener (SWT.MouseDown, listener); + shell.addListener (SWT.MouseUp, listener); + shell.addListener (SWT.MouseMove, listener); + shell.setSize (300, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java new file mode 100644 index 0000000000..5e4d5afc76 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet47.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: create tool bar (normal, hot and disabled images) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet47 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + + Image image = new Image (display, 20, 20); + Color color = display.getSystemColor (SWT.COLOR_BLUE); + GC gc = new GC (image); + gc.setBackground (color); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + + Image disabledImage = new Image (display, 20, 20); + color = display.getSystemColor (SWT.COLOR_GREEN); + gc = new GC (disabledImage); + gc.setBackground (color); + gc.fillRectangle (disabledImage.getBounds ()); + gc.dispose (); + + Image hotImage = new Image (display, 20, 20); + color = display.getSystemColor (SWT.COLOR_RED); + gc = new GC (hotImage); + gc.setBackground (color); + gc.fillRectangle (hotImage.getBounds ()); + gc.dispose (); + + ToolBar bar = new ToolBar (shell, SWT.BORDER | SWT.FLAT); + bar.setSize (200, 32); + for (int i=0; i<12; i++) { + ToolItem item = new ToolItem (bar, 0); + item.setImage (image); + item.setDisabledImage (disabledImage); + item.setHotImage (hotImage); + if (i % 3 == 0) item.setEnabled (false); + } + + shell.open (); + bar.getItems () [1].setImage (disabledImage); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + disabledImage.dispose (); + hotImage.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java new file mode 100644 index 0000000000..4c9285364d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; +/* + * Copyright (c) 2000, 2003 IBM Corp. All rights reserved. + * This file is made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + */ + +/* + * Canvas example snippet: scroll an image (flicker free, no double buffering) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet48 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display, + SWT.SHELL_TRIM | SWT.NO_BACKGROUND | + SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); + Image originalImage = null; + FileDialog dialog = new FileDialog (shell, SWT.OPEN); + dialog.setText ("Open an image file or cancel"); + String string = dialog.open (); + if (string != null) { + originalImage = new Image (display, string); + } + if (originalImage == null) { + int width = 150, height = 200; + originalImage = new Image (display, width, height); + GC gc = new GC (originalImage); + gc.fillRectangle (0, 0, width, height); + gc.drawLine (0, 0, width, height); + gc.drawLine (0, height, width, 0); + gc.drawText ("Default Image", 10, 10); + gc.dispose (); + } + final Image image = originalImage; + final Point origin = new Point (0, 0); + final ScrollBar hBar = shell.getHorizontalBar (); + hBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + int hSelection = hBar.getSelection (); + int destX = -hSelection - origin.x; + Rectangle rect = image.getBounds (); + shell.scroll (destX, 0, 0, 0, rect.width, rect.height, false); + origin.x = -hSelection; + } + }); + final ScrollBar vBar = shell.getVerticalBar (); + vBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + int vSelection = vBar.getSelection (); + int destY = -vSelection - origin.y; + Rectangle rect = image.getBounds (); + shell.scroll (0, destY, 0, 0, rect.width, rect.height, false); + origin.y = -vSelection; + } + }); + shell.addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Rectangle rect = image.getBounds (); + Rectangle client = shell.getClientArea (); + hBar.setMaximum (rect.width); + vBar.setMaximum (rect.height); + hBar.setThumb (Math.min (rect.width, client.width)); + vBar.setThumb (Math.min (rect.height, client.height)); + int hPage = rect.width - client.width; + int vPage = rect.height - client.height; + int hSelection = hBar.getSelection (); + int vSelection = vBar.getSelection (); + if (hSelection >= hPage) { + if (hPage <= 0) hSelection = 0; + origin.x = -hSelection; + } + if (vSelection >= vPage) { + if (vPage <= 0) vSelection = 0; + origin.y = -vSelection; + } + shell.redraw (); + } + }); + shell.addListener (SWT.Paint, new Listener () { + public void handleEvent (Event e) { + GC gc = e.gc; + gc.drawImage (image, origin.x, origin.y); + Rectangle rect = image.getBounds (); + Rectangle client = shell.getClientArea (); + int marginWidth = client.width - rect.width; + if (marginWidth > 0) { + gc.fillRectangle (rect.width, 0, marginWidth, client.height); + } + int marginHeight = client.height - rect.height; + if (marginHeight > 0) { + gc.fillRectangle (0, rect.height, client.width, marginHeight); + } + } + }); + shell.setSize (200, 150); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet49.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet49.java new file mode 100644 index 0000000000..d23f4db5b1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet49.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: create tool bar (wrap on resize) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet49 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + final ToolBar toolBar = new ToolBar (shell, SWT.WRAP); + for (int i=0; i<12; i++) { + ToolItem item = new ToolItem (toolBar, SWT.PUSH); + item.setText ("Item " + i); + } + shell.addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Rectangle rect = shell.getClientArea (); + Point size = toolBar.computeSize (rect.width, SWT.DEFAULT); + toolBar.setSize (size); + } + }); + toolBar.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet5.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet5.java new file mode 100644 index 0000000000..a6f64aa1e1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet5.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ScrolledComposite example snippet: scroll a control in a scrolled composite + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.custom.*; + +public class Snippet5 { + +public static void main (String [] args) +{ + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + + // this button is always 400 x 400. Scrollbars appear if the window is resized to be + // too small to show part of the button + ScrolledComposite c1 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); + Button b1 = new Button(c1, SWT.PUSH); + b1.setText("fixed size button"); + b1.setSize(400, 400); + c1.setContent(b1); + + // this button has a minimum size of 400 x 400. If the window is resized to be big + // enough to show more than 400 x 400, the button will grow in size. If the window + // is made too small to show 400 x 400, scrollbars will appear. + ScrolledComposite c2 = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); + Button b2 = new Button(c2, SWT.PUSH); + b2.setText("expanding button"); + c2.setContent(b2); + c2.setExpandHorizontal(true); + c2.setExpandVertical(true); + c2.setMinWidth(400); + c2.setMinHeight(400); + + shell.setSize(600, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet50.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet50.java new file mode 100644 index 0000000000..9009af18fb --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet50.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: create a dialog shell + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet50 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setText ("Shell"); + shell.setSize (200, 200); + shell.open (); + Shell dialog = new Shell (shell); + dialog.setText ("Dialog"); + dialog.setSize (200, 200); + dialog.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet51.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet51.java new file mode 100644 index 0000000000..a81a0c083a --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet51.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: scroll a table (set the top index) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet51 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setSize (200, 200); + for (int i=0; i<128; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("Item " + i); + } + table.setTopIndex (95); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet52.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet52.java new file mode 100644 index 0000000000..a04f0f727d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet52.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: select an index (select and scroll) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet52 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setSize (200, 200); + for (int i=0; i<128; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("Item " + i); + } + table.setSelection (95); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet53.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet53.java new file mode 100644 index 0000000000..4280520936 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet53.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: remove selected items + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet53 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setSize (200, 200); + for (int i=0; i<128; i++) { + TableItem item = new TableItem (table, SWT.NONE); + item.setText ("Item " + i); + } + Menu menu = new Menu (shell, SWT.POP_UP); + table.setMenu (menu); + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Delete Selection"); + item.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + table.remove (table.getSelectionIndices ()); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet54.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet54.java new file mode 100644 index 0000000000..1b99c19db9 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet54.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Sash example snippet: create a sash (allow it to be moved) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet54 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Sash sash = new Sash (shell, SWT.BORDER | SWT.VERTICAL); + sash.setBounds (10, 10, 32, 100); + sash.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + sash.setBounds (e.x, e.y, e.width, e.height); + } + }); + shell.open (); + sash.setFocus (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet55.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet55.java new file mode 100644 index 0000000000..78dc86e8cf --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet55.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Text example snippet: resize a text control (show about 10 characters) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet55 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Text text = new Text (shell, SWT.BORDER); + int columns = 10; + GC gc = new GC (text); + FontMetrics fm = gc.getFontMetrics (); + int width = columns * fm.getAverageCharWidth (); + int height = fm.getHeight (); + gc.dispose (); + text.setSize (text.computeSize (width, height)); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet56.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet56.java new file mode 100644 index 0000000000..af9500bf98 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet56.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ProgressBar example snippet: update a progress bar (from another thread) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet56 { + +public static void main (String [] args) { + final Display display = new Display (); + Shell shell = new Shell (display); + final ProgressBar bar = new ProgressBar (shell, SWT.SMOOTH); + bar.setBounds (10, 10, 200, 32); + shell.open (); + final int maximum = bar.getMaximum (); + new Thread () { + public void run () { + for (final int [] i=new int [1]; i [0] <= maximum; i [0]++) { + try {Thread.sleep (100);} catch (Throwable th) {} + display.asyncExec (new Runnable () { + public void run () { + if (bar.isDisposed ()) return; + bar.setSelection (i [0]); + } + }); + } + } + }.start (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet57.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet57.java new file mode 100644 index 0000000000..d4c16a49a9 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet57.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ProgressBar example snippet: update a progress bar (from the UI thread) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet57 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + ProgressBar bar = new ProgressBar (shell, SWT.SMOOTH); + bar.setBounds (10, 10, 200, 32); + shell.open (); + for (int i=0; i<=bar.getMaximum (); i++) { + try {Thread.sleep (100);} catch (Throwable th) {} + bar.setSelection (i); + } + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet58.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet58.java new file mode 100644 index 0000000000..3a01609b2e --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet58.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: place a combo box in a tool bar + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet58 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + ToolBar bar = new ToolBar (shell, SWT.BORDER); + for (int i=0; i<4; i++) { + ToolItem item = new ToolItem (bar, 0); + item.setText ("Item " + i); + } + ToolItem sep = new ToolItem (bar, SWT.SEPARATOR); + int start = bar.getItemCount (); + for (int i=start; i<start+4; i++) { + ToolItem item = new ToolItem (bar, 0); + item.setText ("Item " + i); + } + Combo combo = new Combo (bar, SWT.READ_ONLY); + for (int i=0; i<4; i++) { + combo.add ("Item " + i); + } + combo.pack (); + sep.setWidth (combo.getSize ().x); + sep.setControl (combo); + bar.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet59.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet59.java new file mode 100644 index 0000000000..54f19976c6 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet59.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * List example snippet: print selected items in a list + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet59 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); + for (int i=0; i<128; i++) list.add ("Item " + i); + list.setBounds (0, 0, 100, 100); + list.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + String string = ""; + int [] selection = list.getSelectionIndices (); + for (int i=0; i<selection.length; i++) string += selection [i] + " "; + System.out.println ("Selection={" + string + "}"); + } + }); + list.addListener (SWT.DefaultSelection, new Listener () { + public void handleEvent (Event e) { + String string = ""; + int [] selection = list.getSelectionIndices (); + for (int i=0; i<selection.length; i++) string += selection [i] + " "; + System.out.println ("DefaultSelection={" + string + "}"); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet6.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet6.java new file mode 100644 index 0000000000..0af4470ef3 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet6.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * GridLayout example snippet: insert widgets into a grid layout + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet6 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + shell.setLayout(new GridLayout()); + final Composite c = new Composite(shell, SWT.NONE); + GridLayout layout = new GridLayout(); + layout.numColumns = 3; + c.setLayout(layout); + for (int i = 0; i < 10; i++) { + Button b = new Button(c, SWT.PUSH); + b.setText("Button "+i); + } + + Button b = new Button(shell, SWT.PUSH); + b.setText("add a new button at row 2 column 1"); + final int[] index = new int[1]; + b.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + Button b = new Button(c, SWT.PUSH); + b.setText("Special "+index[0]); + index[0]++; + Control[] children = c.getChildren(); + b.moveAbove(children[3]); + shell.layout(true); + } + }); + + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet60.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet60.java new file mode 100644 index 0000000000..9d449591db --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet60.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Display example snippet: create two one shot timers (5000 ms, 2000 ms) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.widgets.*; + +public class Snippet60 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setSize (200, 200); + shell.open (); + display.timerExec (5000, new Runnable () { + public void run () { + System.out.println ("5000"); + } + }); + display.timerExec (2000, new Runnable () { + public void run () { + System.out.println ("2000"); + } + }); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet61.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet61.java new file mode 100644 index 0000000000..af5fa42dc3 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet61.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: print selected items in a tree + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet61 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); + for (int i=0; i<4; i++) { + TreeItem item0 = new TreeItem (tree, 0); + item0.setText ("Item " + i); + for (int j=0; j<4; j++) { + TreeItem item1 = new TreeItem (item0, 0); + item1.setText ("SubItem " + i + " " + j); + for (int k=0; k<4; k++) { + TreeItem item2 = new TreeItem (item1, 0); + item2.setText ("SubItem " + i + " " + j + " " + k); + } + } + } + tree.setBounds (0, 0, 100, 100); + tree.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + String string = ""; + TreeItem [] selection = tree.getSelection (); + for (int i=0; i<selection.length; i++) string += selection [i] + " "; + System.out.println ("Selection={" + string + "}"); + } + }); + tree.addListener (SWT.DefaultSelection, new Listener () { + public void handleEvent (Event e) { + String string = ""; + TreeItem [] selection = tree.getSelection (); + for (int i=0; i<selection.length; i++) string += selection [i] + " "; + System.out.println ("DefaultSelection={" + string + "}"); + } + }); + tree.addListener (SWT.Expand, new Listener () { + public void handleEvent (Event e) { + String string = ""; + System.out.println ("Expand={" + e.item + "}"); + } + }); + tree.addListener (SWT.Collapse, new Listener () { + public void handleEvent (Event e) { + String string = ""; + System.out.println ("Collapse={" + e.item + "}"); + } + }); + tree.getItems () [0].setExpanded (true); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet62.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet62.java new file mode 100644 index 0000000000..2f7ff21bc8 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet62.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Control example snippet: print mouse state and button (down, move, up) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet62 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + Listener listener = new Listener () { + public void handleEvent (Event e) { + String string = "Unknown"; + switch (e.type) { + case SWT.MouseDown: string = "DOWN"; break; + case SWT.MouseMove: string = "MOVE"; break; + case SWT.MouseUp: string = "UP"; break; + } + string +=": button: " + e.button + ", "; + string += "stateMask=0x" + Integer.toHexString (e.stateMask); + if ((e.stateMask & SWT.BUTTON1) != 0) string += " BUTTON1"; + if ((e.stateMask & SWT.BUTTON2) != 0) string += " BUTTON2"; + if ((e.stateMask & SWT.BUTTON3) != 0) string += " BUTTON3"; + System.out.println (string); + } + }; + shell.addListener (SWT.MouseDown, listener); + shell.addListener (SWT.MouseMove, listener); + shell.addListener (SWT.MouseUp, listener); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet63.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet63.java new file mode 100644 index 0000000000..129eff83c5 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet63.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: create a dialog shell (prompt for a value) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet63 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.pack (); + shell.open (); + final boolean [] result = new boolean [1]; + final Shell dialog = new Shell (shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); + dialog.setLayout (new RowLayout ()); + final Button ok = new Button (dialog, SWT.PUSH); + ok.setText ("Ok"); + Button cancel = new Button (dialog, SWT.PUSH); + cancel.setText ("Cancel"); + Listener listener =new Listener () { + public void handleEvent (Event event) { + result [0] = event.widget == ok; + dialog.close (); + } + }; + ok.addListener (SWT.Selection, listener); + cancel.addListener (SWT.Selection, listener); + dialog.pack (); + dialog.open (); + System.out.println ("Prompt ..."); + while (!dialog.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + System.out.println ("Result: " + result [0]); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet64.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet64.java new file mode 100644 index 0000000000..96f4feb066 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet64.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: print selected items in a table + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet64 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Table table = new Table (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL); + for (int i=0; i<16; i++) { + TableItem item = new TableItem (table, 0); + item.setText ("Item " + i); + } + table.setBounds (0, 0, 100, 100); + table.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + String string = ""; + TableItem [] selection = table.getSelection (); + for (int i=0; i<selection.length; i++) string += selection [i] + " "; + System.out.println ("Selection={" + string + "}"); + } + }); + table.addListener (SWT.DefaultSelection, new Listener () { + public void handleEvent (Event e) { + String string = ""; + TableItem [] selection = table.getSelection (); + for (int i=0; i<selection.length; i++) string += selection [i] + " "; + System.out.println ("DefaultSelection={" + string + "}"); + } + }); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet65.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet65.java new file mode 100644 index 0000000000..840fcdb728 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet65.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * FormLayout example snippet: create a simple dialog using form layout + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet65 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + Label label = new Label (shell, SWT.WRAP); + label.setText ("This is a long text string that will wrap when the dialog is resized."); + List list = new List (shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); + list.setItems (new String [] {"Item 1", "Item2"}); + Button button1 = new Button (shell, SWT.PUSH); + button1.setText ("Ok"); + Button button2 = new Button (shell, SWT.PUSH); + button2.setText ("Cancel"); + + final int insetX = 4, insetY = 4; + FormLayout formLayout = new FormLayout (); + formLayout.marginWidth = insetX; + formLayout.marginHeight = insetY; + shell.setLayout (formLayout); + + Point size = label.computeSize (SWT.DEFAULT, SWT.DEFAULT); + final FormData labelData = new FormData (size.x, SWT.DEFAULT); + labelData.left = new FormAttachment (0, 0); + labelData.right = new FormAttachment (100, 0); + label.setLayoutData (labelData); + shell.addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Rectangle rect = shell.getClientArea (); + labelData.width = rect.width - insetX * 2; + shell.layout (); + } + }); + + FormData button2Data = new FormData (); + button2Data.right = new FormAttachment (100, -insetX); + button2Data.bottom = new FormAttachment (100, 0); + button2.setLayoutData (button2Data); + + FormData button1Data = new FormData (); + button1Data.right = new FormAttachment (button2, -insetX); + button1Data.bottom = new FormAttachment (100, 0); + button1.setLayoutData (button1Data); + + FormData listData = new FormData (); + listData.left = new FormAttachment (0, 0); + listData.right = new FormAttachment (100, 0); + listData.top = new FormAttachment (label, insetY); + listData.bottom = new FormAttachment (button2, -insetY); + list.setLayoutData (listData); + + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet66.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet66.java new file mode 100644 index 0000000000..8762bd9302 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet66.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * GC example snippet: implement a simple scribble program + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet66 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + Listener listener = new Listener () { + int lastX = 0, lastY = 0; + public void handleEvent (Event event) { + switch (event.type) { + case SWT.MouseMove: + if ((event.stateMask & SWT.BUTTON1) == 0) break; + GC gc = new GC (shell); + gc.drawLine (lastX, lastY, event.x, event.y); + gc.dispose (); + //FALL THROUGH + case SWT.MouseDown: + lastX = event.x; + lastY = event.y; + break; + } + } + }; + shell.addListener (SWT.MouseDown, listener); + shell.addListener (SWT.MouseMove, listener); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet67.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet67.java new file mode 100644 index 0000000000..714fcb4ec8 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet67.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: place a drop down menu in a tool bar + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet67 { + +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + final ToolBar toolBar = new ToolBar (shell, SWT.NONE); + final Menu menu = new Menu (shell, SWT.POP_UP); + for (int i=0; i<8; i++) { + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Item " + i); + } + final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN); + item.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + if (event.detail == SWT.ARROW) { + Rectangle rect = item.getBounds (); + Point pt = new Point (rect.x, rect.y + rect.height); + pt = toolBar.toDisplay (pt); + menu.setLocation (pt.x, pt.y); + menu.setVisible (true); + } + } + }); + toolBar.pack (); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + menu.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet68.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet68.java new file mode 100644 index 0000000000..2400454837 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet68.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Display example snippet: stop a repeating timer when a button is pressed + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet68 { + +public static void main (String [] args) { + final Display display = new Display (); + final Color red = display.getSystemColor (SWT.COLOR_RED); + final Color blue = display.getSystemColor (SWT.COLOR_BLUE); + Shell shell = new Shell (display); + shell.setLayout (new RowLayout ()); + Button button = new Button (shell, SWT.PUSH); + button.setText ("Stop Timer"); + final Label label = new Label (shell, SWT.BORDER); + label.setBackground (red); + final int time = 500; + final Runnable timer = new Runnable () { + public void run () { + if (label.isDisposed ()) return; + Color color = label.getBackground ().equals (red) ? blue : red; + label.setBackground (color); + display.timerExec (time, this); + } + }; + display.timerExec (time, timer); + button.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event event) { + display.timerExec (-1, timer); + } + }); + button.pack (); + label.setLayoutData (new RowData (button.getSize ())); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet69.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet69.java new file mode 100644 index 0000000000..5379edabee --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet69.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * FormLayout example snippet: center a label and single line text using a form layout + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet69 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Label label = new Label (shell, SWT.NONE | SWT.BORDER); + label.setText ("Name"); + Text text = new Text (shell, SWT.NONE); + + FormLayout layout = new FormLayout (); + layout.marginWidth = layout.marginHeight = 5; + shell.setLayout (layout); + + GC gc = new GC (text); + FontMetrics fm = gc.getFontMetrics (); + int charWidth = fm.getAverageCharWidth (); + int width = text.computeSize (charWidth * 8, SWT.DEFAULT).x; + gc.dispose (); + FormData data = new FormData (width, SWT.DEFAULT); + text.setLayoutData (data); + data.left = new FormAttachment (label, 5); + data.top = new FormAttachment (label, 0, SWT.CENTER); + + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java new file mode 100644 index 0000000000..48fa373f83 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet7.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * example snippet: create a table (lazy) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet7 { + +public static void main (String [] args) { + final Display display = new Display (); + final Image image = new Image (display, 16, 16); + GC gc = new GC (image); + gc.setBackground (display.getSystemColor (SWT.COLOR_RED)); + gc.fillRectangle (image.getBounds ()); + gc.dispose (); + final Shell shell = new Shell (display); + shell.setText ("Lazy Table"); + shell.setLayout (new FillLayout ()); + final Table table = new Table (shell, SWT.BORDER | SWT.MULTI); + table.setSize (200, 200); + Thread thread = new Thread () { + public void run () { + for (int i=0; i<20000; i++) { + if (table.isDisposed ()) return; + final int [] index = new int [] {i}; + display.syncExec (new Runnable () { + public void run () { + if (table.isDisposed ()) return; + TableItem item = new TableItem (table, SWT.NULL); + item.setText ("Table Item " + index [0]); + item.setImage (image); + } + }); + } + } + }; + thread.start (); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + image.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java new file mode 100644 index 0000000000..4c1f59842c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet70.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * GC example snippet: create an icon (in memory) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet70 { + +public static void main (String [] args) { + Display display = new Display (); + Color red = display.getSystemColor (SWT.COLOR_RED); + Color white = display.getSystemColor (SWT.COLOR_WHITE); + Color black = display.getSystemColor (SWT.COLOR_BLACK); + + Image image = new Image (display, 20, 20); + GC gc = new GC (image); + gc.setBackground (red); + gc.fillRectangle (5, 5, 10, 10); + gc.dispose (); + ImageData imageData = image.getImageData (); + + PaletteData palette = new PaletteData ( + new RGB [] { + new RGB (0, 0, 0), + new RGB (0xFF, 0xFF, 0xFF), + }); + ImageData maskData = new ImageData (20, 20, 1, palette); + Image mask = new Image (display, maskData); + gc = new GC (mask); + gc.setBackground (black); + gc.fillRectangle (0, 0, 20, 20); + gc.setBackground (white); + gc.fillRectangle (5, 5, 10, 10); + gc.dispose (); + maskData = mask.getImageData (); + + Image icon = new Image (display, imageData, maskData); + Shell shell = new Shell (display); + Button button = new Button (shell, SWT.PUSH); + button.setImage (icon); + button.setSize (60, 60); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + icon.dispose (); + image.dispose (); + mask.dispose (); + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet71.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet71.java new file mode 100644 index 0000000000..6bef937528 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet71.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * FormLayout example snippet: create a simple OK/CANCEL dialog using form layout + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet71 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.pack (); + shell.open (); + Shell dialog = new Shell (shell, SWT.DIALOG_TRIM); + Label label = new Label (dialog, SWT.NONE); + label.setText ("Exit the application?"); + Button okButton = new Button (dialog, SWT.PUSH); + okButton.setText ("&OK"); + Button cancelButton = new Button (dialog, SWT.PUSH); + cancelButton.setText ("&Cancel"); + + FormLayout form = new FormLayout (); + form.marginWidth = form.marginHeight = 8; + dialog.setLayout (form); + FormData okData = new FormData (); + okData.top = new FormAttachment (label, 8); + okButton.setLayoutData (okData); + FormData cancelData = new FormData (); + cancelData.left = new FormAttachment (okButton, 8); + cancelData.top = new FormAttachment (okButton, 0, SWT.TOP); + cancelButton.setLayoutData (cancelData); + + dialog.setDefaultButton (okButton); + dialog.pack (); + dialog.open (); + + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet72.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet72.java new file mode 100644 index 0000000000..4397358dc1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet72.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * FileDialog example snippet: prompt for a file name (to save) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet72 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.open (); + FileDialog dialog = new FileDialog (shell, SWT.SAVE); + dialog.setFilterNames (new String [] {"Batch Files", "All Files (*.*)"}); + dialog.setFilterExtensions (new String [] {"*.bat", "*.*"}); //Windows wild cards + dialog.setFilterPath ("c:\\"); //Windows path + dialog.setFileName ("fred.bat"); + System.out.println ("Save to: " + dialog.open ()); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet73.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet73.java new file mode 100644 index 0000000000..ccefbbff0f --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet73.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Menu example snippet: enable menu items dynamically (when menu shown) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet73 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI); + final Menu menu = new Menu (shell, SWT.POP_UP); + tree.setMenu (menu); + for (int i=0; i<12; i++) { + TreeItem treeItem = new TreeItem (tree, SWT.NULL); + treeItem.setText ("Item " + i); + MenuItem menuItem = new MenuItem (menu, SWT.PUSH); + menuItem.setText (treeItem.getText ()); + } + menu.addListener (SWT.Show, new Listener () { + public void handleEvent (Event event) { + MenuItem [] menuItems = menu.getItems (); + TreeItem [] treeItems = tree.getSelection (); + for (int i=0; i<menuItems.length; i++) { + String text = menuItems [i].getText (); + int index = 0; + while (index<treeItems.length) { + if (treeItems [index].getText ().equals (text)) break; + index++; + } + menuItems [i].setEnabled (index != treeItems.length); + } + } + }); + tree.setSize (200, 200); + shell.setSize (300, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet74.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet74.java new file mode 100644 index 0000000000..ed511163f4 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet74.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Caret example snippet: create a caret + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet74 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Caret caret = new Caret (shell, SWT.NONE); + caret.setBounds (10, 10, 2, 32); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet75.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet75.java new file mode 100644 index 0000000000..c2ed8928f3 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet75.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Composite example snippet: set the tab traversal order of children + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet75 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new RowLayout ()); + + Composite c1 = new Composite (shell, SWT.BORDER); + c1.setLayout (new RowLayout ()); + Button b1 = new Button (c1, SWT.PUSH); + b1.setText ("B1"); + Button [] radios = new Button [3]; + for (int i=0; i<radios.length; i++) { + radios [i] = new Button (c1, SWT.RADIO); + radios [i].setText ("R" + (i ==1 ? "&" : "") + i); + } + Button b2 = new Button (c1, SWT.PUSH); + b2.setText ("B2"); + List l1 = new List (c1, SWT.SINGLE | SWT.BORDER); + l1.setItems (new String [] {"L1"}); + Button b3 = new Button (c1, SWT.PUSH); + b3.setText ("B&3"); + Button b3_1 = new Button (c1, SWT.PUSH); + b3_1.setText ("B3_1"); + + Composite c2 = new Composite (shell, SWT.BORDER); + c2.setLayout (new RowLayout ()); + Button b4 = new Button (c2, SWT.PUSH); + b4.setText ("B&4"); + Button b5 = new Button (c2, SWT.PUSH); + b5.setText ("B&5"); + + List l2 = new List (shell, SWT.SINGLE | SWT.BORDER); + l2.setItems (new String [] {"L2"}); + + ToolBar tb1 = new ToolBar (shell, SWT.FLAT | SWT.BORDER); + ToolItem i1 = new ToolItem (tb1, SWT.RADIO); + i1.setText ("I1"); + ToolItem i2 = new ToolItem (tb1, SWT.RADIO); + i2.setText ("I&2"); + Combo combo1 = new Combo (tb1, SWT.READ_ONLY | SWT.BORDER); + combo1.setItems (new String [] {"C1"}); + combo1.setText ("C1"); + combo1.pack (); + ToolItem i3 = new ToolItem (tb1, SWT.SEPARATOR); + i3.setWidth (combo1.getSize ().x); + i3.setControl (combo1); + i3.setText ("I3"); + ToolItem i4 = new ToolItem (tb1, SWT.PUSH); + i4.setText ("I4"); + ToolItem i5 = new ToolItem (tb1, SWT.CHECK); + i5.setText ("I5"); + + Button b6 = new Button (shell, SWT.PUSH); + b6.setText ("B&6"); + + Composite c4 = new Composite (shell, SWT.BORDER); + c4.setSize (32, 32); + Composite c5 = new Composite (c4, SWT.BORDER); + c5.setSize (20, 20); + + Control [] list1 = new Control [] {b2, b1, b3_1, b3}; + c1.setTabList (list1); + Control [] list2 = new Control [] {c1, b6, tb1, c4, c2, l2}; + shell.setTabList (list2); + + shell.pack (); + shell.open (); + + Control [] x = c1.getChildren (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet76.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet76.java new file mode 100644 index 0000000000..171c2089da --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet76.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TabFolder example snippet: create a tab folder (six pages) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet76 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + final TabFolder tabFolder = new TabFolder (shell, SWT.BORDER); + for (int i=0; i<6; i++) { + TabItem item = new TabItem (tabFolder, SWT.NULL); + item.setText ("TabItem " + i); + Button button = new Button (tabFolder, SWT.PUSH); + button.setText ("Page " + i); + item.setControl (button); + } + tabFolder.setSize (400, 200); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet77.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet77.java new file mode 100644 index 0000000000..88e04f3d71 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet77.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Table example snippet: resize columns as table resizes + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet77 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + + final Composite comp = new Composite(shell, SWT.NONE); + final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL); + table.setHeaderVisible(true); + table.setLinesVisible(true); + final TableColumn column1 = new TableColumn(table, SWT.NONE); + column1.setText("Column 1"); + final TableColumn column2 = new TableColumn(table, SWT.NONE); + column2.setText("Column 2"); + for (int i = 0; i < 10; i++) { + TableItem item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"item 0" + i, "item 1"+i}); + } + comp.addControlListener(new ControlAdapter() { + public void controlResized(ControlEvent e) { + Rectangle area = comp.getClientArea(); + Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT); + int width = area.width - 2*table.getBorderWidth(); + if (preferredSize.y > area.height) { + // Subtract the scrollbar width from the total column width + // if a vertical scrollbar will be required + Point vBarSize = table.getVerticalBar().getSize(); + width -= vBarSize.x; + } + Point oldSize = table.getSize(); + if (oldSize.x > area.width) { + // table is getting smaller so make the columns + // smaller first and then resize the table to + // match the client area width + column1.setWidth(width/3); + column2.setWidth(width - column1.getWidth()); + table.setSize(area.width, area.height); + } else { + // table is getting bigger so make the table + // bigger first and then make the columns wider + // to match the client area width + table.setSize(area.width, area.height); + column1.setWidth(width/3); + column2.setWidth(width - column1.getWidth()); + } + } + }); + + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet78.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet78.java new file mode 100644 index 0000000000..bb4301ffe8 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet78.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Drag and Drop example snippet: drag text between two labels + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.dnd.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet78 { + +public static void main (String [] args) { + + Display display = Display.getDefault (); + final Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + final Label label1 = new Label (shell, SWT.BORDER); + label1.setText ("TEXT"); + final Label label2 = new Label (shell, SWT.BORDER); + setDragDrop (label1); + setDragDrop (label2); + shell.setSize (200, 200); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +public static void setDragDrop (final Label label) { + + Transfer[] types = new Transfer[] {TextTransfer.getInstance()}; + int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; + + final DragSource source = new DragSource (label, operations); + source.setTransfer(types); + source.addDragListener (new DragSourceListener () { + public void dragStart(DragSourceEvent event) { + event.doit = (label.getText ().length () != 0); + }; + public void dragSetData (DragSourceEvent event) { + event.data = label.getText (); + } + public void dragFinished(DragSourceEvent event) { + if (event.detail == DND.DROP_MOVE) + label.setText (""); + } + }); + + DropTarget target = new DropTarget(label, operations); + target.setTransfer(types); + target.addDropListener (new DropTargetAdapter() { + public void drop(DropTargetEvent event) { + if (event.data == null) { + event.detail = DND.DROP_NONE; + return; + } + label.setText ((String) event.data); + } + }); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java new file mode 100644 index 0000000000..46714a233d --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet79.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Drag and Drop example snippet: define my own data transfer type + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import java.io.*; +import org.eclipse.swt.dnd.*; + +public class Snippet79 extends ByteArrayTransfer { + + /* The data being transferred is an array of type MyType which is define as: */ + public static class MyType { + String fileName; + long fileLength; + long lastModified; + } + + private static final String MYTYPENAME = "name_for_my_type"; + private static final int MYTYPEID = registerType(MYTYPENAME); + private static Snippet79 _instance = new Snippet79(); + +public static Snippet79 getInstance () { + return _instance; +} +public void javaToNative (Object object, TransferData transferData) { + if (object == null || !(object instanceof MyType[])) return; + + if (isSupportedType(transferData)) { + MyType[] myTypes = (MyType[]) object; + try { + // write data to a byte array and then ask super to convert to pMedium + ByteArrayOutputStream out = new ByteArrayOutputStream(); + DataOutputStream writeOut = new DataOutputStream(out); + for (int i = 0, length = myTypes.length; i < length; i++){ + byte[] buffer = myTypes[i].fileName.getBytes(); + writeOut.writeInt(buffer.length); + writeOut.write(buffer); + writeOut.writeLong(myTypes[i].fileLength); + writeOut.writeLong(myTypes[i].lastModified); + } + byte[] buffer = out.toByteArray(); + writeOut.close(); + + super.javaToNative(buffer, transferData); + + } catch (IOException e) { + } + } +} +public Object nativeToJava(TransferData transferData){ + + if (isSupportedType(transferData)) { + + byte[] buffer = (byte[])super.nativeToJava(transferData); + if (buffer == null) return null; + + MyType[] myData = new MyType[0]; + try { + ByteArrayInputStream in = new ByteArrayInputStream(buffer); + DataInputStream readIn = new DataInputStream(in); + while(readIn.available() > 20) { + MyType datum = new MyType(); + int size = readIn.readInt(); + byte[] name = new byte[size]; + readIn.read(name); + datum.fileName = new String(name); + datum.fileLength = readIn.readLong(); + datum.lastModified = readIn.readLong(); + MyType[] newMyData = new MyType[myData.length + 1]; + System.arraycopy(myData, 0, newMyData, 0, myData.length); + newMyData[myData.length] = datum; + myData = newMyData; + } + readIn.close(); + } catch (IOException ex) { + return null; + } + return myData; + } + + return null; +} +protected String[] getTypeNames(){ + return new String[]{MYTYPENAME}; +} +protected int[] getTypeIds(){ + return new int[] {MYTYPEID}; +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet8.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet8.java new file mode 100644 index 0000000000..59f54da6be --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet8.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: create a tree (lazy) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +import java.io.*; + +public class Snippet8 { + +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + shell.setText ("Lazy Tree"); + shell.setLayout (new FillLayout ()); + final Tree tree = new Tree (shell, SWT.BORDER); + File [] roots = File.listRoots (); + for (int i=0; i<roots.length; i++) { + TreeItem root = new TreeItem (tree, 0); + root.setText (roots [i].toString ()); + root.setData (roots [i]); + new TreeItem (root, 0); + } + tree.addListener (SWT.Expand, new Listener () { + public void handleEvent (final Event event) { + final TreeItem root = (TreeItem) event.item; + TreeItem [] items = root.getItems (); + for (int i= 0; i<items.length; i++) { + if (items [i].getData () != null) return; + items [i].dispose (); + } + File file = (File) root.getData (); + File [] files = file.listFiles (); + if (files == null) return; + for (int i= 0; i<files.length; i++) { + TreeItem item = new TreeItem (root, 0); + item.setText (files [i].getName ()); + item.setData (files [i]); + if (files [i].isDirectory()) { + new TreeItem (item, 0); + root.setExpanded (false); + } + } + } + }); + Point size = tree.computeSize (300, SWT.DEFAULT); + int width = Math.max (300, size.x); + int height = Math.max (300, size.y); + shell.setSize (shell.computeSize (width, height)); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet80.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet80.java new file mode 100644 index 0000000000..15aa03cf57 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet80.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: limit selection to items that match a pattern + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet80 { + +public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI); + for (int i = 0; i < 2; i++) { + TreeItem item = new TreeItem(tree, SWT.NONE); + item.setText("item " + i); + for (int j = 0; j < 2; j++) { + TreeItem subItem = new TreeItem(item, SWT.NONE); + subItem.setText("item " + j); + for (int k = 0; k < 2; k++) { + TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); + subsubItem.setText("item " + k); + } + } + } + + tree.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + TreeItem[] selection = tree.getSelection(); + TreeItem[] revisedSelection = new TreeItem[0]; + for (int i = 0; i < selection.length; i++) { + String text = selection[i].getText(); + if (text.indexOf("1") > 0) { + TreeItem[] newSelection = new TreeItem[revisedSelection.length + 1]; + System.arraycopy(revisedSelection, 0, newSelection, 0, revisedSelection.length); + newSelection[revisedSelection.length] = selection[i]; + revisedSelection = newSelection; + } + } + tree.setSelection(revisedSelection); + } + }); + + shell.setSize(300, 300); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet81.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet81.java new file mode 100644 index 0000000000..ce49638e32 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet81.java @@ -0,0 +1,149 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * OLE and ActiveX example snippet: browse the typelibinfo for a program id + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.internal.ole.win32.*; +import org.eclipse.swt.ole.win32.*; +import org.eclipse.swt.widgets.*; + +public class Snippet81 { + +public static void main(String[] args) { + + if (args.length == 0) { + System.out.println("Usage: java Main <program id>"); + return; + } + + String progID = args[0]; + + Shell shell = new Shell(); + + OleFrame frame = new OleFrame(shell, SWT.NONE); + OleControlSite site = null; + OleAutomation auto = null; + try { + site = new OleControlSite(frame, SWT.NONE, progID); + auto = new OleAutomation(site); + } catch (SWTException ex) { + System.out.println("Unable to open type library for "+progID); + return; + } + + TYPEATTR typeattr = auto.getTypeInfoAttributes(); + if (typeattr != null) { + if (typeattr.cFuncs > 0) System.out.println("Functions for "+progID+" :\n"); + for (int i = 0; i < typeattr.cFuncs; i++) { + OleFunctionDescription data = auto.getFunctionDescription(i); + String argList = ""; + int firstOptionalArgIndex = data.args.length - data.optionalArgCount; + for (int j = 0; j < data.args.length; j++) { + argList += "["; + if (j >= firstOptionalArgIndex) argList += "optional, "; + argList += getDirection(data.args[j].flags)+"] "+getTypeName(data.args[j].type)+" "+data.args[j].name; + if ( j < data.args.length - 1) argList += ", "; + } + System.out.println(getInvokeKind(data.invokeKind)+" (id = "+data.id+") : " + +"\n\tSignature : "+getTypeName(data.returnType)+" "+data.name+"("+argList+")" + +"\n\tDescription : "+data.documentation + +"\n\tHelp File : "+data.helpFile+"\n"); + } + + if (typeattr.cVars > 0) System.out.println("\n\nVariables for "+progID+" :\n"); + for (int i = 0; i < typeattr.cVars; i++) { + OlePropertyDescription data = auto.getPropertyDescription(i); + System.out.println("PROPERTY (id = "+data.id+") :" + +"\n\tName : "+data.name + +"\n\tType : "+getTypeName(data.type)+"\n"); + } + } + + auto.dispose(); + shell.dispose(); + +} +private static String getTypeName(int type) { + switch (type) { + case OLE.VT_BOOL : return "boolean"; + case OLE.VT_R4 : return "float"; + case OLE.VT_R8 : return "double"; + case OLE.VT_I4 : return "int"; + case OLE.VT_DISPATCH : return "IDispatch"; + case OLE.VT_UNKNOWN : return "IUnknown"; + case OLE.VT_I2 : return "short"; + case OLE.VT_BSTR : return "String"; + case OLE.VT_VARIANT : return "Variant"; + case OLE.VT_CY : return "Currency"; + case OLE.VT_DATE : return "Date"; + case OLE.VT_UI1 : return "unsigned char"; + case OLE.VT_UI4 : return "unsigned int"; + case OLE.VT_USERDEFINED : return "UserDefined"; + case OLE.VT_HRESULT : return "int"; + case OLE.VT_VOID : return "void"; + + case OLE.VT_BYREF | OLE.VT_BOOL : return "boolean *"; + case OLE.VT_BYREF | OLE.VT_R4 : return "float *"; + case OLE.VT_BYREF | OLE.VT_R8 : return "double *"; + case OLE.VT_BYREF | OLE.VT_I4 : return "int *"; + case OLE.VT_BYREF | OLE.VT_DISPATCH : return "IDispatch *"; + case OLE.VT_BYREF | OLE.VT_UNKNOWN : return "IUnknown *"; + case OLE.VT_BYREF | OLE.VT_I2 : return "short *"; + case OLE.VT_BYREF | OLE.VT_BSTR : return "String *"; + case OLE.VT_BYREF | OLE.VT_VARIANT : return "Variant *"; + case OLE.VT_BYREF | OLE.VT_CY : return "Currency *"; + case OLE.VT_BYREF | OLE.VT_DATE : return "Date *"; + case OLE.VT_BYREF | OLE.VT_UI1 : return "unsigned char *"; + case OLE.VT_BYREF | OLE.VT_UI4 : return "unsigned int *"; + case OLE.VT_BYREF | OLE.VT_USERDEFINED : return "UserDefined *"; + } + return "unknown "+ type; +} +private static String getDirection(int direction){ + String dirString = ""; + boolean comma = false; + if ((direction & OLE.IDLFLAG_FIN) != 0) { + dirString += "in"; + comma = true; + } + if ((direction & OLE.IDLFLAG_FOUT) != 0){ + if (comma) dirString += ", "; + dirString += "out"; + comma = true; + } + if ((direction & OLE.IDLFLAG_FLCID) != 0){ + if (comma) dirString += ", "; + dirString += "lcid"; + comma = true; + } + if ((direction & OLE.IDLFLAG_FRETVAL) != 0){ + if (comma) dirString += ", "; + dirString += "retval"; + } + + return dirString; +} +private static String getInvokeKind(int invKind) { + switch (invKind) { + case OLE.INVOKE_FUNC : return "METHOD"; + case OLE.INVOKE_PROPERTYGET : return "PROPERTY GET"; + case OLE.INVOKE_PROPERTYPUT : return "PROPERTY PUT"; + case OLE.INVOKE_PROPERTYPUTREF : return "PROPERTY PUT BY REF"; + } + return "unknown "+invKind; +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet82.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet82.java new file mode 100644 index 0000000000..aff7291240 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet82.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * CTabFolder example snippet: prevent an item from closing + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.custom.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet82 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + CTabFolder folder = new CTabFolder(shell, SWT.CLOSE); + for (int i = 0; i < 4; i++) { + CTabItem item = new CTabItem(folder, SWT.NONE); + item.setText("Item "+i); + Text text = new Text(folder, SWT.BORDER | SWT.MULTI); + text.setText("Content for Item "+i); + item.setControl(text); + } + + final CTabItem specialItem = new CTabItem(folder, SWT.NONE); + specialItem.setText("Don't Close Me"); + Text text = new Text(folder, SWT.BORDER | SWT.MULTI); + text.setText("This tab can never be closed"); + specialItem.setControl(text); + + folder.addCTabFolder2Listener(new CTabFolder2Adapter() { + public void close(CTabFolderEvent event) { + if (event.item.equals(specialItem)) { + event.doit = false; + } + } + }); + shell.setSize(400, 400); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet83.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet83.java new file mode 100644 index 0000000000..d8618eee68 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet83.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Drag and Drop example snippet: determine data types available (win32 only) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.dnd.*; +import org.eclipse.swt.internal.ole.win32.*; +import org.eclipse.swt.internal.win32.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet83 extends ByteArrayTransfer { + +private static Snippet83 _instance = new Snippet83(); +private int[] ids; +private String[] names; + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Canvas canvas = new Canvas(shell, SWT.NONE); + DropTarget target = new DropTarget(canvas, DND.DROP_DEFAULT | DND.DROP_LINK); + target.setTransfer(new Transfer[] {Snippet83.getInstance()}); + target.addDropListener(new DropTargetAdapter() { + public void dragEnter(DropTargetEvent event) { + String ops = null; + if ((event.operations & DND.DROP_COPY) != 0) ops += "Copy;"; + if ((event.operations & DND.DROP_MOVE) != 0) ops += "Move;"; + if ((event.operations & DND.DROP_LINK) != 0) ops += "Link;"; + System.out.println("Allowed Operations are "+ops); + + TransferData[] data = event.dataTypes; + for (int i = 0; i < data.length; i++) { + int id = data[i].type; + String name = getNameFromId(id); + System.out.println("Data type is "+id+" "+name); + } + } + }); + + shell.setSize(400, 400); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} + +public static Snippet83 getInstance () { + return _instance; +} +Snippet83() { + ids = new int[50000]; + names = new String[50000]; + for (int i = 0; i < ids.length; i++) { + ids[i] = i; + names[i] = getNameFromId(i); + } +} +public void javaToNative (Object object, TransferData transferData) { +} +public Object nativeToJava(TransferData transferData){ + return "Hello World"; +} +protected String[] getTypeNames(){ + return names; +} +protected int[] getTypeIds(){ + return ids; +} +static String getNameFromId(int id) { + String name = null; + int maxSize = 128; + TCHAR buffer = new TCHAR(0, maxSize); + int size = COM.GetClipboardFormatName(id, buffer, maxSize); + String type = null; + if (size != 0) { + name = buffer.toString(0, size); + } else { + switch (id) { + case COM.CF_HDROP: + name = "CF_HDROP"; + break; + case COM.CF_TEXT: + name = "CF_TEXT"; + break; + case COM.CF_BITMAP: + name = "CF_BITMAP"; + break; + case COM.CF_METAFILEPICT: + name = "CF_METAFILEPICT"; + break; + case COM.CF_SYLK: + name = "CF_SYLK"; + break; + case COM.CF_DIF: + name = "CF_DIF"; + break; + case COM.CF_TIFF: + name = "CF_TIFF"; + break; + case COM.CF_OEMTEXT: + name = "CF_OEMTEXT"; + break; + case COM.CF_DIB: + name = "CF_DIB"; + break; + case COM.CF_PALETTE: + name = "CF_PALETTE"; + break; + case COM.CF_PENDATA: + name = "CF_PENDATA"; + break; + case COM.CF_RIFF: + name = "CF_RIFF"; + break; + case COM.CF_WAVE: + name = "CF_WAVE"; + break; + case COM.CF_UNICODETEXT: + name = "CF_UNICODETEXT"; + break; + case COM.CF_ENHMETAFILE: + name = "CF_ENHMETAFILE"; + break; + case COM.CF_LOCALE: + name = "CF_LOCALE"; + break; + case COM.CF_MAX: + name = "CF_MAX"; + break; + } + + } + return name; +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet84.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet84.java new file mode 100644 index 0000000000..03c67e1278 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet84.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Drag and Drop example snippet: define a default operation (in this example, Copy) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.dnd.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet84 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + final Text text = new Text(shell, SWT.BORDER | SWT.MULTI); + DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_COPY); + target.setTransfer(new Transfer[] {TextTransfer.getInstance()}); + target.addDropListener(new DropTargetAdapter() { + public void dragEnter(DropTargetEvent event) { + if (event.detail == DND.DROP_DEFAULT) event.detail = DND.DROP_COPY; + } + public void dragOperationChanged(DropTargetEvent event) { + if (event.detail == DND.DROP_DEFAULT) event.detail = DND.DROP_COPY; + } + public void drop(DropTargetEvent event) { + text.setText((String)event.data); + } + }); + + shell.setSize(400, 400); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet85.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet85.java new file mode 100644 index 0000000000..eb6abfd7f7 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet85.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * PocketPC example snippet: Hello World + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet85 { + +public static void main(String[] args) { + Display display = new Display(); + + /* + * Create a Shell with the default style + * i.e. full screen, no decoration on PocketPC. + */ + Shell shell = new Shell(display); + + /* + * Set a text so that the top level Shell + * also appears in the PocketPC task list + */ + shell.setText("Main"); + + /* + * Set a menubar to follow UI guidelines + * on PocketPC + */ + Menu mb = new Menu(shell, SWT.BAR); + shell.setMenuBar(mb); + + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet86.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet86.java new file mode 100644 index 0000000000..74b30bb231 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet86.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * PocketPC Shell example snippet: 'Ok' button + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet86 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display, SWT.CLOSE); + shell.setText("Main"); + Menu mb = new Menu(shell, SWT.BAR); + shell.setMenuBar(mb); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet87.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet87.java new file mode 100644 index 0000000000..a829f08f76 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet87.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * PocketPC Shell example snippet: resize automatically as SIP is on or off + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet87 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display, SWT.RESIZE); + shell.setText("Main"); + Menu mb = new Menu(shell, SWT.BAR); + shell.setMenuBar(mb); + /* Add a button to make the resize more visual */ + FillLayout layout = new FillLayout(); + shell.setLayout(layout); + Button button = new Button(shell, SWT.PUSH); + button.setText("Main"); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet88.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet88.java new file mode 100644 index 0000000000..3b89f60363 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet88.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TableEditor example snippet: edit the text of a table item (in place) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.custom.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet88 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + final Table table = new Table(shell, SWT.FULL_SELECTION | SWT.HIDE_SELECTION); + TableColumn column1 = new TableColumn(table, SWT.NONE); + TableColumn column2 = new TableColumn(table, SWT.NONE); + for (int i = 0; i < 10; i++) { + TableItem item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"item " + i, "edit this value"}); + } + column1.pack(); + column2.pack(); + + final TableEditor editor = new TableEditor(table); + //The editor must have the same size as the cell and must + //not be any smaller than 50 pixels. + editor.horizontalAlignment = SWT.LEFT; + editor.grabHorizontal = true; + editor.minimumWidth = 50; + // editing the second column + final int EDITABLECOLUMN = 1; + + table.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + // Clean up any previous editor control + Control oldEditor = editor.getEditor(); + if (oldEditor != null) oldEditor.dispose(); + + // Identify the selected row + TableItem item = (TableItem)e.item; + if (item == null) return; + + // The control that will be the editor must be a child of the Table + Text newEditor = new Text(table, SWT.NONE); + newEditor.setText(item.getText(EDITABLECOLUMN)); + newEditor.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent e) { + Text text = (Text)editor.getEditor(); + editor.getItem().setText(EDITABLECOLUMN, text.getText()); + } + }); + newEditor.selectAll(); + newEditor.setFocus(); + editor.setEditor(newEditor, item, EDITABLECOLUMN); + } + }); + shell.setSize(300, 300); + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet89.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet89.java new file mode 100644 index 0000000000..011186409a --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet89.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Menu example snippet: create a menu with radio items + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.events.*; + +public class Snippet89 { +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + Menu menu = new Menu (shell, SWT.POP_UP); + for (int i=0; i<4; i++) { + MenuItem item = new MenuItem (menu, SWT.RADIO); + item.setText ("Item " + i); + item.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + MenuItem item = (MenuItem)e.widget; + if (item.getSelection ()) { + System.out.println (item + " selected"); + } else { + System.out.println (item + " unselected"); + } + } + }); + } + shell.setMenu (menu); + shell.setSize (300, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet9.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet9.java new file mode 100644 index 0000000000..4dae67c730 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet9.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Composite example snippet: scroll a child control automatically + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet9 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display, SWT.SHELL_TRIM | SWT.H_SCROLL | SWT.V_SCROLL); + final Composite composite = new Composite (shell, SWT.BORDER); + composite.setSize (200, 400); + final ScrollBar hBar = shell.getHorizontalBar (); + hBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + Point location = composite.getLocation (); + location.x = -hBar.getSelection (); + composite.setLocation (location); + } + }); + final ScrollBar vBar = shell.getVerticalBar (); + vBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + Point location = composite.getLocation (); + location.y = -vBar.getSelection (); + composite.setLocation (location); + } + }); + shell.addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Point size = composite.getSize (); + Rectangle rect = shell.getClientArea (); + hBar.setMaximum (size.x); + vBar.setMaximum (size.y); + hBar.setThumb (Math.min (size.x, rect.width)); + vBar.setThumb (Math.min (size.y, rect.height)); + int hPage = size.x - rect.width; + int vPage = size.y - rect.height; + int hSelection = hBar.getSelection (); + int vSelection = vBar.getSelection (); + Point location = composite.getLocation (); + if (hSelection >= hPage) { + if (hPage <= 0) hSelection = 0; + location.x = -hSelection; + } + if (vSelection >= vPage) { + if (vPage <= 0) vSelection = 0; + location.y = -vSelection; + } + composite.setLocation (location); + } + }); + shell.open (); + while (!shell.isDisposed()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet90.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet90.java new file mode 100644 index 0000000000..0511208abb --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet90.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Tree example snippet: detect mouse down in a tree item + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet90 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + final Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI); + for (int i=0; i<12; i++) { + TreeItem treeItem = new TreeItem (tree, SWT.NULL); + treeItem.setText ("Item " + i); + } + tree.addListener (SWT.MouseDown, new Listener () { + public void handleEvent (Event event) { + Point point = new Point (event.x, event.y); + TreeItem item = tree.getItem (point); + if (item != null) { + System.out.println ("Mouse down: " + item); + } + } + }); + tree.setSize (200, 200); + shell.setSize (300, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} + +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet91.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet91.java new file mode 100644 index 0000000000..bbc8f6d8f4 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet91.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Drag and Drop example snippet: drag leaf items in a tree + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.dnd.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet91 { + +public static void main (String [] args) { + + Display display = Display.getDefault (); + final Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + final Tree tree = new Tree(shell, SWT.BORDER); + for (int i = 0; i < 3; i++) { + TreeItem item = new TreeItem(tree, SWT.NONE); + item.setText("item "+i); + for (int j = 0; j < 3; j++) { + TreeItem subItem = new TreeItem(item, SWT.NONE); + subItem.setText("item "+i+" "+j); + for (int k = 0; k < 3; k++) { + TreeItem subsubItem = new TreeItem(subItem, SWT.NONE); + subsubItem.setText("item "+i+" "+j+" "+k); + } + } + } + + Transfer[] types = new Transfer[] {TextTransfer.getInstance()}; + int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK; + + final DragSource source = new DragSource (tree, operations); + source.setTransfer(types); + final TreeItem[] dragSourceItem = new TreeItem[1]; + source.addDragListener (new DragSourceListener () { + public void dragStart(DragSourceEvent event) { + TreeItem[] selection = tree.getSelection(); + if (selection.length > 0 && selection[0].getItemCount() == 0) { + event.doit = true; + dragSourceItem[0] = selection[0]; + } else { + event.doit = false; + } + }; + public void dragSetData (DragSourceEvent event) { + event.data = dragSourceItem[0].getText(); + } + public void dragFinished(DragSourceEvent event) { + if (event.detail == DND.DROP_MOVE) + dragSourceItem[0].dispose(); + dragSourceItem[0] = null; + } + }); + + DropTarget target = new DropTarget(tree, operations); + target.setTransfer(types); + target.addDropListener (new DropTargetAdapter() { + public void dragOver(DropTargetEvent event) { + event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL | DND.FEEDBACK_SELECT; + } + public void drop(DropTargetEvent event) { + if (event.data == null) { + event.detail = DND.DROP_NONE; + return; + } + String text = (String)event.data; + if (event.item == null) { + TreeItem item = new TreeItem(tree, SWT.NONE); + item.setText(text); + } else { + TreeItem parent = (TreeItem)event.item; + TreeItem item = new TreeItem(parent, SWT.NONE); + item.setText(text); + } + } + }); + + shell.setSize (400, 400); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet92.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet92.java new file mode 100644 index 0000000000..1dbd768fd1 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet92.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Cursor example snippet: create a cursor from a source and a mask + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet92 { + +public static void main (String [] args) { + Display display = new Display(); + Color white = display.getSystemColor (SWT.COLOR_WHITE); + Color black = display.getSystemColor (SWT.COLOR_BLACK); + + //Create a source ImageData of depth 1 (monochrome) + PaletteData palette = new PaletteData (new RGB [] {white.getRGB(), black.getRGB(),}); + ImageData sourceData = new ImageData (20, 20, 1, palette); + for (int i = 0; i < 10; i ++) { + for (int j = 0; j < 20; j++) { + sourceData.setPixel(i, j, 1); + } + } + + //Create a mask ImageData of depth 1 (monochrome) + palette = new PaletteData (new RGB [] {white.getRGB(), black.getRGB(),}); + ImageData maskData = new ImageData (20, 20, 1, palette); + for (int i = 0; i < 20; i ++) { + for (int j = 0; j < 10; j++) { + maskData.setPixel(i, j, 1); + } + } + //Create cursor + Cursor cursor = new Cursor(display, sourceData, maskData, 10, 10); + + Shell shell = new Shell(display); + final Image source = new Image (display,sourceData); + final Image mask = new Image (display, maskData); + //Draw source and mask just to show what they look like + shell.addPaintListener(new PaintListener() { + public void paintControl(PaintEvent e) { + GC gc = e.gc; + gc.drawString("source: ", 10, 10); + gc.drawImage(source, 0, 0, 20, 20, 50, 10, 20, 20); + gc.drawString("mask: ",10, 40); + gc.drawImage(mask, 0, 0, 20, 20, 50, 40, 20, 20); + } + }); + shell.setSize(150, 150); + shell.open(); + shell.setCursor(cursor); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + cursor.dispose(); + source.dispose(); + mask.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet93.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet93.java new file mode 100644 index 0000000000..617182fe06 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet93.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * GC example snippet: measure a string + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.layout.*; + +public class Snippet93 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout (new RowLayout ()); + Label label = new Label (shell, SWT.NULL); + GC gc = new GC (label); + Point size = gc.textExtent ("Hello"); + gc.dispose (); + label.setText ("Hello -> " + size); + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet94.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet94.java new file mode 100644 index 0000000000..7ac0764dec --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet94.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Clipboard example snippet: copy and paste data with the clipboard + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.dnd.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet94 { + +public static void main( String[] args) { + Display display = Display.getDefault (); + final Clipboard cb = new Clipboard(display); + final Shell shell = new Shell (display); + shell.setLayout(new FormLayout()); + final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); + + Button copy = new Button(shell, SWT.PUSH); + copy.setText("Copy"); + copy.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + String textData = text.getSelectionText(); + TextTransfer textTransfer = TextTransfer.getInstance(); + cb.setContents(new Object[]{textData}, new Transfer[]{textTransfer}); + } + }); + + Button paste = new Button(shell, SWT.PUSH); + paste.setText("Paste"); + paste.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + TextTransfer transfer = TextTransfer.getInstance(); + String data = (String)cb.getContents(transfer); + if (data != null) { + text.insert(data); + } + } + }); + + FormData data = new FormData(); + data.right = new FormAttachment(100, -5); + data.top = new FormAttachment(0, 5); + copy.setLayoutData(data); + + data = new FormData(); + data.right = new FormAttachment(100, -5); + data.top = new FormAttachment(copy, 5); + paste.setLayoutData(data); + + data = new FormData(); + data.left = new FormAttachment(0, 5); + data.top = new FormAttachment(0, 5); + data.right = new FormAttachment(copy, -5); + data.bottom = new FormAttachment(100, -5); + text.setLayoutData(data); + + shell.setSize(200, 200); + shell.open(); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + cb.dispose(); + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet95.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet95.java new file mode 100644 index 0000000000..aa2b627368 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet95.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * GC example snippet: capture a widget image with a GC + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet95 { + +public static void main(String[] args) { + final Display display = new Display(); + final Shell shell = new Shell(display); + shell.setText("Widget"); + shell.setBounds(10, 10, 200, 200); + + final Table table = new Table(shell, SWT.MULTI); + table.setLinesVisible(true); + table.setBounds(10, 10, 100, 100); + for (int i = 0; i < 9; i++) { + new TableItem(table, SWT.NONE).setText("item" + i); + } + + Button button = new Button(shell, SWT.PUSH); + button.setText("Capture"); + button.setBounds(10, 140, 50, 20); + button.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + Point tableSize = table.getSize(); + GC gc = new GC(table); + final Image image = + new Image(display, tableSize.x, tableSize.y); + gc.copyArea(image, 0, 0); + gc.dispose(); + + Shell popup = new Shell(shell); + popup.setText("Image"); + popup.setBounds(50, 50, 200, 200); + popup.addListener(SWT.Close, new Listener() { + public void handleEvent(Event event) { + image.dispose(); + } + }); + + Canvas canvas = new Canvas(popup, SWT.NONE); + canvas.setBounds(10, 10, 150, 150); + canvas.addPaintListener(new PaintListener() { + public void paintControl(PaintEvent e) { + e.gc.drawImage(image, 0, 0); + } + }); + popup.open(); + } + }); + + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java new file mode 100644 index 0000000000..d6688c0658 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java @@ -0,0 +1,131 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * TableCursor example snippet: navigate a table cells with arrow keys. + * Edit when user hits Return key. Exit edit mode by hitting Escape (cancels edit) + * or Return (applies edit to table). + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.custom.*; +import org.eclipse.swt.events.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet96 { + +public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + + // create a a table with 3 columns and fill with data + final Table table = new Table(shell, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); + table.setLayoutData(new GridData(GridData.FILL_BOTH)); + TableColumn column1 = new TableColumn(table, SWT.NONE); + TableColumn column2 = new TableColumn(table, SWT.NONE); + TableColumn column3 = new TableColumn(table, SWT.NONE); + for (int i = 0; i < 100; i++) { + TableItem item = new TableItem(table, SWT.NONE); + item.setText(new String[] {"cell " + i + " 0", "cell " + i + " 1", "cell " + i + " 2" }); + } + column1.pack(); + column2.pack(); + column3.pack(); + + // create a TableCursor to navigate around the table + final TableCursor cursor = new TableCursor(table, SWT.NONE); + // create an editor to edit the cell when the user hits "ENTER" + // while over a cell in the table + final ControlEditor editor = new ControlEditor(cursor); + editor.grabHorizontal = true; + editor.grabVertical = true; + + cursor.addSelectionListener(new SelectionAdapter() { + // when the TableEditor is over a cell, select the corresponding row in + // the table + public void widgetSelected(SelectionEvent e) { + table.setSelection(new TableItem[] { cursor.getRow()}); + } + // when the user hits "ENTER" in the TableCursor, pop up a text editor so that + // they can change the text of the cell + public void widgetDefaultSelected(SelectionEvent e) { + final Text text = new Text(cursor, SWT.NONE); + TableItem row = cursor.getRow(); + int column = cursor.getColumn(); + text.setText(row.getText(column)); + text.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent e) { + // close the text editor and copy the data over + // when the user hits "ENTER" + if (e.character == SWT.CR) { + TableItem row = cursor.getRow(); + int column = cursor.getColumn(); + row.setText(column, text.getText()); + text.dispose(); + } + // close the text editor when the user hits "ESC" + if (e.character == SWT.ESC) { + text.dispose(); + } + } + }); + editor.setEditor(text); + text.setFocus(); + } + }); + // Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key. + // This alows the user to select multiple items in the table. + cursor.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent e) { + if (e.keyCode == SWT.CTRL + || e.keyCode == SWT.SHIFT + || (e.stateMask & SWT.CONTROL) != 0 + || (e.stateMask & SWT.SHIFT) != 0) { + cursor.setVisible(false); + } + } + }); + // Show the TableCursor when the user releases the "SHIFT" or "CTRL" key. + // This signals the end of the multiple selection task. + table.addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0) + return; + if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0) + return; + if (e.keyCode != SWT.CONTROL + && (e.stateMask & SWT.CONTROL) != 0) + return; + if (e.keyCode != SWT.SHIFT && (e.stateMask & SWT.SHIFT) != 0) + return; + + TableItem[] selection = table.getSelection(); + TableItem row = (selection.length == 0) ? table.getItem(table.getTopIndex()) : selection[0]; + table.showItem(row); + cursor.setSelection(row, 0); + cursor.setVisible(true); + cursor.setFocus(); + } + }); + + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet97.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet97.java new file mode 100644 index 0000000000..2fdbc59df6 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet97.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Menu example snippet: fill a menu dynamically (when menu shown) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet97 { + +public static void main (String [] args) { + final Display display = new Display (); + Shell shell = new Shell (display); + final Tree tree = new Tree (shell, SWT.BORDER | SWT.MULTI); + final Menu menu = new Menu (shell, SWT.POP_UP); + tree.setMenu (menu); + for (int i=0; i<12; i++) { + TreeItem item = new TreeItem (tree, SWT.NULL); + item.setText ("Item " + i); + } + menu.addListener (SWT.Show, new Listener () { + public void handleEvent (Event event) { + MenuItem [] menuItems = menu.getItems (); + for (int i=0; i<menuItems.length; i++) { + menuItems [i].dispose (); + } + TreeItem [] treeItems = tree.getSelection (); + for (int i=0; i<treeItems.length; i++) { + MenuItem menuItem = new MenuItem (menu, SWT.PUSH); + menuItem.setText (treeItems [i].getText ()); + } + } + }); + tree.setSize (200, 200); + shell.setSize (300, 300); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet98.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet98.java new file mode 100644 index 0000000000..8bcdadc87c --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet98.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Composite example snippet: create and dispose children of a composite + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet98 { + +static int pageNum = 0; +static Composite pageComposite; + +public static void main(String args[]) { + Display display = new Display(); + final Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + Button button = new Button(shell, SWT.PUSH); + button.setText("Push"); + pageComposite = new Composite(shell, SWT.NONE); + pageComposite.setLayout(new GridLayout()); + pageComposite.setLayoutData(new GridData()); + + button.addListener(SWT.Selection, new Listener() { + public void handleEvent(Event event) { + if ((pageComposite != null) && (!pageComposite.isDisposed())) { + pageComposite.dispose(); + } + pageComposite = new Composite(shell, SWT.NONE); + pageComposite.setLayout(new GridLayout()); + pageComposite.setLayoutData(new GridData()); + if (pageNum++ % 2 == 0) { + Table table = new Table(pageComposite, SWT.BORDER); + table.setLayoutData(new GridData()); + for (int i = 0; i < 5; i++) { + new TableItem(table, SWT.NONE).setText("table item " + i); + } + } else { + new Button(pageComposite, SWT.RADIO).setText("radio"); + } + shell.layout(true); + } + }); + + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); +} +} diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet99.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet99.java new file mode 100644 index 0000000000..cefcdb6101 --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet99.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Shell example snippet: prevent a shell from closing (prompt the user) + * + * For a list of all SWT example snippets see + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets + */ +import org.eclipse.swt.*; +import org.eclipse.swt.widgets.*; + +public class Snippet99 { + +public static void main (String [] args) { + Display display = new Display (); + final Shell shell = new Shell (display); + shell.addListener (SWT.Close, new Listener () { + public void handleEvent (Event event) { + int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO; + MessageBox messageBox = new MessageBox (shell, style); + messageBox.setText ("Information"); + messageBox.setMessage ("Close the shell?"); + event.doit = messageBox.open () == SWT.YES; + } + }); + shell.pack (); + shell.open(); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +} +} |