JIPTextField.java 6.8 KB

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