📄 textwindow.java
字号:
int amtRead = is.read(buf, 0, READ_BUFFER_SIZE); if (amtRead <= 0) break; String addString = new String(buf, 0, amtRead); textArea.append(addString); } stream.close(); } catch (IOException e) { System.out.println("Error reading the file"); } } /** * Method to save this TextWindow to a disk file. */ public static void writeTextCell() { WindowFrame wf = WindowFrame.getCurrentWindowFrame(); if (wf == null) return; WindowContent content = wf.getContent(); if (!(content instanceof TextWindow)) { Job.getUserInterface().showErrorMessage("You must first be editing a text cell (a cell whose view is textual, such as 'doc').", "Cannot import text file"); return; } TextWindow tw = (TextWindow)content; String fileName = OpenFile.chooseInputFile(FileType.TEXT, null); if (fileName == null) return; tw.readTextCell(fileName); } /** * Method to write text cell into a file * @param fileName * @return true if no errors were found */ public boolean writeTextCell(String fileName) { if (fileName == null) { System.out.println("Bad filename: "+fileName); return false; } try { PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); Document doc = textArea.getDocument(); Element paragraph = doc.getDefaultRootElement(); int lines = paragraph.getElementCount(); for(int i=0; i<lines; i++) { Element e = paragraph.getElement(i); int startPos = e.getStartOffset(); int endPos = e.getEndOffset(); try { String line = textArea.getText(startPos, endPos - startPos); printWriter.print(line); } catch (BadLocationException ex) {} } printWriter.close(); } catch (IOException e) { System.out.println("Error saving " + fileName); return false; } System.out.println("Wrote " + fileName); return true; } /** * Method to select a line number in this TextWindow. * @param lineNumber the line to select (1-based). */ public void goToLineNumber(int lineNumber) { Document doc = textArea.getDocument(); Element paragraph = doc.getDefaultRootElement(); int lines = paragraph.getElementCount(); if (lineNumber <= 0 || lineNumber > lines) { System.out.println("Line numbers must be between 1 and "+lines); return; } Element e = paragraph.getElement(lineNumber-1); int startPos = e.getStartOffset(); int endPos = e.getEndOffset(); textArea.setSelectionStart(startPos); textArea.setSelectionEnd(endPos); } private void updateText(String [] strings) { textArea.setText(makeOneString(strings)); } /** * Method to update text for a cell (if it is being displayed). * This is called when the text for a cell has been changed by some other part of the system, * and should be redisplayed where appropriate. * @param cell the Cell whose text changed. */ public static void updateText(Cell cell) { for(Iterator<WindowFrame> it = WindowFrame.getWindows(); it.hasNext(); ) { WindowFrame wf = it.next(); WindowContent content = wf.getContent(); if (content instanceof TextWindow) { if (content.getCell() == cell) { TextWindow tw = (TextWindow)content; if (!tw.finishing) { String [] strings = cell.getTextViewContents(); tw.updateText(strings); } } } } } /** * Method to convert an array of strings to a single string. * @param strings the array of strings. * @return the single string. */ private static String makeOneString(String [] strings) { StringBuffer sb = new StringBuffer(); int len = strings.length; for(int i=0; i<len; i++) { sb.append(strings[i]); sb.append("\n"); } return sb.toString(); } /** * Method to return the number of lines of text in this TextWindow. * @return the number of lines of text in this TextWindow. */ public int getLineCount() { Document doc = textArea.getDocument(); Element paragraph = doc.getDefaultRootElement(); int lines = paragraph.getElementCount(); return lines; } /** * Method to convert the document in this window to an array of strings. * @return an array of strings with the current text. */ public String [] convertToStrings() { Document doc = textArea.getDocument(); Element paragraph = doc.getDefaultRootElement(); int lines = paragraph.getElementCount(); String [] strings = new String[lines]; for(int i=0; i<lines; i++) { Element e = paragraph.getElement(i); int startPos = e.getStartOffset(); int endPos = e.getEndOffset(); try { strings[i] = textArea.getText(startPos, endPos - startPos - 1); } catch (BadLocationException ex) {} } return strings; } public void rightScrollChanged(int value) {} public void bottomScrollChanged(int value) {} public void repaint() {} public void fullRepaint() {}// /** Returns true if we can go back in history list, false otherwise */// public boolean cellHistoryCanGoBack() { return false; }//// /** Returns true if we can go forward in history list, false otherwise */// public boolean cellHistoryCanGoForward() { return false; }// public void cellHistoryGoBack() {}//// public void cellHistoryGoForward() {} /** * Method to pan and zoom the screen so that the entire cell is displayed. */ public void fillScreen() {} public void zoomOutContents() {} public void zoomInContents() {} public void focusOnHighlighted() {} private String searchString = null; private boolean searchCaseSensitive = false; /** * Method to initialize for a new text search. * @param search the string to locate. * @param caseSensitive true to match only where the case is the same. * @param regExp true if the search string is a regular expression. * @param whatToSearch a collection of text types to consider. * @param codeRestr a restriction on types of Code to consider (null to consider all Code values). * @param unitRestr a restriction on types of Units to consider (null to consider all Unit values). * @param highlightedOnly true to search only in the highlighted area. */ public void initTextSearch(String search, boolean caseSensitive, boolean regExp, Set<TextUtils.WhatToSearch> whatToSearch, CodeExpression.Code codeRestr, TextDescriptor.Unit unitRestr, boolean highlightedOnly) { if (regExp) System.out.println("Text windows don't yet implement Regular Expression matching"); searchString = search; searchCaseSensitive = caseSensitive; } /** * Method to find the next occurrence of a string. * @param reverse true to find in the reverse direction. * @return true if something was found. */ public boolean findNextText(boolean reverse) { Document doc = textArea.getDocument(); Element paragraph = doc.getDefaultRootElement(); int lines = paragraph.getElementCount(); int lineNo = 0; int searchPoint = textArea.getSelectionEnd(); if (reverse) searchPoint = textArea.getSelectionStart(); try { lineNo = textArea.getLineOfOffset(searchPoint); } catch (BadLocationException e) { return false; } for(int i=0; i<=lines; i++) { int index = lineNo + i; if (reverse) index = lineNo - i + lines; Element e = paragraph.getElement(index % lines); int startPos = e.getStartOffset(); int endPos = e.getEndOffset(); if (i == 0) { if (reverse) endPos = searchPoint+1; else startPos = searchPoint; } String theLine = null; try { theLine = textArea.getText(startPos, endPos - startPos - 1); } catch (BadLocationException ex) { return false; } int foundPos = TextUtils.findStringInString(theLine, searchString, 0, searchCaseSensitive, reverse); if (foundPos >= 0) { textArea.setSelectionStart(startPos + foundPos); textArea.setSelectionEnd(startPos + foundPos + searchString.length()); return true; } } return false; } /** * Method to replace the text that was just selected with findNextText(). * @param replace the new text to replace. */ public void replaceText(String replace) { int startSelection = textArea.getSelectionStart(); int endSelection = textArea.getSelectionEnd(); textArea.replaceRange(replace, startSelection, endSelection); } /** * Method to replace all selected text. * @param replace the new text to replace everywhere. */ public void replaceAllText(String replace) { Document doc = textArea.getDocument(); Element paragraph = doc.getDefaultRootElement(); int lines = paragraph.getElementCount(); for(int i=0; i<lines; i++) { Element e = paragraph.getElement(i); int startPos = e.getStartOffset(); int endPos = e.getEndOffset()-1; String theLine = null; try { theLine = textArea.getText(startPos, endPos - startPos); } catch (BadLocationException ex) { return; } boolean found = false; int scanPos = 0; for(;;) { int foundPos = TextUtils.findStringInString(theLine, searchString, scanPos, searchCaseSensitive, false); if (foundPos < 0) break; theLine = theLine.substring(0, foundPos) + replace + theLine.substring(foundPos+searchString.length()); scanPos = foundPos + replace.length(); found = true; } if (found) textArea.replaceRange(theLine, startPos, endPos); } } /** * Method to export directly PNG file * @param ep printable object. * @param filePath */ public void writeImage(ElectricPrinter ep, String filePath) { System.out.println("TextWindow:writeImage not implemented"); } /** * Method to intialize for printing. * @param ep the ElectricPrinter object. * @param pageFormat information about the print job. * @return Always true. */ public boolean initializePrinting(ElectricPrinter ep, PageFormat pageFormat) { int pageWid = (int)pageFormat.getImageableWidth() * ep.getDesiredDPI() / 72; int pageHei = (int)pageFormat.getImageableHeight() * ep.getDesiredDPI() / 72; overall.setSize(pageWid, pageHei); overall.validate(); overall.repaint(); return true; } /** * Method to print window using offscreen canvas. * @param ep printable object. * @return the image to print (null on error). */ public BufferedImage getPrintImage(ElectricPrinter ep) { return null; } /** * Method to pan along X or Y according to fixed amount of ticks * @param direction * @param panningAmounts * @param ticks */ public void panXOrY(int direction, double[] panningAmounts, int ticks) { // Nothing in this case } /** * Method to shift the window so that the current cursor location becomes the center. */ public void centerCursor() { // Nothing in this case }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -