1 2 23 24 package net.fenyo.gnetwatch.GUI; 25 26 import net.fenyo.gnetwatch.Config; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.eclipse.swt.events.*; 31 import org.eclipse.swt.widgets.*; 32 33 38 39 public class IpAddressEditor implements KeyListener { 40 public static final long serialVersionUID = 1L; 41 42 private static Log log = LogFactory.getLog(IpAddressEditor.class); 43 44 private final Text text; 45 private final Config config; 46 47 51 public IpAddressEditor(final Config config, final Text text) { 53 this.text = text; 54 this.config = config; 55 text.setText("000.000.000.000"); 56 text.addKeyListener(this); 57 } 58 59 64 private void handleKey(KeyEvent e) { 65 String content = text.getText(); 66 int position = text.getCaretPosition(); 67 if (e.character == '.') { 69 if (position <= 3) position = 4; 70 else if (position > 3 && position <= 7) position = 8; 71 else if (position > 7 && position <= 11) position = 12; 72 text.setSelection(position, position); 73 return; 74 } 75 if (e.character == '\10') { 76 if (position > 0) { 77 position--; 78 text.setSelection(position, position); 79 } 80 return; 81 } 82 if (e.character == '\177') { 83 if (position < 15) { 84 position++; 85 text.setSelection(position, position); 86 } 87 return; 88 } 89 if (position >= 15) position = 14; 90 if (position == 3 || position == 7 || position == 11) position++; 91 String new_content = content.substring(0, position) + e.character + content.substring(position + 1); 92 if (new_content.matches("^[0-9][0-9][0-9]\\.[0-9][0-9][0-9]\\.[0-9][0-9][0-9]\\.[0-9][0-9][0-9]$") == false) return; 93 if (new Integer (new_content.substring(0, 3)).intValue() > 255 || 94 new Integer (new_content.substring(4, 7)).intValue() > 255 || 95 new Integer (new_content.substring(8, 11)).intValue() > 255 || 96 new Integer (new_content.substring(12, 15)).intValue() > 255) { 97 if (position > 13) return; 98 new_content = content.substring(0, position) + e.character + '0' + content.substring(position + 2); 99 if (new Integer (new_content.substring(0, 3)).intValue() > 255 || 100 new Integer (new_content.substring(4, 7)).intValue() > 255 || 101 new Integer (new_content.substring(8, 11)).intValue() > 255 || 102 new Integer (new_content.substring(12, 15)).intValue() > 255) 103 return; 104 text.setText(new_content); 105 if (position < 15) position++; 106 text.setSelection(position, position); 107 return; 108 } 109 text.setText(new_content); 110 if (position < 15) position++; 111 text.setSelection(position, position); 112 } 113 114 119 public void keyPressed(KeyEvent e) { 121 if (config.getProperty("ipaddresseditor.insertonkeypressed").equals("true")) 122 handleKey(e); 123 } 124 125 130 public void keyReleased(KeyEvent e) { 132 if (!config.getProperty("ipaddresseditor.insertonkeypressed").equals("true")) 133 handleKey(e); 134 } 135 136 } 137 | Popular Tags |