1 /*
2  * TerminalHelper.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 
16 package org.rstudio.studio.client.workbench.views.terminal;
17 
18 import org.rstudio.core.client.widget.MessageDialog;
19 import org.rstudio.studio.client.application.events.EventBus;
20 import org.rstudio.studio.client.common.GlobalDisplay;
21 import org.rstudio.studio.client.workbench.prefs.model.UserPrefs;
22 import org.rstudio.studio.client.workbench.views.terminal.events.TerminalBusyEvent;
23 
24 import com.google.gwt.user.client.Command;
25 import com.google.inject.Inject;
26 import com.google.inject.Singleton;
27 
28 @Singleton
29 public class TerminalHelper
30 {
31    @Inject
TerminalHelper(EventBus events, GlobalDisplay globalDisplay)32    TerminalHelper(EventBus events,
33                   GlobalDisplay globalDisplay)
34    {
35       events_ = events;
36       globalDisplay_ = globalDisplay;
37 
38       // track busy terminals
39       events_.addHandler(TerminalBusyEvent.TYPE,
40             event -> warnBeforeClosing_ = event.isBusy());
41    }
42 
warnBeforeClosing(String busyMode)43    public boolean warnBeforeClosing(String busyMode)
44    {
45       if (busyMode == UserPrefs.BUSY_DETECTION_NEVER)
46          warnBeforeClosing_ = false;
47 
48       return warnBeforeClosing_;
49    }
50 
warnBusyTerminalBeforeCommand(final Command command, String caption, String question, String busyMode)51    public void warnBusyTerminalBeforeCommand(final Command command,
52                                              String caption,
53                                              String question,
54                                              String busyMode)
55    {
56       if (!warnBeforeClosing(busyMode))
57       {
58          command.execute();
59          return;
60       }
61 
62       globalDisplay_.showYesNoMessage(
63             MessageDialog.QUESTION,
64             caption,
65             "The terminal is currently busy. " + question,
66             command::execute,
67             true);
68    }
69 
70    private boolean warnBeforeClosing_;
71 
72    // Injected ----
73    private GlobalDisplay globalDisplay_;
74    private EventBus events_;
75  }
76