Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / java / util / SpinBox.java
1
2 //Title:        Geological editor
3 //Version:
4 //Copyright:    Copyright (c) 1998
5 //Author:       User Interface Group (Nizhny Novgorod)
6 //Company:      EQCC
7 //Description:  Prototype of BRGM project
8
9
10 package util;
11
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.awt.image.*;
15 import javax.swing.*;
16
17
18
19 public abstract class SpinBox extends JPanel implements ActionListener,
20                                                  KeyListener,
21                                                  FocusListener
22 {
23   protected JButton btnUp = new JButton();
24   protected JButton btnDown = new JButton();
25   protected JTextField txtFld = new JTextField(1);
26   JPanel pnlBtn = new JPanel(new GridLayout(2, 1));
27
28   static Icon imgUp = getUpIcon();
29   static Icon imgDown = getDownIcon();
30
31
32 //*********************************************************************
33   public SpinBox()
34   {
35     super();
36     try
37     {
38       jbInit();
39     }
40     catch (Exception e)
41     {
42       e.printStackTrace();
43     }
44     txtFld.setCaretPosition(0);
45   }
46
47 //*********************************************************************
48   private void jbInit() throws Exception
49   {
50     this.setLayout(new BorderLayout());
51     this.add(txtFld, BorderLayout.CENTER);
52
53     Insets aIns = new Insets(1, 1, 1, 1);
54     btnUp.setMargin(aIns);
55     btnDown.setMargin(aIns);
56
57     btnUp.setIcon(imgUp);
58     btnDown.setIcon(imgDown);
59     btnUp.setFocusPainted(false);
60     btnDown.setFocusPainted(false);
61
62     btnUp.addActionListener(this);
63     btnDown.addActionListener(this);
64
65     pnlBtn.add(btnUp);
66     pnlBtn.add(btnDown);
67     this.add(pnlBtn, BorderLayout.EAST);
68
69     txtFld.addKeyListener(this);
70     txtFld.addFocusListener(this);
71   }
72
73 //*********************************************************************
74   private static Icon getUpIcon()
75   {
76     BufferedImage aImg = new BufferedImage(8, 4, BufferedImage.TYPE_INT_RGB);
77     Graphics2D aGr = aImg.createGraphics();
78
79     aGr.setColor(SystemColor.menu);
80     aGr.fillRect(0, 0, 8, 4);
81     int aX[] = {0, 8, 4};
82     int aY[] = {4, 4, 0};
83     aGr.setColor(Color.black);
84     aGr.fillPolygon(aX, aY, 3);
85     ImageIcon aNew = new ImageIcon(aImg);
86     return aNew;
87   }
88
89 //*********************************************************************
90   private static Icon getDownIcon()
91   {
92     BufferedImage aImg = new BufferedImage(8, 4, BufferedImage.TYPE_INT_RGB);
93     Graphics2D aGr = aImg.createGraphics();
94
95     aGr.setColor(SystemColor.menu);
96     aGr.fillRect(0, 0, 8, 4);
97     int aX[] = {1, 3, 7};
98     int aY[] = {0, 3, 0};
99     aGr.setColor(Color.black);
100     aGr.fillPolygon(aX, aY, 3);
101     ImageIcon aNew = new ImageIcon(aImg);
102     return aNew;
103   }
104
105   protected abstract void IncreaseValue();
106   protected abstract void DecreaseValue();
107
108 //*********************************************************************
109   public void setColumns(int aColumns)
110   {
111     txtFld.setColumns(aColumns);
112   }
113
114 //*********************************************************************
115   public int getColumns()
116   {
117     return txtFld.getColumns();
118   }
119
120 //*********************************************************************
121   public void setEnabled(boolean aState)
122   {
123     super.setEnabled(aState);
124     txtFld.setEnabled(aState);
125     btnDown.setEnabled(aState);
126     btnUp.setEnabled(aState);
127   }
128
129 //*********************************************************************
130   public void addFocusListener(FocusListener l)
131   {
132     super.addFocusListener(l);
133     if (txtFld != null) txtFld.addFocusListener(l);
134     if (btnUp != null) btnUp.addFocusListener(l);
135     if (btnDown != null) btnDown.addFocusListener(l);
136   }
137
138 //*********************************************************************
139 //                   ActionListener
140 //*********************************************************************
141   public void actionPerformed(ActionEvent e)
142   {
143     if (e.getSource() instanceof JButton)
144     {
145       JButton aBtn = (JButton)e.getSource();
146       if (aBtn == btnUp) IncreaseValue();
147       else if (aBtn == btnDown) DecreaseValue();
148     }
149   }
150
151 //*********************************************************************
152 //                   KeyListener
153 //*********************************************************************
154   public void keyPressed(KeyEvent e)
155   {
156   }
157
158   public void keyReleased(KeyEvent e)
159   {
160   }
161
162   public void keyTyped(KeyEvent e)
163   {
164   }
165
166 //*********************************************************************
167 //                   FocusListener
168 //*********************************************************************
169   public void focusGained(FocusEvent e)
170   {
171   }
172
173   public void focusLost(FocusEvent e)
174   {
175   }
176 }