Java Tutorial/SWT/ControlEditor

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

Control Editors

ControlEditor offers basic control-editing abilities. It has three derived classes: TableEditor, TreeEditor, and TableTreeEditor.



   <source lang="java">

import java.util.HashMap; import java.util.Map; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ControlEditor; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ControlEditorSample {

 private static final Map COLORS = new HashMap();
 static {
   COLORS.put("red", new RGB(255, 0, 0));
   COLORS.put("green", new RGB(0, 255, 0));
   COLORS.put("blue", new RGB(0, 0, 255));
   COLORS.put("yellow", new RGB(255, 255, 0));
   COLORS.put("black", new RGB(0, 0, 0));
   COLORS.put("white", new RGB(255, 255, 255));
 }
 static Display display = new Display();
 static Color color = new Color(display, 255, 0, 0);
 public static void main(String[] args) {
   final Shell shell = new Shell(display);
   shell.setText("Control Editor");
   final Composite composite = new Composite(shell, SWT.NONE);
   composite.setBackground(color);
   composite.setBounds(0, 0, 300, 100);
   ControlEditor editor = new ControlEditor(composite);
   final Text text = new Text(composite, SWT.BORDER);
   text.addModifyListener(new ModifyListener() {
     public void modifyText(ModifyEvent event) {
       RGB rgb = (RGB) COLORS.get(text.getText());
       if (rgb != null) {
         if (color != null)
           color.dispose();
         color = new Color(shell.getDisplay(), rgb);
         composite.setBackground(color);
       }
     }
   });
   // Place the editor in the top middle of the parent composite
   editor.horizontalAlignment = SWT.CENTER;
   editor.verticalAlignment = SWT.TOP;
   Point size = text.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
   editor.minimumWidth = size.x;
   editor.minimumHeight = size.y;
   editor.setEditor(text);
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   if (color != null)
     color.dispose();
   display.dispose();
 }

}</source>





Control Editor With Dialog

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ControlEditor; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ColorDialog; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ControlEditorWithDialog {

 static Display display = new Display();
 static Color color = new Color(display, 255, 0, 0);
 public static void main(String[] args) {
   final Shell shell = new Shell(display);
   shell.setText("Control Editor Two");
   final Composite composite = new Composite(shell, SWT.NONE);
   composite.setBackground(color);
   composite.setBounds(0, 0, 300, 100);
   ControlEditor editor = new ControlEditor(composite);
   // Create the control associated with the editor
   Button button = new Button(composite, SWT.PUSH);
   button.setText("Change Color...");
   button.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent event) {
       ColorDialog dialog = new ColorDialog(shell);
       if (color != null)
         dialog.setRGB(color.getRGB());
       RGB rgb = dialog.open();
       if (rgb != null) {
         if (color != null)
           color.dispose();
         color = new Color(shell.getDisplay(), rgb);
         composite.setBackground(color);
       }
     }
   });
   // Place the editor along the bottom of the parent composite
   editor.grabHorizontal = true;
   editor.verticalAlignment = SWT.BOTTOM;
   Point size = button.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
   editor.minimumHeight = size.y;
   editor.setEditor(button);
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   if (color != null)
     color.dispose();
   display.dispose();
 }

}</source>