1 /* 2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 3 * Copyright (C) 2010 - Calixte DENIZET 4 * 5 * Copyright (C) 2012 - 2016 - Scilab Enterprises 6 * 7 * This file is hereby licensed under the terms of the GNU GPL v2.0, 8 * pursuant to article 5.3.4 of the CeCILL v.2.1. 9 * This file was originally licensed under the terms of the CeCILL v2.1, 10 * and continues to be available under such terms. 11 * For more information, see the COPYING file which you should have received 12 * along with this program. 13 * 14 */ 15 16 package org.scilab.modules.ui_data.variableeditor.actions; 17 18 import java.awt.Dialog.ModalityType; 19 import java.awt.GridBagConstraints; 20 import java.awt.GridBagLayout; 21 import java.awt.GridLayout; 22 import java.awt.Insets; 23 import java.awt.event.ActionEvent; 24 import java.awt.event.ActionListener; 25 26 import javax.swing.BorderFactory; 27 import javax.swing.ImageIcon; 28 import javax.swing.JButton; 29 import javax.swing.JDialog; 30 import javax.swing.JLabel; 31 import javax.swing.JPanel; 32 import javax.swing.JSpinner; 33 import javax.swing.JTable; 34 import javax.swing.JTextField; 35 import javax.swing.SpinnerNumberModel; 36 37 import org.scilab.modules.commons.gui.FindIconHelper; 38 import org.scilab.modules.commons.gui.ScilabLAF; 39 import org.scilab.modules.commons.gui.ScilabKeyStroke; 40 import org.scilab.modules.gui.bridge.menuitem.SwingScilabMenuItem; 41 import org.scilab.modules.gui.events.callback.CommonCallBack; 42 import org.scilab.modules.gui.menuitem.MenuItem; 43 import org.scilab.modules.gui.menuitem.ScilabMenuItem; 44 import org.scilab.modules.ui_data.UI_data; 45 import org.scilab.modules.ui_data.datatable.SwingEditvarTableModel; 46 import org.scilab.modules.ui_data.utils.UiDataMessages; 47 import org.scilab.modules.ui_data.variableeditor.SwingScilabVariableEditor; 48 49 /** 50 * RefreshAction class 51 * @author Calixte DENIZET 52 */ 53 @SuppressWarnings(value = { "serial" }) 54 public final class CreateNewVariableAction extends CommonCallBack { 55 56 private static final String KEY = "OSSCKEY N"; 57 private static final String CREATENEW = "Create new"; 58 private static final int GAP = 5; 59 60 private final SwingScilabVariableEditor editor; 61 62 /** 63 * Constructor 64 * @param editor the editor 65 * @param name the name of the action 66 */ CreateNewVariableAction(SwingScilabVariableEditor editor, String name)67 private CreateNewVariableAction(SwingScilabVariableEditor editor, String name) { 68 super(name); 69 this.editor = editor; 70 } 71 72 /** 73 * @param editor the editor 74 * @param table where to put the action 75 */ registerAction(SwingScilabVariableEditor editor, JTable table)76 public static void registerAction(SwingScilabVariableEditor editor, JTable table) { 77 table.getActionMap().put(CREATENEW, new CreateNewVariableAction(editor, CREATENEW)); 78 table.getInputMap().put(ScilabKeyStroke.getKeyStroke(KEY), CREATENEW); 79 } 80 81 /** 82 * {@inheritDoc} 83 */ 84 @Override callBack()85 public void callBack() { 86 JTable table = editor.getCurrentTable(); 87 Object[] values = askForNewMatrix(); 88 if (!((String) values[0]).isEmpty()) { 89 SwingEditvarTableModel model = (SwingEditvarTableModel) table.getModel(); 90 String defaultValue = SwingEditvarTableModel.getDataAsScilabString(((String) values[3])); 91 if (defaultValue.isEmpty()) { 92 defaultValue = "0"; 93 } 94 95 StringBuilder command = new StringBuilder(); 96 command.append("L$8625083632641564277=warning(\"query\");warning(\"off\");"); 97 command.append("if execstr(\""); 98 command.append((String) values[0]); 99 command.append("="); 100 command.append("repmat("); 101 command.append(defaultValue); 102 command.append(","); 103 command.append(((Integer) values[1]).toString()); 104 command.append(","); 105 command.append(((Integer) values[2]).toString()); 106 command.append(");editvar(\"\""); 107 command.append((String) values[0]); 108 command.append("\"\")\",\"errcatch\") <> 0 then messagebox(\"Could not create variable: \" + lasterror() + \"\""); 109 command.append(",\"Variable editor\",\"error\",\"modal\");"); 110 command.append("end;"); 111 command.append("warning(L$8625083632641564277);clear(\"L$8625083632641564277\");"); 112 command.append("updatebrowsevar()"); 113 114 model.execCommand(command.toString()); 115 } 116 } 117 askForNewMatrix()118 public Object[] askForNewMatrix() { 119 final JDialog dialog = new JDialog(); 120 final Object[] ret = new Object[] { "", new Integer(0), new Integer(0), "" }; 121 dialog.setModalityType(ModalityType.APPLICATION_MODAL); 122 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 123 dialog.setIconImage(new ImageIcon(FindIconHelper.findIcon("rrze_table", "256x256")).getImage()); 124 125 JPanel panel = new JPanel(new GridBagLayout()); 126 GridBagConstraints gbc = new GridBagConstraints(); 127 128 JLabel label = new JLabel(UiDataMessages.VARNAME); 129 gbc.gridx = 0; 130 gbc.gridy = 0; 131 gbc.gridwidth = 1; 132 gbc.gridheight = 1; 133 gbc.insets = new Insets(GAP, GAP, 0, GAP); 134 gbc.anchor = GridBagConstraints.WEST; 135 panel.add(label, gbc); 136 137 final JTextField textField = new JTextField(24 + 1); 138 textField.setText(UI_data.getUnnamedVariable()); 139 textField.selectAll(); 140 gbc.gridx = 1; 141 gbc.gridwidth = GridBagConstraints.REMAINDER; 142 gbc.fill = GridBagConstraints.HORIZONTAL; 143 gbc.insets = new Insets(GAP, 0, 0, GAP); 144 panel.add(textField, gbc); 145 146 JLabel labelRow = new JLabel(UiDataMessages.NUMROWS); 147 gbc.gridx = 0; 148 gbc.gridy = 1; 149 gbc.gridwidth = 1; 150 gbc.gridheight = 1; 151 gbc.fill = GridBagConstraints.NONE; 152 gbc.insets = new Insets(GAP, GAP, 0, GAP); 153 panel.add(labelRow, gbc); 154 155 final JSpinner spinRow = new JSpinner(); 156 ((SpinnerNumberModel) spinRow.getModel()).setMinimum(new Comparable<Integer>() { 157 @Override 158 public int compareTo(Integer o) { 159 return -o.intValue(); 160 } 161 }); 162 spinRow.setValue(0); 163 ((JSpinner.DefaultEditor) spinRow.getEditor()).getTextField().setColumns(6); 164 gbc.gridx = 1; 165 gbc.insets = new Insets(GAP, 0, 0, GAP); 166 panel.add(spinRow, gbc); 167 168 JLabel labelCol = new JLabel(UiDataMessages.NUMCOLS); 169 gbc.gridx = 0; 170 gbc.gridy = 2; 171 gbc.insets = new Insets(GAP, GAP, 0, GAP); 172 panel.add(labelCol, gbc); 173 174 final JSpinner spinCol = new JSpinner(); 175 ((SpinnerNumberModel) spinCol.getModel()).setMinimum(new Comparable<Integer>() { 176 @Override 177 public int compareTo(Integer o) { 178 return -o.intValue(); 179 } 180 }); 181 spinCol.setValue(0); 182 ((JSpinner.DefaultEditor) spinCol.getEditor()).getTextField().setColumns(6); 183 gbc.gridx = 1; 184 gbc.insets = new Insets(GAP, 0, 0, GAP); 185 panel.add(spinCol, gbc); 186 187 JLabel labelFill = new JLabel(UiDataMessages.FILLWITH); 188 gbc.gridx = 0; 189 gbc.gridy = 3; 190 gbc.insets = new Insets(GAP, GAP, 0, GAP); 191 panel.add(labelFill, gbc); 192 193 final JTextField fillField = new JTextField(15); 194 gbc.gridx = 1; 195 gbc.gridwidth = GridBagConstraints.REMAINDER; 196 gbc.fill = GridBagConstraints.HORIZONTAL; 197 gbc.insets = new Insets(GAP, 0, 0, GAP); 198 panel.add(fillField, gbc); 199 200 JButton cancelButton = new JButton(UiDataMessages.CANCEL); 201 JButton okButton = new JButton(UiDataMessages.OK); 202 JPanel panelButton = new JPanel(); 203 okButton.setPreferredSize(cancelButton.getPreferredSize()); 204 panelButton.setLayout(new GridLayout(1, 2, GAP, GAP)); 205 panelButton.setBorder(BorderFactory.createEmptyBorder(GAP, 0, 0, 0)); 206 panelButton.add(cancelButton); 207 panelButton.add(okButton); 208 209 gbc.gridx = 1; 210 gbc.gridy = 4; 211 gbc.gridwidth = 1; 212 gbc.weightx = 0; 213 gbc.fill = GridBagConstraints.NONE; 214 gbc.anchor = GridBagConstraints.EAST; 215 panel.add(panelButton, gbc); 216 217 dialog.setContentPane(panel); 218 219 cancelButton.addActionListener(new ActionListener() { 220 @Override 221 public void actionPerformed(ActionEvent e) { 222 dialog.dispose(); 223 } 224 }); 225 226 okButton.addActionListener(new ActionListener() { 227 @Override 228 public void actionPerformed(ActionEvent e) { 229 dialog.dispose(); 230 ret[0] = textField.getText(); 231 ret[1] = spinRow.getValue(); 232 ret[2] = spinCol.getValue(); 233 ret[3] = fillField.getText(); 234 } 235 }); 236 237 dialog.setTitle(UiDataMessages.CREATENEWVAR); 238 dialog.pack(); 239 dialog.setResizable(false); 240 dialog.setLocationRelativeTo(editor); 241 dialog.setVisible(true); 242 243 return ret; 244 } 245 246 /** 247 * Create a button for a tool bar 248 * @param editor the associated editor 249 * @param title tooltip for the button 250 * @return the button 251 */ createButton(SwingScilabVariableEditor editor, String title)252 public static JButton createButton(SwingScilabVariableEditor editor, String title) { 253 JButton button = new JButton(); 254 ScilabLAF.setDefaultProperties(button); 255 button.addActionListener(new CreateNewVariableAction(editor, title)); 256 button.setToolTipText(title); 257 ImageIcon imageIcon = new ImageIcon(FindIconHelper.findIcon("variable-new")); 258 button.setIcon(imageIcon); 259 260 return button; 261 } 262 263 /** 264 * Create a menu item 265 * @param editor the associated editor 266 * @param title the menu title 267 * @return the menu item 268 */ createMenuItem(SwingScilabVariableEditor editor, String title)269 public static MenuItem createMenuItem(SwingScilabVariableEditor editor, String title) { 270 MenuItem menu = ScilabMenuItem.createMenuItem(); 271 menu.setCallback(new CreateNewVariableAction(editor, title)); 272 menu.setText(title); 273 ((SwingScilabMenuItem) menu.getAsSimpleMenuItem()).setAccelerator(ScilabKeyStroke.getKeyStroke(KEY)); 274 275 return menu; 276 } 277 278 /** 279 * Create a menu item as a SwingScilabMenuItem 280 * @param editor the associated editor 281 * @param title the menu title 282 * @return the menu item 283 */ createJMenuItem(SwingScilabVariableEditor editor, String title)284 public static SwingScilabMenuItem createJMenuItem(SwingScilabVariableEditor editor, String title) { 285 return (SwingScilabMenuItem) createMenuItem(editor, title).getAsSimpleMenuItem(); 286 } 287 } 288