Hi leute war auf der suche nach einem eingabefeld für ip adressen ... da ich nur ASIPAddressField finden konnte und das nicht kostenfrei ist hab ich mir einfach selber etwas geschrieben.
der code ist zwar noch etwas quick&dirty, es entspricht nicht der ip-eingabe von windows aber es funktioniert und könnte sicher für den ein oder anderen nützlich sein.
freu mich bereits auf feedback ... bin für alle verbesserungsvorschläge offen.
hf
der code ist zwar noch etwas quick&dirty, es entspricht nicht der ip-eingabe von windows aber es funktioniert und könnte sicher für den ein oder anderen nützlich sein.
freu mich bereits auf feedback ... bin für alle verbesserungsvorschläge offen.
hf
Java:
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;
// (c) JohnVonWelebil
//IPv4
public class IP
{
public final static int ONE = 0, TWO =1, THREE =2, FOUR =3; //um direktes verändern zu verhinden
private String one, two, three, four; //string damit ich auch nichts drin stehen haben kann... könnte natürlich auch im toString berücksichtigt werden
public IP()
{
reset();
}
public IP(String ip) throws BadLocationException
{
if(!setIP(ip)) reset();
}
public static boolean isIp(String ip)
{
int counterOfDot = 0;
for (int i = 0; i < ip.length(); i++)
{
if(ip.charAt(i) == '.') counterOfDot++;
if(!('0' <= ip.charAt(i) && ip.charAt(i) <= '9' || ip.charAt(i) == '.')) return false;
}
if(counterOfDot != 3) return false;
int beginIndex = 0, endIndex = 0, num;
String temp;
int position = IP.ONE;
while(endIndex >= 0 && position<=IP.FOUR) // überprüft ob die zahlen im bereich einer ip sind
{
endIndex = ip.indexOf('.', beginIndex);
if(endIndex < 0) endIndex = ip.length();
temp = ip.substring(beginIndex, endIndex);
if(temp.compareTo("") != 0)
{
num = Integer.parseInt(temp);
correctNumber(num, position);
}
beginIndex = endIndex + 1;
position++;
}
return true;
}
private static int correctNumber(int number,int position)
{
if(number<0 || number>255)
{
if(number<=0)number=0;
else number = 255;
Toolkit.getDefaultToolkit().beep();
}
if((number<=0 || number>=255) && position == FOUR)// kein broadcast und normales netzwerk
{
if(number<=0)number=1;
else number = 254;
Toolkit.getDefaultToolkit().beep();
}
if(number>223 && position == ONE) //wikipedia.org
{
number = 223;
Toolkit.getDefaultToolkit().beep();
}
return number;
}
public boolean setIP(String ip) throws BadLocationException
{
if(!isIp(ip)) return false;
int beginIndex = 0, endIndex = 0, position = IP.ONE;
String temp;
while(endIndex < ip.length())
{
endIndex = ip.indexOf('.', beginIndex);
if(endIndex < 0) endIndex = ip.length();
temp = ip.substring(beginIndex, endIndex);
if(temp.compareTo("") == 0) resetElementAt(position);
else setElementAt(Integer.parseInt(temp), position);
beginIndex = endIndex + 1;
position++;
}
return true;
}
public boolean setElementAt(Integer number, int position)
{
number = correctNumber(number, position);
switch(position)
{
case ONE: one = number.toString();break;
case TWO: two = number.toString();break;
case THREE: three = number.toString();break;
case FOUR: four = number.toString();break;
}
return true;
}
public void reset()
{
for(int i=0; i<4; i++)
{
resetElementAt(i);
}
}
public void resetElementAt(int position)
{
switch(position)
{
case ONE: one = ""; break;
case TWO: two = ""; break;
case THREE: three = ""; break;
case FOUR: four = ""; break;
}
}
public String getElementAt(int position)
{
switch(position)
{
case ONE: return one;
case TWO: return two;
case THREE: return three;
case FOUR: return four;
default: return null;
}
}
public Integer getNumberAt(int position)
{
switch(position)
{
case ONE: return Integer.parseInt(one);
case TWO: return Integer.parseInt(two);
case THREE: return Integer.parseInt(three);
case FOUR: return Integer.parseInt(four);
default: return null;
}
}
public String getIP()
{
return toString();
}
public String toString()
{
return one+"."+two+"."+three+"."+four;
}
}
Java:
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
// (c) JohnVonWelebil
public class IPDocument extends PlainDocument
{
IP ip;
private IPField field;
int cursor;
AttributeSet a;
public IPDocument(IPField field) throws BadLocationException
{
super();
ip = new IP();
this.field = field;
reload();
}
public IPDocument(IPField field, String ipAddress) throws BadLocationException
{
super();
ip = new IP();
this.field = field;
ip.setIP(ipAddress);
reload();
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
String text = field.getText();
this.cursor = offs;
this.a = a;
str.trim();
for (int i = 0; i < str.length(); i++)
{
if(!('0' <= str.charAt(i) && str.charAt(i) <= '9' || str.charAt(i) == '.')) return;
}
if(str.length() > 15) return;
else if(IP.isIp(str))
{
ip.setIP(str);
reload();
}
else if(str.charAt(0)== '.' && str.length()==1)
{
field.setCaretPosition(text.indexOf(".", cursor)+1);
}
else if(Integer.parseInt(str)<=254)
{
int position = IP.ONE;
int search = text.indexOf(".",0);
while(cursor>search && search!=-1)
{
search = text.indexOf(".",search+1);
position++;
}
String number = ip.getElementAt(position);
int length = number.length();
if(number == null) return;
if (search == -1)search = text.length();
number = number.substring(0, length-(search-cursor))+str+number.substring(length-(search-cursor), length);//zahl dazufügen
if(!ip.setElementAt(Integer.parseInt(number), position)) return;
if(number.length()==3 && position != IP.FOUR) cursor++;
cursor++;
reload();
}
}
public void remove(int offs, int len) throws BadLocationException
{
String selected = field.getText().substring(offs, offs+len);
String dots = "";
for (int i = 0; i < selected.length(); i++)
{
if(selected.charAt(i) == '.') dots+=".";
}
super.remove(offs, len);
super.insertString(offs, dots, null);
ip.setIP(field.getText());
}
private void reload() throws BadLocationException
{
super.remove(0, field.getText().length());
super.insertString(0, ip.toString(), a);
if(cursor > 15)cursor = 15;
field.setCaretPosition(cursor);
}
}
Java:
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
// (c) JohnVonWelebil
public class IPField extends JTextField
{
public IPField() throws BadLocationException
{
super(15);
setDocument(new IPDocument(this));
setHorizontalAlignment(JTextField.CENTER);
}
public IPField(String ip) throws BadLocationException
{
super(15);
setDocument(new IPDocument(this, ip));
setHorizontalAlignment(JTextField.CENTER);
}
}
Zuletzt bearbeitet: