// TODO not used public void appendString(String str) { Document doc = getDocument(); try { int start = doc.getLength(); doc.insertString(doc.getLength(), str, null); int end = doc.getLength(); // end = parseLine(start, end, patHistoryBtnStr); } catch (Exception e) { Debug.error(me + "appendString: Problem while trying to append\n%s", e.getMessage()); } }
private void updateBoard(DocumentEvent e) throws BadLocationException { Document doc = e.getDocument(); int index = (int) doc.getProperty("index"); String valueString = doc.getText(0, doc.getLength()); if (doc.getLength() == 0) valueString = "0"; int value = Integer.parseInt(valueString); gameBoard.changeCellAt(index, value); // gameBoard.out(); if (gameBoard.checkGameOver()) { JOptionPane.showMessageDialog(frame, "NUMBRIX COMPLETED!!!"); } }
@Override @SuppressWarnings("SleepWhileHoldingLock") public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum(doc.getLength()); status.add(progress); status.revalidate(); // start writing Writer out = new FileWriter(f); Segment text = new Segment(); text.setPartialReturn(true); int charsLeft = doc.getLength(); int offset = 0; while (charsLeft > 0) { doc.getText(offset, Math.min(4096, charsLeft), text); out.write(text.array, text.offset, text.count); charsLeft -= text.count; offset += text.count; progress.setValue(offset); try { Thread.sleep(10); } catch (InterruptedException e) { Logger.getLogger(FileSaver.class.getName()).log(Level.SEVERE, null, e); } } out.flush(); out.close(); } catch (IOException e) { final String msg = e.getMessage(); SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog( getFrame(), "Could not save file: " + msg, "Error saving file", JOptionPane.ERROR_MESSAGE); } }); } catch (BadLocationException e) { System.err.println(e.getMessage()); } // we are done... get rid of progressbar status.removeAll(); status.revalidate(); }
/** * Updates the AttributedCharacterIterator by invoking <code>formatToCharacterIterator</code> on * the <code>Format</code>. If this is successful, <code>updateMask(AttributedCharacterIterator) * </code> is then invoked to update the internal bitmask. */ void updateMask() { if (getFormat() != null) { Document doc = getFormattedTextField().getDocument(); validMask = false; if (doc != null) { try { string = doc.getText(0, doc.getLength()); } catch (BadLocationException ble) { string = null; } if (string != null) { try { Object value = stringToValue(string); AttributedCharacterIterator iterator = getFormat().formatToCharacterIterator(value); updateMask(iterator); } catch (ParseException pe) { } catch (IllegalArgumentException iae) { } catch (NullPointerException npe) { } } } } }
private void addText(String str, String styleName, JTextPane pane) { Document doc = pane.getDocument(); int len = doc.getLength(); try { doc.insertString(len, str, pane.getStyle(styleName)); } catch (javax.swing.text.BadLocationException e) { } }
/* * public int search(String str){ * return search(str, true); * } */ public int search(String str, int pos, boolean forward) { boolean isCaseSensitive = true; String toSearch = str; if (str.startsWith("!")) { str = str.substring(1).toUpperCase(); isCaseSensitive = false; } int ret = -1; Document doc = getDocument(); Debug.log(9, "search caret: " + pos + ", " + doc.getLength()); try { String body; int begin; if (forward) { int len = doc.getLength() - pos; body = doc.getText(pos, len > 0 ? len : 0); begin = pos; } else { body = doc.getText(0, pos); begin = 0; } if (!isCaseSensitive) { body = body.toUpperCase(); } Pattern pattern = Pattern.compile(Pattern.quote(str)); Matcher matcher = pattern.matcher(body); ret = continueSearch(matcher, begin, forward); if (ret < 0) { if (forward && pos != 0) { return search(toSearch, 0, forward); } if (!forward && pos != doc.getLength()) { return search(toSearch, doc.getLength(), forward); } } } catch (BadLocationException e) { Debug.log(7, "search caret: " + pos + ", " + doc.getLength() + e.getStackTrace()); } return ret; }
/** Resets the value of the JFormattedTextField to be <code>value</code>. */ void resetValue(Object value) throws BadLocationException, ParseException { Document doc = getFormattedTextField().getDocument(); String string = valueToString(value); try { ignoreDocumentMutate = true; doc.remove(0, doc.getLength()); doc.insertString(0, string, null); } finally { ignoreDocumentMutate = false; } updateValue(value); }
@Override public void replace( DocumentFilter.FilterBypass fp, int offset, int length, String string, AttributeSet aset) throws BadLocationException { Document doc = fp.getDocument(); String oldText = doc.getText(0, doc.getLength()); StringBuilder sb = new StringBuilder(oldText); sb.replace(offset, offset + length, oldText); int len = string.length(); boolean isValidInteger = true; for (int i = 0; i < len; i++) { if (!Character.isDigit(string.charAt(i))) { isValidInteger = false; break; } } if (isValidInteger && verifyText(sb.toString())) { super.replace(fp, offset, length, string, aset); } else Toolkit.getDefaultToolkit().beep(); }