SEDoubleTextWithSpinForSWT.java
Go to the documentation of this file.
1 package com.generalrobotix.ui.view.graph;
2 
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.events.FocusEvent;
5 import org.eclipse.swt.events.FocusListener;
6 import org.eclipse.swt.events.VerifyEvent;
7 import org.eclipse.swt.events.VerifyListener;
8 import org.eclipse.swt.events.MouseEvent;
9 import org.eclipse.swt.events.MouseListener;
10 import org.eclipse.swt.events.MouseTrackListener;
11 import org.eclipse.swt.events.MouseMoveListener;
12 import org.eclipse.swt.events.ControlEvent;
13 import org.eclipse.swt.events.ControlListener;
14 
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Text;
21 import org.eclipse.swt.widgets.Display;
22 
23 public class SEDoubleTextWithSpinForSWT extends Composite{
24  private SEDouble value_;
25  private double max_;
26  private double min_;
27  private double step_;
28 
29  private Composite parent_;
30  private Text text_;
31  private Button buttonUP_;
32  private Button buttonDOWN_;
33 
34  private int mouseBtnActionNum = 0;
35  private int mouseBtnRepeat_ = 200;
36  private int mouseBtnAccelNeed_ = 5;
37  private int mouseBtnAccelRepeat_ = 50;
38  private boolean isMouseOverBtnUp_ = false;
39  private boolean isMouseOverBtnDown_ = false;
40  private Runnable incrementRun_;
41  private Runnable decrementRun_;
42 
43  public SEDoubleTextWithSpinForSWT (Composite parent,int style,double min,double max,double step) {
44  super(parent,style);
45  parent_ = parent;
46  value_ = new SEDouble(0);
47 
48  max_ = max;
49  min_ = min;
50  step_ = step;
51 
52  GridLayout gl =new GridLayout(2,false);
53  gl.horizontalSpacing = 0;
54  gl.verticalSpacing = 0;
55  setLayout(gl);
56 
57  text_ = new Text(this,SWT.SINGLE | SWT.BORDER);
58  text_.addFocusListener(new FocusListener(){
59  public void focusLost(FocusEvent e){
60  setValue(text_.getText());
61  updateValue();
62  }
63  public void focusGained(FocusEvent e){
64  }
65  });
66  text_.addVerifyListener(new VerifyListener(){
67  public void verifyText(VerifyEvent e){
68  // 最大値が無限大のときは無限大文字列は受け取る
69  if(max_ == Double.POSITIVE_INFINITY)
70  {
71  if(e.text.equals(new SEDouble(Double.POSITIVE_INFINITY).toString()))
72  return;
73  }
74 
75  for(int i = 0; i < e.text.length(); i++){
76  char c = e.text.charAt(i);
77  if(!(('0' <= c && c <= '9') ||
78  c == '.' ||
79  c == '-' ||
80  c == 'e' || c == 'E'))
81  {
82  e.doit = false;
83  }
84  }
85  }
86  });
87 
88  GridData gridData = new GridData();
89  gridData.widthHint = 80;
90  text_.setLayoutData(gridData);
91 
92  Composite inner = new Composite(this, SWT.NULL);
93  GridLayout rlInner =new GridLayout(1,false);
94  rlInner.marginHeight = 0;
95  rlInner.marginWidth = 0;
96  rlInner.horizontalSpacing = 0;
97  rlInner.verticalSpacing = 0;
98  inner.setLayout(rlInner);
99  GridData innerGrid = new GridData();
100  innerGrid.verticalAlignment = SWT.FILL;
101  inner.setLayoutData(innerGrid);
102 
103  buttonUP_ = new Button(inner,SWT.ARROW | SWT.UP);
104  buttonUP_.addMouseTrackListener(new MouseTrackListener(){
105  public void mouseEnter(MouseEvent e){
106  isMouseOverBtnUp_ = true;
107  }
108  public void mouseExit(MouseEvent e){
109  isMouseOverBtnUp_ = false;
110  }
111  public void mouseHover(MouseEvent e){
112  }
113  });
114 
115  buttonUP_.addMouseMoveListener(new MouseMoveListener(){
116  public void mouseMove(MouseEvent e){
117  isMouseOverBtnUp_ = (0 <= e.x && e.x < buttonUP_.getSize().x && 0 <= e.y && e.y < buttonUP_.getSize().y);
118  }
119  });
120  buttonUP_.addMouseListener(new MouseListener(){
121  public void mouseDoubleClick(MouseEvent e){
122  }
123  public void mouseDown(MouseEvent e) {
124  if(e.button == 1){
125  incrementValue();
127  }
128  }
129  public void mouseUp(MouseEvent e) {
130  if(e.button == 1){
132  }
133  }
134  });
135 
136  buttonDOWN_ = new Button(inner,SWT.ARROW | SWT.DOWN);
137  buttonDOWN_.addMouseTrackListener(new MouseTrackListener(){
138  public void mouseEnter(MouseEvent e){
139  isMouseOverBtnDown_ = true;
140  }
141  public void mouseExit(MouseEvent e){
142  isMouseOverBtnDown_ = false;
143  }
144  public void mouseHover(MouseEvent e){
145  }
146  });
147 
148  buttonDOWN_.addMouseMoveListener(new MouseMoveListener(){
149  public void mouseMove(MouseEvent e){
150  isMouseOverBtnDown_ = (0 <= e.x && e.x < buttonDOWN_.getSize().x && 0 <= e.y && e.y < buttonDOWN_.getSize().y);
151  }
152  });
153  buttonDOWN_.addMouseListener(new MouseListener(){
154  public void mouseDoubleClick(MouseEvent e){
155  }
156  public void mouseDown(MouseEvent e) {
157  if(e.button == 1){
158  decrementValue();
160  }
161  }
162  public void mouseUp(MouseEvent e) {
163  if(e.button == 1){
165  }
166  }
167  });
168 
169  text_.addControlListener(new ControlListener(){
170  public void controlMoved(ControlEvent e) {
171  }
172  public void controlResized(ControlEvent e) {
173  GridData buttonUpGrid = new GridData();
174  buttonUpGrid.heightHint= text_.getSize().y/2;
175  buttonUP_.setLayoutData(buttonUpGrid);
176  GridData buttonDownGrid = new GridData();
177  buttonDownGrid.heightHint= text_.getSize().y/2;
178  buttonDOWN_.setLayoutData(buttonDownGrid);
179  parent_.layout();
180  //layout();
181  }
182  });
183 
184  incrementRun_ = new Runnable() {
185  public void run() {
186  Display display = text_.getDisplay();
187  if (!display.isDisposed())
188  {
189  if(isMouseOverBtnUp_)
190  {
191  mouseBtnActionNum += 1;
192  incrementValue();
193  }
194  display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_), this);
195  }
196  }
197  };
198  decrementRun_ = new Runnable() {
199  public void run() {
200  Display display = text_.getDisplay();
201  if (!display.isDisposed())
202  {
203  if(isMouseOverBtnDown_)
204  {
205  mouseBtnActionNum += 1;
206  decrementValue();
207  }
208  display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_), this);
209  }
210  }
211  };
212 
213  setValue((max+min)/2);
214  }
215 
216 
217  protected double text2value(){
218  String s = text_.getText();
219  double v = 0;
220  try {
221  v = new SEDouble(s).doubleValue();
222  } catch (Exception e) {
223  v = value_.doubleValue();
224  }
225  return v;
226  }
227 
228  public SEDouble getValue() {
229  return value_;
230  }
231  public double getValueDouble() {
232  return value_.doubleValue();
233  }
234 
235  public void setValue(String s) {
236  double v = new SEDouble(s).doubleValue();
237  setValue(v);
238  }
239 
240  public void setValue(double v) {
241  if (isOk(v)) {
242  value_.setValue(new Double(v));
243  }
244 
245  text_.setText(value_.toString());
246  }
247 
248  public boolean isOk(double v) {
249  return (min_ <= v && v<=max_);
250  }
251 
252  public void setEnabled(boolean flag) {
253  super.setEnabled(flag);
254  Control[] cmps = getChildren();
255  for (int i = 0; i < cmps.length; i++) {
256  cmps[i].setEnabled(flag);
257  }
258  }
259 
260  private void incrementValue(){
261  double v = text2value() + step_;
262  setValue(v);
263  updateValue();
264  }
265  private void decrementValue(){
266  double v = text2value() - step_;
267  setValue(v);
268  updateValue();
269  }
270  private void startIncrementTimer(){
271  mouseBtnActionNum = 0;
272 
273  Display display = text_.getDisplay();
274  if (!display.isDisposed())
275  {
276  display.timerExec(mouseBtnRepeat_, incrementRun_);
277  }
278  }
279  private void stopIncrementTimer(){
280  mouseBtnActionNum = 0;
281 
282  Display display = text_.getDisplay();
283  if (!display.isDisposed())
284  {
285  display.timerExec(-1, incrementRun_);
286  }
287  }
288  private void startDecrementTimer(){
289  mouseBtnActionNum = 0;
290 
291  Display display = text_.getDisplay();
292  if (!display.isDisposed())
293  {
294  display.timerExec(mouseBtnRepeat_, decrementRun_);
295  }
296  }
297  private void stopDecrementTimer(){
298  mouseBtnActionNum = 0;
299 
300  Display display = text_.getDisplay();
301  if (!display.isDisposed())
302  {
303  display.timerExec(-1, decrementRun_);
304  }
305  }
306 
307  public void fixValue(){
308  setValue(text_.getText());
309  updateValue();
310  }
311 
312  // 値が変更されたときに呼び出される。
313  protected void updateValue(){
314  }
315 }
int c
Definition: autoplay.py:16
static int min(int a, int b)
png_uint_32 i
Definition: png.h:2735
def run(tree, args)
org
png_infop png_uint_32 flag
Definition: png.h:2159
static int max(int a, int b)
SEDoubleTextWithSpinForSWT(Composite parent, int style, double min, double max, double step)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:25