Java/SWT JFace Eclipse/Spinner

Материал из Java эксперт
Версия от 05:55, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

create and initialize a SWT spinner widget

/*
 * Spinner example snippet: create and initialize a spinner widget
 * 
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
public class Snippet184 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Spinner spinner = new Spinner(shell, SWT.BORDER);
    spinner.setMinimum(0);
    spinner.setMaximum(1000);
    spinner.setSelection(500);
    spinner.setIncrement(1);
    spinner.setPageIncrement(100);
    spinner.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Floating point values in SWT Spinner

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
/*
 * Floating point values in Spinner
 * 
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
public class Snippet190 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Spinner with float values");
    shell.setLayout(new GridLayout());
    final Spinner spinner = new Spinner(shell, SWT.NONE);
    // allow 3 decimal places
    spinner.setDigits(3);
    // set the minimum value to 0.001
    spinner.setMinimum(1);
    // set the maximum value to 20
    spinner.setMaximum(20000);
    // set the increment value to 0.010
    spinner.setIncrement(10);
    // set the seletion to 3.456
    spinner.setSelection(3456);
    spinner.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        int selection = spinner.getSelection();
        int digits = spinner.getDigits();
        System.out.println("Selection is "
            + (selection / Math.pow(10, digits)));
      }
    });
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





SWT Spinner Sample

/*
 * 
 * (c) Copyright IBM Corp. 2000, 2001.
 * 
 * All Rights Reserved.
 *  
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TypedListener;
public class Spinner extends Composite {
  static final int BUTTON_WIDTH = 16;
  Text text;
  Button up, down;
  int minimum, maximum;
  public Spinner(Composite parent, int style) {
    super(parent, style);
    text = new Text(this, style | SWT.SINGLE | SWT.BORDER);
    up = new Button(this, style | SWT.ARROW | SWT.UP);
    down = new Button(this, style | SWT.ARROW | SWT.DOWN);
    text.addListener(SWT.Verify, new Listener() {
      public void handleEvent(Event e) {
        verify(e);
      }
    });
    text.addListener(SWT.Traverse, new Listener() {
      public void handleEvent(Event e) {
        traverse(e);
      }
    });
    up.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event e) {
        up();
      }
    });
    down.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event e) {
        down();
      }
    });
    addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event e) {
        resize();
      }
    });
    addListener(SWT.FocusIn, new Listener() {
      public void handleEvent(Event e) {
        focusIn();
      }
    });
    text.setFont(getFont());
    minimum = 0;
    maximum = 9;
    setSelection(minimum);
  }
  void verify(Event e) {
    try {
      Integer.parseInt(e.text);
    } catch (NumberFormatException ex) {
      e.doit = false;
    }
  }
  void traverse(Event e) {
    switch (e.detail) {
    case SWT.TRAVERSE_ARROW_PREVIOUS:
      if (e.keyCode == SWT.ARROW_UP) {
        e.doit = true;
        e.detail = SWT.NULL;
        up();
      }
      break;
    case SWT.TRAVERSE_ARROW_NEXT:
      if (e.keyCode == SWT.ARROW_DOWN) {
        e.doit = true;
        e.detail = SWT.NULL;
        down();
      }
      break;
    }
  }
  void up() {
    setSelection(getSelection() + 1);
    notifyListeners(SWT.Selection, new Event());
  }
  void down() {
    setSelection(getSelection() - 1);
    notifyListeners(SWT.Selection, new Event());
  }
  void focusIn() {
    text.setFocus();
  }
  public void setFont(Font font) {
    super.setFont(font);
    text.setFont(font);
  }
  public void setSelection(int selection) {
    if (selection < minimum) {
      selection = minimum;
    } else if (selection > maximum) {
      selection = maximum;
    }
    text.setText(String.valueOf(selection));
    text.selectAll();
    text.setFocus();
  }
  public int getSelection() {
    return Integer.parseInt(text.getText());
  }
  public void setMaximum(int maximum) {
    checkWidget();
    this.maximum = maximum;
    resize();
  }
  public int getMaximum() {
    return maximum;
  }
  public void setMinimum(int minimum) {
    this.minimum = minimum;
  }
  public int getMinimum() {
    return minimum;
  }
  void resize() {
    Point pt = computeSize(SWT.DEFAULT, SWT.DEFAULT);
    int textWidth = pt.x - BUTTON_WIDTH;
    int buttonHeight = pt.y / 2;
    text.setBounds(0, 0, textWidth, pt.y);
    up.setBounds(textWidth, 0, BUTTON_WIDTH, buttonHeight);
    down.setBounds(textWidth, pt.y - buttonHeight, BUTTON_WIDTH,
        buttonHeight);
  }
  public Point computeSize(int wHint, int hHint, boolean changed) {
    GC gc = new GC(text);
    Point textExtent = gc.textExtent(String.valueOf(maximum));
    gc.dispose();
    Point pt = text.ruputeSize(textExtent.x, textExtent.y);
    int width = pt.x + BUTTON_WIDTH;
    int height = pt.y;
    if (wHint != SWT.DEFAULT)
      width = wHint;
    if (hHint != SWT.DEFAULT)
      height = hHint;
    return new Point(width, height);
  }
  public void addSelectionListener(SelectionListener listener) {
    if (listener == null)
      throw new SWTError(SWT.ERROR_NULL_ARGUMENT);
    addListener(SWT.Selection, new TypedListener(listener));
  }
}





SWT Spinner Test

/*
 * (c) Copyright IBM Corp. 2000, 2001. All Rights Reserved.
 */
