JIPTextField.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 net.sf.l2j.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. public void addFocusListener(FocusListener fl)
  112. {
  113. if (_focusListeners == null)
  114. {
  115. _focusListeners = new LinkedList<FocusListener>();
  116. }
  117. if (fl != null && !_focusListeners.contains(fl))
  118. {
  119. _focusListeners.add(fl);
  120. }
  121. }
  122. public void removeFocusListener(FocusListener fl)
  123. {
  124. if (_focusListeners != null)
  125. {
  126. _focusListeners.remove(fl);
  127. }
  128. }
  129. public String getText()
  130. {
  131. String str = "";
  132. for (int i = 0; i < 4; i++)
  133. {
  134. if (_textFields[i].getText().length() == 0)
  135. {
  136. str += "0";
  137. }
  138. else
  139. {
  140. str += _textFields[i].getText();
  141. }
  142. if (i < 3)
  143. {
  144. str += ".";
  145. }
  146. }
  147. return str;
  148. }
  149. public void setText(String str)
  150. {
  151. try
  152. {
  153. // make sure string is not null; throw a NullPointerException otherwise
  154. str.length();
  155. InetAddress ip = InetAddress.getByName(str);
  156. byte b[] = ip.getAddress();
  157. for (int i = 0; i < 4; i++)
  158. {
  159. // byte always have a sign in Java, IP addresses aren't
  160. if (b[i] >= 0)
  161. {
  162. _textFields[i].setText(Byte.toString(b[i]));
  163. }
  164. else
  165. {
  166. _textFields[i].setText(Integer.toString(b[i] + 256));
  167. }
  168. }
  169. return;
  170. }
  171. catch (UnknownHostException ex)
  172. {
  173. }
  174. catch (NullPointerException npe)
  175. {
  176. }
  177. for (int i = 0; i < 4; i++)
  178. {
  179. _textFields[i].setText("");
  180. }
  181. }
  182. public void setEnabled(boolean enabled)
  183. {
  184. for(int i=0;i<_textFields.length;i++)
  185. {
  186. if(_textFields[i] != null)
  187. {
  188. _textFields[i].setEnabled(enabled);
  189. }
  190. }
  191. }
  192. public boolean isEmpty()
  193. {
  194. for (int i = 0; i < 4; i++)
  195. {
  196. if (_textFields[i].getText().length() != 0)
  197. {
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. public boolean isCorrect()
  204. {
  205. for (int i = 0; i < 4; i++)
  206. {
  207. if (_textFields[i].getText().length() == 0)
  208. {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. public void focusGained(FocusEvent event)
  215. {
  216. if (_focusListeners != null)
  217. {
  218. for (FocusListener fl : _focusListeners)
  219. {
  220. fl.focusGained(event);
  221. }
  222. }
  223. }
  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. public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
  255. {
  256. if (getLength() + str.length() > _max)
  257. {
  258. if (getNext() != null)
  259. {
  260. if (this.getNext().getText().length() > 0)
  261. {
  262. this.getNext().select(0, this.getNext().getText().length());
  263. }
  264. else
  265. {
  266. this.getNext().getDocument().insertString(0, str, a);
  267. }
  268. getNext().requestFocusInWindow();
  269. }
  270. else
  271. {
  272. Toolkit.getDefaultToolkit().beep();
  273. }
  274. }
  275. else
  276. {
  277. super.insertString(offset, str, a);
  278. }
  279. }
  280. /**
  281. * @param next The next to set.
  282. */
  283. public void setNext(JTextField next)
  284. {
  285. _next = next;
  286. }
  287. /**
  288. * @return Returns the next.
  289. */
  290. public JTextField getNext()
  291. {
  292. return _next;
  293. }
  294. }
  295. }