Bug 4840 fixed: The more there was text in the console, the slower was the display
[scilab.git] / scilab / modules / console / src / java / org / scilab / modules / console / InterruptScilabWorkOrCopy.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007-2008 - INRIA - Vincent COUVERT
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.console;
14
15 import java.awt.datatransfer.StringSelection;
16 import java.awt.event.ActionEvent;
17 import java.awt.Toolkit;
18 import javax.swing.JPanel;
19 import javax.swing.JEditorPane;
20 import javax.swing.JTextPane;
21
22 import org.scilab.modules.action_binding.InterpreterManagement;
23
24 import com.artenum.rosetta.core.action.AbstractConsoleAction;
25 import com.artenum.rosetta.util.StringConstants;
26
27 /**
28  * Stops Scilab current work to enter pause mode
29  * Or copy selected text (if there is)
30  * This event is configured in configuration.xml file
31  * @author Vincent COUVERT
32  */
33 public class InterruptScilabWorkOrCopy extends AbstractConsoleAction {
34
35         private static final long serialVersionUID = 1L;
36
37         /**
38          * Constructor
39          */
40         public InterruptScilabWorkOrCopy() {
41                 super();
42
43         }
44
45         /**
46          * Threats the event
47          * @param e the action event that occured
48          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
49          */
50         public void actionPerformed(ActionEvent e) {
51
52                 if (((JTextPane) configuration.getInputCommandView()).getSelectedText() != null) {
53                         /* Text selected in the input --> Copy */
54                         StringSelection strSelected = new StringSelection(((JTextPane) configuration.getInputCommandView()).getSelectedText());
55                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSelected, null);
56                 } else if (((JEditorPane) configuration.getOutputView()).getSelectedText() != null) {
57                         /* Text selected in the output --> Copy */
58                         StringSelection strSelected = new StringSelection(((JEditorPane) configuration.getOutputView()).getSelectedText());
59                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSelected, null);
60                 } else {
61                         /* Interrupt Scilab */
62                         InterpreterManagement.interruptScilab();
63
64                         // If Scilab is on prompt, then emulate a user entry
65                         if (((JPanel) configuration.getPromptView()).isVisible()) {
66                                 configuration.getOutputView().append(StringConstants.NEW_LINE);
67                                 configuration.getOutputView().append(configuration.getPromptView().getDefaultPrompt());
68                                 configuration.getOutputView().append(configuration.getInputParsingManager().getCommandLine());
69                                 configuration.getOutputView().append(StringConstants.NEW_LINE);
70                                 ((SciOutputView) configuration.getOutputView()).getConsole().sendCommandsToScilab("", false, false);
71                                 configuration.getInputParsingManager().reset();
72                         }
73                 }
74         }
75
76 }