# HG changeset patch # User Frank Benoit # Date 1207926500 -7200 # Node ID caaf053c44d6daf173cbdd1712bb0da0dd3bf124 # Parent ffb8196501b3d25f4c725d6fb2f47262a56e6037 more examples diff -r ffb8196501b3 -r caaf053c44d6 dsss.conf --- a/dsss.conf Fri Apr 11 03:01:31 2008 +0200 +++ b/dsss.conf Fri Apr 11 17:08:20 2008 +0200 @@ -79,6 +79,9 @@ version(DwtAddons){ [dwtexamples/sleak/SleakExample.d] [jface/ActionAndStatusbar.d] + [jface/FileTreeViewer.d] + [jface/ShowPrefs.d] + [jface/ShowFieldPrefs.d] } version(Derelict){ diff -r ffb8196501b3 -r caaf053c44d6 jface/FileTreeViewer.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/FileTreeViewer.d Fri Apr 11 17:08:20 2008 +0200 @@ -0,0 +1,226 @@ +module jface.FileTreeViewer; + +import dwt.DWT; +import dwt.widgets.Label; +import dwt.widgets.Control; +import dwt.widgets.Composite; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Button; + +import dwt.events.SelectionAdapter; +import dwt.events.SelectionEvent; + +import dwt.graphics.Image; +import dwt.graphics.ImageData; + +import dwt.layout.GridLayout; +import dwt.layout.GridData; + +import dwtx.jface.window.ApplicationWindow; + +import dwtx.jface.viewers.Viewer; +import dwtx.jface.viewers.TreeViewer; +import dwtx.jface.viewers.ITreeContentProvider; +import dwtx.jface.viewers.ILabelProvider; +import dwtx.jface.viewers.ILabelProviderListener; +import dwtx.jface.viewers.LabelProviderChangedEvent; + +import jive.stacktrace; + +import dwt.dwthelper.utils; +import dwt.dwthelper.ByteArrayInputStream; + +//------------------------------------ +//import dwt.dwthelper.utils; +//------------------------------------ + +import tango.io.FileRoots; +import tango.io.FilePath; + +import tango.util.collection.model.Seq; +import tango.util.collection.ArraySeq; + + +void main(){ + auto hw = new FileTree; + hw.run(); +} + +class FileTree : ApplicationWindow { + + TreeViewer tv; + + this(){ + super(null); + } + + public void run(){ + setBlockOnOpen(true); + open(); + Display.getCurrent().dispose(); + } + + protected void configureShell( Shell shell ){ + super.configureShell(shell); + shell.setText( "File Tree" ); + shell.setSize( 400, 400 ); + } + + protected Control createContents(Composite parent){ +/+ Label label = new Label( parent, DWT.CENTER ); + label.setText( "Hello, World" ); + return label;+/ + + auto composite = new Composite( parent, DWT.NONE ); + composite.setLayout( new GridLayout(1,false)); + + // Add a checkbox to toggle whether the labels preserve case + auto preserveCase = new Button( composite, DWT.CHECK ); + preserveCase.setText( "&Preserve case" ); + + // Create the tree viewer to display the file tree + tv = new TreeViewer( composite ); + tv.getTree().setLayoutData( new GridData( GridData.FILL_BOTH )); + tv.setContentProvider( new FileTreeContentProvider()); + tv.setLabelProvider( new FileTreeLabelProvider() ); + tv.setInput( stringcast("root") ); + + // When user checks the checkbox, toggle the preserve case attribute + // of the label provider + preserveCase.addSelectionListener( new class SelectionAdapter{ + public void widgetSelected( SelectionEvent event ){ + auto preserveCase = (cast(Button)event.widget).getSelection(); + auto ftlp = cast(FileTreeLabelProvider) tv.getLabelProvider(); + ftlp.setPreserveCase(preserveCase); + } + }); + return composite; + } +} + +class FileTreeContentProvider : ITreeContentProvider { + public override Object[] getChildren( Object arg0 ){ + Trace.formatln( "trc line={} ", __LINE__ ); + try{ + auto fp = cast(FilePath)arg0; + Object[] res; + if( !fp.isFolder()) { + return null; + } + foreach( item; fp ){ + res ~= FilePath.from( item ); + } + + Trace.formatln( "trc line={} ", __LINE__ ); + return res; + } + catch( Exception e ){ + ExceptionPrintStackTrace(e); + return null; + } + } + + public override Object getParent(Object arg0 ){ + Trace.formatln( "trc line={} ", __LINE__ ); + auto fp = cast(FilePath)arg0; + return fp.pop; + } + + public override bool hasChildren(Object arg0 ){ + Trace.formatln( "trc line={} ", __LINE__ ); + auto obj = getChildren(arg0); + return obj is null ? false : obj.length > 0; + } + + public override Object[] getElements( Object arg0 ){ + Trace.formatln( "trc line={} ", __LINE__ ); + Object[] res; + res ~= new FilePath( "/" ); +// foreach( root; FileRoots.list()){ +// res ~= new FilePath( root ); +// } + return res; + } + + public override void dispose(){ + } + + public override void inputChanged(Viewer arg0, Object arg1, Object arg2 ){ + } + +} + +class FileTreeLabelProvider : ILabelProvider { + + private Seq!(ILabelProviderListener) listeners; + + private Image file; + private Image dir; + + private bool preserveCase; + + public this(){ + Trace.formatln( "trc line={} Label", __LINE__ ); + listeners = new ArraySeq!(ILabelProviderListener); + + file = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "file.png" )))); + dir = new Image( null, new ImageData( new ByteArrayInputStream( cast(byte[])import( "folder.png" )))); + } + + public void setPreserveCase(bool preserveCase){ + Trace.formatln( "trc line={} Label", __LINE__ ); + this.preserveCase = preserveCase; + auto event = new LabelProviderChangedEvent(this); + for( int i = 0, n = listeners.size(); i < n; i++ ){ + auto ilpl = listeners.get(i); + ilpl.labelProviderChanged(event); + } + } + + public override Image getImage(Object arg0){ + Trace.formatln( "trc line={} Label", __LINE__ ); + return (cast(FilePath)arg0).isFolder() ? dir : file; + } + + public override char[] getText(Object arg0){ + auto text = (cast(FilePath)arg0).name(); +// if( text.length is 0 ){ +// text = (cast(FilePath)arg0).pop.name(); +// } + Trace.formatln( "name={} ", text ); + Trace.formatln( "trc line={} Label", __LINE__ ); + return "x" ~ (preserveCase ? text : text.toUpperCase()); + } + + public void addListener( ILabelProviderListener arg0 ){ + listeners.append(arg0); + } + + public void dispose(){ + if( dir !is null ) dir.dispose(); + if( file !is null ) file.dispose(); + } + + public bool isLabelProperty(Object arg0, char[] arg1){ + return false; + } + + public void removeListener(ILabelProviderListener arg0){ + listeners.remove(arg0); + } +} + + + + + + + + + + + + + + diff -r ffb8196501b3 -r caaf053c44d6 jface/ShowFieldPrefs.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/ShowFieldPrefs.d Fri Apr 11 17:08:20 2008 +0200 @@ -0,0 +1,202 @@ +module jface.PreferenceDlgTest; + +import dwtx.jface.preference.BooleanFieldEditor; +import dwtx.jface.preference.ColorFieldEditor; +import dwtx.jface.preference.DirectoryFieldEditor; +import dwtx.jface.preference.FileFieldEditor; +import dwtx.jface.preference.FontFieldEditor; +import dwtx.jface.preference.FieldEditorPreferencePage; +import dwtx.jface.preference.RadioGroupFieldEditor; +import dwtx.jface.preference.PathEditor; +import dwtx.jface.preference.IntegerFieldEditor; +import dwtx.jface.preference.ScaleFieldEditor; +import dwtx.jface.preference.StringFieldEditor; +import dwtx.jface.preference.IPreferenceStore; +import dwtx.jface.preference.PreferenceManager; +import dwtx.jface.preference.PreferencePage; +import dwtx.jface.preference.PreferenceNode; +import dwtx.jface.preference.PreferenceStore; +import dwtx.jface.preference.PreferenceDialog; +import dwtx.jface.resource.ImageDescriptor; + +import dwt.widgets.Display; +import dwt.widgets.Composite; +import dwt.widgets.Label; +import dwt.widgets.Button; +import dwt.widgets.Text; +import dwt.widgets.Control; +import dwt.events.SelectionAdapter; +import dwt.events.SelectionEvent; +import dwt.layout.RowLayout; +import dwt.layout.GridLayout; +import dwt.layout.GridData; +import dwt.DWT; +import dwt.dwthelper.utils; + +import tango.io.File; +import tango.io.FilePath; + +version(JIVE) import jive.stacktrace; + +const char[] FILENAME = "showfieldprefs"; + +/** + * This class demonstrates field editors + */ +public class FieldEditorPageOne : FieldEditorPreferencePage { + public this() { + // Use the "flat" layout + super(FLAT); + } + + /** + * Creates the field editors + */ + protected void createFieldEditors() { + // Add a bool field + BooleanFieldEditor bfe = new BooleanFieldEditor("myBoolean", "Boolean", + getFieldEditorParent()); + addField(bfe); + + // Add a color field + ColorFieldEditor cfe = new ColorFieldEditor("myColor", "Color:", + getFieldEditorParent()); + addField(cfe); + + // Add a directory field + DirectoryFieldEditor dfe = new DirectoryFieldEditor("myDirectory", + "Directory:", getFieldEditorParent()); + addField(dfe); + + // Add a file field + FileFieldEditor ffe = new FileFieldEditor("myFile", "File:", + getFieldEditorParent()); + addField(ffe); + + // Add a font field + FontFieldEditor fontFe = new FontFieldEditor("myFont", "Font:", + getFieldEditorParent()); + addField(fontFe); + + // Add a radio group field + RadioGroupFieldEditor rfe = new RadioGroupFieldEditor("myRadioGroup", + "Radio Group", 2, [ [ "First Value", "first"], + [ "Second Value", "second"], [ "Third Value", "third"], + [ "Fourth Value", "fourth"]], getFieldEditorParent(), true); + addField(rfe); + + // Add a path field + PathEditor pe = new PathEditor("myPath", "Path:", "Choose a Path", + getFieldEditorParent()); + addField(pe); + } +} + + +/** + * This class demonstrates field editors + */ +public class FieldEditorPageTwo : FieldEditorPreferencePage { + public this() { + // Use the "grid" layout + super(GRID); + } + + /** + * Creates the field editors + */ + protected void createFieldEditors() { + // Add an integer field + IntegerFieldEditor ife = new IntegerFieldEditor("myInt", "Int:", + getFieldEditorParent()); + addField(ife); + + // Add a scale field + ScaleFieldEditor sfe = new ScaleFieldEditor("myScale", "Scale:", + getFieldEditorParent(), 0, 100, 1, 10); + addField(sfe); + + // Add a string field + StringFieldEditor stringFe = new StringFieldEditor("myString", "String:", + getFieldEditorParent()); + addField(stringFe); + } +} + + +/** + * This class demonstrates JFace preferences and field editors + */ +public class ShowFieldPrefs { + /** + * Runs the application + */ + public void run() { +// Display display = new Display(); + + // Create the preference manager + PreferenceManager mgr = new PreferenceManager(); + + // Create the nodes + PreferenceNode one = new PreferenceNode("one", "One", null, + FieldEditorPageOne.classinfo.name ); + PreferenceNode two = new PreferenceNode("two", "Two", null, + FieldEditorPageTwo.classinfo.name ); + + // Add the nodes + mgr.addToRoot(one); + mgr.addToRoot(two); + + // Create the preferences dialog + PreferenceDialog dlg = new PreferenceDialog(null, mgr); + + // Set the preference store + PreferenceStore ps = new PreferenceStore(FILENAME); + try { + ps.load(); + } catch (IOException e) { + // Ignore + } + dlg.setPreferenceStore(ps); + + // Open the dialog + dlg.open(); + + try { + // Save the preferences + ps.save(); + } catch (IOException e) { + ExceptionPrintStackTrace(e); + } +// display.dispose(); + } + + /** + * The application entry point + * + * @param args the command line arguments + */ + public static void main(String[] args) { + (new ShowFieldPrefs()).run(); + } +} + + +void main(){ + scope fn = new FilePath( FILENAME ); + if( !fn.exists ){ + scope prefs = new File( fn ); + version(linux){ + prefs.write( import("jface.showfieldprefs.properties.linux" )); + } + version(Windows){ + prefs.write( import("jface.showfieldprefs.properties.win" )); + } + } + ShowFieldPrefs.main( null ); +} + + + + + diff -r ffb8196501b3 -r caaf053c44d6 jface/ShowPrefs.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jface/ShowPrefs.d Fri Apr 11 17:08:20 2008 +0200 @@ -0,0 +1,321 @@ +module jface.ShowPrefs; + +import dwtx.jface.preference.BooleanFieldEditor; +import dwtx.jface.preference.ColorFieldEditor; +import dwtx.jface.preference.DirectoryFieldEditor; +import dwtx.jface.preference.FileFieldEditor; +import dwtx.jface.preference.FontFieldEditor; +import dwtx.jface.preference.FieldEditorPreferencePage; +import dwtx.jface.preference.RadioGroupFieldEditor; +import dwtx.jface.preference.PathEditor; +import dwtx.jface.preference.IntegerFieldEditor; +import dwtx.jface.preference.ScaleFieldEditor; +import dwtx.jface.preference.StringFieldEditor; +import dwtx.jface.preference.IPreferenceStore; +import dwtx.jface.preference.PreferenceManager; +import dwtx.jface.preference.PreferencePage; +import dwtx.jface.preference.PreferenceNode; +import dwtx.jface.preference.PreferenceStore; +import dwtx.jface.preference.PreferenceDialog; +import dwtx.jface.resource.ImageDescriptor; + +import dwt.widgets.Display; +import dwt.widgets.Composite; +import dwt.widgets.Label; +import dwt.widgets.Button; +import dwt.widgets.Text; +import dwt.widgets.Control; +import dwt.events.SelectionAdapter; +import dwt.events.SelectionEvent; +import dwt.layout.RowLayout; +import dwt.layout.GridLayout; +import dwt.layout.GridData; +import dwt.DWT; +import dwt.dwthelper.utils; + +import tango.io.File; +import tango.io.FilePath; + +version(JIVE) import jive.stacktrace; + +const char[] FILENAME = "showprefs"; + +/** + * This class creates a preference page + */ +public class PrefPageOne : PreferencePage { + // Names for preferences + private static final String ONE = "one.one"; + private static final String TWO = "one.two"; + private static final String THREE = "one.three"; + + // Text fields for user to enter preferences + private Text fieldOne; + private Text fieldTwo; + private Text fieldThree; + + /** + * Creates the controls for this page + */ + protected Control createContents(Composite parent) { + Composite composite = new Composite(parent, DWT.NONE); + composite.setLayout(new GridLayout(2, false)); + + // Get the preference store + IPreferenceStore preferenceStore = getPreferenceStore(); + + // Create three text fields. + // Set the text in each from the preference store + (new Label(composite, DWT.LEFT)).setText("Field One:"); + fieldOne = new Text(composite, DWT.BORDER); + fieldOne.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + fieldOne.setText(preferenceStore.getString(ONE)); + + (new Label(composite, DWT.LEFT)).setText("Field Two:"); + fieldTwo = new Text(composite, DWT.BORDER); + fieldTwo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + fieldTwo.setText(preferenceStore.getString(TWO)); + + (new Label(composite, DWT.LEFT)).setText("Field Three:"); + fieldThree = new Text(composite, DWT.BORDER); + fieldThree.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + fieldThree.setText(preferenceStore.getString(THREE)); + + return composite; + } + + /** + * Called when user clicks Restore Defaults + */ + protected void performDefaults() { + // Get the preference store + IPreferenceStore preferenceStore = getPreferenceStore(); + + // Reset the fields to the defaults + fieldOne.setText(preferenceStore.getDefaultString(ONE)); + fieldTwo.setText(preferenceStore.getDefaultString(TWO)); + fieldThree.setText(preferenceStore.getDefaultString(THREE)); + } + + /** + * Called when user clicks Apply or OK + * + * @return bool + */ + public bool performOk() { + // Get the preference store + IPreferenceStore preferenceStore = getPreferenceStore(); + + // Set the values from the fields + if (fieldOne !is null) preferenceStore.setValue(ONE, fieldOne.getText()); + if (fieldTwo !is null) preferenceStore.setValue(TWO, fieldTwo.getText()); + if (fieldThree !is null) + preferenceStore.setValue(THREE, fieldThree.getText()); + + // Return true to allow dialog to close + return true; + } +} + + +/** + * This class creates a preference page + */ +public class PrefPageTwo : PreferencePage { + // Names for preferences + private static final String ONE = "two.one"; + private static final String TWO = "two.two"; + private static final String THREE = "two.three"; + + // The checkboxes + private Button checkOne; + private Button checkTwo; + private Button checkThree; + + /** + * PrefPageTwo constructor + */ + public this() { + super("Two"); + setDescription("Check the checks"); + } + + /** + * Creates the controls for this page + */ + protected Control createContents(Composite parent) { + Composite composite = new Composite(parent, DWT.NONE); + composite.setLayout(new RowLayout(DWT.VERTICAL)); + + // Get the preference store + IPreferenceStore preferenceStore = getPreferenceStore(); + + // Create three checkboxes + checkOne = new Button(composite, DWT.CHECK); + checkOne.setText("Check One"); + checkOne.setSelection(preferenceStore.getBoolean(ONE)); + + checkTwo = new Button(composite, DWT.CHECK); + checkTwo.setText("Check Two"); + checkTwo.setSelection(preferenceStore.getBoolean(TWO)); + + checkThree = new Button(composite, DWT.CHECK); + checkThree.setText("Check Three"); + checkThree.setSelection(preferenceStore.getBoolean(THREE)); + + return composite; + } + + /** + * Add buttons + * + * @param parent the parent composite + */ + protected void contributeButtons(Composite parent) { + // Add a select all button + Button selectAll = new Button(parent, DWT.PUSH); + selectAll.setText("Select All"); + selectAll.addSelectionListener(new class SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + checkOne.setSelection(true); + checkTwo.setSelection(true); + checkThree.setSelection(true); + } + }); + + // Add a select all button + Button clearAll = new Button(parent, DWT.PUSH); + clearAll.setText("Clear All"); + clearAll.addSelectionListener(new class SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + checkOne.setSelection(false); + checkTwo.setSelection(false); + checkThree.setSelection(false); + } + }); + + // Add two columns to the parent's layout + (cast(GridLayout) parent.getLayout()).numColumns += 2; + } + + /** + * Change the description label + */ + protected Label createDescriptionLabel(Composite parent) { + Label label = null; + String description = getDescription(); + if (description != null) { + // Upper case the description + description = description.toUpperCase(); + + // Right-align the label + label = new Label(parent, DWT.RIGHT); + label.setText(description); + } + return label; + } + + /** + * Called when user clicks Restore Defaults + */ + protected void performDefaults() { + // Get the preference store + IPreferenceStore preferenceStore = getPreferenceStore(); + + // Reset the fields to the defaults + checkOne.setSelection(preferenceStore.getDefaultBoolean(ONE)); + checkTwo.setSelection(preferenceStore.getDefaultBoolean(TWO)); + checkThree.setSelection(preferenceStore.getDefaultBoolean(THREE)); + } + + /** + * Called when user clicks Apply or OK + * + * @return bool + */ + public bool performOk() { + // Get the preference store + IPreferenceStore preferenceStore = getPreferenceStore(); + + // Set the values from the fields + if (checkOne !is null) preferenceStore.setValue(ONE, checkOne.getSelection()); + if (checkTwo !is null) preferenceStore.setValue(TWO, checkTwo.getSelection()); + if (checkThree !is null) + preferenceStore.setValue(THREE, checkThree.getSelection()); + + // Return true to allow dialog to close + return true; + } +} + + + +/** + * This class demonstrates JFace preferences + */ +public class ShowPrefs { + /** + * Runs the application + */ + public void run() { +// Display display = new Display(); + + // Create the preference manager + PreferenceManager mgr = new PreferenceManager(); + + // Create the nodes + PreferenceNode one = new PreferenceNode("one", "One", ImageDescriptor + .createFromFile(getImportData!("cancel.gif")), PrefPageOne.classinfo.name ); + PreferenceNode two = new PreferenceNode("two", new PrefPageTwo()); + + // Add the nodes + mgr.addToRoot(one); + mgr.addTo(one.getId(), two); + + // Create the preferences dialog + PreferenceDialog dlg = new PreferenceDialog(null, mgr); + + // Set the preference store + PreferenceStore ps = new PreferenceStore( FILENAME ); + try { + ps.load(); + } catch (IOException e) { + // Ignore + } + dlg.setPreferenceStore(ps); + + // Open the dialog + dlg.open(); + + try { + // Save the preferences + ps.save(); + } catch (IOException e) { + ExceptionPrintStackTrace(e); + } +// display.dispose(); + } + + /** + * The application entry point + * + * @param args the command line arguments + */ + public static void main(String[] args) { + (new ShowPrefs()).run(); + } +} + +void main(){ + scope fn = new FilePath( FILENAME ); + if( !fn.exists ){ + scope prefs = new File( fn ); + prefs.write( import("jface.showprefs.properties" )); + } + ShowPrefs.main( null ); +} + + + + + diff -r ffb8196501b3 -r caaf053c44d6 res/jface.showfieldprefs.properties.linux --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/res/jface.showfieldprefs.properties.linux Fri Apr 11 17:08:20 2008 +0200 @@ -0,0 +1,10 @@ +#Sat Feb 28 16:06:57 GMT-05:00 2004 +myPath=~/:/: +myRadioGroup= +myScale=0 +myColor=0,128,0 +myFont=1|Terminal|8|0|WINDOWS|1|-13|0|0|0|400|0|0|0|-1|1|2|1|49|Terminal; +myFile=./showfieldprefs +myString= +myBoolean=true +myDirectory=/tmp diff -r ffb8196501b3 -r caaf053c44d6 res/jface.showfieldprefs.properties.win --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/res/jface.showfieldprefs.properties.win Fri Apr 11 17:08:20 2008 +0200 @@ -0,0 +1,10 @@ +#Sat Feb 28 16:06:57 GMT-05:00 2004 +myPath=C\:\\Documents and Settings\\Owner\\My Documents;C\:\\; +myRadioGroup= +myScale=0 +myColor=0,128,0 +myFont=1|Terminal|8|0|WINDOWS|1|-13|0|0|0|400|0|0|0|-1|1|2|1|49|Terminal; +myFile=.\\0249f1701.bmp +myString= +myBoolean=true +myDirectory=C\:\\Documents and Settings\\Owner\\My Documents diff -r ffb8196501b3 -r caaf053c44d6 res/jface.showprefs.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/res/jface.showprefs.properties Fri Apr 11 17:08:20 2008 +0200 @@ -0,0 +1,5 @@ +#Fri Feb 27 02:14:51 GMT-05:00 2004 +two.two=false +two.one=false +two.three=false +one.one=Book