Xcos: save and load the DEBUG_LEVEL
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / actions / dialog / DebugLevelDialog.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - DIGITEO - Clement DAVID
4  * Copyright (C) 2015 - Scilab Enterprises - Clement DAVID
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11  *
12  */
13
14 package org.scilab.modules.xcos.actions.dialog;
15
16 import java.awt.Component;
17 import java.awt.Dimension;
18 import java.awt.GridBagConstraints;
19 import java.awt.GridBagLayout;
20 import java.awt.Image;
21 import java.awt.Insets;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.beans.PropertyVetoException;
25 import java.util.logging.Logger;
26
27 import javax.swing.BorderFactory;
28 import javax.swing.Box;
29 import javax.swing.BoxLayout;
30 import javax.swing.ImageIcon;
31 import javax.swing.JButton;
32 import javax.swing.JDialog;
33 import javax.swing.JLabel;
34 import javax.swing.JList;
35 import javax.swing.JPanel;
36 import javax.swing.ListSelectionModel;
37
38 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
39 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
40 import org.scilab.modules.commons.gui.FindIconHelper;
41 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
42 import org.scilab.modules.xcos.JavaController;
43 import org.scilab.modules.xcos.actions.DebugLevelAction;
44 import org.scilab.modules.xcos.actions.DebugLevelAction.DebugLevel;
45 import org.scilab.modules.xcos.graph.ScicosParameters;
46 import org.scilab.modules.xcos.utils.XcosMessages;
47
48 /**
49  * Dialog for the {@link DebugLevelAction}.
50  *
51  * Note that this dialog break the Data Abstraction Coupling metric because of
52  * the numbers of graphical components involved in the GUI creation. For the
53  * same reason (GUI class), constants are not used on this code.
54  */
55 // CSOFF: ClassDataAbstractionCoupling
56 // CSOFF: MagicNumber
57 @SuppressWarnings(value = { "serial" })
58 public class DebugLevelDialog extends JDialog {
59     private final ScicosParameters parameters;
60
61     private JList debugList;
62
63     /**
64      * Default contructor
65      *
66      * @param parent
67      *            the parent component
68      * @param parameters
69      *            the associated parameters
70      */
71     public DebugLevelDialog(Component parent, ScicosParameters parameters) {
72         this.parameters = parameters;
73
74         ImageIcon scilabIcon = new ImageIcon(FindIconHelper.findIcon("scilab"));
75         Image imageForIcon = scilabIcon.getImage();
76         setLayout(new GridBagLayout());
77         setIconImage(imageForIcon);
78         setTitle(XcosMessages.SET_DEBUG);
79         setModal(true);
80         setLocationRelativeTo(parent);
81         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
82         ScilabSwingUtilities.closeOnEscape(this);
83
84         initComponents();
85     }
86
87     /**
88      * Initialize the components
89      */
90     private void initComponents() {
91         JLabel textLabel = new JLabel(XcosMessages.DEBUG_LEVEL_LABEL);
92         debugList = new JList(DebugLevel.values());
93         debugList.setSelectedIndex(parameters.getDebugLevel(new JavaController()));
94         debugList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
95
96         JButton cancelButton = new JButton(XcosMessages.CANCEL);
97         JButton okButton = new JButton(XcosMessages.OK);
98         okButton.setPreferredSize(cancelButton.getPreferredSize());
99
100         JPanel buttonPane = new JPanel();
101         buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
102         buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
103         buttonPane.add(okButton);
104         buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
105         buttonPane.add(cancelButton);
106
107         GridBagConstraints gbc = new GridBagConstraints();
108
109         gbc.gridwidth = GridBagConstraints.REMAINDER;
110         gbc.gridx = 0;
111         gbc.gridy = 1;
112         gbc.fill = GridBagConstraints.BOTH;
113         gbc.weightx = 1.0;
114         gbc.insets = new Insets(10, 10, 10, 10);
115         add(textLabel, gbc);
116
117         gbc.gridy = GridBagConstraints.RELATIVE;
118         gbc.fill = GridBagConstraints.BOTH;
119         gbc.weighty = 1.0;
120         add(debugList, gbc);
121
122         gbc.gridwidth = 1;
123         gbc.gridheight = 1;
124         gbc.anchor = GridBagConstraints.LAST_LINE_END;
125         gbc.fill = GridBagConstraints.NONE;
126         gbc.weighty = 0;
127         gbc.insets = new Insets(5, 0, 10, 10);
128         add(buttonPane, gbc);
129
130         cancelButton.addActionListener(new ActionListener() {
131             @Override
132             public void actionPerformed(ActionEvent e) {
133                 dispose();
134             }
135         });
136
137         okButton.addActionListener(new ActionListener() {
138             @Override
139             public void actionPerformed(ActionEvent e) {
140                 int value = ((DebugLevel) debugList.getSelectedValue()).getValue();
141                 try {
142                     parameters.setDebugLevel(new JavaController(), value);
143                     ScilabInterpreterManagement.synchronousScilabExec("scicos_debug", value);
144                     dispose();
145                 } catch (InterpreterException e1) {
146                     Logger.getLogger(DebugLevelAction.class.getName()).severe(e1.toString());
147                 } catch (PropertyVetoException e2) {
148                     Logger.getLogger(DebugLevelAction.class.getName()).severe(e2.toString());
149                 }
150             }
151         });
152     }
153 }
154 // CSON: ClassDataAbstractionCoupling
155 // CSON: MagicNumber