Add Tree cell editor : Tree Editor « SWT « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » SWT » Tree Editor 
17. 59. 7. Add Tree cell editor
Add Tree cell editor
/*******************************************************************************
 * Copyright (c) 2000, 2004 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
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * TreeEditor example snippet: edit the text of a tree item (in place, fancy)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class TreeCellEditor {

  public static void main(String[] args) {
    final Display display = new Display();
    final Color black = display.getSystemColor(SWT.COLOR_BLACK);
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER);
    for (int i = 0; i < 16; i++) {
      TreeItem itemI = new TreeItem(tree, SWT.NONE);
      itemI.setText("Item " + i);
      for (int j = 0; j < 16; j++) {
        TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
        itemJ.setText("Item " + j);
      }
    }
    final TreeItem[] lastItem = new TreeItem[1];
    final TreeEditor editor = new TreeEditor(tree);
    tree.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        final TreeItem item = (TreeItemevent.item;
        if (item != null && item == lastItem[0]) {
          boolean isCarbon = SWT.getPlatform().equals("carbon");
          final Composite composite = new Composite(tree, SWT.NONE);
          if (!isCarbon)
            composite.setBackground(black);
          final Text text = new Text(composite, SWT.NONE);
          final int inset = isCarbon ? 1;
          composite.addListener(SWT.Resize, new Listener() {
            public void handleEvent(Event e) {
              Rectangle rect = composite.getClientArea();
              text.setBounds(rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height
                  - inset * 2);
            }
          });
          Listener textListener = new Listener() {
            public void handleEvent(final Event e) {
              switch (e.type) {
              case SWT.FocusOut:
                item.setText(text.getText());
                composite.dispose();
                break;
              case SWT.Verify:
                String newText = text.getText();
                String leftText = newText.substring(0, e.start);
                String rightText = newText.substring(e.end, newText.length());
                GC gc = new GC(text);
                Point size = gc.textExtent(leftText + e.text + rightText);
                gc.dispose();
                size = text.computeSize(size.x, SWT.DEFAULT);
                editor.horizontalAlignment = SWT.LEFT;
                Rectangle itemRect = item.getBounds(),
                rect = tree.getClientArea();
                editor.minimumWidth = Math.max(size.x, itemRect.width+ inset * 2;
                int left = itemRect.x,
                right = rect.x + rect.width;
                editor.minimumWidth = Math.min(editor.minimumWidth, right - left);
                editor.minimumHeight = size.y + inset * 2;
                editor.layout();
                break;
              case SWT.Traverse:
                switch (e.detail) {
                case SWT.TRAVERSE_RETURN:
                  item.setText(text.getText());
                // FALL THROUGH
                case SWT.TRAVERSE_ESCAPE:
                  composite.dispose();
                  e.doit = false;
                }
                break;
              }
            }
          };
          text.addListener(SWT.FocusOut, textListener);
          text.addListener(SWT.Traverse, textListener);
          text.addListener(SWT.Verify, textListener);
          editor.setEditor(composite, item);
          text.setText(item.getText());
          text.selectAll();
          text.setFocus();
        }
        lastItem[0= item;
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}
17. 59. Tree Editor
17. 59. 1. Using TreeEditorUsing TreeEditor
17. 59. 2. Use Text as the Table Cell EditorUse Text as the Table Cell Editor
17. 59. 3. Exchanging DataExchanging Data
17. 59. 4. Press F2 to launch a dialog as the Tree EditorPress F2 to launch a dialog as the Tree Editor
17. 59. 5. Use Mouse action to activate(install) Tree editor
17. 59. 6. Use Button as the Tree EditorUse Button as the Tree Editor
17. 59. 7. Add Tree cell editorAdd Tree cell editor
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.