Bug 103045 - [EditorMgmt] [RCP] Need way to disable "New Editor" action
Summary: [EditorMgmt] [RCP] Need way to disable "New Editor" action
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: All All
: P3 enhancement with 5 votes (vote)
Target Milestone: ---   Edit
Assignee: Platform UI Triaged CLA
QA Contact:
URL:
Whiteboard:
Keywords: bugday, helpwanted
: 181585 190081 282388 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-07-07 13:45 EDT by adrian CLA
Modified: 2017-05-26 09:25 EDT (History)
18 users (show)

See Also:


Attachments
Initial patch (13.85 KB, patch)
2009-03-25 20:47 EDT, Philipp Kursawe CLA
no flags Details | Diff
mylyn/context/zip (1.63 KB, application/octet-stream)
2009-03-25 20:47 EDT, Philipp Kursawe CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description adrian CLA 2005-07-07 13:45:31 EDT
1.- I need a way to disable R3.1's "New Editor" action in the scope of my
editor.  My editor may perform certain adjustments (e.g., for those columns in
each text line defined as 'serial numbers') on the document when it receives
IDocument change notifications - having two (or more) editors with conflicting
settings may create an unstable combination...

2.- My editor has its own split-window function, so another possibility would be
to provide the capability for an editor to reimplement this action.

I see this action in two places:
 - on the "Window" top pull-down menu, and
 - on the context menu of the editor tab.

3.- Any workaround suggestions for the current Eclipse 3.1 release version are
also welcome (short of throwing an exception when the editor receives an open
request for an already-opened-by-it editor input)...
Comment 1 Nick Edgar CLA 2005-07-07 15:54:33 EDT
Even before New Editor was added, it was possible to get multiple editors on the
same input by using multiple windows (Window > New Window, then opening the same
input).

The recommended approach is to make the editors support a shared document model,
like the text editors in the IDE.

Sorry, but I don't know of a better workaround at the moment.
Comment 2 Edoardo Comar CLA 2005-10-20 11:23:19 EDT
whatever the solution, the logic for the new editor action is duplicated in

org.eclipse.ui.internal.presentations.SystemMenuNewEditor
and
org.eclipse.ui.internal.actions.NewEditorAction
Comment 3 Alan Boxall CLA 2007-09-17 15:11:41 EDT
I would also like a way to disable this action.

At present I don't own the editor that I extend.   The underlying editor doesn't support opening multiple views on the same document in different windows.

Comment 4 Boris Bokowski CLA 2007-09-17 16:01:06 EDT
*** Bug 190081 has been marked as a duplicate of this bug. ***
Comment 5 Boris Bokowski CLA 2007-09-17 22:36:29 EDT
Any help with this would be greatly appreciated. A new method on the workbench window configurer would be a good way to expose this setting for RCP apps.
Comment 6 Philipp Kursawe CLA 2009-03-25 17:14:53 EDT
I think this could be set as a preference from the products customization like the defaultPerspectiveId. That way you would not have to code anything. You just set
org.eclipse.ui/SHOW_NEW_EDITOR=false

Are there any RCP apps that do not use a product configuration? They would have to have a way in code to disable that. But first I would implement a patch for using a preference.
Comment 7 Ralf Ebert CLA 2009-03-25 17:51:20 EDT
Even better would be to let the editor decide if it can handle multiple instances. This could be declared as an attribute 'allowMultiple' for 'editor' elements in 'org.eclipse.ui.editors' extensions (analog to allowMultiple for views). Default would be true for backward-compatibility, the new editor handler would be disabled if allowMultiple = false.

This is bug is tedious for RCP developers because you need to be prepared for multiple editors even if you don't want to support this feature in your application.
Comment 8 Philipp Kursawe CLA 2009-03-25 18:11:56 EDT
I have to agree with Ralf. This sounds even better than enabling/disabling new editors by preference or workbench advisor.
Comment 9 Philipp Kursawe CLA 2009-03-25 20:47:15 EDT
Created attachment 129911 [details]
Initial patch

