Es folgen spezielle Informationen und Beispiele für die Integration von jowidgets Code in Projekte, welche auf dem Standard Widget Toolkit (Swt) basieren.
Das SwtToJoFramesSnipped demonstriert die Erzeugung von Fenstern innerhalb einer nativen SWT Applikation:
1 package org.jowidgets.examples.swt.snipped; 2 3 import net.miginfocom.swt.MigLayout; 4 5 import org.eclipse.swt.SWT; 6 import org.eclipse.swt.events.SelectionAdapter; 7 import org.eclipse.swt.events.SelectionEvent; 8 import org.eclipse.swt.widgets.Button; 9 import org.eclipse.swt.widgets.Display; 10 import org.eclipse.swt.widgets.Shell; 11 import org.jowidgets.api.toolkit.Toolkit; 12 import org.jowidgets.api.widgets.IFrame; 13 import org.jowidgets.api.widgets.blueprint.IDialogBluePrint; 14 import org.jowidgets.api.widgets.blueprint.IFrameBluePrint; 15 import org.jowidgets.common.types.Dimension; 16 import org.jowidgets.tools.widgets.blueprint.BPF; 17 18 public final class SwtToJoFramesSnipped { 19 20 private SwtToJoFramesSnipped() {} 21 22 public static void main(final String[] args) throws Exception { 23 final Display display = new Display(); 24 25 final Shell shell = new Shell(display); 26 shell.setSize(1024, 768); 27 shell.setLayout(new MigLayout("", "[]", "[][]")); 28 29 //add button to open a jowidgets root frame 30 final Button frameButton = new Button(shell, SWT.NONE); 31 frameButton.setText("Create root frame"); 32 frameButton.addSelectionListener(new SelectionAdapter() { 33 34 @Override 35 public void widgetSelected(final SelectionEvent e) { 36 final IFrameBluePrint frameBp = BPF.frame("Jowidgets Root Frame"); 37 frameBp.setSize(new Dimension(400, 300)); 38 frameBp.setAutoDispose(true); 39 final IFrame frame = Toolkit.createRootFrame(frameBp); 40 frame.add(BPF.textLabel("This is a jowidgets root frame")); 41 frame.setVisible(true); 42 } 43 44 }); 45 46 //add button to open a jowidgets modal dialog 47 final Button dialogButton = new Button(shell, SWT.NONE); 48 dialogButton.setText("Create dialog"); 49 dialogButton.addSelectionListener(new SelectionAdapter() { 50 51 @Override 52 public void widgetSelected(final SelectionEvent e) { 53 final IDialogBluePrint dialogBp = BPF.dialog("Jowidgets dialog"); 54 dialogBp.setSize(new Dimension(400, 300)); 55 final IFrame dialog = Toolkit.getWidgetFactory().create(shell, dialogBp); 56 dialog.add(BPF.textLabel("This is a jowidgets modal dialog")); 57 dialog.setVisible(true); 58 } 59 60 }); 61 62 shell.open(); 63 while (!shell.isDisposed()) { 64 if (!display.readAndDispatch()) { 65 display.sleep(); 66 } 67 } 68 display.dispose(); 69 } 70 71 }
Um für ein Swt Composite
ein
IComposite
Wrapper zu erzeugen, kann man
die Utility Klasse SwtToJoWrapper
verwenden, welche sich im Modul
org.jowidgets.spi.impl.swt.addons
befindet.
Der folgende Code zeigt einen Teil der Implementierung:
1 public final class SwtToJoWrapper {
2
3 private SwtToJoWrapper() {}
4
5 public static IComposite create(final Composite composite) {
6 Assert.paramNotNull(composite, "composite");
7 final IComposite result = Toolkit.getWidgetWrapperFactory().createComposite(composite);
8 composite.addDisposeListener(new DisposeListener() {
9 @Override
10 public void widgetDisposed(final DisposeEvent e) {
11 result.dispose();
12 }
13 });
14 return result;
15 }
16
17 }
In Zeile 8 wird auf dem SWT Composite ein
DisposeListener
hinzugefügt, der
dasIComposite
Wrapper disposed, sobald das
SWT Composite disposed wird. Dies ist im Normalfall genau da
gewünschte Verhalten.
Das folgende Beispiel demonstriert die Verwendung:
1 final IComposite joComposite = SwtToJoWrapper.create(swtComposite);
Das
SwtToJoCompositeSnipped
demonstriert die Erzeugung eines IComposite
Wrapper innerhalb einer nativen SWT Applikation:
1 import org.eclipse.swt.SWT; 2 import org.eclipse.swt.layout.FillLayout; 3 import org.eclipse.swt.widgets.Composite; 4 import org.eclipse.swt.widgets.Display; 5 import org.eclipse.swt.widgets.Shell; 6 import org.jowidgets.api.widgets.IComposite; 7 import org.jowidgets.api.widgets.IInputField; 8 import org.jowidgets.common.widgets.controller.IInputListener; 9 import org.jowidgets.common.widgets.layout.MigLayoutDescriptor; 10 import org.jowidgets.spi.impl.swt.addons.SwtToJoWrapper; 11 import org.jowidgets.tools.widgets.blueprint.BPF; 12 13 public final class SwtToJoCompositeSnipped { 14 15 private SwtToJoCompositeSnipped() {} 16 17 public static void main(final String[] args) throws Exception { 18 //create a swt display 19 final Display display = new Display(); 20 21 //create a swt shell 22 final Shell shell = new Shell(display); 23 shell.setSize(400, 300); 24 shell.setText("SwtToJo composite snipped"); 25 shell.setLayout(new FillLayout()); 26 27 //create a swt composite 28 final Composite swtComposite = new Composite(shell, SWT.NONE); 29 30 //create a jowidgets composite wrapper and do some jowidgets stuff 31 final IComposite joComposite = SwtToJoWrapper.create(swtComposite); 32 joComposite.setLayout(new MigLayoutDescriptor("[][grow]", "[]")); 33 34 //add a label 35 joComposite.add(BPF.textLabel("Name")); 36 37 //add a input field for double values 38 final IInputField<String> nameField = joComposite.add(BPF.inputFieldString(), "grow x"); 39 nameField.addInputListener(new IInputListener() { 40 @Override 41 public void inputChanged() { 42 //CHECKSTYLE:OFF 43 System.out.println("Hello " + nameField.getValue()); 44 //CHECKSTYLE:ON 45 } 46 }); 47 48 //open the swt shell and start event dispatching 49 shell.open(); 50 while (!shell.isDisposed()) { 51 if (!display.readAndDispatch()) { 52 display.sleep(); 53 } 54 } 55 display.dispose(); 56 } 57 }