JIPTextField.java 6.7 KB

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