# HG changeset patch # User Bill Baxter # Date 1211208168 -32400 # Node ID cbba80cceb7ac6a3a15dc8d8cb67065382578677 # Parent 9ed020f0c2a5e8b20929ed68e39c355b69f3370b Port of jface Snippets 1,2,4,5,7 diff -r 9ed020f0c2a5 -r cbba80cceb7a jface/dsss.conf --- a/jface/dsss.conf Mon May 19 14:30:49 2008 +0200 +++ b/jface/dsss.conf Mon May 19 23:42:48 2008 +0900 @@ -21,7 +21,11 @@ [ActionAndStatusbar.d] [ShowFieldPrefs.d] [ShowPrefs.d] +[snippets/Snippet001TableViewer.d] +[snippets/Snippet002TreeViewer.d] +[snippets/Snippet004HideSelection.d] +[snippets/Snippet005TreeCustomMenu.d] [snippets/Snippet006TableMultiLineCells.d] +[snippets/Snippet007FullSelection.d] [snippets/Snippet040TableViewerSorting.d] - diff -r 9ed020f0c2a5 -r cbba80cceb7a jface/snippets/Snippet001TableViewer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/Snippet001TableViewer.d Mon May 19 23:42:48 2008 +0900 @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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 + * Port to the D programming language: + * wbaxter at gmail dot com + *******************************************************************************/ + +module jface.snippets.Snippet001TableViewer; + +import dwtx.jface.viewers.IStructuredContentProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.TableViewer; +import dwtx.jface.viewers.Viewer; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.DWT; + +import dwt.dwthelper.utils; + +import tango.util.Convert; +import tango.util.collection.ArraySeq; + +/** + * A simple TableViewer to demonstrate usage + * + * @author Tom Schindl + * + */ +public class Snippet001TableViewer { + private class MyContentProvider : IStructuredContentProvider { + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + public Object[] getElements(Object inputElement) { + return (cast(ArraySeq!(MyModel))inputElement).toArray; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.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 "Item " ~ to!(char[])(this.counter); + } + } + + public this(Shell shell) { + final TableViewer v = new TableViewer(shell); + v.setLabelProvider(new LabelProvider()); + v.setContentProvider(new MyContentProvider()); + ArraySeq!(MyModel) model = createModel(); + v.setInput(model); + v.getTable().setLinesVisible(true); + } + + private ArraySeq!(MyModel) createModel() { + ArraySeq!(MyModel) elements = new ArraySeq!(MyModel); + elements.capacity = 10; + + for( int i = 0; i < 10; i++ ) { + elements ~= new MyModel(i); + } + + return elements; + } + +} + +void main() +{ + Display display = new Display (); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet001TableViewer(shell); + shell.open (); + + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + + display.dispose (); + +} + diff -r 9ed020f0c2a5 -r cbba80cceb7a jface/snippets/Snippet002TreeViewer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/Snippet002TreeViewer.d Mon May 19 23:42:48 2008 +0900 @@ -0,0 +1,146 @@ +/******************************************************************************* + * 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 + * Port to the D programming language: + * wbaxter at gmail dot com + *******************************************************************************/ + +module jface.snippets.Snippet002TreeViewer; + +import dwt.DWT; +import dwtx.jface.viewers.ITreeContentProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.TreeViewer; +import dwtx.jface.viewers.Viewer; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; + +import dwt.dwthelper.utils; + +import tango.util.Convert; + +/** + * A simple TreeViewer to demonstrate usage + * + * @author Tom Schindl + * + */ +class Snippet002TreeViewer { + private class MyContentProvider : ITreeContentProvider { + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + public Object[] getElements(Object inputElement) { + return (cast(MyModel)inputElement).child/*.dup*/; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) + */ + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) + */ + public Object[] getChildren(Object parentElement) { + return getElements(parentElement); + } + + /* (non-Javadoc) + * @see org.eclipse.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 org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) + */ + public bool hasChildren(Object element) { + return (cast(MyModel)element).child.length > 0; + } + + } + + public class MyModel { + public MyModel parent; + public MyModel[] child; + public int counter; + + public this(int counter, MyModel parent) { + this.parent = parent; + this.counter = counter; + } + + public String toString() { + String rv = "Item "; + if( parent !is null ) { + rv = parent.toString() ~ "."; + } + + rv ~= to!(char[])(counter); + + return rv; + } + } + + public this(Shell shell) { + TreeViewer v = new TreeViewer(shell); + v.setLabelProvider(new LabelProvider()); + v.setContentProvider(new MyContentProvider()); + v.setInput(createModel()); + } + + 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 ~= tmp; + for( int j = 1; j < i; j++ ) { + tmp.child ~= new MyModel(j,tmp); + } + } + + return root; + } + +} + + +void main() { + Display display = new Display (); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet002TreeViewer(shell); + shell.open (); + + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + + display.dispose (); +} diff -r 9ed020f0c2a5 -r cbba80cceb7a jface/snippets/Snippet004HideSelection.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/Snippet004HideSelection.d Mon May 19 23:42:48 2008 +0900 @@ -0,0 +1,129 @@ +/******************************************************************************* + * 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 + * Port to the D programming language: + * wbaxter at gmail dot com + *******************************************************************************/ + +module jface.snippets.Snippet004HideSelection; + +import dwtx.jface.viewers.IStructuredContentProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.StructuredSelection; +import dwtx.jface.viewers.TableViewer; +import dwtx.jface.viewers.Viewer; +import dwt.DWT; +import dwt.events.MouseAdapter; +import dwt.events.MouseEvent; +import dwt.graphics.Point; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; + +import dwt.dwthelper.utils; + +import tango.util.Convert; +import tango.util.collection.ArraySeq; + +/** + * Snippet that hides the selection when nothing is selected. + * + * @author Tom Schindl + * + */ +public class Snippet004HideSelection { + alias ArraySeq!(MyModel) MyModelArray; + private class MyContentProvider : IStructuredContentProvider { + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + public Object[] getElements(Object inputElement) { + return (cast(MyModelArray)inputElement).toArray; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.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 "Item " ~ to!(char[])(this.counter); + } + } + + public this(Shell shell) { + final TableViewer v = new TableViewer(shell,DWT.BORDER|DWT.FULL_SELECTION); + v.setLabelProvider(new LabelProvider()); + v.setContentProvider(new MyContentProvider()); + MyModelArray model = createModel(); + v.setInput(model); + v.getTable().setLinesVisible(true); + v.getTable().addMouseListener(new class(v) MouseAdapter { + private TableViewer v; + this(TableViewer v_) { + this.v = v_; + } + + /* (non-Javadoc) + * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent) + */ + public void mouseDown(MouseEvent e) { + if( v.getTable().getItem(new Point(e.x,e.y)) is null ) { + v.setSelection(new StructuredSelection()); + } + } + + }); + } + + private MyModelArray createModel() { + MyModelArray elements = new MyModelArray; + elements.capacity = 10; + for( int i = 0; i < 10; i++ ) { + elements ~= new MyModel(i); + } + + return elements; + } + +} + +static void main() { + Display display = new Display (); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet004HideSelection(shell); + shell.open (); + + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + + display.dispose (); + +} + diff -r 9ed020f0c2a5 -r cbba80cceb7a jface/snippets/Snippet005TreeCustomMenu.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/Snippet005TreeCustomMenu.d Mon May 19 23:42:48 2008 +0900 @@ -0,0 +1,183 @@ +/******************************************************************************* + * 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 + * Port to the D programming language: + * wbaxter at gmail dot com + *******************************************************************************/ + +module jface.snippets.Snippet005TreeCustomMenu; + +import dwtx.jface.action.Action; +import dwtx.jface.action.IMenuListener; +import dwtx.jface.action.IMenuManager; +import dwtx.jface.action.MenuManager; +import dwtx.jface.viewers.IStructuredSelection; +import dwtx.jface.viewers.ITreeContentProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.TreeViewer; +import dwtx.jface.viewers.Viewer; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.DWT; + +import dwt.dwthelper.utils; + +import tango.util.Convert; +import tango.io.Stdout; + +/** + * Customized context menu based on TreeItem-Selection + * + * @author Tom Schindl + * + */ +public class Snippet005TreeCustomMenu { + private class MyContentProvider : ITreeContentProvider { + + public Object[] getElements(Object inputElement) { + return (cast(MyModel) inputElement).child; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) + */ + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) + */ + public Object[] getChildren(Object parentElement) { + return getElements(parentElement); + } + + /* (non-Javadoc) + * @see org.eclipse.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 org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) + */ + public bool hasChildren(Object element) { + return (cast(MyModel) element).child.length > 0; + } + + } + + public class MyModel { + public MyModel parent; + + public MyModel[] child; + + public int counter; + + public this(int counter, MyModel parent) { + this.parent = parent; + this.counter = counter; + } + + public String toString() { + String rv = "Item "; + if (parent !is null) { + rv = parent.toString() ~ "."; + } + + rv ~= to!(char[])(counter); + + return rv; + } + } + + public this(Shell shell) { + final TreeViewer v = new TreeViewer(shell); + v.setLabelProvider(new LabelProvider()); + v.setContentProvider(new MyContentProvider()); + v.setInput(createModel()); + + final Action a = new class Action {}; + final MenuManager mgr = new MenuManager(); + mgr.setRemoveAllWhenShown(true); + + class MyMenuListener : IMenuListener { + TreeViewer v; + Action a; + MenuManager mgr; + + this(TreeViewer v_, Action a_, MenuManager mgr_) { + this.v = v_; this.a = a_; this.mgr = mgr_; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager) + */ + public void menuAboutToShow(IMenuManager manager) { + IStructuredSelection selection = cast(IStructuredSelection) v + .getSelection(); + if (!selection.isEmpty()) { + a.setText("Action for " + ~ (cast(MyModel) selection.getFirstElement()) + .toString()); + mgr.add(a); + } + } + } + + mgr.addMenuListener(new MyMenuListener(v,a,mgr)); + v.getControl().setMenu(mgr.createContextMenu(v.getControl())); + } + + 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 ~= tmp; + for (int j = 1; j < i; j++) { + tmp.child ~= new MyModel(j, tmp); + } + } + + return root; + } + +} + +void main() { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet005TreeCustomMenu(shell); + shell.open(); + + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + + display.dispose(); +} diff -r 9ed020f0c2a5 -r cbba80cceb7a jface/snippets/Snippet007FullSelection.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/snippets/Snippet007FullSelection.d Mon May 19 23:42:48 2008 +0900 @@ -0,0 +1,165 @@ +/******************************************************************************* + * 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 jface.snippets.Snippet007FullSelection; + +import dwtx.jface.viewers.CellEditor; +import dwtx.jface.viewers.ICellModifier; +import dwtx.jface.viewers.IStructuredContentProvider; +import dwtx.jface.viewers.LabelProvider; +import dwtx.jface.viewers.TableViewer; +import dwtx.jface.viewers.TextCellEditor; +import dwtx.jface.viewers.Viewer; +import dwt.DWT; +import dwt.graphics.Color; +import dwt.graphics.GC; +import dwt.graphics.Point; +import dwt.graphics.Rectangle; +import dwt.layout.FillLayout; +import dwt.widgets.Display; +import dwt.widgets.Event; +import dwt.widgets.Listener; +import dwt.widgets.Shell; +import dwt.widgets.TableColumn; +import dwt.widgets.TableItem; + +import dwt.dwthelper.utils; + +import tango.util.Convert; +import tango.util.collection.ArraySeq; + +/** + * TableViewer: Hide full selection + * + * @author Tom Schindl + * + */ +public class Snippet007FullSelection { + alias ArraySeq!(MyModel) MyModelArray; + + private class MyContentProvider : IStructuredContentProvider { + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + public Object[] getElements(Object inputElement) { + return (cast(MyModelArray)inputElement).toArray; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + public void dispose() { + + } + + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.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 "Item " ~ to!(char[])(this.counter); + } + } + + public this(Shell shell) { + final TableViewer v = new TableViewer(shell,DWT.BORDER|DWT.FULL_SELECTION); + v.setLabelProvider(new LabelProvider()); + v.setContentProvider(new MyContentProvider()); + v.setCellModifier(new class(v) ICellModifier { + TableViewer v; + this(TableViewer v_) { this.v=v_; } + + public bool canModify(Object element, String property) { + return true; + } + + public Object getValue(Object element, String property) { + return new ArrayWrapperString( to!(char[])((cast(MyModel)element).counter) ); + } + + public void modify(Object element, String property, Object value) { + auto item = cast(TableItem)element; + auto valuestr = cast(ArrayWrapperString)value; + (cast(MyModel)item.getData()).counter = to!(int)(valuestr.array); + v.update(item.getData(), null); + } + + }); + v.setColumnProperties(["column1", "column2" ]); + v.setCellEditors([ new TextCellEditor(v.getTable()),new TextCellEditor(v.getTable()) ]); + + TableColumn column = new TableColumn(v.getTable(),DWT.NONE); + column.setWidth(100); + column.setText("Column 1"); + + column = new TableColumn(v.getTable(),DWT.NONE); + column.setWidth(100); + column.setText("Column 2"); + + MyModelArray model = createModel(); + v.setInput(model); + v.getTable().setLinesVisible(true); + v.getTable().setHeaderVisible(true); + + v.getTable().addListener(DWT.EraseItem, new class Listener { + + /* (non-Javadoc) + * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event) + */ + public void handleEvent(Event event) { + event.detail &= ~DWT.SELECTED; + } + }); + + } + + private MyModelArray createModel() { + auto elements = new MyModelArray; + elements.capacity = 10; + + for( int i = 0; i < 10; i++ ) { + elements ~= new MyModel(i); + } + + return elements; + } + +} + + +void main() { + Display display = new Display (); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + new Snippet007FullSelection(shell); + shell.open (); + + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + + display.dispose (); + +} +