@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); } }
/** * Creates new form SiteType * * @param title * @param type */ public SiteTypes(String title, String type) { super(title, type); setMode(EDIT_MODE); initComponents(); fillTypes(); PlainDocument addNameDoc = (PlainDocument) tfAddName.getDocument(); addNameDoc.setDocumentFilter(new utils.MyDocFilter(InputType.TEXT15)); PlainDocument updateNameDoc = (PlainDocument) tfUpdateName.getDocument(); updateNameDoc.setDocumentFilter(new utils.MyDocFilter(InputType.TEXT15)); lstTypes.addListSelectionListener( new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { boolean isItemSelected = lstTypes.getSelectedIndex() != -1; btnRemove.setEnabled(isItemSelected); btnUpdate.setEnabled(isItemSelected); if (isItemSelected) { tfUpdateName.setText(lstTypes.getSelectedValue().toString()); } } }); if (lstTypes.getModel().getSize() > 0) { lstTypes.setSelectedIndex(0); } }
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); } }
void shiftLeft() { 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) { int lineStart = lineToStartOffset(doc, currentLine); int lineEnd = lineToEndOffset(doc, currentLine); String text = doc.getText(lineStart, lineEnd - lineStart); if (text.length() > 0 && text.charAt(0) == ' ') { doc.remove(lineStart, 1); } currentLine++; } } catch (javax.swing.text.BadLocationException ex) { throw new IllegalStateException(ex); } }
@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); } }
String getLineText(int offset) throws javax.swing.text.BadLocationException { javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument(); int currentLine = offsetToLine(doc, offset); int lineStart = lineToStartOffset(doc, currentLine); int lineEnd = lineToEndOffset(doc, currentLine); return doc.getText(lineStart, lineEnd - lineStart); }
/** * ************************************************************************ 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
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 setNameText(final String name) { try { myNameDocument.replace(0, myNameDocument.getLength(), name, null); } catch (BadLocationException e) { LOG.error(e); } }
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; } } }
public final String getNameText() { try { return myNameDocument.getText(0, myNameDocument.getLength()); } catch (BadLocationException e) { LOG.error(e); return ""; } }
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); } }
/** * 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()); } }
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 testIsReadLocked() throws Exception { PlainDocument doc = new PlainDocument(); assertFalse(DocumentUtilities.isReadLocked(doc)); doc.readLock(); try { assertTrue(DocumentUtilities.isReadLocked(doc)); } finally { doc.readUnlock(); } }
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(); } }
public void testDebugOffset() throws Exception { PlainDocument doc = new PlainDocument(); // tabSize is 8 // 0123 45 678 90123456 789 doc.insertString(0, "abc\na\tbc\nabcdefg\thij", null); assertEquals("0[1:1]", DocumentUtilities.debugOffset(doc, 0)); assertEquals("5[2:2]", DocumentUtilities.debugOffset(doc, 5)); assertEquals("6[2:9]", DocumentUtilities.debugOffset(doc, 6)); assertEquals("7[2:10]", DocumentUtilities.debugOffset(doc, 7)); assertEquals("16[3:8]", DocumentUtilities.debugOffset(doc, 16)); assertEquals("17[3:9]", DocumentUtilities.debugOffset(doc, 17)); assertEquals("19[3:11]", DocumentUtilities.debugOffset(doc, 19)); }
public void testGetText() throws Exception { PlainDocument doc = new PlainDocument(); CharSequence text = DocumentUtilities.getText(doc); assertEquals(1, text.length()); assertEquals('\n', text.charAt(0)); text = DocumentUtilities.getText(doc); doc.insertString(0, "a\nb", null); for (int i = 0; i < doc.getLength() + 1; i++) { assertEquals(doc.getText(i, 1).charAt(0), text.charAt(i)); } }
@Override public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; int oldLength = super.getLength(); int insertLength = str.length(); if (oldLength + insertLength <= maxLength) { super.insertString(offset, str, attr); } else { str = getNewContent(oldLength, insertLength, str); super.replace(0, oldLength, str, attr); } }
public void remove(int offs, int len) throws BadLocationException { StringBuffer sb = new StringBuffer(getText(0, getLength())); sb.delete(offs, offs + len); if (sb.length() == 0) { super.remove(offs, len); stringValue = null; value = null; } else { Number num = decode(sb.toString()); super.remove(offs, len); stringValue = sb.toString(); value = num; } }
/** Handle remove. */ public void remove(int offs, int length) throws BadLocationException { int sourceLength = getLength(); // Allow user to restore uninitialized state again by removing all if (offs == 0 && sourceLength == length) { super.remove(0, sourceLength); return; } // Do custom remove String sourceText = getText(0, sourceLength); StringBuffer strBuffer = new StringBuffer(sourceText.substring(0, offs)); int counter; for (counter = offs; counter < offs + length; counter++) { // Only remove digits and intDelims char currChar = sourceText.charAt(counter); if (Character.isDigit(currChar) || currChar == intDelim) { continue; } strBuffer.append(currChar); } // Append last part of sourceText if (counter < sourceLength) { strBuffer.append(sourceText.substring(counter)); } // Set text in field super.remove(0, sourceLength); insertString(0, strBuffer.toString(), (AttributeSet) getDefaultRootElement()); // Set caret pos int newDiff = sourceLength - getLength() - 1; if (newDiff < 0) { newDiff = 0; } if (offs - newDiff < 0) { newDiff = 0; } textField.setCaretPosition(offs - newDiff); }
String getHelpTarget(int startPosition) { // determine the current "word" that the cursor is on javax.swing.text.PlainDocument doc = (javax.swing.text.PlainDocument) getDocument(); try { int currentLine = offsetToLine(doc, startPosition); int startLineOffset = lineToStartOffset(doc, currentLine); int lineLength = lineToEndOffset(doc, currentLine) - startLineOffset; String lineText = doc.getText(startLineOffset, lineLength); int selStartInString = startPosition - startLineOffset; return colorizer.getTokenAtPosition(lineText, selStartInString); } catch (javax.swing.text.BadLocationException ex) { throw new IllegalStateException(ex); } }
private void finalizeValue() { try { if (this.value == null) return; Number num = convertValue(this.value); String snum = formatValue(num); super.remove(0, getLength()); super.insertString(0, snum, null); this.stringValue = snum; this.value = num; } catch (Exception ex) {; } }
public void testIsWriteLocked() throws Exception { PlainDocument doc = new PlainDocument(); assertFalse(DocumentUtilities.isWriteLocked(doc)); doc.addDocumentListener( new DocumentListener() { public void insertUpdate(DocumentEvent evt) { assertTrue(DocumentUtilities.isWriteLocked(evt.getDocument())); } public void removeUpdate(DocumentEvent evt) {} public void changedUpdate(DocumentEvent evt) {} }); doc.insertString(0, "test", null); }
/** * 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); }
private SingleConfigurationConfigurable(RunnerAndConfigurationSettingsImpl settings) { super(new ConfigurationSettingsEditorWrapper(settings), settings); final Config configuration = (Config) getSettings().getConfiguration(); myDisplayName = getSettings().getName(); myHelpTopic = null; // TODO myIcon = configuration.getType().getIcon(); myBrokenConfiguration = configuration instanceof UnknownRunConfiguration; setNameText(configuration.getName()); myNameDocument.addDocumentListener( new DocumentAdapter() { public void textChanged(DocumentEvent event) { setModified(true); } }); getEditor() .addSettingsEditorListener( new SettingsEditorListener() { public void stateChanged(SettingsEditor settingsEditor) { myValidationResultValid = false; } }); }
/*重载父类的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); } }
/* * @see javax.swing.text.Document#remove(int, int) */ public void remove(int offs, int len) throws BadLocationException { // Mutators hold write lock & will deadlock if use is not thread safe super.remove(offs, len); setPropertyInternal(getText(0, getLength())); }
/* * @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())); }
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(); }