Class FileEditorTab

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class FileEditorTab extends JPanel
Represents one file opened for editing. Maintains required internal structures. Before Mars 4.0, there was only one editor pane, a tab, and only one file could be open at a time. With 4.0 came the multi-file editor, and existing duties were split between EditPane (now FileEditorTab) and the new EditTabbedPane (now EditTab) class.
Author:
Sanderson and Bumgarner
See Also:
  • Constructor Details

    • FileEditorTab

      public FileEditorTab(VenusUI gui)
      Create a new tab within the "Edit" tab for editing a file.
  • Method Details

    • setSourceCode

      public void setSourceCode(String sourceCode, boolean editable)
      For initializing the source code when opening an ASM file.
      Parameters:
      sourceCode - String containing text
      editable - Set true if code is editable else false
    • discardAllUndoableEdits

      public void discardAllUndoableEdits()
      Get rid of any accumulated undoable edits. It is useful to call this method after opening a file into the text area. The act of setting its text content upon reading the file will generate an undoable edit. Normally you don't want a freshly-opened file to appear with its Undo action enabled. But it will unless you call this after setting the text.
    • getLineNumbersList

      public static String getLineNumbersList(Document document)
      Form string with source code line numbers. Resulting string is HTML, for which JLabel will happily honor <br> to do multiline label (it ignores \n). The line number list is a JLabel with one line number per line.
    • getSourceLineCount

      public int getSourceLineCount()
      Calculate and return number of lines in source code text. Do this by counting newline characters then adding one if last line does not end with newline character.
    • getSource

      public String getSource()
      Get the source code text.
      Returns:
      String containing source code.
    • getFileStatus

      public FileStatus getFileStatus()
      Get the file status of this tab.
      Returns:
      The file status.
    • setFileStatus

      public void setFileStatus(FileStatus fileStatus)
      Set the file status of this tab.
      Parameters:
      fileStatus - The file status.
    • hasUnsavedEdits

      public boolean hasUnsavedEdits()
      See Also:
    • isNew

      public boolean isNew()
      See Also:
    • getFile

      public File getFile()
      Get the file represented by this tab.
      Returns:
      The file object.
    • setFile

      public void setFile(File file)
      Set the file represented by this tab.
      Parameters:
      file - The file object.
    • requestTextAreaFocus

      public void requestTextAreaFocus()
      Delegates to text area's requestFocusInWindow method.
    • getUndoManager

      public UndoManager getUndoManager()
      Get the manager in charge of Undo and Redo operations
      Returns:
      The undo manager object.
    • copyText

      public void copyText()
      Copy the currently selected text to the clipboard.
    • cutText

      public void cutText()
      Cut the currently selected text to the clipboard.
    • pasteText

      public void pasteText()
      Paste the current clipboard contents at the cursor position.
    • selectAllText

      public void selectAllText()
      Select all text.
    • undo

      public void undo()
      Undo the previous edit.
    • redo

      public void redo()
      Redo the previously undone edit.
    • commentLines

      public void commentLines()
      Comment or uncomment the selection, or line at cursor if nothing is selected.
    • updateUndoRedoActions

      public void updateUndoRedoActions()
      Automatically update whether the Undo and Redo actions are enabled or disabled based on the status of the UndoManager.
    • displayCaretPosition

      public void displayCaretPosition(int offset)
      Update the caret position label on the editor's border to display the current line and column. The position is given as text stream offset and will be converted into line and column.
      Parameters:
      offset - Offset into the text stream of caret.
    • displayCaretPosition

      public void displayCaretPosition(Point position)
      Update the status bar display for the caret position.
      Parameters:
      position - Position of the caret (x = column, y = line).
    • convertStreamPositionToLineColumn

      public Point convertStreamPositionToLineColumn(int offset)
      Given character stream offset in text being edited, calculate its column and line number coordinates.
      Parameters:
      offset - Position of character.
      Returns:
      The column and line number position (x = column, y = line).
    • convertLineColumnToStreamPosition

      public int convertLineColumnToStreamPosition(int lineIndex, int columnIndex)
      Given line and column (position in the line) numbers, calculate its byte stream position in text being edited.
      Parameters:
      lineIndex - Line number in file (starts with 0)
      columnIndex - Position within that line (starts with 0)
      Returns:
      corresponding stream position. Returns -1 if there is no corresponding position.
    • selectLine

      public void selectLine(int line)
      Select the specified editor text line. Lines are numbered starting with 1, consistent with line numbers displayed by the editor.
      Parameters:
      line - The line number to select. Numbering starts at 1, and nothing will happen if the parameter value is less than 1
    • selectLine

      public void selectLine(int lineIndex, int columnIndex)
      Select the specified editor text line. Lines are numbered starting with 1, consistent with line numbers displayed by the editor.
      Parameters:
      lineIndex - The line number to select. Numbering starts at 0, and nothing will happen if the parameter value is less than 0
      columnIndex - Desired column at which to place the cursor.
    • doFindText

      public int doFindText(String find, boolean caseSensitive)
      Finds next occurrence of text in a forward search of a string. Search begins at the current cursor location, and wraps around when the end of the string is reached.
      Parameters:
      find - the text to locate in the string
      caseSensitive - true if search is to be case-sensitive, false otherwise
      Returns:
      TEXT_FOUND or TEXT_NOT_FOUND, depending on the result.
    • doReplace

      public int doReplace(String find, String replace, boolean caseSensitive)
      Finds and replaces next occurrence of text in a string in a forward search. If cursor is initially at end of matching selection, will immediately replace then find and select the next occurrence if any. Otherwise it performs a find operation. The replace can be undone with one undo operation.
      Parameters:
      find - the text to locate in the string
      replace - the text to replace the find text with - if the find text exists
      caseSensitive - true for case sensitive. false to ignore case
      Returns:
      Returns TEXT_FOUND if not initially at end of selected match and matching occurrence is found. Returns TEXT_NOT_FOUND if the text is not matched. Returns TEXT_REPLACED_NOT_FOUND_NEXT if replacement is successful but there are no additional matches. Returns TEXT_REPLACED_FOUND_NEXT if replacement is successful and there is at least one additional match.
    • doReplaceAll

      public int doReplaceAll(String find, String replace, boolean caseSensitive)
      Finds and replaces ALL occurrences of text in a string in a forward search. All replacements are bundled into one CompoundEdit, so one Undo operation will undo all of them.
      Parameters:
      find - the text to locate in the string
      replace - the text to replace the find text with - if the find text exists
      caseSensitive - true for case sensitive. false to ignore case
      Returns:
      the number of occurrences that were matched and replaced.