SpinControl.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00017 package com.generalrobotix.ui.view.graph;
00018 
00019 import java.util.ArrayList;
00020 import java.awt.*;
00021 import java.awt.event.*;
00022 
00023 import javax.swing.*;
00024 
00025 import javax.swing.plaf.basic.BasicArrowButton;
00026 
00027 public class SpinControl extends JPanel {
00028     private final static int BUTTON_UP =0;
00029     private final static int BUTTON_DOWN =1;
00030     private int sleepTime_ = 100;
00031     private int sleepTimeFirst_  = 400;
00032     private int sleepMin_  = 6;
00033     JButton btnUp_;
00034     JButton btnDown_;
00035     boolean stopFlag_;
00036     private java.util.List<SpinListener> listenerList_;
00037 
00038     public static JComponent getComponentWithSpin(
00039         JComponent component,
00040         SpinControl spin
00041     ) {
00042         JPanel panel = new JPanel() {   
00043             //オーバーライト
00044             public void setEnabled(boolean flag) {
00045                 super.setEnabled(flag);
00046                 Component[] cmps = getComponents();
00047                 for(int i = 0; i < cmps.length; i ++) {
00048                     cmps[i].setEnabled(flag);
00049                 }
00050             }
00051         };
00052 
00053         panel.setLayout(new BorderLayout());
00054         panel.add(BorderLayout.CENTER,component);
00055         panel.add(BorderLayout.EAST, spin);
00056         return panel;
00057     }
00058 
00059     public SpinControl(SpinListener listener) {
00060         this();
00061         addSpinListener(listener);
00062     }
00063 
00064     public SpinControl() {
00065         listenerList_ = new ArrayList<SpinListener>();
00066         
00067         setLayout(new GridLayout(2,1));
00068         btnUp_ = new BasicArrowButton(BasicArrowButton.NORTH);
00069         btnDown_ = new BasicArrowButton(BasicArrowButton.SOUTH);
00070         
00071         btnUp_.addMouseListener(new MyListener(BUTTON_UP));
00072         btnDown_.addMouseListener(new MyListener(BUTTON_DOWN));
00073         add(btnUp_);
00074         add(btnDown_);
00075     }
00076 
00077     public void setEnabled(boolean flag) {
00078         super.setEnabled(flag);
00079         btnUp_.setEnabled(flag);
00080         btnDown_.setEnabled(flag);
00081     }
00082 
00083     public void addSpinListener(SpinListener listener) {
00084         listenerList_.add(listener);
00085     }
00086     
00087     public void removeSpinListener(SpinListener listener) {
00088         listenerList_.remove(listener);
00089     }
00090     
00091     
00092     private class MyListener extends MouseAdapter {
00093         private int button_;
00094         private Timer timer_ = null;
00095         public MyListener(int button) {
00096             button_ = button;
00097             timer_ = null;//ダミー
00098         }
00099 
00100         public void mousePressed(MouseEvent e) {
00101             if (!isEnabled()) { return; }
00102 
00103             if (e.isAltDown() || e.isMetaDown()) { return; }
00104 
00105             stopFlag_ = false;
00106 
00107             //1回目は、押しただけで起こる。
00108             _broadCast(button_);
00109             
00110             ActionListener listener = new ActionListener() {
00111                 private int allCount_ = 0;
00112                 private int ikiCount_ = 0;
00113 
00114                 //1回目に1/2の処理が入るので初期値は2倍
00115                 private int sleep_ = sleepTime_ * 2;
00116                 private int iki_ = 10 / 2;//1/2倍
00117                 public void actionPerformed(ActionEvent e) {
00118                     if(ikiCount_  % iki_ ==0){
00119                         sleep_ = sleep_ / 2;
00120                         if(sleep_ >= sleepMin_){
00121                             iki_=iki_*2;
00122                             ikiCount_ = 0 ;
00123                             timer_.setDelay(sleep_);
00124                             //timer_.setInitialDelay(sleep_);
00125                             //timer_.restart();
00126                             //System.out.println("sleep_ = " + sleep_);
00127                         }
00128                     }
00129                     allCount_++;
00130                     ikiCount_++;
00131                     _broadCast(button_);
00132                 }
00133             };
00134             timer_ = new Timer(sleepTimeFirst_,listener);
00135             timer_.setInitialDelay(sleepTimeFirst_);
00136             timer_.setCoalesce(true);
00137             timer_.start();
00138         }
00139 
00140         public void mouseReleased(MouseEvent e) {
00141             if (timer_ != null) {
00142                 timer_.stop();
00143             }
00144         }
00145     }
00146 
00147     private synchronized void _broadCast(int button) {
00148         //System.out.print("" + button);
00149         for (int i = 0; i < listenerList_.size(); i ++) {
00150             SpinListener listener = (SpinListener)listenerList_.get(i);
00151             switch (button) {
00152             case BUTTON_UP:
00153                 listener.up();
00154                 break;
00155             case BUTTON_DOWN:
00156                 listener.down();
00157                 break;
00158             }
00159         }
00160     }
00161 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sun Apr 2 2017 03:43:57