⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swedit.java

📁 Text editor implemented in Java using SWT for UI. It is intended to be a minimalist programmer s edi
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                       propertiesFilename + "\" :\n" + e);    }  }//=============================================================================//=============================================================================// BEGIN SWEDIT COMMANDS SECTION//=============================================================================//=============================================================================  // Quit the application.  All buffers will be closed in order, with a  // confirmation dialog for saving if the buffer has changed.  final class SweditQuit extends SweditCommand{    public CmdReturn exec(){      quitFlag = true;      // A global variable is needed to track a cancel request from a dialog      cancelFlag = false;      // If there is at least one more buffer and the user hasn't requested a cancel      while ((buffers.size() > 0) && !cancelFlag){        // Close the buffer        close(true);      }      // If all buffers have been closed      if (buffers.size() == 0){        // Dispose of the main shell.  This effectively terminates the program.        shell.dispose();      }      // Set the cancelFlag back to false for the next dialog.      cancelFlag = false;      return new CmdReturn(false, false, true);    }    public String toString(){      return "Quit";    }  }//=============================================================================  // Save the current buffer to file  final class SweditSave extends SweditCommand{    public CmdReturn exec(){      // if the file name is known, just write the file to disk      if (filename.indexOf("Untitled") == -1){        save(filename);      }else{      // If the file name is not known, it's got to be a Save As        save();      }      return new CmdReturn(true, true, false);    }    public String toString(){      return "Save";    }  }//=============================================================================  // Open a FileDialog to get a file name to save the current buffer to  final class SweditSaveAs extends SweditCommand{    public CmdReturn exec(){      save();      return new CmdReturn(true, true, false);    }    public String toString(){      return "Save As";    }  }//=============================================================================  // Open or Load an existing file through a FileDialog  final class SweditLoad extends SweditCommand{    public CmdReturn exec(){      String path;      File file = new File (filename);      path = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(file.getName()));      // Open a file dialog      FileDialog fileOpen = new FileDialog (shell, SWT.OPEN);      fileOpen.setText("Open File");      fileOpen.setFilterPath(path);       fileOpen.open();      // Check that the filename selected is valid      if (!fileOpen.getFileName().equals("")){        file = new File (fileOpen.getFilterPath(), fileOpen.getFileName());        if (file.exists()){          load(file.getPath());        }      }      editor.redraw();      return new CmdReturn(true, true, false);    }    public String toString(){      return "Load";    }  }//=============================================================================  // Create a new (untitled) file  final class SweditNew extends SweditCommand{    public CmdReturn exec(){      newFile();      return new CmdReturn(true, true, false);    }    public String toString(){      return "New";    }  }//=============================================================================  // Close the current buffer  final class SweditClose extends SweditCommand{    public CmdReturn exec(){      close(false);      editor.redraw();      return new CmdReturn(true, true, false);    }    public String toString(){      return "Close";    }  }//=============================================================================  // Undo the last modification to the text  final class SweditUndo extends SweditCommand{    public CmdReturn exec(){      Command uncommand;      boolean linked = false;        // The while loop handles linked commands.  This could have been done with      // a do while instead of the while(true) with break commands; personal      // preference, I guess.      while(true){        // If the stack is empty there's no command to undo so just return        if (history.empty()){          return new CmdReturn(true, true, false);        }        // Pop the last command off the top of the stack        uncommand = (Command)history.pop();        // Replace the new text with the old text        editor.replaceTextRange(uncommand.pos(),                                 uncommand.newtext().length(),                                 uncommand.oldtext());        // Make sure the caret goes to the end of the old text        editor.setCaretOffset(uncommand.pos() + uncommand.oldtext().length());        // push this same data onto the Redo stack        lastColumn = getColumn();        // If this command is not linked to the next, i.e. we're done undoing        if (!uncommand.link()){          // If the linked flag is set it means that this is the last of a          // series of linked commands.  Because popping from history and          // pushing to rehistory reverses the order, this is now the FIRST          // linked command in the redo stack.          if (linked){            rehistory.push(new Command (uncommand.oldtext(),                                         uncommand.newtext(),                                         uncommand.pos(), true));          }else{            // Normally, just push the command on to the redo stack.            rehistory.push(uncommand);          }          break;        // If this command is linked to the next        }else{          if (linked){            // in the midst of undoing a linked command, and not at the end or            // start of the link, simply push this command on to the redo stack.            rehistory.push(uncommand);          }else{            // If the linked flag is not set it means that this is the first in a            // series of linked commands.  Because popping from history and            // pushing to rehistory reverses the order, this is now the LAST linked            // command in rehistory.  So push on to the redo stack a command that            // is identical to this one, except that link is false            rehistory.push(new Command (uncommand.oldtext(),                                         uncommand.newtext(),                                         uncommand.pos(), false));            // Remember that a linked command has started            linked = true;          }        }      }      editor.redraw();      return new CmdReturn(true, true, false);    }    public String toString(){      return "Undo";    }  }//=============================================================================  // Redo the last uno  final class SweditRedo extends SweditCommand{    public CmdReturn exec(){      Command recommand;      boolean linked = false;        while(true){        // If the stack is empty just return        if (rehistory.empty()){          return new CmdReturn(true, true, false);        }        // Pop the last command off the top of the stack        recommand = (Command)rehistory.pop();        // Replace the old text with the new text        editor.replaceTextRange(recommand.pos(),                                 recommand.oldtext().length(),                                 recommand.newtext());        // Make sure the caret goes to the end of the new text        editor.setCaretOffset(recommand.pos() + recommand.newtext().length());        lastColumn = getColumn();        // If this command is not linked to the next, i.e. we're done undoing        if (!recommand.link()){          // If the linked flag is set it means that this is the last of a series          // of linked commands.  Because popping from history and pushing to          // rehistory reverses the order, this is now the FIRST linked command          // in the undo stack.          if (linked){            history.push(new Command (recommand.oldtext(),                                      recommand.newtext(),                                       recommand.pos(), true));          }else{            // Normally, just push the command on to the undo stack.            history.push(recommand);          }          break;        // If this command is linked to the next        }else{          if (linked){            // in the midst of redoing a linked command, and not at the end or            // start of the link, push this command back on to the undo stack.            history.push(recommand);          }else{            // If the linked flag is not set it means that this is the first in            // a series of linked commands.  Because popping from history and            // pushing to rehistory reverses the order, this is now the LAST            // linked command in history.  So push on to the undo stack a            //  command that is identical to this one, except that link is false            history.push(new Command (recommand.oldtext(),                                       recommand.newtext(),                                       recommand.pos(), false));            // Remember that a linked command has started            linked = true;          }        }      }      editor.redraw();      return new CmdReturn(true, true, false);    }    public String toString(){      return "Redo";    }  }//=============================================================================  // Cut the current selection  final class SweditCut extends SweditCommand{    public CmdReturn exec(){      // There must be selected text      if (editor.getSelectionCount() > 0){        // Get the data on the selected text        int selectionStart = editor.getSelectionRange().x;        Command eventUndo = new Command(editor.getSelectionText(), "",                                        selectionStart, false);        // Cut the selected text and copy it to the clipboard        editor.invokeAction(ST.CUT);        editor.redraw();        lastColumn = getColumn();        // this command can be undone        history.push(eventUndo);        // redo stack must be cleared        rehistory.clear();      }      return new CmdReturn(true, true, false);    }    public String toString(){      return "Cut";    }  }//=============================================================================  // Copy to clipboard  final class SweditCopy extends SweditCommand{    public CmdReturn exec(){      // The StyledText widget already has a copy to clipboard function      // So just invoke that action      editor.invokeAction(ST.COPY);      return new CmdReturn(true, true, false);    }    public String toString(){      return "Copy";    }  }//=============================================================================  // Paste from clipboard  final class SweditPaste extends SweditCommand{    public CmdReturn exec(){      // temp variables      int oldLength, newLength, startPos;      Command eventUndo;      String tempString;      // If text is selected, pasted text replaces it      if (editor.getSelectionCount() > 0){        startPos = editor.getSelectionRange().x;        tempString = new String(editor.getSelectionText());      }else{        startPos = editor.getCaretOffset();        tempString = "";      }      // remember the length of the document prior to the paste      oldLength = editor.getCharCount();      // get the data on the selected text, including the text      // invoke the StyledText widget's built in paste function      editor.invokeAction(ST.PASTE);      // Figure out how long the document now is. This is essentially the new      // length minus the old length, plus the number of any characters that      // were selected.  We need this to grab the pasted text so it can be      // stripped of control characters and stored in the undo stack      newLength = editor.getCharCount() - oldLength + tempString.length();      String pastedText = editor.getTextRange(startPos, newLength);      String newText =         stripControlCharacters(pastedText, expandTabs, editor.getTabs());      editor.replaceTextRange(startPos, pastedText.length(), newText);      // set up the undo information      eventUndo = new Command(tempString, newText, startPos, false);      // push the undo info on to the stack      history.push(eventUndo);      // clear the redo stack      rehistory.clear();      editor.redraw();      lastColumn = getColumn();      return new CmdReturn(true, true, false);    }    public String toString(){      return "Paste";    }  }//=============================================================================  // Cut block-selected text  final class SweditCutBlock extends SweditCommand{    public CmdReturn exec(){      if ((blockHeight <= 0)           || (blockWidth <= 0)          || (blockStart < 0)){        return new CmdReturn(true, false, false);      }      copyBlock();      // Go through each selected line      int startLine = editor.getLineAtOffset(blockStart);      int startCol = blockStart - editor.getOffsetAtLine(startLine);      int pos;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -