db3a7af23931351a0bd87dd3fbcf5707ce174a8c
[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.JTextPane;
20
21 import org.scilab.modules.action_binding.InterpreterManagement;
22
23 import com.artenum.rosetta.core.action.AbstractConsoleAction;
24 import com.artenum.rosetta.util.StringConstants;
25
26 /**
27  * Stops Scilab current work to enter pause mode
28  * Or copy selected text (if there is)
29  * This event is configured in configuration.xml file
30  * @author Vincent COUVERT
31  */
32 public class InterruptScilabWorkOrCopy extends AbstractConsoleAction {
33
34         private static final long serialVersionUID = 1L;
35
36         /**
37          * Constructor
38          */
39         public InterruptScilabWorkOrCopy() {
40                 super();
41
42         }
43
44         /**
45          * Threats the event
46          * @param e the action event that occured
47          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
48          */
49         public void actionPerformed(ActionEvent e) {
50
51                 if (((JTextPane) configuration.getInputCommandView()).getSelectedText() != null) {
52                         /* Text selected in the input --> Copy */
53                         StringSelection strSelected = new StringSelection(((JTextPane) configuration.getInputCommandView()).getSelectedText());
54                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSelected, null);
55                 } else if (((JTextPane) configuration.getOutputView()).getSelectedText() != null) {
56                         /* Text selected in the output --> Copy */
57                         StringSelection strSelected = new StringSelection(((JTextPane) configuration.getOutputView()).getSelectedText());
58                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSelected, null);
59                 } else {
60                         /* Interrupt Scilab */
61                         InterpreterManagement.interruptScilab();
62
63                         // If Scilab is on prompt, then emulate a user entry
64                         if (((JPanel) configuration.getPromptView()).isVisible()) {
65                                 configuration.getOutputView().append(StringConstants.NEW_LINE);
66                                 configuration.getOutputView().append(configuration.getPromptView().getDefaultPrompt());
67                                 configuration.getOutputView().append(configuration.getInputParsingManager().getCommandLine());
68                                 configuration.getOutputView().append(StringConstants.NEW_LINE);
69                                 ((SciOutputView) configuration.getOutputView()).getConsole().sendCommandsToScilab("", false, false);
70                                 configuration.getInputParsingManager().reset();
71                         }
72                 }
73         }
74
75 }