Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.composite.action; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Text; import com.clustercontrol.util.Messages; /** * VerifyListener<BR> * * ????????? * * @version 2.2.0 * @since 2.2.0 */ public class StringVerifyListener implements VerifyListener { private Integer length; /** * * * @param length */ public StringVerifyListener(int length) { this.length = Integer.valueOf(length); } /* * (non-Javadoc) * * @see org.eclipse.swt.events.VerifyListener#verifyText(org.eclipse.swt.events.VerifyEvent) */ @Override public void verifyText(VerifyEvent e) { //???? String textString = null; if (e.getSource() instanceof Text) { Text text = (Text) e.getSource(); textString = text.getText(); } else if (e.getSource() instanceof Combo) { Combo combo = (Combo) e.getSource(); textString = combo.getText(); } else { throw new InternalError("textString is null."); } StringBuilder input = new StringBuilder(textString); //??? if (e.keyCode == 0) { //?????? // input.replace(e.start, e.end, e.text); } else { //BackspaceDelete?????????? if (e.character == SWT.BS || e.character == SWT.DEL) { // input.delete(e.start, e.end); } else { // // input.insert(e.start, e.character); input.replace(e.start, e.end, e.text); } } //? checkLength(e, input.toString()); } /** * ? * * @param e * @param inputText */ private void checkLength(VerifyEvent e, String inputText) { //?? //try { // if(inputText.getBytes("UTF-8").length > length){ /* * DBMS?SQL-ASCIIUTF-8???????? * UTF8????????????? * */ if (inputText.length() > length) { //? e.doit = false; String[] args = { this.length.toString() }; // MessageDialog.openWarning(null, Messages.getString("message.hinemos.1"), Messages.getString("message.hinemos.7", args)); } /*} catch (UnsupportedEncodingException e1) { //? e.doit = false; }*/ } }