I added a method to the IEditorDescriptor allowMultiple() similiar to IViewDescriptor. I added a new property tester "org.eclipse.ui.editors.EditorPartTester" with the namespace "org.eclipse.ui.editors" for IEditorPart that currently only can test if a given IEditorPart supports the creation of a new editor. This new property tester is used by the enablement of the NewEditorHandler core expression.

This will disable the "New Editor" in the editors system menu. I would rather prefer the entry to vanish completly if the editor does not support multiple instance. But since the action is hardcoded into EditorSystemMenu it does not take into account any enablement to make itself invisble if there is not handler. Maybe someone else can fix this. 
But as it is for now I can live with an disabled menu entry.
That covers the UI enablement pretty good I think.

However, we should also take care that its not possible to open another editor programmatically. So the methods for opening editors would have to be fixed also to take "allowMultiple" into account and activate the currently open editor in case it is tried to open a second instance of a "single" editor. We would have to discuss if in that case the already open editor is updated with a new IEditorInput.
Comment 10 Philipp Kursawe CLA 2009-03-25 20:47:25 EDT
Created attachment 129912 [details]
mylyn/context/zip
Comment 11 Boris Bokowski CLA 2009-03-30 11:28:03 EDT
(In reply to comment #9)
> This will disable the "New Editor" in the editors system menu. I would rather
> prefer the entry to vanish completly if the editor does not support multiple
> instance. But since the action is hardcoded into EditorSystemMenu it does not
> take into account any enablement to make itself invisble if there is not
> handler. Maybe someone else can fix this. 

Maybe Paul? ;-)

