ItemPropertyDoubleSpin.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.util;
00018 
00019 import java.awt.BorderLayout;
00020 import java.awt.Component;
00021 import java.awt.event.ActionEvent;
00022 import java.awt.event.ActionListener;
00023 
00024 import javax.swing.JPanel;
00025 import javax.swing.JTextField;
00026 
00027 import com.generalrobotix.ui.GrxBaseItem;
00028 import com.generalrobotix.ui.view.graph.SEDouble;
00029 import com.generalrobotix.ui.view.graph.SpinControl;
00030 import com.generalrobotix.ui.view.graph.SpinListener;
00031 
00032 @SuppressWarnings("serial")
00033 public class ItemPropertyDoubleSpin extends JPanel implements SpinListener {
00034     private SEDouble value_;
00035     private double max_;
00036     private double min_;
00037     private double step_;
00038     private JTextField text_;
00039     
00040     private GrxBaseItem item_;
00041     private String key_;
00042     
00043     public ItemPropertyDoubleSpin(double min,double max,double step) {
00044         value_ = new SEDouble(0);
00045         
00046         max_ = max;
00047         min_ = min;
00048         step_ = step;
00049         SpinControl spin = new SpinControl(this);
00050         text_ = new JTextField();
00051         text_.addActionListener(
00052             new ActionListener() {
00053                public void actionPerformed(ActionEvent e) {
00054                    setValue(text_.getText());
00055                }
00056             }
00057         );
00058         
00059         setLayout(new BorderLayout());
00060         add(BorderLayout.CENTER,text_);
00061         add(BorderLayout.EAST,spin);
00062         
00063         setValue((max+min)/2);
00064     }
00065 
00066     public void up() {
00067         setValue(text_.getText());
00068         double v = value_.doubleValue() + step_;
00069         setValue(v);
00070     }
00071 
00072     public void down() {
00073         setValue(text_.getText());
00074         double v = value_.doubleValue() - step_;
00075         setValue(v);
00076     }
00077     
00078     public double getValue() {
00079         setValue(text_.getText());
00080         return value_.doubleValue();
00081     }
00082     
00083     public void setValue(String s) {
00084         double v = 0;
00085         try {
00086                 v = new SEDouble(s).doubleValue();
00087         } catch (Exception e) {
00088                 v = value_.doubleValue();
00089         }
00090         
00091         setValue(v);
00092     }
00093     
00094     public void setValue(double v) {
00095         if (isOk(v)) {
00096             value_.setValue(new Double(v));
00097         }
00098         
00099                 text_.setText(value_.toString());
00100                 
00101         if (item_ != null && key_ != null)
00102                 item_.setDbl(key_, v);
00103     }
00104 
00105     public boolean isOk(double v) {
00106         return (min_ <= v && v<=max_);
00107     }
00108 
00109     public void setEnabled(boolean flag) {
00110         super.setEnabled(flag);
00111         Component[] cmps = getComponents();
00112         for (int i = 0; i < cmps.length; i++) {
00113             cmps[i].setEnabled(flag);
00114         }
00115     }
00116     
00117     public void setItem(GrxBaseItem item, String key) {
00118         if (item_ != null && key_ != null)
00119                 item_.setProperty(key_, value_.toString());
00120         item_ = item;
00121         key_ = key;
00122     }
00123 }


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:17