diff dwt/custom/TreeEditor.d @ 41:6337764516f1

Sync dwt/custom with dwt-linux (took copy of complete folder)
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 16:29:55 +0200
parents e831403a80a9
children 63a09873578e
line wrap: on
line diff
--- a/dwt/custom/TreeEditor.d	Tue Oct 07 14:41:31 2008 +0200
+++ b/dwt/custom/TreeEditor.d	Tue Oct 07 16:29:55 2008 +0200
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2008 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
@@ -7,14 +7,27 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
  *******************************************************************************/
-module dwt.custom;
+module dwt.custom.TreeEditor;
+
+import dwt.dwthelper.utils;
 
 
-import dwt.*;
-import dwt.events.*;
-import dwt.graphics.*;
-import dwt.widgets.*;
+import dwt.DWT;
+import dwt.events.ControlEvent;
+import dwt.events.ControlListener;
+import dwt.events.TreeEvent;
+import dwt.events.TreeListener;
+import dwt.graphics.Rectangle;
+import dwt.widgets.Control;
+import dwt.widgets.Display;
+import dwt.widgets.Tree;
+import dwt.widgets.TreeColumn;
+import dwt.widgets.TreeItem;
+import dwt.custom.ControlEditor;
+import dwt.dwthelper.Runnable;
 
 /**
 *
@@ -34,30 +47,30 @@
 *           subItem.setText("item " + i + " " + j);
 *       }
 *   }
-*   
+*
 *   final TreeEditor editor = new TreeEditor(tree);
 *   //The editor must have the same size as the cell and must
 *   //not be any smaller than 50 pixels.
 *   editor.horizontalAlignment = DWT.LEFT;
 *   editor.grabHorizontal = true;
 *   editor.minimumWidth = 50;
-*   
+*
 *   tree.addSelectionListener(new SelectionAdapter() {
 *       public void widgetSelected(SelectionEvent e) {
 *           // Clean up any previous editor control
 *           Control oldEditor = editor.getEditor();
 *           if (oldEditor !is null) oldEditor.dispose();
-*   
+*
 *           // Identify the selected row
-*           TreeItem item = cast(TreeItem)e.item;
+*           TreeItem item = (TreeItem)e.item;
 *           if (item is null) return;
-*   
+*
 *           // The control that will be the editor must be a child of the Tree
 *           Text newEditor = new Text(tree, DWT.NONE);
 *           newEditor.setText(item.getText());
 *           newEditor.addModifyListener(new ModifyListener() {
 *               public void modifyText(ModifyEvent e) {
-*                   Text text = cast(Text)editor.getEditor();
+*                   Text text = (Text)editor.getEditor();
 *                   editor.getItem().setText(text.getText());
 *               }
 *           });
@@ -67,16 +80,20 @@
 *       }
 *   });
 * </pre></code>
+*
+* @see <a href="http://www.eclipse.org/swt/snippets/#treeeditor">TreeEditor snippets</a>
+* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 */
-public class TreeEditor : ControlEditor {   
+
+public class TreeEditor : ControlEditor {
     Tree tree;
     TreeItem item;
     int column = 0;
     ControlListener columnListener;
     TreeListener treeListener;
     Runnable timer;
-    static final int TIMEOUT = 1500;
-    
+    static const int TIMEOUT = 1500;
+
 /**
 * Creates a TreeEditor for the specified Tree.
 *
@@ -87,7 +104,7 @@
     super(tree);
     this.tree = tree;
 
-    columnListener = new ControlListener() {
+    columnListener = new class() ControlListener {
         public void controlMoved(ControlEvent e){
             layout();
         }
@@ -95,38 +112,41 @@
             layout();
         }
     };
-    timer = new Runnable () {
+    timer = new class() Runnable {
         public void run() {
             layout ();
         }
     };
-    treeListener = new TreeListener () {
-        final Runnable runnable = new Runnable() {
-            public void run() {
-                if (editor is null || editor.isDisposed()) return;
-                if (TreeEditor.this.tree.isDisposed()) return;
-                layout();
-                editor.setVisible(true);
-            }
-        };
+    treeListener = new class() TreeListener {
+        Runnable runnable;
+        this(){
+            runnable = new class() Runnable {
+                public void run() {
+                    if (this.outer.outer.editor is null || this.outer.outer.editor.isDisposed()) return;
+                    if (this.outer.outer.tree.isDisposed()) return;
+                    layout();
+                    this.outer.outer.editor.setVisible(true);
+                }
+            };
+        }
         public void treeCollapsed(TreeEvent e) {
-            if (editor is null || editor.isDisposed ()) return;
-            editor.setVisible(false);
+            if (this.outer.editor is null || this.outer.editor.isDisposed ()) return;
+            this.outer.editor.setVisible(false);
             e.display.asyncExec(runnable);
         }
         public void treeExpanded(TreeEvent e) {
-            if (editor is null || editor.isDisposed ()) return;
-            editor.setVisible(false);
+            if (this.outer.editor is null || this.outer.editor.isDisposed ()) return;
+            this.outer.editor.setVisible(false);
             e.display.asyncExec(runnable);
         }
     };
     tree.addTreeListener(treeListener);
-    
+
     // To be consistent with older versions of DWT, grabVertical defaults to true
     grabVertical = true;
 }
 
-Rectangle computeBounds () {
+override Rectangle computeBounds () {
     if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
     Rectangle cell = item.getBounds(column);
     Rectangle rect = item.getImageBounds(column);
@@ -142,17 +162,17 @@
 
     if (grabHorizontal) {
         if (tree.getColumnCount() is 0) {
-            // Bounds of tree item only include the text area - stretch out to include 
+            // Bounds of tree item only include the text area - stretch out to include
             // entire client area
             cell.width = area.x + area.width - cell.x;
         }
         editorRect.width = Math.max(cell.width, minimumWidth);
     }
-    
+
     if (grabVertical) {
         editorRect.height = Math.max(cell.height, minimumHeight);
     }
-    
+
     if (horizontalAlignment is DWT.RIGHT) {
         editorRect.x += cell.width - editorRect.width;
     } else if (horizontalAlignment is DWT.LEFT) {
@@ -160,9 +180,9 @@
     } else { // default is CENTER
         editorRect.x += (cell.width - editorRect.width)/2;
     }
-    // don't let the editor overlap with the +/- of the tree
+    // don't let the editor overlap with the + / - of the tree
     editorRect.x = Math.max(cell.x, editorRect.x);
-    
+
     if (verticalAlignment is DWT.BOTTOM) {
         editorRect.y += cell.height - editorRect.height;
     } else if (verticalAlignment is DWT.TOP) {
@@ -177,7 +197,7 @@
  * Removes all associations between the TreeEditor and the row in the tree.  The
  * tree and the editor Control are <b>not</b> disposed.
  */
-public void dispose () {
+public override void dispose () {
     if (tree !is null && !tree.isDisposed()) {
         if (this.column > -1 && this.column < tree.getColumnCount()){
             TreeColumn treeColumn = tree.getColumn(this.column);
@@ -231,7 +251,7 @@
 
 /**
 * Sets the zero based index of the column of the cell being tracked by this editor.
-* 
+*
 * @param column the zero based index of the column of the cell being tracked by this editor
 *
 * @since 3.1
@@ -251,14 +271,19 @@
         this.column = -1;
     }
 
-    if (column < 0  || column >= tree.getColumnCount()) return; 
-        
+    if (column < 0  || column >= tree.getColumnCount()) return;
+
     this.column = column;
     TreeColumn treeColumn = tree.getColumn(this.column);
     treeColumn.addControlListener(columnListener);
     resize();
 }
 
+/**
+* Specifies the <code>TreeItem</code> that is to be edited.
+*
+* @param item the item to be edited
+*/
 public void setItem (TreeItem item) {
     this.item = item;
     resize();
@@ -269,7 +294,7 @@
 *
 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
 * specified in the TreeEditor constructor.
-* 
+*
 * @param editor the Control that is displayed above the cell being edited
 * @param item the TreeItem for the row of the cell being tracked by this editor
 * @param column the zero based index of the column of the cell being tracked by this editor
@@ -281,7 +306,7 @@
     setColumn(column);
     setEditor(editor);
 }
-public void setEditor (Control editor) {
+public override void setEditor (Control editor) {
     super.setEditor(editor);
     resize();
 }
@@ -291,7 +316,7 @@
 *
 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
 * specified in the TreeEditor constructor.
-* 
+*
 * @param editor the Control that is displayed above the cell being edited
 * @param item the TreeItem for the row of the cell being tracked by this editor
 */
@@ -300,12 +325,13 @@
     setEditor(editor);
 }
 
-public void layout () {
+public override void layout () {
     if (tree is null || tree.isDisposed()) return;
-    if (item is null || item.isDisposed()) return;  
+    if (item is null || item.isDisposed()) return;
     int columnCount = tree.getColumnCount();
     if (columnCount is 0 && column !is 0) return;
     if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
     super.layout();
 }
+
 }