# HG changeset patch # User Frank Benoit # Date 1218446257 -7200 # Node ID 7c4b76583cb88c525882491438753b81f450d1c9 # Parent 161f7698cfb8dd1d7164c0a4bd9431f1cc47b2f4 Added new JFace snippets diff -r 161f7698cfb8 -r 7c4b76583cb8 .hgignore --- a/.hgignore Fri Aug 08 14:40:21 2008 +0200 +++ b/.hgignore Mon Aug 11 11:17:37 2008 +0200 @@ -55,19 +55,22 @@ ^jface/ShowFieldPrefs$ ^jface/Librarian$ -^jface/snippets/Snippet001TableViewer$ -^jface/snippets/Snippet002TreeViewer$ -^jface/snippets/Snippet004HideSelection$ -^jface/snippets/Snippet005TreeCustomMenu$ -^jface/snippets/Snippet006TableMultiLineCells$ -^jface/snippets/Snippet007FullSelection$ -^jface/snippets/Snippet010OwnerDraw$ -^jface/snippets/Snippet014TreeViewerNoMandatoryLabelProvider$ -^jface/snippets/Snippet016TableLayout$ -^jface/snippets/Snippet026TreeViewerTabEditing$ -^jface/snippets/Snippet031TableViewerCustomTooltipsMultiSelection$ -^jface/snippets/Snippet040TableViewerSorting$ -^jface/snippets/Snippet043NoColumnTreeViewerKeyboardEditing$ -^jface/snippets/Snippet047VirtualLazyTreeViewer$ +^jface/snippets/viewers/Snippet001TableViewer$ +^jface/snippets/viewers/Snippet002TreeViewer$ +^jface/snippets/viewers/Snippet004HideSelection$ +^jface/snippets/viewers/Snippet005TreeCustomMenu$ +^jface/snippets/viewers/Snippet006TableMultiLineCells$ +^jface/snippets/viewers/Snippet007FullSelection$ +^jface/snippets/viewers/Snippet010OwnerDraw$ +^jface/snippets/viewers/Snippet014TreeViewerNoMandatoryLabelProvider$ +^jface/snippets/viewers/Snippet016TableLayout$ +^jface/snippets/viewers/Snippet026TreeViewerTabEditing$ +^jface/snippets/viewers/Snippet031TableViewerCustomTooltipsMultiSelection$ +^jface/snippets/viewers/Snippet040TableViewerSorting$ +^jface/snippets/viewers/Snippet043NoColumnTreeViewerKeyboardEditing$ +^jface/snippets/viewers/Snippet047VirtualLazyTreeViewer$ +^jface/snippets/window/Snippet020CustomizedControlTooltips$ +^jface/snippets/window/Snippet023TreeViewerCustomTooltips$ +^jface/snippets/window/Snippet031TableStaticTooltip$ diff -r 161f7698cfb8 -r 7c4b76583cb8 jface/snippets/viewers/Snippet040TableViewerSorting.d --- a/jface/snippets/viewers/Snippet040TableViewerSorting.d Fri Aug 08 14:40:21 2008 +0200 +++ b/jface/snippets/viewers/Snippet040TableViewerSorting.d Mon Aug 11 11:17:37 2008 +0200 @@ -32,6 +32,8 @@ import dwt.widgets.Shell; import dwt.dwthelper.utils; +version(JIVE) import jive.stacktrace; + /** * Example usage of ViewerComparator in tables to allow sorting * diff -r 161f7698cfb8 -r 7c4b76583cb8 jface/snippets/window/Snippet020CustomizedControlTooltips.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/window/Snippet020CustomizedControlTooltips.d Mon Aug 11 11:17:37 2008 +0200 @@ -0,0 +1,261 @@ +/******************************************************************************* + * Copyright (c) 2006 Tom Schindl and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl - initial API and implementation + *******************************************************************************/ + +module dwtx.jface.snippets.window.Snippet020CustomizedControlTooltips; + + +import dwtx.jface.resource.ImageDescriptor; +import dwtx.jface.resource.JFaceResources; +import dwtx.jface.util.Policy; +import dwtx.jface.window.DefaultToolTip; +import dwtx.jface.window.ToolTip; +import dwt.DWT; +import dwt.events.MouseAdapter; +import dwt.events.MouseEvent; +import dwt.events.SelectionAdapter; +import dwt.events.SelectionEvent; +import dwt.events.SelectionListener; +import dwt.graphics.Point; +import dwt.graphics.RGB; +import dwt.layout.FillLayout; +import dwt.layout.GridData; +import dwt.layout.GridLayout; +import dwt.layout.RowLayout; +import dwt.widgets.Button; +import dwt.widgets.Composite; +import dwt.widgets.Control; +import dwt.widgets.Display; +import dwt.widgets.Event; +import dwt.widgets.Label; +import dwt.widgets.Link; +import dwt.widgets.MessageBox; +import dwt.widgets.Shell; +import dwt.widgets.Text; + +import dwt.dwthelper.utils; +version(JIVE) import jive.stacktrace; + +/** + * Demonstrate usage of custom toolstips for controls + * + * @author Tom Schindl + * + */ +public class Snippet020CustomizedControlTooltips { + protected class MyToolTip : ToolTip { + + private Shell parentShell; + + private String headerText = "ToolTip-Header"; + + public static const String HEADER_BG_COLOR = Policy.JFACE ~ ".TOOLTIP_HEAD_BG_COLOR"; + + public static const String HEADER_FG_COLOR = Policy.JFACE ~ ".TOOLTIP_HEAD_FG_COLOR"; + + public static const String HEADER_FONT = Policy.JFACE ~ ".TOOLTIP_HEAD_FONT"; + + public static const String HEADER_CLOSE_ICON = Policy.JFACE ~ ".TOOLTIP_CLOSE_ICON"; + public static const String HEADER_HELP_ICON = Policy.JFACE ~ ".TOOLTIP_HELP_ICON"; + + public this(Control control) { + super(control); + this.parentShell = control.getShell(); + } + + protected Composite createToolTipContentArea(Event event, + Composite parent) { + Composite comp = new Composite(parent,DWT.NONE); + + GridLayout gl = new GridLayout(1,false); + gl.marginBottom=0; + gl.marginTop=0; + gl.marginHeight=0; + gl.marginWidth=0; + gl.marginLeft=0; + gl.marginRight=0; + gl.verticalSpacing=1; + comp.setLayout(gl); + + Composite topArea = new Composite(comp,DWT.NONE); + GridData data = new GridData(DWT.FILL,DWT.FILL,true,false); + data.widthHint=200; + topArea.setLayoutData(data); + topArea.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR)); + + gl = new GridLayout(2,false); + gl.marginBottom=2; + gl.marginTop=2; + gl.marginHeight=0; + gl.marginWidth=0; + gl.marginLeft=5; + gl.marginRight=2; + + topArea.setLayout(gl); + + Label l = new Label(topArea,DWT.NONE); + l.setText(headerText); + l.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR)); + l.setFont(JFaceResources.getFontRegistry().get(HEADER_FONT)); + l.setForeground(JFaceResources.getColorRegistry().get(HEADER_FG_COLOR)); + l.setLayoutData(new GridData(GridData.FILL_BOTH)); + + Composite iconComp = new Composite(topArea,DWT.NONE); + iconComp.setLayoutData(new GridData()); + iconComp.setLayout(new GridLayout(2,false)); + iconComp.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR)); + + gl = new GridLayout(2,false); + gl.marginBottom=0; + gl.marginTop=0; + gl.marginHeight=0; + gl.marginWidth=0; + gl.marginLeft=0; + gl.marginRight=0; + iconComp.setLayout(gl); + + Label helpIcon = new Label(iconComp,DWT.NONE); + helpIcon.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR)); + helpIcon.setImage(JFaceResources.getImage(HEADER_HELP_ICON)); + helpIcon.addMouseListener(new class MouseAdapter { + + public void mouseDown(MouseEvent e) { + hide(); + openHelp(); + } + }); + + + Label closeIcon = new Label(iconComp,DWT.NONE); + closeIcon.setBackground(JFaceResources.getColorRegistry().get(HEADER_BG_COLOR)); + closeIcon.setImage(JFaceResources.getImage(HEADER_CLOSE_ICON)); + closeIcon.addMouseListener(new class MouseAdapter { + + public void mouseDown(MouseEvent e) { + parentShell.setFocus(); + hide(); + } + }); + + createContentArea(comp).setLayoutData(new GridData(GridData.FILL_BOTH)); + + return comp; + } + + protected Composite createContentArea(Composite parent) { + return new Composite(parent,DWT.NONE); + } + + protected void openHelp() { + parentShell.setFocus(); + } + } + + class MyToolTip2 : MyToolTip { + public this(Control control) { + super(control); + } + protected Composite createContentArea(Composite parent) { + Composite comp = super.createContentArea(parent); + comp.setBackground(parent.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND)); + FillLayout layout = new FillLayout(); + layout.marginWidth=5; + comp.setLayout(layout); + Link l = new Link(comp,DWT.NONE); + l.setText("This a custom tooltip you can: \n- pop up any control you want\n- define delays\n - ... \nGo and get Eclipse M4 from http://www.eclipse.org"); + l.setBackground(parent.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND)); + l.addSelectionListener(new class SelectionAdapter { + public void widgetSelected(SelectionEvent e) { + openURL(); + } + }); + return comp; + } + + protected void openURL() { + MessageBox box = new MessageBox(parent,DWT.ICON_INFORMATION); + box.setText("Eclipse.org"); + box.setMessage("Here is where we'd open the URL."); + box.open(); + } + + protected void openHelp() { + MessageBox box = new MessageBox(parent,DWT.ICON_INFORMATION); + box.setText("Info"); + box.setMessage("Here is where we'd show some information."); + box.open(); + } + + }; + Shell parent; + DefaultToolTip toolTipDelayed; + + public this(Shell parent_) { + this.parent = parent_; + JFaceResources.getColorRegistry().put(MyToolTip.HEADER_BG_COLOR, new RGB(255,255,255)); + JFaceResources.getFontRegistry().put(MyToolTip.HEADER_FONT, JFaceResources.getFontRegistry().getBold(JFaceResources.getDefaultFont().getFontData()[0].getName()).getFontData()); + + + JFaceResources.getImageRegistry().put(MyToolTip.HEADER_CLOSE_ICON,ImageDescriptor.createFromFile(getImportData!("jface.snippets.showerr_tsk.gif"))); + JFaceResources.getImageRegistry().put(MyToolTip.HEADER_HELP_ICON,ImageDescriptor.createFromFile(getImportData!("jface.snippets.linkto_help.gif"))); + + Text text = new Text(parent,DWT.BORDER); + text.setText("Hello World"); + + MyToolTip myTooltipLabel = new MyToolTip2(text); + myTooltipLabel.setShift(new Point(-5, -5)); + myTooltipLabel.setHideOnMouseDown(false); + myTooltipLabel.activate(); + + text = new Text(parent,DWT.BORDER); + text.setText("Hello World"); + DefaultToolTip toolTip = new DefaultToolTip(text); + toolTip.setText("Hello World\nHello World"); + toolTip.setBackgroundColor(parent.getDisplay().getSystemColor(DWT.COLOR_RED)); + + Button b = new Button(parent,DWT.PUSH); + b.setText("Popup on press"); + + toolTipDelayed = new DefaultToolTip(b,ToolTip.RECREATE,true); + toolTipDelayed.setText("Hello World\nHello World"); + toolTipDelayed.setBackgroundColor(parent.getDisplay().getSystemColor(DWT.COLOR_RED)); + toolTipDelayed.setHideDelay(2000); + + b.addSelectionListener(new class SelectionAdapter { + public void widgetSelected(SelectionEvent e) { + toolTipDelayed.show(new Point(0,0)); + } + }); + + + } + + public static void main(String[] args) { + Display display = new Display(); + + Shell shell = new Shell(display); + shell.setLayout(new RowLayout()); + new Snippet020CustomizedControlTooltips(shell); + + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + + display.dispose(); + } +} + +void main(){ + Snippet020CustomizedControlTooltips.main(null); +} + diff -r 161f7698cfb8 -r 7c4b76583cb8 jface/snippets/window/Snippet023TreeViewerCustomTooltips.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/window/Snippet023TreeViewerCustomTooltips.d Mon Aug 11 11:17:37 2008 +0200 @@ -0,0 +1,253 @@ +/******************************************************************************* + * Copyright (c) 2006 Tom Schindl and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl - initial API and implementation + *******************************************************************************/ + +module dwtx.jface.snippets.window.Snippet023TreeViewerCustomTooltips; + +import dwtx.jface.viewers.CellLabelProvider; +import dwtx.jface.viewers.ITreeContentProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.TreeViewer; +import dwtx.jface.viewers.Viewer; +import dwt.DWT; +import dwt.graphics.Point; +import dwt.graphics.Rectangle; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Event; +import dwt.widgets.Label; +import dwt.widgets.Listener; +import dwt.widgets.Shell; +import dwt.widgets.TreeItem; + +import dwtx.dwtxhelper.Collection; +import dwt.dwthelper.utils; +import tango.text.convert.Format; +import tango.util.log.Trace; + +/** + * A simple TreeViewer to demonstrate how custom tooltips could be created + * easily. This is an extended version from + * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/dwt.snippets/src/org/eclipse/swt/snippets/Snippet125.java + * + * This code is for users pre 3.3 others could use newly added tooltip support in + * {@link CellLabelProvider} + * + * @author Tom Schindl + * + */ +public class Snippet023TreeViewerCustomTooltips { + private class MyContentProvider : ITreeContentProvider { + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + public Object[] getElements(Object inputElement) { + return (cast(MyModel) inputElement).child.toArray(); + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer, + * java.lang.Object, java.lang.Object) + */ + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) + */ + public Object[] getChildren(Object parentElement) { + return getElements(parentElement); + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) + */ + public Object getParent(Object element) { + if (element is null) { + return null; + } + + return (cast(MyModel) element).parent; + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) + */ + public bool hasChildren(Object element) { + return (cast(MyModel) element).child.size() > 0; + } + + } + + public class MyModel { + public MyModel parent; + + public ArrayList child; + + public int counter; + + public this(int counter, MyModel parent) { + this.parent = parent; + this.counter = counter; + child = new ArrayList(); + } + + public String toString() { + return Format( "Item {}{}", (parent !is null) ? "." : "", counter ); + } + } + + TreeViewer v; + Listener labelListener; + + public this(Shell shell) { + v = new TreeViewer(shell); + v.setLabelProvider(new LabelProvider()); + v.setContentProvider(new MyContentProvider()); + v.setInput(createModel()); + v.getTree().setToolTipText(""); + + labelListener = dgListener ( (Event event){ + Label label = cast(Label)event.widget; + Shell shell_ = label.getShell (); + switch (event.type) { + case DWT.MouseDown: + Event e = new Event (); + e.item = cast(TreeItem) label.getData ("_TABLEITEM"); + // Assuming table is single select, set the selection as if + // the mouse down event went through to the table + v.getTree().setSelection ([cast(TreeItem) e.item]); + v.getTree().notifyListeners (DWT.Selection, e); + shell_.dispose (); + v.getTree().setFocus(); + break; + case DWT.MouseExit: + shell_.dispose (); + break; + default: + } + }); + + Listener treeListener = new MyTreeListener(); + v.getTree().addListener (DWT.Dispose, treeListener); + v.getTree().addListener (DWT.KeyDown, treeListener); + v.getTree().addListener (DWT.MouseMove, treeListener); + v.getTree().addListener (DWT.MouseHover, treeListener); + } + + class MyTreeListener : Listener { + Shell tip = null; + Label label = null; + public void handleEvent (Event event) { + switch (event.type) { + case DWT.Dispose: + case DWT.KeyDown: + case DWT.MouseMove: { + if (tip is null) break; + tip.dispose (); + tip = null; + label = null; + break; + } + case DWT.MouseHover: { + Point coords = new Point(event.x, event.y); + TreeItem item = v.getTree().getItem(coords); + if (item !is null) { + int columns = v.getTree().getColumnCount(); + + for (int i = 0; i < columns || i is 0; i++) { + if (item.getBounds(i).contains(coords)) { + if (tip !is null && !tip.isDisposed ()) tip.dispose (); + tip = new Shell (v.getTree().getShell(), DWT.ON_TOP | DWT.NO_FOCUS | DWT.TOOL); + tip.setBackground (v.getTree().getDisplay().getSystemColor (DWT.COLOR_INFO_BACKGROUND)); + FillLayout layout = new FillLayout (); + layout.marginWidth = 2; + tip.setLayout (layout); + label = new Label (tip, DWT.NONE); + label.setForeground (v.getTree().getDisplay().getSystemColor (DWT.COLOR_INFO_FOREGROUND)); + label.setBackground (v.getTree().getDisplay().getSystemColor (DWT.COLOR_INFO_BACKGROUND)); + label.setData ("_TABLEITEM", item); + label.setText (Format("Tooltip: {} => {}", item.getData(), i)); + label.addListener (DWT.MouseExit, labelListener); + label.addListener (DWT.MouseDown, labelListener); + Point size = tip.computeSize (DWT.DEFAULT, DWT.DEFAULT); + Rectangle rect = item.getBounds (i); + Point pt = v.getTree().toDisplay (rect.x, rect.y); + tip.setBounds (pt.x, pt.y, size.x, size.y); + tip.setVisible (true); + break; + } + } + } + } + default: + } + } + }; + + private MyModel createModel() { + + MyModel root = new MyModel(0, null); + root.counter = 0; + + MyModel tmp; + for (int i = 1; i < 10; i++) { + tmp = new MyModel(i, root); + root.child.add(tmp); + for (int j = 1; j < i; j++) { + tmp.child.add(new MyModel(j, tmp)); + } + } + + return root; + } + + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet023TreeViewerCustomTooltips(shell); + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + + display.dispose(); + } +} + +void main(){ + Snippet023TreeViewerCustomTooltips.main(null); +} + + diff -r 161f7698cfb8 -r 7c4b76583cb8 jface/snippets/window/Snippet031TableStaticTooltip.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/window/Snippet031TableStaticTooltip.d Mon Aug 11 11:17:37 2008 +0200 @@ -0,0 +1,190 @@ +/******************************************************************************* + * Copyright (c) 2006 Tom Schindl and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl - initial API and implementation + *******************************************************************************/ + +module dwtx.jface.snippets.window.Snippet031TableStaticTooltip; + +import dwtx.jface.viewers.IStructuredContentProvider; +import dwtx.jface.viewers.ITableLabelProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.TableViewer; +import dwtx.jface.viewers.Viewer; +import dwtx.jface.window.DefaultToolTip; +import dwtx.jface.window.ToolTip; +import dwt.DWT; +import dwt.graphics.Color; +import dwt.graphics.GC; +import dwt.graphics.Image; +import dwt.graphics.Point; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.TableColumn; + +import dwt.dwthelper.utils; +import tango.text.convert.Format; + + +/** + * Example how one can create a tooltip which is not recreated for every table + * cell + * + * @author Tom Schindl + * + */ +public class Snippet031TableStaticTooltip { + private static Image[] images; + + private class MyContentProvider : IStructuredContentProvider { + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + public Object[] getElements(Object inputElement) { + return arrayFromObject!(MyModel)( inputElement ); + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.viewers.IContentProvider#inputChanged(dwtx.jface.viewers.Viewer, + * java.lang.Object, java.lang.Object) + */ + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + + } + + } + + public class MyModel { + public int counter; + + public this(int counter) { + this.counter = counter; + } + + public String toString() { + return Format("Item {}", this.counter ); + } + } + + public class MyLabelProvider : LabelProvider, + ITableLabelProvider { + + public Image getColumnImage(Object element, int columnIndex) { + if (columnIndex == 1) { + return images[(cast(MyModel) element).counter % 4]; + } + + return null; + } + + public String getColumnText(Object element, int columnIndex) { + return Format("Column {} => {}", columnIndex, element.toString()); + } + + } + + private static Image createImage(Display display, int red, int green, + int blue) { + Color color = new Color(display, red, green, blue); + Image image = new Image(display, 10, 10); + GC gc = new GC(image); + gc.setBackground(color); + gc.fillRectangle(0, 0, 10, 10); + gc.dispose(); + + return image; + } + + TableViewer v; + public this(Shell shell) { + v = new TableViewer(shell, DWT.BORDER + | DWT.FULL_SELECTION); + v.setLabelProvider(new MyLabelProvider()); + v.setContentProvider(new MyContentProvider()); + + TableColumn column = new TableColumn(v.getTable(), DWT.NONE); + column.setWidth(200); + column.setText("Column 1"); + + column = new TableColumn(v.getTable(), DWT.NONE); + column.setWidth(200); + column.setText("Column 2"); + + MyModel[] model = createModel(); + v.setInput(new ArrayWrapperObject(arraycast!(Object)(model))); + v.getTable().setLinesVisible(true); + v.getTable().setHeaderVisible(true); + + DefaultToolTip toolTip = new DefaultToolTip(v.getControl(), + ToolTip.NO_RECREATE, false); + toolTip.setText("Hello World\nHello World"); + toolTip.setBackgroundColor(v.getTable().getDisplay().getSystemColor( + DWT.COLOR_RED)); + toolTip.setShift(new Point(10, 5)); + } + + private MyModel[] createModel() { + MyModel[] elements = new MyModel[10]; + + for (int i = 0; i < 10; i++) { + elements[i] = new MyModel(i); + } + + return elements; + } + + /** + * @param args + */ + public static void main(String[] args) { + Display display = new Display(); + + images = new Image[4]; + images[0] = createImage(display, 0, 0, 255); + images[1] = createImage(display, 0, 255, 255); + images[2] = createImage(display, 0, 255, 0); + images[3] = createImage(display, 255, 0, 255); + + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet031TableStaticTooltip(shell); + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + + for (int i = 0; i < images.length; i++) { + images[i].dispose(); + } + + display.dispose(); + + } + +} + +void main(){ + Snippet031TableStaticTooltip.main(null); +} \ No newline at end of file diff -r 161f7698cfb8 -r 7c4b76583cb8 jface/snippets/wizard/Snippet047WizardWithLongRunningOperation.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/wizard/Snippet047WizardWithLongRunningOperation.d Mon Aug 11 11:17:37 2008 +0200 @@ -0,0 +1,285 @@ +/******************************************************************************* + * Copyright (c) 2005, 2007 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Port to the D programming language: + * Frank Benoit + *******************************************************************************/ +module jface.snippets.wizard.Snippet047WizardWithLongRunningOperation; + +import dwtx.jface.viewers.ArrayContentProvider; +import dwtx.jface.viewers.ISelectionChangedListener; +import dwtx.jface.viewers.SelectionChangedEvent; +import dwtx.jface.viewers.TableViewer; +import dwtx.jface.wizard.IWizardPage; +import dwtx.jface.wizard.Wizard; +import dwtx.jface.wizard.WizardDialog; +import dwtx.jface.wizard.WizardPage; +import dwt.DWT; +import dwt.events.SelectionAdapter; +import dwt.events.SelectionEvent; +import dwt.layout.FillLayout; +import dwt.layout.GridData; +import dwt.layout.GridLayout; +import dwt.widgets.Button; +import dwt.widgets.Composite; +import dwt.widgets.Display; +import dwt.widgets.Label; +import dwt.widgets.ProgressBar; +import dwt.widgets.Shell; + +import dwt.dwthelper.utils; +import dwt.dwthelper.Runnable; +import dwtx.dwtxhelper.Collection; +import tango.core.Thread; +import tango.text.convert.Format; +import tango.util.log.Trace; + +import dwtx.jface.operation.ModalContext; + +/** + * Example how to load data from a background thread into a TableViewer + * + * @author Tom Schindl + * @since 1.0 + */ +public class Snippet047WizardWithLongRunningOperation { + + private static class MyWizard : Wizard { + + private int loadingType; + + public this(int loadingType) { + this.loadingType = loadingType; + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.wizard.Wizard#addPages() + */ + public void addPages() { + addPage(new MyWizardPageThread("Thread Page", loadingType)); + addPage(new MyWizardPage("Standard Page")); + } + + public bool performFinish() { + return true; + } + + /* + * (non-Javadoc) + * + * @see dwtx.jface.wizard.Wizard#canFinish() + */ + public bool canFinish() { + IWizardPage[] pages = getPages(); + for (int i = 0; i < pages.length; i++) { + if (!pages[i].isPageComplete()) { + return false; + } + } + + return true; + } + + }; + + private static class MyWizardPage : WizardPage { + + protected this(String pageName) { + super(pageName); + setTitle(pageName); + } + + public /+override+/ void createControl(Composite parent) { + Composite comp = new Composite(parent, DWT.NONE); + setControl(comp); + } + } + + private static class MyWizardPageThread : WizardPage { + private int loadingType; + private bool loading = true; + private TableViewer v; + + protected this(String pageName, int loadingType) { + super(pageName); + this.loadingType = loadingType; + setTitle(pageName); + } + + public /+override+/ void createControl(Composite parent) { + auto mt = new MyThread(); + mt.parent = parent; + + mt.comp = new Composite(parent, DWT.NONE); + mt.comp.setLayout(new GridLayout(1, false)); + + v = new TableViewer(mt.comp, DWT.FULL_SELECTION); + v.setContentProvider(new ArrayContentProvider!(Object)()); + v.getTable().setLayoutData(new GridData(GridData.FILL_BOTH)); + v.addSelectionChangedListener(new class ISelectionChangedListener { + + public void selectionChanged(SelectionChangedEvent event) { + getWizard().getContainer().updateButtons(); + } + + }); + + mt.barContainer = new Composite(mt.comp, DWT.NONE); + mt.barContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + mt.barContainer.setLayout(new GridLayout(2, false)); + + Label l = new Label(mt.barContainer, DWT.NONE); + l.setText("Loading Data"); + + mt.bar = new ProgressBar(mt.barContainer, + (loadingType == 1) ? DWT.INDETERMINATE : DWT.NONE); + mt.bar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + + if (loadingType == 2) { + mt.bar.setMaximum(10); + } + + setControl(mt.comp); + + ModalContext.run( dgRunnable(&mt.threadWork), true, getProgressMonitor(), getShell().getDisplay() ); + + //Thread t = new Thread(&mt.threadWork); + + // t.start(); + } + class MyThread { + private Composite parent; + private Composite barContainer; + private Composite comp; + private ProgressBar bar; + + private void threadWork(){ + if (loadingType == 1) { + try { + Thread.sleep(10.000); + ArrayList ms = new ArrayList(); + for (int i = 0; i < 10; i++) { + ms.add(new MyModel(i)); + } + + if (v.getTable().isDisposed()) { + return; + } + + parent.getDisplay().asyncExec(dgRunnable((ArrayList ms_){ + v.setInput(ms_); + (cast(GridData) barContainer.getLayoutData()).exclude = true; + comp.layout(true); + }, ms )); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + ExceptionPrintStackTrace(e); + } + } else { + ArrayList ms = new ArrayList(); + parent.getDisplay().syncExec( dgRunnable( (ArrayList ms_){ + v.setInput(ms_); + }, ms )); + + for (int i = 0; i < 10; i++) { + int j = i; + if (v.getTable().isDisposed()) { + return; + } + parent.getDisplay().asyncExec( dgRunnable( (int j_){ + MyModel tmp = new MyModel(j_); + v.add(tmp); + ms.add(tmp); + bar.setSelection(j_ + 1); + }, j )); + + try { + Thread.sleep(1.000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + ExceptionPrintStackTrace(e); + } + } + + parent.getDisplay().asyncExec(dgRunnable( { + + (cast(GridData) barContainer.getLayoutData()).exclude = true; + comp.layout(true); + })); + } + + parent.getDisplay().syncExec(dgRunnable( { + loading = false; + getWizard().getContainer().updateButtons(); + })); + } + } + public bool isPageComplete() { + return !loading && !v.getSelection().isEmpty(); + } + + } + + private static class MyModel { + private int index; + + public this(int index) { + this.index = index; + } + + public String toString() { + return Format("Item-{}", index); + } + } + + static Shell shell; + public static void main(String[] args) { + Display display = new Display(); + + shell = new Shell(display); + shell.setLayout(new FillLayout()); + + Button b = new Button(shell, DWT.PUSH); + b.setText("Load in one Chunk"); + b.addSelectionListener(new class SelectionAdapter { + + public void widgetSelected(SelectionEvent e) { + WizardDialog dialog = new WizardDialog(shell, new MyWizard(1)); + dialog.open(); + } + + }); + + b = new Button(shell, DWT.PUSH); + b.setText("Load Item by Item"); + b.addSelectionListener(new class SelectionAdapter { + + public void widgetSelected(SelectionEvent e) { + WizardDialog dialog = new WizardDialog(shell, new MyWizard(2)); + dialog.open(); + } + + }); + + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + + display.dispose(); + } +} + +void main(){ + Snippet047WizardWithLongRunningOperation.main(null); +} diff -r 161f7698cfb8 -r 7c4b76583cb8 res/jface.snippets.linkto_help.gif Binary file res/jface.snippets.linkto_help.gif has changed diff -r 161f7698cfb8 -r 7c4b76583cb8 res/jface.snippets.showerr_tsk.gif Binary file res/jface.snippets.showerr_tsk.gif has changed