SpinControl.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
17 package com.generalrobotix.ui.view.graph;
18 
19 import java.util.ArrayList;
20 import java.awt.*;
21 import java.awt.event.*;
22 
23 import javax.swing.*;
24 
25 import javax.swing.plaf.basic.BasicArrowButton;
26 
27 public class SpinControl extends JPanel {
28  private final static int BUTTON_UP =0;
29  private final static int BUTTON_DOWN =1;
30  private int sleepTime_ = 100;
31  private int sleepTimeFirst_ = 400;
32  private int sleepMin_ = 6;
33  JButton btnUp_;
34  JButton btnDown_;
35  boolean stopFlag_;
36  private java.util.List<SpinListener> listenerList_;
37 
38  public static JComponent getComponentWithSpin(
39  JComponent component,
40  SpinControl spin
41  ) {
42  JPanel panel = new JPanel() {
43  //オーバーライト
44  public void setEnabled(boolean flag) {
45  super.setEnabled(flag);
46  Component[] cmps = getComponents();
47  for(int i = 0; i < cmps.length; i ++) {
48  cmps[i].setEnabled(flag);
49  }
50  }
51  };
52 
53  panel.setLayout(new BorderLayout());
54  panel.add(BorderLayout.CENTER,component);
55  panel.add(BorderLayout.EAST, spin);
56  return panel;
57  }
58 
59  public SpinControl(SpinListener listener) {
60  this();
61  addSpinListener(listener);
62  }
63 
64  public SpinControl() {
65  listenerList_ = new ArrayList<SpinListener>();
66 
67  setLayout(new GridLayout(2,1));
68  btnUp_ = new BasicArrowButton(BasicArrowButton.NORTH);
69  btnDown_ = new BasicArrowButton(BasicArrowButton.SOUTH);
70 
71  btnUp_.addMouseListener(new MyListener(BUTTON_UP));
72  btnDown_.addMouseListener(new MyListener(BUTTON_DOWN));
73  add(btnUp_);
74  add(btnDown_);
75  }
76 
77  public void setEnabled(boolean flag) {
78  super.setEnabled(flag);
79  btnUp_.setEnabled(flag);
80  btnDown_.setEnabled(flag);
81  }
82 
83  public void addSpinListener(SpinListener listener) {
84  listenerList_.add(listener);
85  }
86 
87  public void removeSpinListener(SpinListener listener) {
88  listenerList_.remove(listener);
89  }
90 
91 
92  private class MyListener extends MouseAdapter {
93  private int button_;
94  private Timer timer_ = null;
95  public MyListener(int button) {
96  button_ = button;
97  timer_ = null;//ダミー
98  }
99 
100  public void mousePressed(MouseEvent e) {
101  if (!isEnabled()) { return; }
102 
103  if (e.isAltDown() || e.isMetaDown()) { return; }
104 
105  stopFlag_ = false;
106 
107  //1回目は、押しただけで起こる。
108  _broadCast(button_);
109 
110  ActionListener listener = new ActionListener() {
111  private int allCount_ = 0;
112  private int ikiCount_ = 0;
113 
114  //1回目に1/2の処理が入るので初期値は2倍
115  private int sleep_ = sleepTime_ * 2;
116  private int iki_ = 10 / 2;//1/2倍
117  public void actionPerformed(ActionEvent e) {
118  if(ikiCount_ % iki_ ==0){
119  sleep_ = sleep_ / 2;
120  if(sleep_ >= sleepMin_){
121  iki_=iki_*2;
122  ikiCount_ = 0 ;
123  timer_.setDelay(sleep_);
124  //timer_.setInitialDelay(sleep_);
125  //timer_.restart();
126  //System.out.println("sleep_ = " + sleep_);
127  }
128  }
129  allCount_++;
130  ikiCount_++;
131  _broadCast(button_);
132  }
133  };
134  timer_ = new Timer(sleepTimeFirst_,listener);
135  timer_.setInitialDelay(sleepTimeFirst_);
136  timer_.setCoalesce(true);
137  timer_.start();
138  }
139 
140  public void mouseReleased(MouseEvent e) {
141  if (timer_ != null) {
142  timer_.stop();
143  }
144  }
145  }
146 
147  private synchronized void _broadCast(int button) {
148  //System.out.print("" + button);
149  for (int i = 0; i < listenerList_.size(); i ++) {
150  SpinListener listener = (SpinListener)listenerList_.get(i);
151  switch (button) {
152  case BUTTON_UP:
153  listener.up();
154  break;
155  case BUTTON_DOWN:
156  listener.down();
157  break;
158  }
159  }
160  }
161 }
#define null
our own NULL pointer
Definition: IceTypes.h:57
java.util.List< SpinListener > listenerList_
png_uint_32 i
Definition: png.h:2735
void removeSpinListener(SpinListener listener)
void addSpinListener(SpinListener listener)
png_infop png_uint_32 flag
Definition: png.h:2159
static JComponent getComponentWithSpin(JComponent component, SpinControl spin)
synchronized void _broadCast(int button)


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