KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > GUI > IpAddressEditor


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.GUI;
25
26 import net.fenyo.gnetwatch.Config;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.eclipse.swt.events.*;
31 import org.eclipse.swt.widgets.*;
32
33 /**
34  * This class implements an IPv4 address editor.
35  * @author Alexandre Fenyo
36  * @version $Id: IpAddressEditor.java,v 1.9 2007/03/03 00:38:19 fenyo Exp $
37  */

38
39 public class IpAddressEditor implements KeyListener {
40   public static final long serialVersionUID = 1L;
41
42   private static Log log = LogFactory.getLog(IpAddressEditor.class);
43
44   private final Text text;
45   private final Config config;
46
47   /**
48    * Constructor.
49    * @param text control that is managed by this editor.
50    */

51   // GUI thread
52
public IpAddressEditor(final Config config, final Text text) {
53     this.text = text;
54     this.config = config;
55     text.setText("000.000.000.000");
56     text.addKeyListener(this);
57   }
58
59   /**
60    * Handles a key event.
61    * @param e event.
62    * @return void.
63    */

64   private void handleKey(KeyEvent e) {
65     String JavaDoc content = text.getText();
66     int position = text.getCaretPosition();
67     // 000.000.000.000
68
if (e.character == '.') {
69       if (position <= 3) position = 4;
70       else if (position > 3 && position <= 7) position = 8;
71       else if (position > 7 && position <= 11) position = 12;
72       text.setSelection(position, position);
73       return;
74     }
75     if (e.character == '\10') {
76       if (position > 0) {
77         position--;
78         text.setSelection(position, position);
79       }
80       return;
81     }
82     if (e.character == '\177') {
83       if (position < 15) {
84         position++;
85         text.setSelection(position, position);
86       }
87       return;
88     }
89     if (position >= 15) position = 14;
90     if (position == 3 || position == 7 || position == 11) position++;
91     String JavaDoc new_content = content.substring(0, position) + e.character + content.substring(position + 1);
92     if (new_content.matches("^[0-9][0-9][0-9]\\.[0-9][0-9][0-9]\\.[0-9][0-9][0-9]\\.[0-9][0-9][0-9]$") == false) return;
93     if (new Integer JavaDoc(new_content.substring(0, 3)).intValue() > 255 ||
94       new Integer JavaDoc(new_content.substring(4, 7)).intValue() > 255 ||
95       new Integer JavaDoc(new_content.substring(8, 11)).intValue() > 255 ||
96       new Integer JavaDoc(new_content.substring(12, 15)).intValue() > 255) {
97       if (position > 13) return;
98       new_content = content.substring(0, position) + e.character + '0' + content.substring(position + 2);
99       if (new Integer JavaDoc(new_content.substring(0, 3)).intValue() > 255 ||
100           new Integer JavaDoc(new_content.substring(4, 7)).intValue() > 255 ||
101           new Integer JavaDoc(new_content.substring(8, 11)).intValue() > 255 ||
102           new Integer JavaDoc(new_content.substring(12, 15)).intValue() > 255)
103         return;
104       text.setText(new_content);
105       if (position < 15) position++;
106       text.setSelection(position, position);
107       return;
108     }
109     text.setText(new_content);
110     if (position < 15) position++;
111     text.setSelection(position, position);
112   }
113
114   /**
115    * Handles a key press event.
116    * @param e event.
117    * @return void.
118    */

119   // GUI thread
120
public void keyPressed(KeyEvent e) {
121     if (config.getProperty("ipaddresseditor.insertonkeypressed").equals("true"))
122       handleKey(e);
123     }
124
125   /**
126    * Handles a key release event.
127    * @param e event.
128    * @return void.
129    */

130   // GUI thread
131
public void keyReleased(KeyEvent e) {
132     if (!config.getProperty("ipaddresseditor.insertonkeypressed").equals("true"))
133       handleKey(e);
134   }
135
136 }
137
Popular Tags