@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) { } } } } }
public void insertUpdate(DocumentEvent e) { Document doc = e.getDocument(); try { prop.set(doc.getText(0, doc.getLength())); } catch (BadLocationException b) { // Once again, no idea what this is supposed to be. // I don't think I like this interface much :-(. System.out.println(b); } }
/** * This function inserts or replaces the user's input in a string buffer, to create the text * which would be on screen if we allowed it. We declare to throw bad location due to * doc.getText, but since the paramters are coming from the farmework, that should never * happen.... in theory. If insert is true we insert, if false we replace. Length parameter only * used when replacing. */ private StringBuffer getTextPrototype( boolean insert, FilterBypass fb, int offs, String str, int length) throws BadLocationException { Document doc = fb.getDocument(); String text = doc.getText(0, doc.getLength()); StringBuffer sb = new StringBuffer(text); if (insert) { sb.insert(offs, str); } else { sb.replace(offs, offs + length, str); } return sb; } // end of getTextPrototype
/** 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); }
/** * Updates the shared value instance if updateValue is true. Always forces the redraw of * everything. */ protected void doValueUpdate(FilterBypass fb) throws BadLocationException { Document doc = fb.getDocument(); String text = doc.getText(0, doc.getLength()); if (text.isEmpty()) { text = "0"; } if (updateValue == true) { try { Double value = new Double(text); double newValue = multiplier.getValueToBeSet(value.doubleValue()); cValue.setValue(newValue); } catch (NumberFormatException e) { // do nothing, since we allow '-' } } topContainer.forceRedraw(); }
void updateTheLabel(DocumentEvent e) { Document doc = (Document) e.getDocument(); String text = null; try { text = doc.getText(0, doc.getLength()); doc = null; } catch (BadLocationException ex) { text = null; } if (text != null) { try { double number = Double.parseDouble(text); if (number > 1) { label.setText(labelPair.getPlural()); } else { label.setText(labelPair.getSingular()); } } catch (NumberFormatException ex) { // Do nothing } finally { text = null; } } }