View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * Vectomatic is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.client.rep.view;
19  
20  import org.vectomatic.client.rep.RepApplication;
21  
22  import com.google.gwt.user.client.ui.ChangeListener;
23  import com.google.gwt.user.client.ui.ClickListener;
24  import com.google.gwt.user.client.ui.FlexTable;
25  import com.google.gwt.user.client.ui.PushButton;
26  import com.google.gwt.user.client.ui.SimplePanel;
27  import com.google.gwt.user.client.ui.TextBox;
28  import com.google.gwt.user.client.ui.Widget;
29  
30  /**
31   * Spinner widget class
32   */
33  public class Spinner extends SimplePanel {
34  	private TextBox _box;
35  	private PushButton _addButton;
36  	private PushButton _subButton;
37  	private int _min;
38  	private int _max;
39  	private int _value;
40  	public Spinner(int min, int max, int value) {
41  		_min = min;
42  		_max = max;
43  		_value = value;
44  		_box = new TextBox();
45  		int length = 1 + (int)((Math.log(_max) / Math.log(10)));
46  		_box.setMaxLength(length);
47  		_box.setVisibleLength(length);
48  		_box.addChangeListener(new ChangeListener() {
49  			public void onChange(Widget sender) {
50  				try {
51  					int value = Integer.parseInt(_box.getText());
52  					if (value >= _min && value <= _max) {
53  						_value = value;
54  					}
55  				} catch(NumberFormatException e) {
56  				}
57  				update();
58  			}
59  		}); 
60  		_addButton = new PushButton(RepApplication.app._icons.upIcon().createImage());
61  		_addButton.setStyleName("spinner-button");
62  		_addButton.addClickListener(new ClickListener() {
63  			public void onClick(Widget sender) {
64  				add();
65  			}		
66  		});
67  		_subButton = new PushButton(RepApplication.app._icons.downIcon().createImage());
68  		_subButton.setStyleName("spinner-button");
69  		_subButton.addClickListener(new ClickListener() {
70  			public void onClick(Widget sender) {
71  				sub();
72  			}		
73  		});
74  		FlexTable table = new FlexTable();
75  		table.setWidget(0, 0, _box);
76  		table.setWidget(0, 1, _addButton);
77  		table.setWidget(1, 0, _subButton);
78  		table.getFlexCellFormatter().setRowSpan(0, 0, 2);
79  		table.setCellSpacing(0);
80  		setWidget(table);
81  		update();
82  	}
83  	
84  	public void add() {
85  		_value++;
86  		update();
87  	}
88  	public void sub() {
89  		_value--;
90  		update();
91  	}
92  	public int getValue() {
93  		return _value;
94  	}
95  	public void setValue(int value) {
96  		_value = value;
97  		update();
98  	}
99  	public void update() {
100 		_addButton.setEnabled(_value < _max);
101 		_subButton.setEnabled(_value > _min);
102 		_box.setText(Integer.toString(_value));						
103 	}
104 }