import java.util.Hashtable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TypedListener;
public class SpinnerTest {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());
    final Spinner spinner = new Spinner(shell, 0);
    spinner.setMaximum(999);
    System.out.println("max set to " + spinner.getMaximum());
    spinner.setSelection(500);
    System.out.println("selection set to " + spinner.getSelection());
    spinner.setMinimum(100);
    System.out.println("min set to " + spinner.getMinimum());
    Font font = new Font(display, "Courier", 20, SWT.NORMAL);
    spinner.setFont(font);
    spinner.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println(spinner.getSelection());
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    font.dispose();
  }
}
class Spinner extends Composite {
  int handleSpinner;
  static Hashtable table = new Hashtable();
  static {
    System.loadLibrary("spinner");
  }
  public Spinner(Composite parent, int style) {
    super(parent, style);
    int handleParent = handle;
    handleSpinner = createControl(handleParent);
    if (handleSpinner == 0)
      SWT.error(SWT.ERROR_NO_HANDLES);
    table.put(new Integer(handleSpinner), this);
    addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent e) {
        Spinner.this.widgetDisposed(e);
      }
    });
    addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent e) {
        Spinner.this.controlResized(e);
      }
    });
    addFocusListener(new FocusAdapter() {
      public void focusGained(FocusEvent e) {
        Spinner.this.focusGained(e);
      }
    });
    Font font = getFont();
    setFont(handleSpinner, font.handle);
  }
  public void setFont(Font font) {
    super.setFont(font);
    int hFont = 0;
    if (font != null)
      hFont = font.handle;
    setFont(handleSpinner, hFont);
  }
  public int getSelection() {
    checkWidget();
    return getPosition(handleSpinner);
  }
  public void setSelection(int selection) {
    checkWidget();
    setPosition(handleSpinner, selection);
  }
  public void setMaximum(int maximum) {
    checkWidget();
    setMaximum(handleSpinner, maximum);
  }
  public int getMaximum() {
    checkWidget();
    return getMaximum(handleSpinner);
  }
  public void setMinimum(int minimum) {
    checkWidget();
    setMinimum(handleSpinner, minimum);
  }
  public int getMinimum() {
    checkWidget();
    return getMinimum(handleSpinner);
  }
  public void widgetDisposed(DisposeEvent e) {
    table.remove(new Integer(handleSpinner));
    handleSpinner = 0;
  }
  public void controlResized(ControlEvent e) {
    Rectangle rect = getClientArea();
    resizeControl(handleSpinner, rect.x, rect.y, rect.width, rect.height);
  }
  public void focusGained(FocusEvent e) {
    setFocus(handleSpinner);
  }
  public Point computeSize(int wHint, int hHint, boolean changed) {
    checkWidget();
    int[] result = new int[2];
    computeSize(handleSpinner, result);
    if (wHint != SWT.DEFAULT)
      result[0] = wHint;
    if (hHint != SWT.DEFAULT)
      result[1] = hHint;
    int border = getBorderWidth();
    return new Point(result[0] + border * 2, result[1] + border * 2);
  }
  public void addSelectionListener(SelectionListener listener) {
    if (listener == null)
      throw new SWTError(SWT.ERROR_NULL_ARGUMENT);
    addListener(SWT.Selection, new TypedListener(listener));
  }
  static void widgetSelected(int handle) {
    Spinner spinner = (Spinner) table.get(new Integer(handle));
    if (spinner == null)
      return;
    spinner.notifyListeners(SWT.Selection, new Event());
  }
  /** ********* JAVA NATIVES *********** */
  static final native int createControl(int handle);
  static final native void computeSize(int handle, int[] result);
  static final native void resizeControl(int handle, int x, int y, int width,
      int height);
  static final native void setPosition(int handle, int position);
  static final native int getPosition(int handle);
  static final native void setMaximum(int handle, int max);
  static final native int getMaximum(int handle);
  static final native void setMinimum(int handle, int min);
  static final native int getMinimum(int handle);
  static final native void setFont(int handle, int hFont);
  static final native void setFocus(int handle);
}