JIPTextField.java 6.8 KB

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