123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- /*
- * Copyright (C) 2004-2015 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.tools.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.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;
- /**
- * @author KenM
- */
- public class JIPTextField extends JPanel implements FocusListener
- {
- /**
- * Comment for <code>serialVersionUID</code>
- */
- private static final long serialVersionUID = 1L;
- private JTextField[] _textFields;
- private List<FocusListener> _focusListeners;
-
- public JIPTextField(String textIp)
- {
- super.addFocusListener(this);
-
- initIPTextField(textIp);
-
- for (JTextField _textField : _textFields)
- {
- _textField.addFocusListener(this);
- }
- }
-
- public JIPTextField()
- {
- this("...");
- }
-
- /**
- * @param value
- */
- public JIPTextField(Inet4Address value)
- {
- this(value.getHostAddress());
- }
-
- private void initIPTextField(String textIp)
- {
- final ActionListener nextfocusaction = evt -> ((Component) evt.getSource()).transferFocus();
-
- 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;
- // ic.weightx = 1;
- add(_textFields[i], cons);
- _textFields[i].addActionListener(nextfocusaction);
- cons.gridx++;
- }
- }
-
- @Override
- public void addFocusListener(FocusListener fl)
- {
- if (_focusListeners == null)
- {
- _focusListeners = new LinkedList<>();
- }
-
- if ((fl != null) && !_focusListeners.contains(fl))
- {
- _focusListeners.add(fl);
- }
- }
-
- @Override
- public void removeFocusListener(FocusListener fl)
- {
- if (_focusListeners != null)
- {
- _focusListeners.remove(fl);
- }
- }
-
- public String getText()
- {
- StringBuilder str = new StringBuilder();
- for (int i = 0; i < 4; i++)
- {
- if (_textFields[i].getText().length() == 0)
- {
- str.append('0');
- }
- else
- {
- str.append(_textFields[i].getText());
- }
-
- if (i < 3)
- {
- str.append('.');
- }
- }
- return str.toString();
- }
-
- public void setText(String str)
- {
- try
- {
- // make sure string is not null; throw a NullPointerException otherwise
- str.length();
-
- InetAddress ip = InetAddress.getByName(str);
- byte b[] = ip.getAddress();
- for (int i = 0; i < 4; i++)
- {
- // byte always have a sign in Java, IP addresses aren't
- 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 (JTextField _textField : _textFields)
- {
- if (_textField != null)
- {
- _textField.setEnabled(enabled);
- }
- }
- }
-
- public boolean isEmpty()
- {
- for (int i = 0; i < 4; i++)
- {
- if (!_textFields[i].getText().isEmpty())
- {
- return false;
- }
- }
- return true;
- }
-
- public boolean isCorrect()
- {
- for (int i = 0; i < 4; i++)
- {
- if (_textFields[i].getText().length() == 0)
- {
- return false;
- }
- }
- return true;
- }
-
- @Override
- public void focusGained(FocusEvent event)
- {
- if (_focusListeners != null)
- {
- for (FocusListener fl : _focusListeners)
- {
- fl.focusGained(event);
- }
- }
- }
-
- @Override
- public void focusLost(FocusEvent event)
- {
- if (isCorrect() || isEmpty())
- {
- if (_focusListeners != null)
- {
- for (FocusListener fl : _focusListeners)
- {
- fl.focusLost(event);
- }
- }
- }
- }
-
- public class MaxLengthDocument extends PlainDocument
- {
-
- /**
- * Comment for <code>serialVersionUID</code>
- */
- private static final long serialVersionUID = 1L;
-
- private final 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 (getNext().getText().length() > 0)
- {
- getNext().select(0, getNext().getText().length());
- }
- else
- {
- getNext().getDocument().insertString(0, str, a);
- }
- getNext().requestFocusInWindow();
- }
- else
- {
- Toolkit.getDefaultToolkit().beep();
- }
- }
- else
- {
- super.insertString(offset, str, a);
- }
- }
-
- /**
- * @param next The next to set.
- */
- public void setNext(JTextField next)
- {
- _next = next;
- }
-
- /**
- * @return Returns the next.
- */
- public JTextField getNext()
- {
- return _next;
- }
- }
- }
|