2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - Han DONG
4 * Copyright (C) 2011 - DIGITEO - Vincent COUVERT
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
13 package org.scilab.modules.gui.bridge.uitable;
15 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
18 import java.awt.Color;
21 import javax.swing.JLabel;
22 import javax.swing.JList;
23 import javax.swing.JScrollPane;
24 import javax.swing.JTable;
25 import javax.swing.UIManager;
26 import javax.swing.border.Border;
28 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
29 import org.scilab.modules.gui.SwingViewObject;
30 import org.scilab.modules.gui.SwingViewWidget;
31 import org.scilab.modules.gui.events.callback.CommonCallBack;
32 import org.scilab.modules.gui.menubar.MenuBar;
33 import org.scilab.modules.gui.textbox.TextBox;
34 import org.scilab.modules.gui.toolbar.ToolBar;
35 import org.scilab.modules.gui.uitable.SimpleUiTable;
36 import org.scilab.modules.gui.utils.Position;
37 import org.scilab.modules.gui.utils.PositionConverter;
38 import org.scilab.modules.gui.utils.ScilabAlignment;
39 import org.scilab.modules.gui.utils.ScilabRelief;
40 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
41 import org.scilab.modules.gui.utils.Size;
44 * Swing implementation for Scilab UiTable in GUIs
47 public class SwingScilabUiTable extends JScrollPane implements SwingViewObject, SimpleUiTable {
49 private static final long serialVersionUID = -5497171010652701217L;
53 private Border defaultBorder = null;
55 private JTable uiTable;
56 private JList rowHeader;
58 private Object[] colNames = {};
59 private Object[] rowNames = {};
60 private Object[][] data = {};
70 public SwingScilabUiTable() {
72 getViewport().add(getUiTable());
73 setRowHeaderView(getCustomRowHeader());
77 * Apply a new font for the uiTable.
78 * @param font new font to use.
80 public void setFont(Font font) {
81 getLabel().setFont(font);
85 * To get the Font of the element.
86 * @return font the Font
88 public Font getFont() {
89 return getLabel().getFont();
93 * To get the Foreground color of the element.
94 * @return color the Color
96 public Color getForeground() {
97 return getLabel().getForeground();
101 * To set the Foreground color of the element.
102 * @param color the Color
104 public void setForeground(Color color) {
105 getLabel().setForeground(color);
109 * To set the Background color of the element.
110 * @param color the Color
112 public void setBackground(Color color) {
113 getLabel().setBackground(color);
117 * To get the Background color of the element.
118 * @return color the Color
120 public Color getBackground() {
121 return getLabel().getBackground();
125 * Draws a swing Scilab PushButton
126 * @see org.scilab.modules.gui.uielement.UIElement#draw()
129 this.setVisible(true);
134 * Sets the visibility status of an UIElement
135 * @param newVisibleState the visibility status we want to set for the
136 * UIElement (true if the UIElement is visible, false if not)
138 public void setVisible(boolean newVisibleState) {
139 super.setVisible(newVisibleState);
140 //getLabel().setVisible(newVisibleState);
144 * Gets the dimensions (width and height) of a swing Scilab element
145 * @return the dimensions of the element
146 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
148 public Size getDims() {
149 return new Size(getWidth(), getHeight());
153 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab
155 * @return the position of the element
156 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
158 public Position getPosition() {
159 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
163 * Sets the dimensions (width and height) of a swing Scilab element
164 * @param newSize the dimensions to set to the element
165 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
167 public void setDims(Size newSize) {
168 setSize(newSize.getWidth(), newSize.getHeight());
172 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab
174 * @param newPosition the position to set to the element
175 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
177 public void setPosition(Position newPosition) {
178 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
179 setLocation(javaPosition.getX(), javaPosition.getY());
183 * Add a callback to the UiTable
184 * @param callback the callback to set.
186 public void setCallback(CommonCallBack callback) {
192 * @param menuBarToAdd the MenuBar associated to the UiTable.
194 public void addMenuBar(MenuBar menuBarToAdd) {
195 /* Unimplemented for UiTables */
196 throw new UnsupportedOperationException();
201 * @param toolBarToAdd the ToolBar associated to the UiTable.
203 public void addToolBar(ToolBar toolBarToAdd) {
204 /* Unimplemented for UiTables */
205 throw new UnsupportedOperationException();
210 * @return MenuBar: the MenuBar associated to the UiTable.
212 public MenuBar getMenuBar() {
213 /* Unimplemented for UiTables */
214 throw new UnsupportedOperationException();
219 * @return ToolBar: the ToolBar associated to the UiTable.
221 public ToolBar getToolBar() {
222 /* Unimplemented for UiTables */
223 throw new UnsupportedOperationException();
227 * Set the horizontal alignment for the UiTable text
228 * @param alignment the value for the alignment (See ScilabAlignment.java)
230 public void setHorizontalAlignment(String alignment) {
231 getLabel().setHorizontalAlignment(ScilabAlignment.toSwingAlignment(alignment));
235 * Set the vertical alignment for the UiTable text
236 * @param alignment the value for the alignment (See ScilabAlignment.java)
238 public void setVerticalAlignment(String alignment) {
239 getLabel().setVerticalAlignment(ScilabAlignment.toSwingAlignment(alignment));
243 * Set the Relief of the UiTable
244 * @param reliefType the type of the relief to set (See ScilabRelief.java)
246 public void setRelief(String reliefType) {
247 if (defaultBorder == null) {
248 defaultBorder = getBorder();
250 setBorder(ScilabRelief.getBorderFromRelief(reliefType, defaultBorder));
254 * Destroy the UiTable
256 public void destroy() {
257 ScilabSwingUtilities.removeFromParent(this);
262 * @param infoBarToAdd the InfoBar associated to the UiTable.
264 public void addInfoBar(TextBox infoBarToAdd) {
265 /* Unimplemented for UiTables */
266 throw new UnsupportedOperationException();
271 * @return the InfoBar associated to the UiTable.
273 public TextBox getInfoBar() {
274 /* Unimplemented for UiTables */
275 throw new UnsupportedOperationException();
279 * Create/Return the uiTable Java object
280 * @return the uiTable
282 private JTable getUiTable() {
283 if (uiTable == null) {
284 uiTable = new JTable(data, colNames);
285 uiTable.setFillsViewportHeight(true);
286 if (uiTable.getGridColor().equals(Color.WHITE)) {
287 uiTable.setGridColor(new Color(128, 128, 128));
289 uiTable.setShowHorizontalLines(true);
290 uiTable.setShowVerticalLines(true);
296 * Create/Return the rowHeader Java Object
297 * @return the rowHeader
299 private JList getCustomRowHeader() {
300 if (rowHeader == null) {
301 rowHeader = new JList(rowNames);
302 rowHeader.setFixedCellWidth(50);
303 rowHeader.setFixedCellHeight(uiTable.getRowHeight());
304 rowHeader.setCellRenderer(new RowHeaderRenderer(uiTable));
310 * Create/Return the uiTable Java object
311 * @return the uiTable
313 private JLabel getLabel() {
315 label = new JLabel();
321 * gets directory of image in image render
322 * @return the directory string
323 * @see org.scilab.modules.gui.text.SimpleText#getText()
325 public String getText() {
326 return getLabel().getText();
330 * Sets the directory for image to render
331 * @param newText the new directory to image
333 public void setText(String newText) {
334 getLabel().setText(newText);
338 * Sets the column names for uitable
339 * @param names the String[] that contains column names
341 public void setColumnNames(String[] names) {
342 //updates table with new column names
345 uiTable = new JTable(data, names);
346 getViewport().add(uiTable);
351 * Sets the row names for uitable
352 * @param names the String[] that contains row names
354 public void setRowNames(String[] names) {
355 //updates table with new row names
358 rowHeader = new JList(names);
359 rowHeader.setFixedCellWidth(50);
360 rowHeader.setFixedCellHeight(uiTable.getRowHeight());
361 rowHeader.setCellRenderer(new RowHeaderRenderer(uiTable));
362 setRowHeaderView(rowHeader);
367 * Sets the Data for uitable
368 * @param text the String that contains row data delimited by a '|' and
369 * column data delimited by " ". Example: 1.26 3.47 | a b | d e | a b
371 public void setData(String[] text) {
372 //initializes data structure with number of rows and columns
374 nRow = text.length / nCol;
376 data = new Object[nRow][nCol];
381 while (nbElements < text.length && i < nRow && j < nCol) {
382 data[i][j] = text[nbElements];
391 //if no row names were provided, it will set numeric ones according to number of rows. (1, 2, 3, 4, ...)
394 rowNames = new Object[nRow];
395 for (int k = 0; k < nRow; k++) {
398 rowHeader = new JList(rowNames);
399 rowHeader.setFixedCellWidth(50);
400 rowHeader.setFixedCellHeight(uiTable.getRowHeight());
401 rowHeader.setCellRenderer(new RowHeaderRenderer(uiTable));
402 setRowHeaderView(rowHeader);
405 //adds and updates table with new data
406 uiTable = new JTable(data, colNames);
407 getViewport().add(uiTable);
415 public void setId(Integer id) {
423 public Integer getId() {
428 * Generic update method
429 * @param property property name
430 * @param value property value
432 public void update(int property, Object value) {
433 GraphicController controller = GraphicController.getController();
436 case __GO_UI_STRING__: {
437 // Update column names
438 String[] stringValue = (String[]) value;
439 int colNb = ((Integer) controller.getProperty(uid, __GO_UI_STRING_COLNB__));
440 String[] colNames = new String[colNb - 1];
441 for (int k = 1; k < colNb; k++) {
442 colNames[k - 1] = stringValue[k * (stringValue.length / colNb)];
445 setColumnNames(colNames);
448 String[] rowNames = new String[stringValue.length / colNb - 1];
449 for (int k = 1; k < stringValue.length / colNb; k++) {
450 rowNames[k - 1] = stringValue[k];
453 setRowNames(rowNames);
456 String[] tableData = new String[rowNames.length * colNames.length];
458 for (int kCol = 1; kCol <= colNames.length; kCol++) {
459 for (int kRow = 1; kRow <= rowNames.length; kRow++) {
460 tableData[kData++] = stringValue[kCol * (stringValue.length / colNb) + kRow];
464 if (tableData.length != 0) {
470 SwingViewWidget.update(this, property, value);
475 public void resetBackground() {
476 Color color = (Color) UIManager.getLookAndFeelDefaults().get("ScrollPane.background");
478 setBackground(color);
482 public void resetForeground() {
483 Color color = (Color)UIManager.getLookAndFeelDefaults().get("ScrollPane.foreground");
485 setForeground(color);