Java Tutorial/SWT/Tree Editor

Материал из Java эксперт
Перейти к: навигация, поиск

Add Tree cell editor

   <source lang="java">

/*******************************************************************************

* 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.ruposite; 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 = (TreeItem) event.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 ? 0 : 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.ruputeSize(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();
 }

}</source>





Exchanging Data

  1. Set the text from the cell into your editor"s control when you create the control.
  2. Set the text from the control back into the cell any time it"s modified.

Add Combo to Table as the Cell Editor and Exchange data between them



   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; public class TableCellEditorComboDataExchange {

 public static void main(String[] args) {
   Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setText("Text Table Editor");
   shell.setLayout(new FillLayout());
   final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
   table.setHeaderVisible(true);
   table.setLinesVisible(true);
   for (int i = 0; i < 5; i++) {
     TableColumn column = new TableColumn(table, SWT.CENTER);
     column.setText("Column " + (i + 1));
     column.pack();
   }
   for (int i = 0; i < 5; i++) {
     final TableItem item = new TableItem(table, SWT.NONE);
   }
   final TableEditor editor = new TableEditor(table);
   editor.horizontalAlignment = SWT.LEFT;
   editor.grabHorizontal = true;
   table.addMouseListener(new MouseAdapter() {
     public void mouseDown(MouseEvent event) {
       Control old = editor.getEditor();
       if (old != null)
         old.dispose();
       Point pt = new Point(event.x, event.y);
       final TableItem item = table.getItem(pt);
       if (item == null) {
         return;
       }
       int column = -1;
       for (int i = 0, n = table.getColumnCount(); i < n; i++) {
         Rectangle rect = item.getBounds(i);
         if (rect.contains(pt)) {
           column = i;
           break;
         }
       }
       if (column != 1) {
         return;
       }
       final CCombo combo = new CCombo(table, SWT.READ_ONLY);
       for (int i = 0; i < 3; i++) {
         combo.add("" + i);
       }
       combo.select(combo.indexOf(item.getText(column)));
       editor.minimumWidth = combo.ruputeSize(SWT.DEFAULT, SWT.DEFAULT).x;
       table.getColumn(column).setWidth(editor.minimumWidth);
       combo.setFocus();
       editor.setEditor(combo, item, column);
       final int col = column;
       combo.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(SelectionEvent event) {
           item.setText(col, combo.getText());
           combo.dispose();
         }
       });
     }
   });
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Press F2 to launch a dialog as the Tree Editor

  1. You can also associate the editor with a button to launch a dialog box for editing tree nodes.
  2. You create the editor and control just like you do with the other editor classes.



   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.TreeEditor; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class TreeEditorF2Trigger {

 public static void main(String[] args) {
   Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setText("Text Tree Editor");
   shell.setLayout(new FillLayout());
   final Tree tree = new Tree(shell, SWT.SINGLE);
   for (int i = 0; i < 3; i++) {
     TreeItem iItem = new TreeItem(tree, SWT.NONE);
     iItem.setText("Item " + (i + 1));
     for (int j = 0; j < 3; j++) {
       TreeItem jItem = new TreeItem(iItem, SWT.NONE);
       jItem.setText("Sub Item " + (j + 1));
       for (int k = 0; k < 3; k++) {
         new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
       }
       jItem.setExpanded(true);
     }
     iItem.setExpanded(true);
   }
   final TreeEditor editor = new TreeEditor(tree);
   editor.horizontalAlignment = SWT.LEFT;
   editor.grabHorizontal = true;
   tree.addKeyListener(new KeyAdapter() {
     public void keyPressed(KeyEvent event) {
       if (event.keyCode == SWT.F2 && tree.getSelectionCount() == 1) {
         final TreeItem item = tree.getSelection()[0];
         
         int style = SWT.ICON_QUESTION |SWT.YES | SWT.NO;
         
         MessageBox messageBox = new MessageBox(shell, style);
         messageBox.setMessage("Message");
         int rc = messageBox.open();
         
         item.setText(rc+"");
       }
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Use Button as the Tree Editor

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.TreeEditor; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class TreeEditorButton {

 public static void main(String[] args) {
   Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setText("Text Tree Editor");
   shell.setLayout(new FillLayout());
   final Tree tree = new Tree(shell, SWT.SINGLE);
   for (int i = 0; i < 3; i++) {
     TreeItem iItem = new TreeItem(tree, SWT.NONE);
     iItem.setText("Item " + (i + 1));
     for (int j = 0; j < 3; j++) {
       TreeItem jItem = new TreeItem(iItem, SWT.NONE);
       jItem.setText("Sub Item " + (j + 1));
       for (int k = 0; k < 3; k++) {
         new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
       }
       jItem.setExpanded(true);
     }
     iItem.setExpanded(true);
   }
   final TreeEditor editor = new TreeEditor(tree);
   editor.horizontalAlignment = SWT.LEFT;
   editor.grabHorizontal = true;
   tree.addMouseListener(new MouseAdapter() {
     public void mouseDown(MouseEvent event) {
       final TreeItem item = tree.getSelection()[0];
       final Button bn = new Button(tree, SWT.NONE);
       bn.setText("click");
       bn.setFocus();
       bn.addSelectionListener(new SelectionListener() {
         public void widgetSelected(SelectionEvent arg0) {
           int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;
           MessageBox messageBox = new MessageBox(shell, style);
           messageBox.setMessage("Message");
           int rc = messageBox.open();
           item.setText(rc + "");
           bn.dispose();
         }
         public void widgetDefaultSelected(SelectionEvent arg0) {
         }
       });
       editor.setEditor(bn, item);
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Use Mouse action to activate(install) Tree editor

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.TreeEditor; import org.eclipse.swt.events.FocusAdapter; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; 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 TreeEditorMouseActivate {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setText("Text Tree Editor");
   shell.setLayout(new FillLayout());
   final Tree tree = new Tree(shell, SWT.SINGLE);
   for (int i = 0; i < 3; i++) {
     TreeItem iItem = new TreeItem(tree, SWT.NONE);
     iItem.setText("Item " + (i + 1));
     for (int j = 0; j < 3; j++) {
       TreeItem jItem = new TreeItem(iItem, SWT.NONE);
       jItem.setText("Sub Item " + (j + 1));
       for (int k = 0; k < 3; k++) {
         new TreeItem(jItem, SWT.NONE).setText("Sub Sub Item " + (k + 1));
       }
       jItem.setExpanded(true);
     }
     iItem.setExpanded(true);
   }
   final TreeEditor editor = new TreeEditor(tree);
   editor.horizontalAlignment = SWT.LEFT;
   editor.grabHorizontal = true;
   tree.addMouseListener(new MouseAdapter() {
     public void mouseDown(MouseEvent event) {
       final TreeItem item = tree.getSelection()[0];
       final Text text = new Text(tree, SWT.NONE);
       text.setText(item.getText());
       text.selectAll();
       text.setFocus();
       text.addFocusListener(new FocusAdapter() {
         public void focusLost(FocusEvent event) {
           item.setText(text.getText());
           text.dispose();
         }
       });
       text.addKeyListener(new KeyAdapter() {
         public void keyPressed(KeyEvent event) {
           switch (event.keyCode) {
           case SWT.CR:
             item.setText(text.getText());
           case SWT.ESC:
             text.dispose();
             break;
           }
         }
       });
       editor.setEditor(text, item);
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Use Text as the Table Cell Editor

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; public class TableCellEditorText {

 public static void main(String[] args) {
   Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setText("Text Table Editor");
   shell.setLayout(new FillLayout());
   final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
   table.setHeaderVisible(true);
   table.setLinesVisible(true);
   for (int i = 0; i < 5; i++) {
     TableColumn column = new TableColumn(table, SWT.CENTER);
     column.setText("Column " + (i + 1));
     column.pack();
   }
   // Create five table editors for color
   TableEditor[] colorEditors = new TableEditor[5];
   // Create five buttons for changing color
   Text[] textEditor = new Text[5];
   for (int i = 0; i < 5; i++) {
     final TableItem item = new TableItem(table, SWT.NONE);
     colorEditors[i] = new TableEditor(table);
     textEditor[i] = new Text(table, SWT.SINGLE|SWT.BORDER);
     textEditor[i].setText("editor");
     textEditor[i].ruputeSize(SWT.DEFAULT, table.getItemHeight());
     colorEditors[i].grabHorizontal = true;
     colorEditors[i].minimumHeight = textEditor[i].getSize().y;
     colorEditors[i].minimumWidth = textEditor[i].getSize().x;
     colorEditors[i].setEditor(textEditor[i], item, 0);
     
 
   }
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Using TreeEditor

  1. A TreeEditor allows users to edit the text associated with a tree"s nodes.
  2. TreeEditor is derived from ControlEditor.
  3. Its parent is always a Tree, which is passed in the constructor:



   <source lang="java">

public TreeEditor(Tree parent)</source>