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