// ---------------------------------------------------- // return last location found public int searchBackward(String lastFindStr) { try { // backward if (isFindAgain) { // otherwise you find same string int foo = lastFindIndex; // begining of word if (foo >= 0) { editor1.setCaretPosition(foo); } // System.out.println("Debug:TextViewer:searchBackward: lastFindIndex: "+foo); } int carPos = editor1.getCaretPosition(); // search backward // todo: should we use the getText(pos,len,segment); String chunk1 = editor1.getDocument().getText(0, carPos); int lastFindIndexTemp = chunk1.lastIndexOf(lastFindStr); if (lastFindIndexTemp == -1) { boolean okPressed = okCancelPopup.display("TextViewer:string not found. Try forward?"); // handle forward if (okPressed) { forwardFindDirection = true; lastFindIndex = searchForward(lastFindStr); } } else { lastFindIndex = lastFindIndexTemp; editor1.setCaretPosition(lastFindIndex); // ready to type in body editor1.moveCaretPosition(lastFindIndex + lastFindStr.length()); // ready to type in body } } catch (BadLocationException badexception) { String errstr = "TextViewer:searchBackward: " + badexception.getMessage(); warningPopup.display(errstr); return (-1); } return (lastFindIndex); }
@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(); }
@Override public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum((int) f.length()); status.add(progress); status.revalidate(); // try to start reading Reader in = new FileReader(f); char[] buff = new char[4096]; int nch; while ((nch = in.read(buff, 0, buff.length)) != -1) { doc.insertString(doc.getLength(), new String(buff, 0, nch), null); progress.setValue(progress.getValue() + nch); } } catch (IOException e) { final String msg = e.getMessage(); SwingUtilities.invokeLater( new Runnable() { public void run() { JOptionPane.showMessageDialog( getFrame(), "Could not open file: " + msg, "Error opening file", JOptionPane.ERROR_MESSAGE); } }); } catch (BadLocationException e) { System.err.println(e.getMessage()); } doc.addUndoableEditListener(undoHandler); // we are done... get rid of progressbar status.removeAll(); status.revalidate(); resetUndoManager(); if (elementTreePanel != null) { SwingUtilities.invokeLater( new Runnable() { public void run() { elementTreePanel.setEditor(getEditor()); } }); } }
public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum((int) f2.length()); status.add(progress); status.revalidate(); // try to start reading Reader in = new FileReader(f2); char[] buff = new char[4096]; int nch; while ((nch = in.read(buff, 0, buff.length)) != -1) { doc2.insertString(doc2.getLength(), new String(buff, 0, nch), null); progress.setValue(progress.getValue() + nch); } // we are done... get rid of progressbar // doc2.addUndoableEditListener(undoHandler); status.removeAll(); status.revalidate(); // resetUndoManager(); } catch (IOException e) { System.err.println("TextViewer:FileLoader " + e.toString()); } catch (BadLocationException e) { System.err.println("TextViewer:FileLoader " + e.getMessage()); } /* aa if (elementTreePanel != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { elementTreePanel.setEditor(getEditor()); } }); } */ }
// ---------------------- // return last location found public int searchForward(String lastFindStr) { try { // forward // System.out.println("Debug:TextViewer:in searchForward"); int carPos = editor1.getCaretPosition(); // System.out.println("Debug:TextViewer: carPos "+carPos); // int strLength=editor1.getDocument().getEndPosition().getOffset(); int strLength = editor1.getDocument().getLength(); // if ((strLength-carPos)<0) { // System.out.println("Debug:TextViewer: carPos "+carPos); // System.out.println("Debug:TextViewer: strLength "+strLength); // } // search Forward // todo: should we use the getText(pos,len,segment); String chunk1 = editor1.getDocument().getText(carPos, (strLength - carPos)); // offset,length int lastFindIndexTemp = chunk1.indexOf(lastFindStr); if (lastFindIndexTemp == -1) { boolean okPressed = okCancelPopup.display("TextViewer:string not found. Try backward?"); // handle backward if (okPressed) { forwardFindDirection = false; lastFindIndex = searchBackward(lastFindStr); // System.out.println("Debug:TextViewer: lastFindIndex "+lastFindIndex); } } else { lastFindIndex = carPos + lastFindIndexTemp; // System.out.println("Debug:TextViewer: lastFindIndex "+lastFindIndex); editor1.setCaretPosition(lastFindIndex); // ready to type in body editor1.moveCaretPosition(lastFindIndex + lastFindStr.length()); // ready to type in body } } catch (BadLocationException badexception) { String errstr = "TextViewer:searchForward: " + badexception.getMessage(); warningPopup.display(errstr); return (-1); } return (lastFindIndex); }