public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (Beans.isDesignTime()) { super.insertString(offs, str, a); return; } if (!validateEntry) { super.insertString(offs, str, a); validateEntry = true; return; } StringBuffer sb = new StringBuffer(getText(0, getLength())); sb.insert(offs, str); if (sb.toString().equals("-")) { super.insertString(offs, str, a); stringValue = null; value = null; } else { String text = sb.toString(); if (text.equals("-.")) sb.insert(1, "0"); else if (text.equals(".")) sb.insert(0, "0"); Number num = decode(sb.toString()); if (num != null) { super.insertString(offs, str, a); if (text.equals("-.") || text.equals(".")) super.insertString(offs, "0", a); stringValue = sb.toString(); value = num; } } }
@Override public void insertString(int offs, String val, AttributeSet attr) throws BadLocationException { if (val.length() == 0) super.insertString(offs, val, attr); if (val.length() == 1) { int numChars = 1; if (offs < getLength() - 1) { String nextChar = getText(offs + 1, 1); if (nextChar.equals("-")) { numChars = 2; val = val + "-"; } } String testString = getText(0, offs) + val + getText(offs + numChars, getLength() - (offs + numChars)); try { dateFormatter.parse(testString); } catch (Exception e) { // LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e); return; } super.remove(offs, numChars); super.insertString(offs, val, attr); } else if (val.matches(testRegex)) { super.remove(0, getLength()); super.insertString(getLength(), val, attr); } }
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } else { if (limit > 25) super.insertString(offset, "Message Truncated:\n" + str.substring(0, limit - 19), attr); } }
public void insertString(int offset, String str, AttributeSet attr) { try { int integerSizeOfField = adaptee.size_ - adaptee.decimal_; if (adaptee.decimal_ > 0 && str.length() == 1) { String wrkStr0 = super.getText(0, super.getLength()); wrkStr0 = wrkStr0.substring(0, offset) + str + wrkStr0.substring(offset, wrkStr0.length()); String wrkStr1 = wrkStr0.replace(".", ""); wrkStr1 = wrkStr1.replace(",", ""); wrkStr1 = wrkStr1.replace("-", ""); if (wrkStr1.length() > adaptee.size_) { wrkStr1 = wrkStr1.substring(0, integerSizeOfField) + "." + wrkStr1.substring(integerSizeOfField, wrkStr1.length() - 1); super.replace(0, super.getLength(), wrkStr1, attr); } else { int posOfDecimal = wrkStr0.indexOf("."); if (posOfDecimal == -1) { if (wrkStr1.length() > integerSizeOfField) { wrkStr1 = wrkStr1.substring(0, integerSizeOfField) + "." + wrkStr1.substring(integerSizeOfField, wrkStr1.length()); super.replace(0, super.getLength(), wrkStr1, attr); } else { super.insertString(offset, str, attr); } } else { int decimalLengthOfInputData = wrkStr0.length() - posOfDecimal - 1; if (decimalLengthOfInputData <= adaptee.decimal_) { super.insertString(offset, str, attr); } } } } else { if (str.contains(".")) { JOptionPane.showMessageDialog(null, XFUtility.RESOURCE.getString("NumberFormatError")); } else { String wrkStr0 = super.getText(0, super.getLength()); wrkStr0 = wrkStr0.substring(0, offset) + str + wrkStr0.substring(offset, wrkStr0.length()); String wrkStr1 = wrkStr0.replace(".", ""); wrkStr1 = wrkStr1.replace(",", ""); wrkStr1 = wrkStr1.replace("-", ""); if (wrkStr1.length() <= adaptee.size_) { super.insertString(offset, str, attr); } } } } catch (BadLocationException e) { e.printStackTrace(); } }
/** * Inserts the string into the document. If the length of the document would violate the maximum * characters restriction, then the string is cut down so that * * @param offs the offset, where the string should be inserted into the document * @param str the string that should be inserted * @param a the attribute set assigned for the document * @throws javax.swing.text.BadLocationException if the offset is not correct */ public void insertString(final int offs, final String str, final AttributeSet a) throws BadLocationException { if (str == null) { return; } if (this.maxlen < 0) { super.insertString(offs, str, a); } final char[] numeric = str.toCharArray(); final StringBuffer b = new StringBuffer(); b.append(numeric, 0, Math.min(this.maxlen, numeric.length)); super.insertString(offs, b.toString(), a); }
public void insertString(int pOffs, String str, AttributeSet a) throws BadLocationException { int offs = pOffs; // return immediately when selecting an item if (selecting) return; // insert the string into the document super.insertString(offs, str, a); // lookup and select a matching item LookupResult lookupResult = lookupItem(getText(0, getLength())); if (lookupResult.matchingItem != null) { setSelectedItem(lookupResult.matchingItem, lookupResult.matchingString); } else { if (strictMatching) { // keep old item selected if there is no match lookupResult.matchingItem = adaptor.getSelectedItem(); lookupResult.matchingString = adaptor.getSelectedItemAsString(); // imitate no insert (later on offs will be incremented by // str.length(): selection won't move forward) offs = offs - str.length(); // provide feedback to the user that his input has been received but can // not be accepted UIManager.getLookAndFeel().provideErrorFeedback(adaptor.getTextComponent()); } else { // no item matches => use the current input as selected item lookupResult.matchingItem = getText(0, getLength()); lookupResult.matchingString = getText(0, getLength()); setSelectedItem(lookupResult.matchingItem, lookupResult.matchingString); } } setText(lookupResult.matchingString); // select the completed part adaptor.markText(offs + str.length()); }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { // return immediately when selecting an item if (selecting) { return; } // insert the string into the document super.insertString(offs, str, a); // lookup and select a matching item Object item = lookupItem(getText(0, getLength())); if (item != null) { setSelectedItem(item); } else { // keep old item selected if there is no match item = comboBox.getSelectedItem(); // imitate no insert (later on offs will be incremented by str.length(): selection won't move // forward) offs = offs - str.length(); // provide feedback to the user that his input has been received but can not be accepted comboBox .getToolkit() .beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox); } setText(item.toString()); // select the completed part highlightCompletedText(offs + str.length()); }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (digits != -1) { if (getLength() >= digits) { toolkit.beep(); return; } } int j = 0; char[] source = str.toCharArray(); char[] result = new char[source.length]; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i]) || (offs == 0 && i == 0 && source[i] == '-')) { result[j++] = source[i]; } else { toolkit.beep(); } } super.insertString(offs, new String(result, 0, j), a); }
@Override public void insertString(final int offs, final String str, final AttributeSet a) throws BadLocationException { // NavigatorLogger.printMessage("Offset:"+offs+" STr:"+str+"L:"+getLength()+"attr:"+a); if ((getLength() + str.length()) <= maxLength) { final char[] source = str.toCharArray(); final char[] result = new char[source.length]; int j = 0; for (int i = 0; i < result.length; i++) { if (Character.isDigit(source[i])) { result[j++] = source[i]; } else { toolkit.beep(); if (log.isDebugEnabled()) { log.debug("insertString: " + source[i]); // NOI18N } } } super.insertString(offs, new String(result, 0, j), a); checked = false; } else { toolkit.beep(); } if ((getLength()) == maxLength) { // getLength() ist schon aktualisiert if (bringFocus2Next == true) { checked = true; nextField.requestFocus(); } // NavigatorLogger.printMessage("Sprung"); // NavigatorLogger.printMessage(nextField); } }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { return; } char[] upper = str.toCharArray(); boolean isDigit = true; boolean additionalDecimal = false; for (int i = 0; i < upper.length; i++) { upper[i] = Character.toUpperCase(upper[i]); if (!Character.isDigit(upper[i]) && upper[i] != '.' && upper[i] != '-') { isDigit = false; break; } else { if (upper[i] == '.') { String text = super.getText(0, super.getLength()); if (text.indexOf('.') != -1) { additionalDecimal = true; break; } } } } if (isDigit && !additionalDecimal) super.insertString(offs, new String(upper), a); }
void insertBeforeEachSelectedLine(String insertion) { javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument(); try { int currentLine = offsetToLine(doc, getSelectionStart()); int endLine = offsetToLine(doc, getSelectionEnd()); // The two following cases are to take care of selections that include // only the very edge of a line of text, either at the top or bottom // of the selection. Because these lines do not have *any* highlighted // text, it does not make sense to modify these lines. ~Forrest (9/22/2006) if (endLine > currentLine && getSelectionEnd() == lineToStartOffset(doc, endLine)) { endLine--; } if (endLine > currentLine && getSelectionStart() == (lineToEndOffset(doc, currentLine) - 1)) { currentLine++; } while (currentLine <= endLine) { doc.insertString(lineToStartOffset(doc, currentLine), insertion, null); currentLine++; } } catch (javax.swing.text.BadLocationException ex) { throw new IllegalStateException(ex); } }
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } }
public void setValue(Number value) { this.value = value; this.stringValue = (value != null) ? value.toString() : null; if (value != null) { try { Number num = convertValue(value); String text = (num == null ? "" : num.toString()); super.remove(0, getLength()); super.insertString(0, text, null); this.stringValue = (num == null ? null : num.toString()); this.value = num; } catch (RuntimeException re) { throw re; } catch (Exception ex) { throw new RuntimeException(ex.getMessage(), ex); } } else { try { super.remove(0, getLength()); } catch (BadLocationException ex) {; } } }
public final void showFormattedText(boolean show) { try { super.remove(0, getLength()); Number num = getValue(); if (num == null) return; if (show) { String snum = formatValue(num); super.insertString(0, snum, null); } else { super.insertString(0, num.toString(), null); } } catch (BadLocationException ex) { } }
@Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { super.insertString(offs, str, a); return; } for (String f : filter) { if (str.contains(f)) { // notifyToUser("\\\\, /, :. *, ?, \\, <, >, |"); notifyToUser(StringUtilities.join(", ", filter)); Toolkit.getDefaultToolkit().beep(); return; } } super.insertString(offs, str, a); }
/* * @see javax.swing.text.Document#insertString( * int, java.lang.String, javax.swing.text.AttributeSet) */ public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { // Mutators hold write lock & will deadlock if use is not thread safe super.insertString(offset, str, a); setPropertyInternal(getText(0, getLength())); }
/** * ************************************************************************ Delete String * * @param origOffset Offeset * @param length length * @throws BadLocationException */ @Override public void remove(int origOffset, int length) throws BadLocationException { log.finest("Offset=" + origOffset + " Length=" + length); if (origOffset < 0 || length < 0) throw new IllegalArgumentException("MDocNumber.remove - invalid argument"); int offset = origOffset; if (length != 1) { super.remove(offset, length); return; } /** Manual Entry */ String content = getText(); // remove all Thousands if (content.indexOf(m_groupingSeparator) != -1) { StringBuffer result = new StringBuffer(); for (int i = 0; i < content.length(); i++) { if (content.charAt(i) == m_groupingSeparator && i != origOffset) { if (i < offset) offset--; } else result.append(content.charAt(i)); } super.remove(0, content.length()); super.insertString(0, result.toString(), null); m_tc.setCaretPosition(offset); } // remove Thousands super.remove(offset, length); } // remove
/*重载父类的insertString函数 ,向文档中插入某些内容*/ public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { if (getLength() + str.length() > maxLength) { // 这里假定你的限制长度为10 return; } else { super.insertString(offset, str, a); } }
@Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { String CPF = getText(0, getLength()); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (!Character.isDigit(c)) { return; } } if (CPF.length() < tamanho) { super.remove(0, getLength()); StringBuilder s = new StringBuilder(CPF + str); if (s.length() == 3) { s.insert(3, "."); } else if (s.length() == 7) { s.insert(7, "."); } else if (s.length() == 11) { s.insert(11, "-"); } super.insertString(0, s.toString(), a); } }
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } else java.awt.Toolkit.getDefaultToolkit().beep(); }
@Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { boolean valid = true; for (char c : str.toCharArray()) { if (c < '0' || c > '9') { valid = false; break; } } if (valid && str.length() > 0 && getLength() > 0) { final StringBuilder sb = new StringBuilder(getText(0, getLength())); sb.insert(offs, str); final String value = sb.toString(); if (value.isEmpty() == false) { try { int port = Integer.parseInt(value); valid = port > 0 && port < 65536; } catch (NumberFormatException ex) { // should never happens Exceptions.printStackTrace(ex); valid = false; } } } if (valid) { super.insertString(offs, str, a); } }
public void setDateTime(LocalDate date) { try { super.remove(0, getLength()); final String dateText = dateFormatter.format(date); super.insertString(0, dateText, null); } catch (BadLocationException e) { LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e); } }
/* (non-Javadoc) * @see javax.swing.text.Document#insertString(int, java.lang.String, javax.swing.text.AttributeSet) */ @Override public void insertString(final int offset, final String strArg, final AttributeSet attr) throws BadLocationException { if (!StringUtils.contains(strArg, '\'') && !StringUtils.contains(strArg, '"') && !StringUtils.contains(strArg, '`')) { super.insertString(offset, strArg, attr); } }
/** * Sets the text of this AutoCompleteDocument to the given text. * * @param text the text that will be set for this document */ private void setText(String text) { try { // remove all text and insert the completed string super.remove(0, getLength()); super.insertString(0, text, null); } catch (BadLocationException e) { throw new RuntimeException(e.toString()); } }
/* (non-Javadoc) * @see javax.swing.text.PlainDocument#insertString(int, java.lang.String, javax.swing.text.AttributeSet) */ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { int max = BaseInputTextControl.this.maxLength; if (max != -1) { int docLength = this.getLength(); if (docLength > max) { return; } int strLen = str.length(); if (docLength + strLen > max) { String shorterStr = str.substring(0, max - docLength); super.insertString(offs, shorterStr, a); } else { super.insertString(offs, str, a); } } else { super.insertString(offs, str, a); } }
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { boolean ok = true; for (int idx = 0; idx < str.length() && ok; idx++) { char ch = str.charAt(idx); ok = ch != File.separatorChar && ch != '\\' && ch != '/' && ch != '|' && ch != ':'; } if (ok) { super.insertString(offs, str, a); } }
@Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str.matches("[0-9.-]+")) { super.insertString(offs, str, a); } else { m = new Mensagens(); m.jopAlerta("Este campo aceita apenas números."); } }
/** * Method to insert string in the right position * * @param offs offset * @param str String to be inserted * @param a AttributeSet * @throws BadLocationException an exception is thrown if the new string is not a number */ @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str != null) { if (!_validateNumberString(str.trim(), offs)) { throw new BadLocationException(null, offs); } super.insertString(offs, str.trim(), a); } }
public void insertString(int ii, String s, AttributeSet attributeset) throws BadLocationException { if (s == null || "".equals(s)) return; String base = getText(0, ii); String match = getMatch(base + s); if (match == null) { super.insertString(ii, s, attributeset); return; } int jj = ii + s.length(); super.remove(0, getLength()); super.insertString(0, base + s + match.substring(jj), attributeset); setSelectionStart(jj); setSelectionEnd(getLength()); }
public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException { if ((limit == 0 || getLength() + s.length() <= limit) && Character.isDigit(s.charAt(0))) { // if we haven't reached the limit, insert the string super.insertString(offset, s, attributeSet); } else { // otherwise, just lose the string java.awt.Toolkit.getDefaultToolkit().beep(); } }