JIPTextField.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. *
  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. @Override
  74. public void actionPerformed(ActionEvent evt)
  75. {
  76. ((Component) evt.getSource()).transferFocus();
  77. }
  78. };
  79. this.setLayout(new GridBagLayout());
  80. _textFields = new JTextField[4];
  81. GridBagConstraints cons = new GridBagConstraints();
  82. cons.anchor = GridBagConstraints.PAGE_START;
  83. cons.fill = GridBagConstraints.HORIZONTAL;
  84. cons.insets = new Insets(1, 1, 1, 1);
  85. cons.gridx = 0;
  86. cons.gridy = 0;
  87. MaxLengthDocument previous = null;
  88. String[] parts = textIp.split("\\.");
  89. for (int i = 0; i < 4; i++)
  90. {
  91. String str = parts[i];
  92. if (i > 0)
  93. {
  94. JLabel dot = new JLabel(".");
  95. cons.weightx = 0;
  96. add(dot, cons);
  97. cons.gridx++;
  98. }
  99. MaxLengthDocument maxDoc = new MaxLengthDocument(3);
  100. _textFields[i] = new JTextField(maxDoc, str, 3);
  101. if (previous != null)
  102. {
  103. previous.setNext(_textFields[i]);
  104. }
  105. previous = maxDoc;
  106. //ic.weightx = 1;
  107. add(_textFields[i], cons);
  108. _textFields[i].addActionListener(nextfocusaction);
  109. cons.gridx++;
  110. }
  111. }
  112. @Override
  113. public void addFocusListener(FocusListener fl)
  114. {
  115. if (_focusListeners == null)
  116. {
  117. _focusListeners = new LinkedList<FocusListener>();
  118. }
  119. if (fl != null && !_focusListeners.contains(fl))
  120. {
  121. _focusListeners.add(fl);
  122. }
  123. }
  124. @Override
  125. public void removeFocusListener(FocusListener fl)
  126. {
  127. if (_focusListeners != null)
  128. {
  129. _focusListeners.remove(fl);
  130. }
  131. }
  132. public String getText()
  133. {
  134. StringBuilder str = new StringBuilder();
  135. for (int i = 0; i < 4; i++)
  136. {
  137. if (_textFields[i].getText().length() == 0)
  138. str.append("0");
  139. else
  140. str.append(_textFields[i].getText());
  141. if (i < 3)
  142. str.append(".");
  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(int i=0;i<_textFields.length;i++)
  183. {
  184. if(_textFields[i] != null)
  185. {
  186. _textFields[i].setEnabled(enabled);
  187. }
  188. }
  189. }
  190. public boolean isEmpty()
  191. {
  192. for (int i = 0; i < 4; i++)
  193. {
  194. if (_textFields[i].getText().length() != 0)
  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 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 (this.getNext().getText().length() > 0)
  262. {
  263. this.getNext().select(0, this.getNext().getText().length());
  264. }
  265. else
  266. {
  267. this.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. }