@@ -356,6 +356,17 @@ public TextEditor(final Context context) {
356356 makeJarWithSource = addToMenu (file , "Export as JAR (With Source)..." , 0 , 0 );
357357 makeJarWithSource .setMnemonic (KeyEvent .VK_X );
358358 file .addSeparator ();
359+ final JCheckBoxMenuItem lockPane = new JCheckBoxMenuItem ("Lock (Make Read Only)" );
360+ lockPane .setToolTipText ("Protects file from accidental editing" );
361+ file .add (lockPane );
362+ lockPane .addActionListener (e -> {
363+ if (lockPane .isSelected ()) {
364+ new SetReadOnlyAction ().actionPerformedImpl (e , getEditorPane ());
365+ } else {
366+ new SetWritableAction ().actionPerformedImpl (e , getEditorPane ());
367+ }
368+ });
369+ tabbed .addChangeListener (e -> lockPane .setSelected (getTab (tabbed .getSelectedIndex ()).editorPane .isLocked ()));
359370 JMenuItem jmi = new JMenuItem ("Revert..." );
360371 jmi .addActionListener (e -> {
361372 if (getEditorPane ().isLocked ()) {
@@ -915,17 +926,6 @@ private void assembleEditMenu() {
915926 + getEditorPane ().getPaneActions ().getAcceleratorLabel (EditorPaneActions .epaToggleCommentAltAction ));
916927 addMappedActionToMenu (editMenu , "Insert Time Stamp" , EditorPaneActions .rtaTimeDateAction , true );
917928 removeTrailingWhitespace = addToMenu (editMenu , "Remove Trailing Whitespace" , 0 , 0 );
918- final JCheckBoxMenuItem lock = new JCheckBoxMenuItem ("Lock Editing" );
919- editMenu .add (lock );
920- lock .addActionListener (e -> {
921- for (int i = 0 ; i < tabbed .getTabCount (); i ++) {
922- if (lock .isSelected ()) {
923- new SetReadOnlyAction ().actionPerformedImpl (e , getEditorPane (i ));
924- } else {
925- new SetWritableAction ().actionPerformedImpl (e , getEditorPane (i ));
926- }
927- }
928- });
929929 zapGremlins = addToMenu (editMenu , "Zap Gremlins" , 0 , 0 );
930930 zapGremlins .setToolTipText ("Removes invalid (non-printable) ASCII characters" );
931931 }
@@ -934,6 +934,10 @@ private void addScritpEditorMacroCommands(final JMenu menu) {
934934 addMenubarSeparator (menu , "Script Editor Macros:" );
935935 final JMenuItem startMacro = new JMenuItem ("Start/Resume Macro Recording" );
936936 startMacro .addActionListener (e -> {
937+ if (getEditorPane ().isLocked ()) {
938+ error ("File is currently locked." );
939+ return ;
940+ }
937941 final String state = (RTextArea .getCurrentMacro () == null ) ? "on" : "resumed" ;
938942 write ("Script Editor: Macro recording " + state );
939943 RTextArea .beginRecordingMacro ();
@@ -984,6 +988,10 @@ private void addScritpEditorMacroCommands(final JMenu menu) {
984988 final JMenuItem playMacro = new JMenuItem ("Run Recorded Macro" );
985989 playMacro .setToolTipText ("Runs current recordings. Prompts for\n recordings file if no recordings exist" );
986990 playMacro .addActionListener (e -> {
991+ if (getEditorPane ().isLocked ()) {
992+ error ("File is currently locked." );
993+ return ;
994+ }
987995 if (null == RTextArea .getCurrentMacro ()) {
988996 final File fileToOpen = getMacroFile (true );
989997 if (fileToOpen != null ) {
@@ -1683,11 +1691,7 @@ else if (source == close) if (tabbed.getTabCount() < 2) processWindowEvent(new W
16831691 if (index > 0 ) index --;
16841692 switchTo (index );
16851693 }
1686- else if (source == cut ) getTextArea ().cut ();
16871694 else if (source == copy ) getTextArea ().copy ();
1688- else if (source == paste ) getTextArea ().paste ();
1689- else if (source == undo ) getTextArea ().undoLastAction ();
1690- else if (source == redo ) getTextArea ().redoLastAction ();
16911695 else if (source == find ) findOrReplace (true );
16921696 else if (source == findNext ) {
16931697 findDialog .setRestrictToConsole (false );
@@ -1708,21 +1712,6 @@ else if (source == chooseFontSize) {
17081712 else if (source == chooseTabSize ) {
17091713 commandService .run (ChooseTabSize .class , true , "editor" , this );
17101714 }
1711- else if (source == addImport ) {
1712- addImport (getSelectedClassNameOrAsk ("Add import (complete qualified name of class/package)" ,
1713- "Which Class to Import?" ));
1714- }
1715- else if (source == removeUnusedImports ) new TokenFunctions (getTextArea ())
1716- .removeUnusedImports ();
1717- else if (source == sortImports ) new TokenFunctions (getTextArea ())
1718- .sortImports ();
1719- else if (source == removeTrailingWhitespace ) new TokenFunctions (
1720- getTextArea ()).removeTrailingWhitespace ();
1721- else if (source == replaceTabsWithSpaces ) getTextArea ()
1722- .convertTabsToSpaces ();
1723- else if (source == replaceSpacesWithTabs ) getTextArea ()
1724- .convertSpacesToTabs ();
1725- else if (source == zapGremlins ) zapGremlins ();
17261715 else if (source == openClassOrPackageHelp ) openClassOrPackageHelp (null );
17271716 else if (source == extractSourceJar ) extractSourceJar ();
17281717 else if (source == openSourceForClass ) {
@@ -1771,6 +1760,36 @@ else if (source == increaseFontSize || source == decreaseFontSize) {
17711760 else if (source == nextTab ) switchTabRelative (1 );
17721761 else if (source == previousTab ) switchTabRelative (-1 );
17731762 else if (handleTabsMenu (source )) return ;
1763+ else {
1764+ // commands that should not run when files are locked!
1765+ if (getEditorPane ().isLocked ()) {
1766+ error ("File is currently locked." );
1767+ return ;
1768+ }
1769+ if (source == cut )
1770+ getTextArea ().cut ();
1771+ else if (source == paste )
1772+ getTextArea ().paste ();
1773+ else if (source == undo )
1774+ getTextArea ().undoLastAction ();
1775+ else if (source == redo )
1776+ getTextArea ().redoLastAction ();
1777+ else if (source == addImport ) {
1778+ addImport (getSelectedClassNameOrAsk ("Add import (complete qualified name of class/package)" ,
1779+ "Which Class to Import?" ));
1780+ } else if (source == removeUnusedImports )
1781+ new TokenFunctions (getTextArea ()).removeUnusedImports ();
1782+ else if (source == sortImports )
1783+ new TokenFunctions (getTextArea ()).sortImports ();
1784+ else if (source == removeTrailingWhitespace )
1785+ new TokenFunctions (getTextArea ()).removeTrailingWhitespace ();
1786+ else if (source == replaceTabsWithSpaces )
1787+ getTextArea ().convertTabsToSpaces ();
1788+ else if (source == replaceSpacesWithTabs )
1789+ getTextArea ().convertSpacesToTabs ();
1790+ else if (source == zapGremlins )
1791+ zapGremlins ();
1792+ }
17741793 }
17751794
17761795 private void setAutoCompletionEnabled (final boolean enabled ) {
@@ -2098,20 +2117,24 @@ public static boolean isBinary(final File file) {
20982117 }
20992118
21002119 /**
2101- * Open a new tab with some content; the languageExtension is like ".java",
2102- * ".py", etc.
2120+ * Opens a new tab with some content.
2121+ *
2122+ * @param content the script content
2123+ * @param languageExtension the file extension associated with the content's
2124+ * language (e.g., ".java", ".py", etc.
2125+ * @return the text editor tab
21032126 */
2104- public TextEditorTab newTab (final String content , final String language ) {
2105- String lang = language ;
2127+ public TextEditorTab newTab (final String content , final String languageExtension ) {
2128+ String lang = languageExtension ;
21062129 final TextEditorTab tab = open (null );
21072130 if (null != lang && lang .length () > 0 ) {
21082131 lang = lang .trim ().toLowerCase ();
21092132
21102133 if ('.' != lang .charAt (0 )) {
2111- lang = "." + language ;
2134+ lang = "." + languageExtension ;
21122135 }
21132136
2114- tab .editorPane .setLanguage (scriptService .getLanguageByName (language ));
2137+ tab .editorPane .setLanguage (scriptService .getLanguageByName (languageExtension ));
21152138 } else {
21162139 final String lastLanguageName = prefService .get (getClass (), LAST_LANGUAGE );
21172140 if (null != lastLanguageName && "none" != lastLanguageName )
@@ -2189,11 +2212,11 @@ public TextEditorTab open(final File file) {
21892212 }
21902213 catch (final FileNotFoundException e ) {
21912214 log .error (e );
2192- error ("The file '" + file + "' was not found." );
2215+ error ("The file\n '" + file + "' was not found." );
21932216 }
21942217 catch (final Exception e ) {
21952218 log .error (e );
2196- error ("There was an error while opening '" + file + "': " + e );
2219+ error ("There was an error while opening\n '" + file + "': " + e );
21972220 }
21982221 return null ;
21992222 }
0 commit comments