1 package org.wcb.common;
2
3 import javax.swing.text.PlainDocument;
4 import javax.swing.text.JTextComponent;
5 import javax.swing.text.AttributeSet;
6 import javax.swing.text.BadLocationException;
7
8 /***
9 /**
10 * Copyright (C) 1999 Walter Bogaardt
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 *
26 */
27 public class TimeDocument extends PlainDocument {
28 public static String initString = "24:00";
29 private static int sep1=2;
30 private JTextComponent textComponent;
31 private int newOffset;
32
33 public TimeDocument(JTextComponent tc) {
34 textComponent = tc;
35 try {
36 insertString(0, initString, null);
37 } catch(Exception err){}
38 }
39
40 public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException {
41 if(s.equals(initString)){
42 super.insertString(offset, s, attributeSet);
43 } else {
44 try {
45 Integer.parseInt(s);
46 } catch(Exception err) {
47 return;
48 }
49 newOffset = offset;
50 if(atSeparator(offset)){
51 newOffset++;
52 textComponent.setCaretPosition(newOffset);
53 }
54 super.remove(newOffset,1);
55 super.insertString(newOffset, s, attributeSet);
56 }
57 }
58
59 public void remove(int offset, int length) throws BadLocationException {
60 if(atSeparator(offset))
61 textComponent.setCaretPosition(offset-1);
62 else
63 textComponent.setCaretPosition(offset);
64 }
65
66 private boolean atSeparator(int offset) {
67 return offset == sep1;
68 }
69 }