001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.metadata.range.swing.stringhandling;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023:
024: import javax.swing.*;
025: import javax.swing.text.*;
026:
027: import org.openharmonise.vfs.metadata.range.*;
028:
029: /**
030:
031: * @author Matthew Large
032: *
033: */
034: public class Counter extends JPanel implements KeyListener {
035:
036: private JTextField m_digit_1 = null;
037: private JTextField m_digit_2 = null;
038: private JTextField m_digit_3 = null;
039:
040: private int m_count = 0;
041:
042: private JTextComponent m_textComp = null;
043:
044: private StringRange m_range = null;
045:
046: private Color COLOR_OK = Color.BLACK;
047:
048: /**
049: *
050: */
051: public Counter(JTextComponent textComp, StringRange range) {
052: super ();
053: this .m_textComp = textComp;
054: this .m_range = range;
055: this .setup();
056: }
057:
058: /**
059: * @param arg0
060: */
061: private Counter(boolean arg0) {
062: super (arg0);
063: }
064:
065: /**
066: * @param arg0
067: */
068: private Counter(LayoutManager arg0) {
069: super (arg0);
070: }
071:
072: /**
073: * @param arg0
074: * @param arg1
075: */
076: private Counter(LayoutManager arg0, boolean arg1) {
077: super (arg0, arg1);
078: }
079:
080: private void setup() {
081:
082: this .m_textComp.addKeyListener(this );
083: this .setFocusable(false);
084: this .setLayout(null);
085:
086: String fontName = "Dialog";
087: int fontSize = 11;
088: Font font = new Font(fontName, Font.PLAIN, fontSize);
089: Font boldFont = new Font(fontName, Font.BOLD, fontSize);
090:
091: m_digit_1 = new JTextField(" 0");
092: m_digit_1.setFont(font);
093: m_digit_1.setBorder(BorderFactory.createEmptyBorder());
094: m_digit_1.setBackground(COLOR_OK);
095: m_digit_1.setForeground(Color.WHITE);
096: m_digit_1.setSize(9, 20);
097: m_digit_1.setEditable(false);
098: m_digit_1.setFocusable(false);
099: this .add(m_digit_1);
100: m_digit_1.setLocation(0, 0);
101:
102: m_digit_2 = new JTextField(" 0");
103: m_digit_2.setBorder(BorderFactory.createEmptyBorder());
104: m_digit_2.setBackground(COLOR_OK);
105: m_digit_2.setForeground(Color.WHITE);
106: m_digit_2.setSize(9, 20);
107: m_digit_2.setEditable(false);
108: m_digit_2.setFocusable(false);
109: this .add(m_digit_2);
110: m_digit_2.setLocation(10, 0);
111:
112: m_digit_3 = new JTextField(" 0");
113: m_digit_3.setBorder(BorderFactory.createEmptyBorder());
114: m_digit_3.setBackground(COLOR_OK);
115: m_digit_3.setForeground(Color.WHITE);
116: m_digit_3.setSize(9, 20);
117: m_digit_3.setEditable(false);
118: m_digit_3.setFocusable(false);
119: this .add(m_digit_3);
120: m_digit_3.setLocation(20, 0);
121:
122: this .setPreferredSize(new Dimension(31, 25));
123: this .refreshCount();
124: }
125:
126: private void refreshCount() {
127: this .m_count = this .m_range.getMaxLength()
128: - this .m_textComp.getText().length();
129:
130: m_digit_1.setText("");
131: m_digit_2.setText("");
132: m_digit_3.setText("");
133:
134: String 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: }
|