123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- package com.l2jserver.configurator;
- import java.awt.Component;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.net.Inet4Address;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.util.LinkedList;
- import java.util.List;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.PlainDocument;
- public class JIPTextField extends JPanel implements FocusListener
- {
-
- private static final long serialVersionUID = 1L;
- private JTextField[] _textFields;
- private List<FocusListener> _focusListeners;
-
- public JIPTextField(String textIp)
- {
- super.addFocusListener(this);
-
- initIPTextField(textIp);
-
- for (int i = 0; i < _textFields.length; i++)
- {
- _textFields[i].addFocusListener(this);
- }
- }
-
- public JIPTextField()
- {
- this("...");
- }
-
-
- public JIPTextField(Inet4Address value)
- {
- this(value.getHostAddress());
- }
-
-
- private void initIPTextField(String textIp)
- {
- final ActionListener nextfocusaction = new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- ((Component) evt.getSource()).transferFocus();
- }
- };
-
- this.setLayout(new GridBagLayout());
- _textFields = new JTextField[4];
-
- GridBagConstraints cons = new GridBagConstraints();
- cons.anchor = GridBagConstraints.PAGE_START;
- cons.fill = GridBagConstraints.HORIZONTAL;
- cons.insets = new Insets(1, 1, 1, 1);
- cons.gridx = 0;
- cons.gridy = 0;
-
-
- MaxLengthDocument previous = null;
- String[] parts = textIp.split("\\.");
- for (int i = 0; i < 4; i++)
- {
- String str = parts[i];
- if (i > 0)
- {
- JLabel dot = new JLabel(".");
- cons.weightx = 0;
- add(dot, cons);
- cons.gridx++;
- }
- MaxLengthDocument maxDoc = new MaxLengthDocument(3);
- _textFields[i] = new JTextField(maxDoc, str, 3);
- if (previous != null)
- {
- previous.setNext(_textFields[i]);
- }
- previous = maxDoc;
-
- add(_textFields[i], cons);
- _textFields[i].addActionListener(nextfocusaction);
- cons.gridx++;
- }
- }
-
- @Override
- public void addFocusListener(FocusListener fl)
- {
- if (_focusListeners == null)
- {
- _focusListeners = new LinkedList<FocusListener>();
- }
-
- if (fl != null && !_focusListeners.contains(fl))
- {
- _focusListeners.add(fl);
- }
- }
-
- @Override
- public void removeFocusListener(FocusListener fl)
- {
- if (_focusListeners != null)
- {
- _focusListeners.remove(fl);
- }
- }
-
- public String getText()
- {
- String str = "";
- for (int i = 0; i < 4; i++)
- {
- if (_textFields[i].getText().length() == 0)
- {
- str += "0";
- }
- else
- {
- str += _textFields[i].getText();
- }
- if (i < 3)
- {
- str += ".";
- }
- }
- return str;
- }
-
- public void setText(String str)
- {
- try
- {
-
- str.length();
-
- InetAddress ip = InetAddress.getByName(str);
- byte b[] = ip.getAddress();
- for (int i = 0; i < 4; i++)
- {
-
- if (b[i] >= 0)
- {
- _textFields[i].setText(Byte.toString(b[i]));
- }
- else
- {
- _textFields[i].setText(Integer.toString(b[i] + 256));
- }
- }
- return;
- }
- catch (UnknownHostException ex)
- {
- }
- catch (NullPointerException npe)
- {
- }
- for (int i = 0; i < 4; i++)
- {
- _textFields[i].setText("");
- }
- }
-
- @Override
- public void setEnabled(boolean enabled)
- {
- for(int i=0;i<_textFields.length;i++)
- {
- if(_textFields[i] != null)
- {
- _textFields[i].setEnabled(enabled);
- }
- }
- }
-
- public boolean isEmpty()
- {
- for (int i = 0; i < 4; i++)
- {
- if (_textFields[i].getText().length() != 0)
- {
- return false;
- }
- }
- return true;
- }
-
- public boolean isCorrect()
- {
- for (int i = 0; i < 4; i++)
- {
- if (_textFields[i].getText().length() == 0)
- {
- return false;
- }
- }
- return true;
- }
-
-
- public void focusGained(FocusEvent event)
- {
- if (_focusListeners != null)
- {
- for (FocusListener fl : _focusListeners)
- {
- fl.focusGained(event);
- }
- }
- }
-
- public void focusLost(FocusEvent event)
- {
- if (isCorrect() || isEmpty())
- {
- if (_focusListeners != null)
- {
- for (FocusListener fl : _focusListeners)
- {
- fl.focusLost(event);
- }
- }
- }
- }
-
- public class MaxLengthDocument extends PlainDocument
- {
-
-
- private static final long serialVersionUID = 1L;
-
- private int _max;
- private JTextField _next;
-
- public MaxLengthDocument(int maxLength)
- {
- this(maxLength, null);
- }
-
- public MaxLengthDocument(int maxLength, JTextField next)
- {
- _max = maxLength;
- setNext(next);
- }
-
- @Override
- public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
- {
- if (getLength() + str.length() > _max)
- {
- if (getNext() != null)
- {
- if (this.getNext().getText().length() > 0)
- {
- this.getNext().select(0, this.getNext().getText().length());
- }
- else
- {
- this.getNext().getDocument().insertString(0, str, a);
- }
- getNext().requestFocusInWindow();
- }
- else
- {
- Toolkit.getDefaultToolkit().beep();
- }
- }
- else
- {
- super.insertString(offset, str, a);
- }
- }
-
-
- public void setNext(JTextField next)
- {
- _next = next;
- }
-
-
- public JTextField getNext()
- {
- return _next;
- }
- }
- }
|