KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > stringhandling > Counter


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.metadata.range.swing.stringhandling;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import javax.swing.*;
25 import javax.swing.text.*;
26
27 import org.openharmonise.vfs.metadata.range.*;
28
29
30 /**
31
32  * @author Matthew Large
33  *
34  */

35 public class Counter extends JPanel implements KeyListener {
36
37     private JTextField m_digit_1 = null;
38     private JTextField m_digit_2 = null;
39     private JTextField m_digit_3 = null;
40     
41     private int m_count = 0;
42
43     private JTextComponent m_textComp = null;
44     
45     private StringRange m_range = null;
46     
47     private Color COLOR_OK = Color.BLACK;
48
49     /**
50      *
51      */

52     public Counter(JTextComponent textComp, StringRange range) {
53         super();
54         this.m_textComp = textComp;
55         this.m_range = range;
56         this.setup();
57     }
58
59     /**
60      * @param arg0
61      */

62     private Counter(boolean arg0) {
63         super(arg0);
64     }
65
66     /**
67      * @param arg0
68      */

69     private Counter(LayoutManager arg0) {
70         super(arg0);
71     }
72
73     /**
74      * @param arg0
75      * @param arg1
76      */

77     private Counter(LayoutManager arg0, boolean arg1) {
78         super(arg0, arg1);
79     }
80     
81     private void setup() {
82         
83         this.m_textComp.addKeyListener(this);
84         this.setFocusable(false);
85         this.setLayout(null);
86         
87         String JavaDoc fontName = "Dialog";
88         int fontSize = 11;
89         Font font = new Font(fontName, Font.PLAIN, fontSize);
90         Font boldFont = new Font(fontName, Font.BOLD, fontSize);
91         
92         m_digit_1 = new JTextField(" 0");
93         m_digit_1.setFont(font);
94         m_digit_1.setBorder( BorderFactory.createEmptyBorder() );
95         m_digit_1.setBackground(COLOR_OK);
96         m_digit_1.setForeground(Color.WHITE);
97         m_digit_1.setSize(9,20);
98         m_digit_1.setEditable(false);
99         m_digit_1.setFocusable(false);
100         this.add(m_digit_1);
101         m_digit_1.setLocation(0,0);
102         
103         m_digit_2 = new JTextField(" 0");
104         m_digit_2.setBorder( BorderFactory.createEmptyBorder() );
105         m_digit_2.setBackground(COLOR_OK);
106         m_digit_2.setForeground(Color.WHITE);
107         m_digit_2.setSize(9,20);
108         m_digit_2.setEditable(false);
109         m_digit_2.setFocusable(false);
110         this.add(m_digit_2);
111         m_digit_2.setLocation(10,0);
112         
113         m_digit_3 = new JTextField(" 0");
114         m_digit_3.setBorder( BorderFactory.createEmptyBorder() );
115         m_digit_3.setBackground(COLOR_OK);
116         m_digit_3.setForeground(Color.WHITE);
117         m_digit_3.setSize(9,20);
118         m_digit_3.setEditable(false);
119         m_digit_3.setFocusable(false);
120         this.add(m_digit_3);
121         m_digit_3.setLocation(20,0);
122         
123         this.setPreferredSize( new Dimension(31,25));
124         this.refreshCount();
125     }
126     
127     private void refreshCount() {
128         this.m_count = this.m_range.getMaxLength()-this.m_textComp.getText().length();
129
130         m_digit_1.setText("");
131         m_digit_2.setText("");
132         m_digit_3.setText("");
133         
134         String JavaDoc sCount = Integer.toString(this.m_count).trim();
135         if(sCount.length()==3) {
136             if(this.m_count>-1 || sCount.startsWith("-")) {
137                 m_digit_1.setText(" " + sCount.substring(0,1));
138             }
139             m_digit_2.setText(" " + sCount.substring(1,2));
140             m_digit_3.setText(" " + sCount.substring(2,3));
141         }
142         if(sCount.length()==2) {
143             if(this.m_count>-1) {
144                 m_digit_1.setText(" 0");
145             }
146             m_digit_2.setText(" " + sCount.substring(0,1));
147             m_digit_3.setText(" " + sCount.substring(1,2));
148         }
149         if(sCount.length()==1) {
150             m_digit_1.setText(" 0");
151             m_digit_2.setText(" 0");
152             m_digit_3.setText(" " + sCount);
153         }
154         
155         if( this.m_count<0) {
156             m_digit_1.setBackground(Color.RED);
157             m_digit_2.setBackground(Color.RED);
158             m_digit_3.setBackground(Color.RED);
159         } else if( this.m_count<20) {
160             m_digit_1.setBackground(Color.ORANGE);
161             m_digit_2.setBackground(Color.ORANGE);
162             m_digit_3.setBackground(Color.ORANGE);
163         } else {
164             m_digit_1.setBackground(COLOR_OK);
165             m_digit_2.setBackground(COLOR_OK);
166             m_digit_3.setBackground(COLOR_OK);
167         }
168         
169         this.repaint();
170     }
171
172     /* (non-Javadoc)
173      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
174      */

175     public void keyPressed(KeyEvent arg0) {
176         // NO-OP
177
}
178
179     /* (non-Javadoc)
180      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
181      */

182     public void keyReleased(KeyEvent arg0) {
183         this.refreshCount();
184     }
185
186     /* (non-Javadoc)
187      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
188      */

189     public void keyTyped(KeyEvent arg0) {
190         // NO-OP
191
}
192
193 }
194
Popular Tags