JIPTextField.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. StringBuilder str = new StringBuilder();
  134. for (int i = 0; i < 4; i++)
  135. {
  136. if (_textFields[i].getText().length() == 0)
  137. str.append("0");
  138. else
  139. str.append(_textFields[i].getText());
  140. if (i < 3)
  141. str.append(".");
  142. }
  143. return str.toString();
  144. }
  145. public void setText(String str)
  146. {
  147. try
  148. {
  149. // make sure string is not null; throw a NullPointerException otherwise
  150. str.length();
  151. InetAddress ip = InetAddress.getByName(str);
  152. byte b[] = ip.getAddress();
  153. for (int i = 0; i < 4; i++)
  154. {
  155. // byte always have a sign in Java, IP addresses aren't
  156. if (b[i] >= 0)
  157. {
  158. _textFields[i].setText(Byte.toString(b[i]));
  159. }
  160. else
  161. {
  162. _textFields[i].setText(Integer.toString(b[i] + 256));
  163. }
  164. }
  165. return;
  166. }
  167. catch (UnknownHostException ex)
  168. {
  169. }
  170. catch (NullPointerException npe)
  171. {
  172. }
  173. for (int i = 0; i < 4; i++)
  174. {
  175. _textFields[i].setText("");
  176. }
  177. }
  178. @Override
  179. public void setEnabled(boolean enabled)
  180. {
  181. for(int i=0;i<_textFields.length;i++)
  182. {
  183. if(_textFields[i] != null)
  184. {
  185. _textFields[i].setEnabled(enabled);
  186. }
  187. }
  188. }
  189. public boolean isEmpty()
  190. {
  191. for (int i = 0; i < 4; i++)
  192. {
  193. if (_textFields[i].getText().length() != 0)
  194. {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. public boolean isCorrect()
  201. {
  202. for (int i = 0; i < 4; i++)
  203. {
  204. if (_textFields[i].getText().length() == 0)
  205. {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. public void focusGained(FocusEvent event)
  212. {
  213. if (_focusListeners != null)
  214. {
  215. for (FocusListener fl : _focusListeners)
  216. {
  217. fl.focusGained(event);
  218. }
  219. }
  220. }
  221. public void focusLost(FocusEvent event)
  222. {
  223. if (isCorrect() || isEmpty())
  224. {
  225. if (_focusListeners != null)
  226. {
  227. for (FocusListener fl : _focusListeners)
  228. {
  229. fl.focusLost(event);
  230. }
  231. }
  232. }
  233. }
  234. public class MaxLengthDocument extends PlainDocument
  235. {
  236. /**
  237. * Comment for <code>serialVersionUID</code>
  238. */
  239. private static final long serialVersionUID = 1L;
  240. private int _max;
  241. private JTextField _next;
  242. public MaxLengthDocument(int maxLength)
  243. {
  244. this(maxLength, null);
  245. }
  246. public MaxLengthDocument(int maxLength, JTextField next)
  247. {
  248. _max = maxLength;
  249. setNext(next);
  250. }
  251. @Override
  252. public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
  253. {
  254. if (getLength() + str.length() > _max)
  255. {
  256. if (getNext() != null)
  257. {
  258. if (this.getNext().getText().length() > 0)
  259. {
  260. this.getNext().select(0, this.getNext().getText().length());
  261. }
  262. else
  263. {
  264. this.getNext().getDocument().insertString(0, str, a);
  265. }
  266. getNext().requestFocusInWindow();
  267. }
  268. else
  269. {
  270. Toolkit.getDefaultToolkit().beep();
  271. }
  272. }
  273. else
  274. {
  275. super.insertString(offset, str, a);
  276. }
  277. }
  278. /**
  279. * @param next The next to set.
  280. */
  281. public void setNext(JTextField next)
  282. {
  283. _next = next;
  284. }
  285. /**
  286. * @return Returns the next.
  287. */
  288. public JTextField getNext()
  289. {
  290. return _next;
  291. }
  292. }
  293. }