1    package com.instantbank.common.uiutils;
2    
3    import java.awt.GridLayout;
4    import javax.swing.JPanel;
5    import javax.swing.SwingConstants;
6    import javax.swing.JButton;
7    import javax.swing.plaf.basic.BasicArrowButton;
8    
9    /**
10    * Spinner control.
11    *
12    * @author Taken from the Swing book by Robinson/Vorobiev.
13    */
14   class Spinner extends JPanel {
15   
16     /**
17      * Spinner layout (Horizontal or Vertical)
18      */
19     private int orientation = SwingConstants.VERTICAL;
20   
21     /**
22      * Increment spinning button.
23      */
24     private BasicArrowButton btnIncrement;
25   
26     /**
27      * Decrement spinning button.
28      */
29     private BasicArrowButton btnDecrement;
30   
31   
32     /**
33      * Spinner default constructor. Orientation is Vertical.
34      */
35     public Spinner() {
36       createComponents();
37     }
38   
39   
40     /**
41      * Spinner constructor with a fixed orientation.
42      *
43      * @param orientation SwingConstants.VERTICAL or SwingConstants.HORIZONTAL
44      */
45     public Spinner(int orientation) {
46       this.orientation = orientation;
47       createComponents();
48     }
49   
50   
51     /**
52      * Enable/disable the spinner.
53      *
54      * @param enable True => spinner enabled. False => spinner disabled.
55      */
56     public void setEnabled(boolean enable) {
57       btnIncrement.setEnabled(enable);
58       btnDecrement.setEnabled(enable);
59     }
60   
61   
62     /**
63      * Test if the spinner is enabled.
64      *
65      * @return True if the spinner is enabled. False otherwise.
66      */
67     public boolean isEnabled() {
68       return (btnIncrement.isEnabled() &&
69         btnDecrement.isEnabled());
70     }
71   
72   
73     /**
74      * Builds the user interface.
75      */
76     protected void createComponents() {
77       if(orientation == SwingConstants.VERTICAL) {
78         setLayout(new GridLayout(2, 1));
79         btnIncrement = new BasicArrowButton(SwingConstants.NORTH);
80         btnDecrement = new BasicArrowButton(SwingConstants.SOUTH);
81         add(btnIncrement);
82         add(btnDecrement);
83       }
84       else if(orientation == SwingConstants.HORIZONTAL) {
85         setLayout(new GridLayout(1, 2));
86         btnIncrement = new BasicArrowButton(SwingConstants.EAST);
87         btnDecrement = new BasicArrowButton(SwingConstants.WEST);
88         add(btnDecrement);
89         add(btnIncrement);
90       }
91     }
92   
93   
94     /**
95      * Delivers the increment button. Useful for the caller to add
96      * listeners to this button.
97      *
98      * @return The incrementButton value
99      */
100    public JButton getIncrementButton() {
101      return (btnIncrement);
102    }
103  
104  
105    /**
106     * Delivers the decrement button. Useful for the caller to add
107     * listeners to this button.
108     *
109     * @return The decrementButton value
110     */
111    public JButton getDecrementButton() {
112      return (btnDecrement);
113    }
114  }
115