JIPTextField.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.tools.configurator;
  20. import java.awt.Component;
  21. import java.awt.GridBagConstraints;
  22. import java.awt.GridBagLayout;
  23. import java.awt.Insets;
  24. import java.awt.Toolkit;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.FocusEvent;
  27. import java.awt.event.FocusListener;
  28. import java.net.Inet4Address;
  29. import java.net.InetAddress;
  30. import java.net.UnknownHostException;
  31. import java.util.LinkedList;
  32. import java.util.List;
  33. import javax.swing.JLabel;
  34. import javax.swing.JPanel;
  35. import javax.swing.JTextField;
  36. import javax.swing.text.AttributeSet;
  37. import javax.swing.text.BadLocationException;
  38. import javax.swing.text.PlainDocument;
  39. /**
  40. * @author KenM
  41. */
  42. public class JIPTextField extends JPanel implements FocusListener
  43. {
  44. /**
  45. * Comment for <code>serialVersionUID</code>
  46. */
  47. private static final long serialVersionUID = 1L;
  48. private JTextField[] _textFields;
  49. private List<FocusListener> _focusListeners;
  50. public JIPTextField(String textIp)
  51. {
  52. super.addFocusListener(this);
  53. initIPTextField(textIp);
  54. for (JTextField _textField : _textFields)
  55. {
  56. _textField.addFocusListener(this);
  57. }
  58. }
  59. public JIPTextField()
  60. {
  61. this("...");
  62. }
  63. /**
  64. * @param value
  65. */
  66. public JIPTextField(Inet4Address value)
  67. {
  68. this(value.getHostAddress());
  69. }
  70. private void initIPTextField(String textIp)
  71. {
  72. final ActionListener nextfocusaction = evt -> ((Component) evt.getSource()).transferFocus();
  73. setLayout(new GridBagLayout());
  74. _textFields = new JTextField[4];
  75. GridBagConstraints cons = new GridBagConstraints();
  76. cons.anchor = GridBagConstraints.PAGE_START;
  77. cons.fill = GridBagConstraints.HORIZONTAL;
  78. cons.insets = new Insets(1, 1, 1, 1);
  79. cons.gridx = 0;
  80. cons.gridy = 0;
  81. MaxLengthDocument previous = null;
  82. String[] parts = textIp.split("\\.");
  83. for (int i = 0; i < 4; i++)
  84. {
  85. String str = parts[i];
  86. if (i > 0)
  87. {
  88. JLabel dot = new JLabel(".");
  89. cons.weightx = 0;
  90. add(dot, cons);
  91. cons.gridx++;
  92. }
  93. MaxLengthDocument maxDoc = new MaxLengthDocument(3);
  94. _textFields[i] = new JTextField(maxDoc, str, 3);
  95. if (previous != null)
  96. {
  97. previous.setNext(_textFields[i]);
  98. }
  99. previous = maxDoc;
  100. // ic.weightx = 1;
  101. add(_textFields[i], cons);
  102. _textFields[i].addActionListener(nextfocusaction);
  103. cons.gridx++;
  104. }
  105. }
  106. @Override
  107. public void addFocusListener(FocusListener fl)
  108. {
  109. if (_focusListeners == null)
  110. {
  111. _focusListeners = new LinkedList<>();
  112. }
  113. if ((fl != null) && !_focusListeners.contains(fl))
  114. {
  115. _focusListeners.add(fl);
  116. }
  117. }
  118. @Override
  119. public void removeFocusListener(FocusListener fl)
  120. {
  121. if (_focusListeners != null)
  122. {
  123. _focusListeners.remove(fl);
  124. }
  125. }
  126. public String getText()
  127. {
  128. StringBuilder str = new StringBuilder();
  129. for (int i = 0; i < 4; i++)
  130. {
  131. if (_textFields[i].getText().length() == 0)
  132. {
  133. str.append('0');
  134. }
  135. else
  136. {
  137. str.append(_textFields[i].getText());
  138. }
  139. if (i < 3)
  140. {
  141. str.append('.');
  142. }
  143. }
  144. return str.toString();
  145. }
  146. public void setText(String str)
  147. {
  148. try
  149. {
  150. // make sure string is not null; throw a NullPointerException otherwise
  151. str.length();
  152. InetAddress ip = InetAddress.getByName(str);
  153. byte b[] = ip.getAddress();
  154. for (int i = 0; i < 4; i++)
  155. {
  156. // byte always have a sign in Java, IP addresses aren't
  157. if (b[i] >= 0)
  158. {
  159. _textFields[i].setText(Byte.toString(b[i]));
  160. }
  161. else
  162. {
  163. _textFields[i].setText(Integer.toString(b[i] + 256));
  164. }
  165. }
  166. return;
  167. }
  168. catch (UnknownHostException ex)
  169. {
  170. }
  171. catch (NullPointerException npe)
  172. {
  173. }
  174. for (int i = 0; i < 4; i++)
  175. {
  176. _textFields[i].setText("");
  177. }
  178. }
  179. @Override
  180. public void setEnabled(boolean enabled)
  181. {
  182. for (JTextField _textField : _textFields)
  183. {
  184. if (_textField != null)
  185. {
  186. _textField.setEnabled(enabled);
  187. }
  188. }
  189. }
  190. public boolean isEmpty()
  191. {
  192. for (int i = 0; i < 4; i++)
  193. {
  194. if (!_textFields[i].getText().isEmpty())
  195. {
  196. return false;
  197. }
  198. }
  199. return true;
  200. }
  201. public boolean isCorrect()
  202. {
  203. for (int i = 0; i < 4; i++)
  204. {
  205. if (_textFields[i].getText().length() == 0)
  206. {
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. @Override
  213. public void focusGained(FocusEvent event)
  214. {
  215. if (_focusListeners != null)
  216. {
  217. for (FocusListener fl : _focusListeners)
  218. {
  219. fl.focusGained(event);
  220. }
  221. }
  222. }
  223. @Override
  224. public void focusLost(FocusEvent event)
  225. {
  226. if (isCorrect() || isEmpty())
  227. {
  228. if (_focusListeners != null)
  229. {
  230. for (FocusListener fl : _focusListeners)
  231. {
  232. fl.focusLost(event);
  233. }
  234. }
  235. }
  236. }
  237. public class MaxLengthDocument extends PlainDocument
  238. {
  239. /**
  240. * Comment for <code>serialVersionUID</code>
  241. */
  242. private static final long serialVersionUID = 1L;
  243. private final int _max;
  244. private JTextField _next;
  245. public MaxLengthDocument(int maxLength)
  246. {
  247. this(maxLength, null);
  248. }
  249. public MaxLengthDocument(int maxLength, JTextField next)
  250. {
  251. _max = maxLength;
  252. setNext(next);
  253. }
  254. @Override
  255. public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
  256. {
  257. if ((getLength() + str.length()) > _max)
  258. {
  259. if (getNext() != null)
  260. {
  261. if (getNext().getText().length() > 0)
  262. {
  263. getNext().select(0, getNext().getText().length());
  264. }
  265. else
  266. {
  267. getNext().getDocument().insertString(0, str, a);
  268. }
  269. getNext().requestFocusInWindow();
  270. }
  271. else
  272. {
  273. Toolkit.getDefaultToolkit().beep();
  274. }
  275. }
  276. else
  277. {
  278. super.insertString(offset, str, a);
  279. }
  280. }
  281. /**
  282. * @param next The next to set.
  283. */
  284. public void setNext(JTextField next)
  285. {
  286. _next = next;
  287. }
  288. /**
  289. * @return Returns the next.
  290. */
  291. public JTextField getNext()
  292. {
  293. return _next;
  294. }
  295. }
  296. }