SEDoubleTextWithSpinForSWT.java
Go to the documentation of this file.
00001 package com.generalrobotix.ui.view.graph;
00002 
00003 import org.eclipse.swt.SWT;
00004 import org.eclipse.swt.events.FocusEvent;
00005 import org.eclipse.swt.events.FocusListener;
00006 import org.eclipse.swt.events.VerifyEvent;
00007 import org.eclipse.swt.events.VerifyListener;
00008 import org.eclipse.swt.events.MouseEvent;
00009 import org.eclipse.swt.events.MouseListener;
00010 import org.eclipse.swt.events.MouseTrackListener;
00011 import org.eclipse.swt.events.MouseMoveListener;
00012 import org.eclipse.swt.events.ControlEvent;
00013 import org.eclipse.swt.events.ControlListener;
00014 
00015 import org.eclipse.swt.layout.GridData;
00016 import org.eclipse.swt.layout.GridLayout;
00017 import org.eclipse.swt.widgets.Button;
00018 import org.eclipse.swt.widgets.Composite;
00019 import org.eclipse.swt.widgets.Control;
00020 import org.eclipse.swt.widgets.Text;
00021 import org.eclipse.swt.widgets.Display;
00022 
00023 public class SEDoubleTextWithSpinForSWT extends Composite{
00024     private SEDouble value_;
00025     private double max_;
00026     private double min_;
00027     private double step_;
00028     
00029     private Composite parent_;
00030     private Text text_;
00031     private Button buttonUP_;
00032     private Button buttonDOWN_;
00033     
00034     private int mouseBtnActionNum = 0;
00035     private int mouseBtnRepeat_ = 200;
00036     private int mouseBtnAccelNeed_ = 5;
00037     private int mouseBtnAccelRepeat_ = 50;
00038     private boolean isMouseOverBtnUp_ = false;
00039     private boolean isMouseOverBtnDown_ = false;
00040     private Runnable incrementRun_;
00041     private Runnable decrementRun_;
00042     
00043     public SEDoubleTextWithSpinForSWT (Composite parent,int style,double min,double max,double step) {
00044         super(parent,style);
00045         parent_ = parent;
00046         value_ = new SEDouble(0);
00047         
00048         max_ = max;
00049         min_ = min;
00050         step_ = step;
00051         
00052         GridLayout gl =new GridLayout(2,false);
00053         gl.horizontalSpacing = 0;
00054         gl.verticalSpacing = 0;
00055         setLayout(gl);
00056         
00057         text_ = new Text(this,SWT.SINGLE | SWT.BORDER);
00058         text_.addFocusListener(new FocusListener(){
00059             public void focusLost(FocusEvent e){
00060                 setValue(text_.getText());
00061                 updateValue();
00062             }
00063             public void focusGained(FocusEvent e){
00064             }
00065         });
00066         text_.addVerifyListener(new VerifyListener(){
00067             public void verifyText(VerifyEvent e){
00068                 // 最大値が無限大のときは無限大文字列は受け取る
00069                 if(max_ == Double.POSITIVE_INFINITY)
00070                 {
00071                     if(e.text.equals(new SEDouble(Double.POSITIVE_INFINITY).toString()))
00072                         return;
00073                 }
00074 
00075                 for(int i = 0; i < e.text.length(); i++){
00076                     char c = e.text.charAt(i);
00077                     if(!(('0' <= c && c <= '9') ||
00078                          c == '.' ||
00079                          c == '-' ||
00080                          c == 'e' || c == 'E'))
00081                     {
00082                         e.doit = false;
00083                     }
00084                 }
00085             }
00086         });
00087 
00088         GridData gridData = new GridData();
00089         gridData.widthHint = 80;
00090         text_.setLayoutData(gridData);
00091         
00092         Composite inner = new Composite(this, SWT.NULL);
00093         GridLayout rlInner =new GridLayout(1,false);
00094         rlInner.marginHeight = 0;
00095         rlInner.marginWidth = 0;
00096         rlInner.horizontalSpacing = 0;
00097         rlInner.verticalSpacing = 0;
00098         inner.setLayout(rlInner); 
00099         GridData innerGrid = new GridData();
00100         innerGrid.verticalAlignment = SWT.FILL;
00101         inner.setLayoutData(innerGrid);
00102 
00103         buttonUP_ = new Button(inner,SWT.ARROW | SWT.UP);
00104         buttonUP_.addMouseTrackListener(new MouseTrackListener(){
00105             public void mouseEnter(MouseEvent e){
00106                 isMouseOverBtnUp_ = true;
00107             }
00108             public void mouseExit(MouseEvent e){
00109                 isMouseOverBtnUp_ = false;
00110             }
00111             public void mouseHover(MouseEvent e){
00112             }
00113         });
00114 
00115         buttonUP_.addMouseMoveListener(new MouseMoveListener(){
00116             public void mouseMove(MouseEvent e){
00117                 isMouseOverBtnUp_ = (0 <= e.x && e.x < buttonUP_.getSize().x && 0 <= e.y && e.y < buttonUP_.getSize().y);
00118             }
00119         });
00120         buttonUP_.addMouseListener(new MouseListener(){
00121             public void mouseDoubleClick(MouseEvent e){
00122             }
00123             public void mouseDown(MouseEvent e) {
00124                 if(e.button == 1){
00125                     incrementValue();
00126                     startIncrementTimer();
00127                 }
00128             }
00129             public void mouseUp(MouseEvent e) {
00130                 if(e.button == 1){
00131                     stopIncrementTimer();
00132                 }
00133             }
00134         });
00135         
00136         buttonDOWN_ = new Button(inner,SWT.ARROW | SWT.DOWN);
00137         buttonDOWN_.addMouseTrackListener(new MouseTrackListener(){
00138             public void mouseEnter(MouseEvent e){
00139                 isMouseOverBtnDown_ = true;
00140             }
00141             public void mouseExit(MouseEvent e){
00142                 isMouseOverBtnDown_ = false;
00143             }
00144             public void mouseHover(MouseEvent e){
00145             }
00146         });
00147 
00148         buttonDOWN_.addMouseMoveListener(new MouseMoveListener(){
00149             public void mouseMove(MouseEvent e){
00150                 isMouseOverBtnDown_ = (0 <= e.x && e.x < buttonDOWN_.getSize().x && 0 <= e.y && e.y < buttonDOWN_.getSize().y);
00151             }
00152         });
00153         buttonDOWN_.addMouseListener(new MouseListener(){
00154             public void mouseDoubleClick(MouseEvent e){
00155             }
00156             public void mouseDown(MouseEvent e) {
00157                 if(e.button == 1){
00158                     decrementValue();
00159                     startDecrementTimer();
00160                 }
00161             }
00162             public void mouseUp(MouseEvent e) {
00163                 if(e.button == 1){
00164                     stopDecrementTimer();
00165                 }
00166             }
00167         });
00168         
00169         text_.addControlListener(new ControlListener(){
00170             public void controlMoved(ControlEvent e) {
00171             } 
00172             public void controlResized(ControlEvent e) {
00173                 GridData buttonUpGrid = new GridData();
00174                 buttonUpGrid.heightHint= text_.getSize().y/2;
00175                 buttonUP_.setLayoutData(buttonUpGrid);
00176                 GridData buttonDownGrid = new GridData();
00177                 buttonDownGrid.heightHint= text_.getSize().y/2;
00178                 buttonDOWN_.setLayoutData(buttonDownGrid);
00179                 parent_.layout();
00180                 //layout();
00181             }
00182         });
00183         
00184         incrementRun_ = new Runnable() {
00185             public void run() { 
00186                 Display display = text_.getDisplay();
00187                 if (!display.isDisposed())
00188                 {
00189                     if(isMouseOverBtnUp_)
00190                     {
00191                         mouseBtnActionNum += 1;
00192                         incrementValue();
00193                     }
00194                     display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_), this);
00195                 }
00196             }
00197         };
00198         decrementRun_ = new Runnable() {
00199             public void run() { 
00200                 Display display = text_.getDisplay();
00201                 if (!display.isDisposed())
00202                 {
00203                     if(isMouseOverBtnDown_)
00204                     {
00205                         mouseBtnActionNum += 1;
00206                         decrementValue();
00207                     }
00208                     display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_), this);
00209                 }
00210             }
00211         };
00212         
00213         setValue((max+min)/2);
00214     }
00215 
00216 
00217     protected double text2value(){
00218         String s = text_.getText();
00219         double v = 0;
00220         try {
00221             v = new SEDouble(s).doubleValue();
00222         } catch (Exception e) {
00223             v = value_.doubleValue();
00224         }
00225         return v;
00226     }
00227     
00228     public SEDouble getValue() {
00229         return value_;
00230     }
00231     public double getValueDouble() {
00232         return value_.doubleValue();
00233     }
00234     
00235     public void setValue(String s) {
00236         double v = new SEDouble(s).doubleValue();
00237         setValue(v);
00238     }
00239     
00240     public void setValue(double v) {
00241         if (isOk(v)) {
00242             value_.setValue(new Double(v));
00243         }
00244 
00245         text_.setText(value_.toString());
00246     }
00247 
00248     public boolean isOk(double v) {
00249         return (min_ <= v && v<=max_);
00250     }
00251 
00252     public void setEnabled(boolean flag) {
00253         super.setEnabled(flag);
00254         Control[] cmps = getChildren();
00255         for (int i = 0; i < cmps.length; i++) {
00256             cmps[i].setEnabled(flag);
00257         }
00258     }
00259 
00260     private void incrementValue(){
00261         double v = text2value() + step_;
00262         setValue(v);
00263         updateValue();
00264     }
00265     private void decrementValue(){
00266         double v = text2value() - step_;
00267         setValue(v);
00268         updateValue();
00269     }
00270     private void startIncrementTimer(){
00271         mouseBtnActionNum = 0;
00272 
00273         Display display = text_.getDisplay();
00274         if (!display.isDisposed())
00275         {
00276             display.timerExec(mouseBtnRepeat_, incrementRun_);
00277         }
00278     }
00279     private void stopIncrementTimer(){
00280         mouseBtnActionNum = 0;
00281 
00282         Display display = text_.getDisplay();
00283         if (!display.isDisposed())
00284         {
00285             display.timerExec(-1, incrementRun_);
00286         }
00287     }
00288     private void startDecrementTimer(){
00289         mouseBtnActionNum = 0;
00290 
00291         Display display = text_.getDisplay();
00292         if (!display.isDisposed())
00293         {
00294             display.timerExec(mouseBtnRepeat_, decrementRun_);
00295         }
00296     }
00297     private void stopDecrementTimer(){
00298         mouseBtnActionNum = 0;
00299 
00300         Display display = text_.getDisplay();
00301         if (!display.isDisposed())
00302         {
00303             display.timerExec(-1, decrementRun_);
00304         }
00305     }
00306 
00307     public void fixValue(){
00308         setValue(text_.getText());
00309         updateValue();
00310     }
00311 
00312     // 値が変更されたときに呼び出される。
00313     protected void updateValue(){
00314     }
00315 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:19