1 /*
2  * AppCommandBinding.java
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 package org.rstudio.core.client.command;
16 
17 import org.rstudio.core.client.command.AppCommand.Context;
18 import org.rstudio.core.client.command.KeyMap.CommandBinding;
19 
20 public class AppCommandBinding implements CommandBinding
21 {
AppCommandBinding(AppCommand command, String disableModes, boolean custom)22    public AppCommandBinding(AppCommand command, String disableModes, boolean custom)
23    {
24       command_ = command;
25       disableModes_ = ShortcutManager.parseDisableModes(disableModes);
26       custom_ = custom;
27    }
28 
29    @Override
getId()30    public String getId()
31    {
32       return command_.getId();
33    }
34 
35    @Override
execute()36    public void execute()
37    {
38       command_.executeFromShortcut();
39    }
40 
41    /**
42     * Indicates whether the binding is enabled in the current editor mode
43     *
44     * @return Whether the binding is enabled
45     */
isEnabledInCurrentMode()46    public boolean isEnabledInCurrentMode()
47    {
48       int mode = ShortcutManager.INSTANCE.getEditorMode();
49       return (disableModes_ & mode) == 0;
50    }
51 
52    @Override
isEnabled()53    public boolean isEnabled()
54    {
55       if (!command_.isEnabled())
56          return false;
57 
58       if (!isEnabledInCurrentMode())
59          return false;
60 
61       return true;
62    }
63 
64    @Override
isUserDefinedBinding()65    public boolean isUserDefinedBinding()
66    {
67       return custom_;
68    }
69 
70    @Override
getContext()71    public Context getContext()
72    {
73       return command_.getContext();
74    }
75 
76    private final AppCommand command_;
77    private final int disableModes_;
78    private final boolean custom_;
79 }
80