Comment 12 Boris Bokowski CLA 2009-03-30 11:29:46 EDT
Btw, thanks for the patch! Not sure if this can be put into 3.5 since we are past the API freez, but I can try to get approval once we have a patch that everybody is happy with.
Comment 13 Paul Webster CLA 2009-03-30 12:47:56 EDT
(In reply to comment #9)
> This will disable the "New Editor" in the editors system menu. I would rather
> prefer the entry to vanish completly if the editor does not support multiple
> instance. But since the action is hardcoded into EditorSystemMenu it does not
> take into account any enablement to make itself invisble if there is not
> handler. Maybe someone else can fix this. 
> But as it is for now I can live with an disabled menu entry.
> That covers the UI enablement pretty good I think.

The code that would be needed is in org.eclipse.ui.internal.presentations.util.StandardEditorSystemMenu.show(Control, Point, IPresentablePart)

If the openAgain action contribution item was saved it could have its visibility set to false based on the action state ... although if it is already disabled, there's something to be gained by not having the menu items hop around.

The other place we would have to check is org.eclipse.ui.IWorkbenchPage.openEditor(IEditorInput, String, boolean, int) and its callers because one of the match flags is #MATCH_NONE (don't check for an existing input)

PW
Comment 14 Boris Bokowski CLA 2009-04-24 12:01:29 EDT
Marking for M6. Adrian, would you be able to update your patch based on Paul's comments?
Comment 15 Boris Bokowski CLA 2009-04-24 12:18:11 EDT
(In reply to comment #14)
> Marking for M6.

Err... 3.6
Comment 16 Boris Bokowski CLA 2009-11-17 13:07:38 EST
Remy is now responsible for watching the [EditorMgmt] component area.
Comment 17 Remy Suen CLA 2009-11-17 15:19:14 EST
*** Bug 181585 has been marked as a duplicate of this bug. ***
Comment 18 Remy Suen CLA 2009-11-24 10:51:08 EST
*** Bug 282388 has been marked as a duplicate of this bug. ***
Comment 19 Remy Suen CLA 2009-11-24 10:52:04 EST
From bug 282388 comment 6:

> Hi,
> 
> I just managed to disable the "new editor" action within Galileo by creating a
> handler for command "org.eclipse.ui.window.newEditor".
> The handler is active for my editors but never enabled 
> => expression: activeWhen/activePart/instanceof(my editors)
> => expression: enabledWhen/count/value -1
> 
> This allows disabling the command for your editors, but not for standard text
> editors.
> 
> Maybe Paul can tell if this might be the correct way to go currently.
Comment 20 Paul Webster CLA 2009-11-24 14:56:56 EST
(In reply to comment #19)
> > handler for command "org.eclipse.ui.window.newEditor".
> > The handler is active for my editors but never enabled 
> > => expression: activeWhen/activePart/instanceof(my editors)
> > => expression: enabledWhen/count/value -1
> > 
> > This allows disabling the command for your editors, but not for standard text
> > editors.

You are basically providing a higher priority handler for that command.  Depending on the activeWhen you write, you can be available some of the time (your own editors) or all of the time.

It will work ... until someone provides a "more specific" handler that's enabled.

PW
Comment 21 Philipp Kursawe CLA 2009-11-26 01:52:02 EST
So what are we doing with this bug now? Is there any way to solve it?
Comment 22 Paul Webster CLA 2009-11-26 08:02:52 EST
(In reply to comment #21)
> So what are we doing with this bug now? Is there any way to solve it?

Right now the solution is to provide a "more specific" handler that's disabled.  That can be used to disable it for the app or for a specific editor type (although the original problem can be avoided using the shared buffer approach as Platform Text does).

No one is working on this (hence the helpwanted, bugday).  If there is more needed, then it should target the specific action (not the editor definitions themselves).

PW
Comment 23 kelvin kelvinsky CLA 2010-05-06 08:06:58 EDT
Here is an example
plugin.xml:
<extension point="org.eclipse.ui.handlers">
    <handler
          class="DisabledNewEditorHandler"
          commandId="org.eclipse.ui.window.newEditor">
       <enabledWhen>
          <count value="-1">
          </count>
       </enabledWhen>
       <activeWhen>
          <not>
             <count value="-1">
             </count>
          </not>
       </activeWhen>
    </handler>
</extension>

public class DisabledNewEditorHandler implements IHandler {
 public boolean isEnabled() {
        return false;
    }
 ...
}
Comment 24 Wolfgang Geck CLA 2010-11-04 08:57:29 EDT
(In reply to comment #0)
> 1.- I need a way to disable R3.1's "New Editor" action in the scope of my
> editor.  My editor may perform certain adjustments (e.g., for those columns in
> each text line defined as 'serial numbers') on the document when it receives
> IDocument change notifications - having two (or more) editors with conflicting
> settings may create an unstable combination...
> 
> 2.- My editor has its own split-window function, so another possibility would be
> to provide the capability for an editor to reimplement this action.
> 
> I see this action in two places:
>  - on the "Window" top pull-down menu, and
>  - on the context menu of the editor tab.
> 
> 3.- Any workaround suggestions for the current Eclipse 3.1 release version are
> also welcome (short of throwing an exception when the editor receives an open
> request for an already-opened-by-it editor input)...

(In reply to comment #23)
> Here is an example
> plugin.xml:
> <extension point="org.eclipse.ui.handlers">
>     <handler
>           class="DisabledNewEditorHandler"
>           commandId="org.eclipse.ui.window.newEditor">
>        <enabledWhen>
>           <count value="-1">
>           </count>
>        </enabledWhen>
>        <activeWhen>
>           <not>
>              <count value="-1">
>              </count>
>           </not>
>        </activeWhen>
>     </handler>
> </extension>
> 
> public class DisabledNewEditorHandler implements IHandler {
>  public boolean isEnabled() {
>         return false;
>     }
>  ...
> }
Comment 25 Wolfgang Geck CLA 2010-11-04 09:00:35 EDT
place the following code somewhere in your WorkbenchAdvisor postStartup method:

public void postStartup() {
   .....
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ICommandService.class);
       commandService.getCommand("org.eclipse.ui.window.newEditor").setHandler(null);
Comment 26 Paul Webster CLA 2010-11-04 09:05:32 EDT
(In reply to comment #25)
> commandService.getCommand("org.eclipse.ui.window.newEditor").setHandler(null);

This is not a reliable way to override the handler, as the HandlerAuthority can call setHandler(*) on the newEditor command at any time when it reconciles it's handlers.

PW
Comment 27 Wolfgang Geck CLA 2010-11-04 09:22:36 EDT
Ok good to know! Although I never run into trouble using this solution i try the disabled handler solution (comment 23)
Comment 28 Lars Vogel CLA 2016-04-20 12:19:52 EDT
Mass move to 4.7 as M7 is approaching. Please move back in case you are planning to fix it for Neon.