2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - Calixte DENIZET
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
13 package org.scilab.modules.ui_data.variableeditor.actions;
15 import java.awt.Component;
16 import java.util.HashMap;
19 import javax.swing.ImageIcon;
20 import javax.swing.JPopupMenu;
21 import javax.swing.JTable;
23 import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem;
24 import org.scilab.modules.gui.bridge.pushbutton.SwingScilabPushButton;
25 import org.scilab.modules.gui.events.callback.CommonCallBack;
26 import org.scilab.modules.gui.menuitem.MenuItem;
27 import org.scilab.modules.gui.menuitem.ScilabMenuItem;
28 import org.scilab.modules.gui.pushbutton.PushButton;
29 import org.scilab.modules.gui.pushbutton.ScilabPushButton;
30 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
31 import org.scilab.modules.ui_data.EditVar;
32 import org.scilab.modules.ui_data.datatable.SwingEditvarTableModel;
33 import org.scilab.modules.ui_data.variableeditor.SwingScilabVariableEditor;
37 * @author Calixte DENIZET
39 @SuppressWarnings(value = { "serial" })
40 public final class PlotAction extends CommonCallBack {
42 public static final int PLOT2D = 0;
43 public static final int MATPLOT = 1;
44 public static final int GRAYPLOT = 2;
45 public static final int SGRAYPLOT = 3;
46 public static final int CHAMP = 4;
47 public static final int HISTPLOT = 5;
48 public static final int MESH = 6;
49 public static final int SURF = 7;
50 public static final int HIST3D = 8;
51 public static final int CONTOUR2D = 9;
52 public static final int PIE = 10;
54 private static final Map<String, Integer> map = new HashMap<String, Integer>();
57 map.put("Matplot", 1);
58 map.put("grayplot", 2);
59 map.put("Sgrayplot", 3);
61 map.put("histplot", 5);
65 map.put("contour2d", 9);
69 private static final String COM_PLOT2D = "plot2d(%s)";
70 private static final String COM_MATPLOT = "Matplot(%s)";
71 private static final String COM_GRAYPLOT = "grayplot(1:%s,1:%s,%s)";
72 private static final String COM_SGRAYPLOT = "Sgrayplot(1:%s,1:%s,%s)";
73 private static final String COM_CHAMP = "champ(1:%s,1:%s,real(%s),imag(%s))";
74 private static final String COM_HISTPLOT = "histplot(10,%s)";
75 private static final String COM_MESH = "mesh(%s)";
76 private static final String COM_SURF = "surf(%s)";
77 private static final String COM_HIST3D = "hist3d(%s)";
78 private static final String COM_CONTOUR2D = "L?8625083632641564278=xget(\"fpf\");xset(\"fpf\",\" \");contour2d(1:%s,1:%s,%s,10);xset(\"fpf\",L?8625083632641564278);clear(\"L?8625083632641564278\")";
79 private static final String COM_PIE = "L?8625083632641564278=%s;pie(L?8625083632641564278(find(L?8625083632641564278>0&L?8625083632641564278<>%%inf&L?8625083632641564278<>%%nan)))";
81 private static final String[] COMMANDS = new String[] {COM_PLOT2D, COM_MATPLOT, COM_GRAYPLOT, COM_SGRAYPLOT, COM_CHAMP, COM_HISTPLOT, COM_MESH, COM_SURF, COM_HIST3D, COM_CONTOUR2D, COM_PIE};
82 private static final String[] IMG = new String[] {
83 "application-x-scilab-plot2d",
84 "application-x-scilab-Matplot",
85 "application-x-scilab-grayplot",
86 "application-x-scilab-Sgrayplot",
87 "application-x-scilab-champ",
88 "application-x-scilab-histplot",
89 "application-x-scilab-mesh",
90 "application-x-scilab-surf",
91 "application-x-scilab-hist3d",
92 "application-x-scilab-contour2d",
93 "application-x-scilab-pie"
96 private static final String CREATE = "Create";
98 private final SwingScilabVariableEditor editor;
99 private final int type;
100 private final boolean onSelection;
104 * @param editor the editor
105 * @param name the name of the action
107 private PlotAction(SwingScilabVariableEditor editor, String name, boolean onSelection) {
109 this.editor = editor;
110 this.type = map.get(name);
111 this.onSelection = onSelection;
118 public void callBack() {
119 JTable table = editor.getCurrentTable();
120 SwingEditvarTableModel model = (SwingEditvarTableModel) table.getModel();
126 rowC = model.getScilabMatrixRowCount();
127 colC = model.getScilabMatrixColCount();
128 datas = model.getVarName();
130 int[] cols = table.getSelectedColumns();
131 int[] rows = table.getSelectedRows();
132 if (cols == null || cols.length == 0 || rows == null || rows.length == 0) {
133 rowC = model.getScilabMatrixRowCount();
134 colC = model.getScilabMatrixColCount();
135 datas = model.getVarName();
137 if (model.getType().equals(EditVar.STRING)) {
141 int[] rowSize = new int[1];
142 int[] colSize = new int[1];
143 datas = model.getScilabSubMatrix(rows, cols, rowSize, colSize);
152 if (rowC == 0 || colC == 0) {
156 if (!model.getType().equals(EditVar.DOUBLE)) {
157 datas = "double(" + datas + ")";
160 String com = COMMANDS[type];
169 com = String.format(com, datas);
173 com = String.format(com, rowC, colC, datas);
176 com = String.format(com, rowC, colC, datas, datas);
179 if (rowC >= 2 && colC >= 2) {
180 com = String.format(com, rowC, colC, datas);
187 model.execCommand("clf();" + com);
191 * Create a button for a tool bar
192 * @param editor the associated editor
193 * @param title tooltip for the button
196 public static PushButton createButton(SwingScilabVariableEditor editor, String title) {
197 final PushButton button = ScilabPushButton.createPushButton();
198 button.setToolTipText(title);
199 ImageIcon imageIcon = new ImageIcon(ScilabSwingUtilities.findIcon("plot"));
200 ((SwingScilabPushButton) button.getAsSimplePushButton()).setIcon(imageIcon);
202 final JPopupMenu popup = new JPopupMenu() {
204 public void show(Component c, int x, int y) {
205 SwingScilabPushButton but = (SwingScilabPushButton) button.getAsSimplePushButton();
206 super.show(but, 0, but.getBounds(null).height);
209 popup.setBorderPainted(true);
211 popup.add(PlotAction.createJMenuItem(editor, "plot2d", true));
212 popup.add(PlotAction.createJMenuItem(editor, "Matplot", true));
213 popup.add(PlotAction.createJMenuItem(editor, "grayplot", true));
214 popup.add(PlotAction.createJMenuItem(editor, "Sgrayplot", true));
215 popup.add(PlotAction.createJMenuItem(editor, "champ", true));
216 popup.add(PlotAction.createJMenuItem(editor, "histplot", true));
217 popup.add(PlotAction.createJMenuItem(editor, "mesh", true));
218 popup.add(PlotAction.createJMenuItem(editor, "surf", true));
219 popup.add(PlotAction.createJMenuItem(editor, "hist3d", true));
220 popup.add(PlotAction.createJMenuItem(editor, "contour2d", true));
221 popup.add(PlotAction.createJMenuItem(editor, "pie", true));
225 ((SwingScilabPushButton) button.getAsSimplePushButton()).addActionListener(new CommonCallBack(null) {
227 public void callBack() {
228 if (!popup.isVisible()) {
229 popup.show(null, 0, 0);
231 popup.setVisible(false);
241 * @param editor the associated editor
242 * @param title the menu title
243 * @return the menu item
245 public static MenuItem createMenuItem(SwingScilabVariableEditor editor, String title, boolean onSelection) {
246 MenuItem menu = ScilabMenuItem.createMenuItem();
247 menu.setCallback(new PlotAction(editor, title, onSelection));
249 ((SwingScilabMenuItem) menu.getAsSimpleMenuItem()).setIcon(new ImageIcon(ScilabSwingUtilities.findIcon(IMG[map.get(title)])));
255 * Create a menu item as a SwingScilabMenuItem
256 * @param editor the associated editor
257 * @param title the menu title
258 * @return the menu item
260 public static SwingScilabMenuItem createJMenuItem(SwingScilabVariableEditor editor, String title, boolean onSelection) {
261 return (SwingScilabMenuItem) createMenuItem(editor, title, onSelection).getAsSimpleMenuItem();