2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - 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.gui.uiwidget;
15 import java.awt.BorderLayout;
16 import java.awt.Color;
17 import java.awt.Component;
18 import java.awt.Container;
19 import java.awt.Cursor;
20 import java.awt.Dimension;
22 import java.awt.GridBagLayout;
23 import java.awt.KeyEventDispatcher;
24 import java.awt.KeyboardFocusManager;
25 import java.awt.LayoutManager;
26 import java.awt.Point;
27 import java.awt.event.KeyEvent;
28 import java.awt.event.MouseAdapter;
29 import java.awt.font.TextAttribute;
30 import java.awt.geom.Rectangle2D;
31 import java.awt.image.BufferedImage;
33 import java.lang.reflect.Constructor;
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
41 import java.util.TreeMap;
43 import javax.imageio.ImageIO;
44 import javax.swing.JComponent;
45 import javax.swing.JFrame;
46 import javax.swing.JPopupMenu;
47 import javax.swing.JScrollBar;
48 import javax.swing.JScrollPane;
49 import javax.swing.JTabbedPane;
50 import javax.swing.RootPaneContainer;
51 import javax.swing.ScrollPaneConstants;
52 import javax.swing.SwingUtilities;
54 import org.flexdock.view.View;
55 import org.scilab.modules.commons.ScilabCommonsUtils;
56 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
57 import org.scilab.modules.gui.utils.ScilabRelief;
58 import org.scilab.modules.types.ScilabType;
59 import org.scilab.modules.gui.uiwidget.callback.UICallback;
60 import org.scilab.modules.gui.uiwidget.components.NoLayout;
61 import org.scilab.modules.gui.uiwidget.components.UIFocusListener;
62 import org.scilab.modules.gui.uiwidget.components.UIMouseListener;
63 import org.scilab.modules.gui.uiwidget.components.UITab;
64 import org.scilab.modules.gui.uiwidget.components.UITools;
65 import org.xml.sax.Attributes;
68 * Main class to handle Java components.
70 public abstract class UIComponent {
73 ScilabCommonsUtils.loadOnUse("graphics");
76 private static final MouseAdapter NOMOUSE = new MouseAdapter() { };
77 private static final KeyEventDispatcher NOKEY = new KeyEventDispatcher() {
78 public boolean dispatchKeyEvent(KeyEvent e) {
83 private static int UID = 0;
85 protected Object component;
86 protected Object modifiableComponent;
87 private boolean thisOrComponent = true;
90 protected UIComponent root;
91 protected UIComponent parent;
92 protected Map<String, UIButtonGroup> buttonGroups;
93 protected Map<String, UIComponent> children;
94 protected List<UIComponent> childrenList;
95 protected Map<String, Map<String, String>> style;
96 protected UIMouseListener mouseListener;
97 protected UIFocusListener focusListener;
98 protected String path;
99 protected Map<String, String> constraint;
100 protected Map<String, String> uistyle;
101 protected String tabTitle;
102 protected boolean enableEvents = true;
103 protected String relief;
104 protected UITools.FontUnit fontUnit = UITools.FontUnit.POINTS;
106 protected NoLayout.NoLayoutConstraint nolayoutconstraint;
109 * Default empty constructor
111 protected UIComponent() { }
115 * @param parent the parent UIComponent
117 public UIComponent(UIComponent parent) throws UIWidgetException {
118 this.parent = parent;
121 if (this.parent != null) {
122 this.root = this.parent.root;
131 * Register this component to its parent
133 private final void registerToParent() {
135 if (parent.childrenList == null) {
136 parent.childrenList = new ArrayList<UIComponent>();
138 parent.childrenList.add(this);
143 * Register id to parent
145 private final void registerIdToParent() {
146 if (parent != null && parent.path != null && id != null) {
147 if (parent.children == null) {
148 parent.children = new HashMap<String, UIComponent>();
151 parent.children.put(id, this);
156 * Get a Map from Attributes object
157 * @param attributes the attributes
158 * @return a Map containing the pairs attribute-name -- attribute-value
160 public static final StringMap getMapFromAttributes(Attributes attributes) {
161 final int len = attributes != null ? attributes.getLength() : 0;
162 StringMap map = new StringMap(len);
163 for (int i = 0; i < len; i++) {
164 map.put(attributes.getLocalName(i), attributes.getValue(i));
171 * Construct an UIComponent
172 * @param pack the Java package name containing the component to instantiate
173 * @param name the UIComponent name
174 * @param attributes an array of length 2xN containing attributes name and value
175 * @param parent the parent UIComponent
176 * @return the newly constructed UIComponent
178 public static final UIComponent getUIComponent(String pack, String name, String[] attributes, UIComponent parent) throws UIWidgetException {
179 final int len = attributes != null ? (attributes.length % 2 == 0 ? attributes.length : attributes.length - 1) : 0;
180 StringMap map = new StringMap(len);
181 for (int i = 0; i < len; i += 2) {
182 map.put(attributes[i], attributes[i + 1]);
185 return getUIComponent(pack, name, map, parent, parent == null ? null : parent.style);
189 * Construct an UIComponent
190 * @param pack the Java package name containing the component to instantiate
191 * @param name the UIComponent name
192 * @param attributes the attributes
193 * @param parent the parent UIComponent
194 * @return the newly constructed UIComponent
196 public static final UIComponent getUIComponent(String pack, String name, Attributes attributes, UIComponent parent, Map<String, Map<String, String>> style) throws UIWidgetException {
197 return getUIComponent(pack, name, getMapFromAttributes(attributes), parent, style);
201 * Construct an UIComponent
202 * @param pack the Java package name containing the component to instantiate
203 * @param name the UIComponent name
204 * @param attributes the attributes
205 * @param parent the parent UIComponent
206 * @return the newly constructed UIComponent
208 public static final UIComponent getUIComponent(String pack, String name, ConvertableMap attributes, final UIComponent parent, Map<String, Map<String, String>> style) throws UIWidgetException {
210 Class clazz = UIClassFinder.findClass(pack, name);
211 final Constructor constructor = clazz.getConstructor(UIComponent.class);
212 final UIComponent[] arr = new UIComponent[1];
214 if (SwingUtilities.isEventDispatchThread()) {
215 arr[0] = (UIComponent) constructor.newInstance(parent);
218 SwingUtilities.invokeAndWait(new Runnable() {
221 arr[0] = (UIComponent) constructor.newInstance(parent);
222 } catch (InvocationTargetException e) {
223 System.err.println(e.getCause());
224 e.getCause().printStackTrace();
225 } catch (Exception e) {
226 System.err.println(e);
231 } catch (InvocationTargetException e) {
232 System.err.println(e.getCause());
233 e.getCause().printStackTrace();
235 } catch (Exception e) {
236 System.err.println(e);
241 UIComponent ui = arr[0];
243 if (attributes.containsKey("id")) {
244 id = (String) attributes.get(String.class, "id", null);
245 attributes.remove("id");
246 } else if (attributes.containsKey("tag")) {
247 id = (String) attributes.get(String.class, "tag", null);
248 attributes.remove("tag");
251 ui.setMapStyle(style);
252 String tabTitle = (String) attributes.get(String.class, "tab-title", null);
253 if (tabTitle != null) {
254 ui.setTabTitle(tabTitle);
255 attributes.remove("tab-title");
257 ui.createNewInstance(attributes);
259 //ui.getPropertiesPairs();
260 } catch (Exception e) {
265 } catch (ClassNotFoundException e) {
266 throw new UIWidgetException("Cannot find the class " + pack + "." + name);
267 } catch (NoSuchMethodException e) {
268 throw new UIWidgetException("Cannot find a valid constructor in class " + pack + "." + name + ":\n" + e.getMessage());
269 } catch (SecurityException e) {
270 throw new UIWidgetException("Cannot find a valid constructor in class " + pack + "." + name + ":\n" + e.getMessage());
271 } catch (InstantiationException e) {
272 throw new UIWidgetException("Cannot instantiate the class " + pack + "." + name + ":\n" + e.getMessage());
273 } catch (IllegalAccessException e) {
274 throw new UIWidgetException("Cannot instantiate the class " + pack + "." + name + ":\n" + e.getMessage());
275 } catch (IllegalArgumentException e) {
276 throw new UIWidgetException("Cannot instantiate the class " + pack + "." + name + ":\n" + e.getMessage());
277 } catch (InvocationTargetException e) {
278 System.err.println(e);
279 e.getTargetException().printStackTrace();
280 throw new UIWidgetException("Cannot instantiate the class " + pack + "." + name + ":\n" + e.getCause());
284 public void setNoLayoutConstraint(double x, double y, double width, double height) {
285 if (nolayoutconstraint == null) {
286 nolayoutconstraint = new NoLayout.NoLayoutConstraint();
288 nolayoutconstraint.setPoint(x, y);
289 nolayoutconstraint.setDims(width, height);
292 public void setNoLayoutConstraint(double width, double height) {
293 if (nolayoutconstraint == null) {
294 nolayoutconstraint = new NoLayout.NoLayoutConstraint();
295 nolayoutconstraint.setPoint(0, 0);
297 nolayoutconstraint.setDims(width, height);
301 * Create a new instance of this UIComponent
302 * @return the created instance
304 public abstract Object newInstance();
307 * Get the backed component
308 * @return the component
310 public Object getComponent() {
316 * @param o the component
318 public void setComponent(Object o) {
319 if (this.component != o) {
320 if (this.component != null) {
321 this.modifiableComponent = null;
330 * Initialize the component if mandatory
332 protected void initialize() {
337 * Get the Scilab representation as used in %h_p.sci
338 * @return an array of string to be evstr
340 public String[] getScilabRepresentation() {
346 * @param b if true events are enabled
348 public void setEnableEvents(boolean b) {
349 if (b != this.enableEvents) {
350 RootPaneContainer root = null;
351 if (component instanceof JComponent) {
352 root = (RootPaneContainer) ((JComponent) component).getTopLevelAncestor();
353 } else if (component instanceof RootPaneContainer) {
354 root = (RootPaneContainer) component;
357 root.getGlassPane().setVisible(!b);
359 KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(NOKEY);
360 root.getGlassPane().removeMouseListener(NOMOUSE);
362 KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(NOKEY);
363 root.getGlassPane().addMouseListener(NOMOUSE);
366 this.enableEvents = b;
371 * Check if events are enabled
372 * @return true if the events are enabled
374 public boolean getEnableEvents() {
375 return this.enableEvents;
378 public void setScreenShot(String file) {
380 if (component instanceof JFrame) {
381 c = ((JFrame) component).getRootPane();
382 } else if (component instanceof Component) {
383 c = (Component) component;
387 BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_RGB);
388 c.paint(image.getGraphics());
391 ImageIO.write(image, "png", new File(file));
392 } catch (Exception e) {
400 * @param cursor the cursor
402 public void setCursor(Cursor cursor) {
403 if (component instanceof Component) {
404 ((Component) component).setCursor(cursor);
412 public Cursor getCursor() {
413 if (component instanceof Component) {
414 return ((Component) component).getCursor();
421 * Get the modifiable component (can be different of component, e.g. with a JScrollPane containing the component)xs
422 * @return the modfifiable component
424 public Object getModifiableComponent() {
425 return modifiableComponent == null ? component : modifiableComponent;
429 * Get the modifiable component as a JComponent
430 * @return modifiable JComponent
432 public JComponent getModifiableJComponent() throws UIWidgetException {
433 Object c = getModifiableComponent();
434 if (c instanceof JComponent) {
435 return (JComponent) c;
438 throw new UIWidgetException("Not a JComponent");
442 * Get the component as a JComponent
443 * @return the JComponent
445 public JComponent getJComponent() throws UIWidgetException {
446 Object c = getComponent();
447 if (c instanceof JComponent) {
448 return (JComponent) c;
451 throw new UIWidgetException("Not a JComponent");
455 * Get the modifiable component as a Container
456 * @return the Container
458 public Container getModifiableContainer() throws UIWidgetException {
459 Object c = getModifiableComponent();
460 if (c instanceof Container) {
461 return (Container) c;
464 throw new UIWidgetException("Not a Container");
468 * Get the component as a Container
469 * @return the Container
471 public Container getContainer() throws UIWidgetException {
472 Object c = getComponent();
473 if (c instanceof Container) {
474 return (Container) c;
477 throw new UIWidgetException("Not a Container");
481 * Get the layout constraint
482 * @return the constraint
484 public Map<String, String> getLayoutConstraint() {
485 return this.constraint;
489 * @return the UIComponent name
491 public String getType() {
496 * @return the UIComponent name
498 public String getStyle() {
499 return this.getClass().getSimpleName();
503 * @return the component visibility
505 public boolean getHandleVisible() {
506 if (component instanceof Component) {
507 return ((Component) component).isVisible();
514 * Set component visibility
515 * @param b true to make it visible
517 public void setHandleVisible(boolean b) {
518 if (component instanceof Component) {
519 ((Component) component).setVisible(b);
526 public int getUid() {
531 * Check the component validity
532 * @return true if the component is valid
534 public boolean isValid() {
535 return component != null;
539 * Set the preferred size
540 * @param dim the preferred dimension
542 public void setPreferredSize(Dimension dim) throws UIWidgetException {
543 getContainer().setPreferredSize(dim);
547 * Get the preferred size
548 * @return the preferred dimension
550 public Dimension getPreferredSize() throws UIWidgetException {
551 return getContainer().getPreferredSize();
558 public void setSize(Dimension d) throws UIWidgetException {
559 if (getComponent() instanceof Component) {
560 Component c = (Component) getComponent();
561 Container p = c.getParent();
562 if (p != null && p.getLayout() instanceof NoLayout) {
563 setNoLayoutConstraint((double) d.width, (double) d.height);
564 if (p != null && p.isVisible()) {
570 if (!c.getSize().equals(d)) {
571 JFrame win = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, c);
573 win.setPreferredSize(null);
574 if (c instanceof SwingScilabTab) {
575 c.setPreferredSize(d);
579 if (win.isVisible()) {
592 * @return the dimension
594 public Dimension getSize() throws UIWidgetException {
595 return getContainer().getSize();
599 * Set the minimum size
600 * @param dim the minimum dimension
602 public void setMinimumSize(Dimension dim) throws UIWidgetException {
603 getContainer().setMinimumSize(dim);
607 * Get the minimum size
608 * @return the minimum dimension
610 public Dimension getMinimumSize() throws UIWidgetException {
611 return getContainer().getMinimumSize();
615 * Set the maximum size
616 * @param dim the maximum dimension
618 public void setMaximumSize(Dimension dim) throws UIWidgetException {
619 getContainer().setMaximumSize(dim);
623 * Get the maximum size
624 * @return the maximum dimension
626 public Dimension getMaximumSize() throws UIWidgetException {
627 return getContainer().getMaximumSize();
632 * @param parent the parent
634 public void setParent(UIComponent parent) throws UIWidgetException {
635 if (parent != this.parent) {
636 if (this.parent != null && this.parent.children != null && id != null) {
637 this.parent.children.remove(id);
639 if (this.parent != null && this.parent.childrenList != null) {
640 this.parent.childrenList.remove(this);
642 if (isRoot() && buttonGroups != null) {
643 if (parent.root.buttonGroups == null) {
644 parent.root.buttonGroups = buttonGroups;
646 parent.root.buttonGroups.putAll(buttonGroups);
651 UILocator.removeFromCachedPaths(this);
652 this.parent = parent;
653 setRoot(parent == null ? this : parent.root);
656 registerIdToParent();
658 UILocator.addRoot(this);
666 * Set the root element
667 * @param root the root element
669 private void setRoot(final UIComponent root) {
671 if (childrenList != null) {
672 for (UIComponent c : childrenList) {
683 * Set the parent with the given id
684 * @param parentId the id of the parent
686 public void setParent(String parentId) throws UIWidgetException {
687 UIComponent parent = UILocator.get(parentId);
688 if (parent != null) {
695 * @param id the id to set
697 public void setId(String id) {
698 if ((id != null && !id.equals(this.id)) || (id == null && this.id != null)) {
699 if (!isRoot() && this.id != null && parent.children != null) {
700 parent.children.remove(this.id);
702 UILocator.removeFromCachedPaths(this);
706 UILocator.addRoot(this);
708 registerIdToParent();
714 * Get the id of this UIComponent
717 public String getId() {
722 * Set the tag (Scilab's UIControl compatibility)
725 public void setTag(String tag) {
730 * Get the tag (id) of this UIComponent
733 public String getTag() {
738 * Invalidate the path to this UIComponent
740 private void invalidateUIPath() {
741 UILocator.removeFromCachedPaths(this);
745 } else if (parent.path != null) {
746 path = parent.path + "/" + id;
750 if (children != null) {
751 for (UIComponent ui : children.values()) {
752 ui.invalidateUIPath();
755 if (childrenList != null) {
756 for (UIComponent child : childrenList) {
757 child.registerIdToParent();
759 if (children != null) {
760 for (UIComponent ui : children.values()) {
761 ui.invalidateUIPath();
771 * Get the path to this UIComponent
774 public String getUIPath() {
779 * Get the path to this UIComponent
782 public String getPath() {
787 * Get the root id of this UIComponent
788 * @return the root id
790 public String getRootId() {
795 * Get the root element
798 public UIComponent getRoot() {
803 * Add a button to a button-group
804 * @param name the name of the button-group
805 * @param button the button to add
807 public void addToButtonGroup(String name, UIComponent button) {
809 if (buttonGroups == null) {
810 buttonGroups = new HashMap<String, UIButtonGroup>();
812 UIButtonGroup bg = buttonGroups.get(name);
814 bg = new UIButtonGroup();
815 buttonGroups.put(name, bg);
822 * Get the uicomponent which is selected in the group
823 * @param name the group name
824 * @return the selected component
826 protected UIComponent getSelectedInGroup(String name) {
827 if (getRoot().buttonGroups != null) {
828 UIButtonGroup group = getRoot().buttonGroups.get(name);
830 return group.getSelected();
838 * Remove a button from a button-group
839 * @param name the name of the button-group
840 * @param button the button to remove
842 public void removeFromButtonGroup(String name, UIComponent button) {
843 if (isRoot() && buttonGroups != null) {
844 UIButtonGroup bg = buttonGroups.get(name);
852 * Check if this UIComponent is a root element (no parent)
853 * @return true if it is a root element
855 public boolean isRoot() {
856 return parent == null;
860 * Get the child with the given id
861 * @param if the child id
862 * @return the corresponding UIComponent
864 public UIComponent getUIComponent(final String id) {
865 if (children != null) {
866 return children.get(id);
873 * Close this UIComponent
875 public void closeUIComponent() { }
878 * Remove the UIComponent from differents cache and delete its children
880 public void remove() {
881 UIComponent oldParent = parent;
884 if (oldParent != null) {
885 if (oldParent.component instanceof JComponent) {
886 JComponent jc = (JComponent) oldParent.component;
889 } else if (oldParent.component instanceof Container) {
890 Container container = (Container) oldParent.component;
891 container.invalidate();
892 container.validate();
899 * Remove the UIComponent from differents cache and delete its children
900 * @param removeFromParent if true remove this from its parent
902 private void remove(boolean removeFromParent) {
903 UserData.removeUIWidgetUserData(uid);
904 UILocator.remove(this);
905 UIComponent oldParent = parent;
906 if (childrenList != null) {
907 for (UIComponent ui : childrenList) {
914 if (removeFromParent && parent != null) {
915 if (parent.childrenList != null) {
916 parent.childrenList.remove(this);
918 if (parent.children != null && id != null) {
919 parent.children.remove(id);
923 if (mouseListener != null) {
924 mouseListener.addListenerToComponent(null);
925 mouseListener = null;
927 if (focusListener != null) {
928 focusListener.addListenerToComponent(null);
929 focusListener = null;
931 if (component instanceof JComponent) {
932 JComponent jc = (JComponent) component;
933 if (jc.getParent() != null) {
934 jc.getParent().remove(jc);
938 if (getModifiableComponent() != component && getModifiableComponent() instanceof JComponent) {
939 JComponent jc = (JComponent) getModifiableComponent();
940 if (jc.getParent() != null) {
941 jc.getParent().remove(jc);
947 modifiableComponent = null;
952 * Set the map containing style element
954 private void setMapStyle(Map<String, Map<String, String>> style) {
959 * Finish the UIComponent creation
961 public void finish() {
965 * Replace the component by another one
966 * @param c the new component
968 public void replaceBy(Component c) {
969 if (component instanceof Component) {
970 Component comp = (Component) component;
971 Container parent = comp.getParent();
972 if (parent != null) {
973 boolean hasChanged = false;
974 if ((parent instanceof View) && (c instanceof Container)) {
975 // Flexdock needs a special treatment...
976 View v = (View) parent;
977 if (v.getContentPane() == comp) {
978 v.setContentPane((Container) c);
984 LayoutManager layout = parent.getLayout();
985 Object constraint = null;
986 if (layout instanceof GridBagLayout) {
987 constraint = ((GridBagLayout) layout).getConstraints(comp);
988 } else if (layout instanceof BorderLayout) {
989 constraint = ((BorderLayout) layout).getConstraints(comp);
990 } else if (layout instanceof NoLayout) {
991 constraint = ((NoLayout) layout).getConstraints(comp);
994 if (constraint != null) {
996 parent.add(c, constraint);
998 int pos = parent.getComponentZOrder(comp);
1000 if (pos > parent.getComponentCount()) {
1016 * Get the parent UIComponent
1017 * @return the parent
1019 public UIComponent getParent() {
1024 * Get the id of this UIComponent
1027 public String getID() {
1032 * Check if the root element is visible
1033 * @return true if the root element is visible
1035 public boolean isRootVisible() {
1037 if (component instanceof Component) {
1038 return ((Component) component).isVisible();
1042 return root.isRootVisible();
1048 * @return the children as an array
1050 public UIComponent[] getChildren() {
1051 if (childrenList != null && !childrenList.isEmpty()) {
1052 return childrenList.toArray(new UIComponent[childrenList.size()]);
1060 * @return uistyle as String
1062 public String getUIStyle() {
1063 if (uistyle != null) {
1065 for (Map.Entry<String, String> entry : uistyle.entrySet()) {
1066 str += entry.getKey() + ":" + entry.getValue();
1077 * @param style a String containing style definition as a CSS string
1079 public void setUiStyle(String style) throws UIWidgetException {
1080 setUiStyle(StyleParser.parseLine(style));
1085 * @param style a map
1087 public void setUiStyle(Map<String, String> style) throws UIWidgetException {
1088 this.uistyle = style;
1089 Object c = getModifiableComponent();
1090 if (c instanceof JComponent) {
1091 ((JComponent) c).setFont(UITools.getFont(((JComponent) c).getFont(), style));
1093 for (String key : style.keySet()) {
1095 setProperty(key, style.get(key));
1096 } catch (UIWidgetException e) { }
1101 * Set the tab title if the component is in a JTabbedPane
1102 * @param title the tab title
1104 public void setTabTitle(String title) throws UIWidgetException {
1105 if (parent instanceof UITab && title != null && component != null) {
1106 JComponent c = getJComponent();
1107 JTabbedPane tab = (JTabbedPane) ((UITab) parent).getJComponent();
1108 int index = tab.indexOfTabComponent(c);
1110 tab.setTitleAt(index, title);
1113 this.tabTitle = title;
1117 * Get the tab title if the component is in a JTabbedPane
1118 * @return the tab title
1120 public String getTabTitle() throws UIWidgetException {
1125 * Set the tooltip text
1126 * @param text the tooltip text
1128 public void setTooltip(String text) throws UIWidgetException {
1129 getModifiableJComponent().setToolTipText(text);
1133 * Get the tooltip text
1134 * @return the tooltip text
1136 public String getTooltip() throws UIWidgetException {
1137 return getModifiableJComponent().getToolTipText();
1141 * Set the tooltip text
1142 * @param text the tooltip text
1144 public void setTooltipString(String text) throws UIWidgetException {
1149 * Get the tooltip text
1150 * @return the tooltip text
1152 public String getTooltipString() throws UIWidgetException {
1153 return getTooltip();
1157 * Set the foreground color
1158 * @param c the color
1160 public void setColor(Color c) throws UIWidgetException {
1161 getModifiableJComponent().setForeground(c);
1165 * Get the foreground color
1166 * @return the foreground color
1168 public Color getColor() throws UIWidgetException {
1169 return getModifiableJComponent().getForeground();
1174 * @param name the font name
1176 public void setFontUnits(UITools.FontUnit unit) throws UIWidgetException {
1177 // TODO: actuellement ds uic, fontunit est ignore et est dc egale a PIXELS
1178 double ratio = UITools.FontUnit.getRatio(fontUnit, UITools.FontUnit.PIXELS);//unit);
1180 this.fontUnit = unit;
1183 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1184 map.put(TextAttribute.SIZE, new Double(f.getSize2D() * ratio));
1185 setFont(new Font(map));
1190 public String getFontUnits() {
1191 return UITools.FontUnit.getAsString(this.fontUnit);
1196 * @param name the font name
1198 public void setFontName(String name) throws UIWidgetException {
1201 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1202 map.put(TextAttribute.FAMILY, name);
1203 setFont(new Font(map));
1209 * @return name the font name
1211 public String getFontName() throws UIWidgetException {
1214 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1215 return (String) map.get(TextAttribute.FAMILY);
1223 * @param size the font size
1225 public void setFontSize(double size) throws UIWidgetException {
1226 double ratio = UITools.FontUnit.getRatio(fontUnit);
1229 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1230 map.put(TextAttribute.SIZE, (Double.isNaN(size) || size < 0 || Double.isInfinite(size)) ? new Double(12.0 * ratio) : new Double(size * ratio));
1231 setFont(new Font(map));
1237 * @return the font size
1239 public double getFontSize() throws UIWidgetException {
1242 double ratio = UITools.FontUnit.getRatio(fontUnit);
1243 return Math.round(f.getSize2D() / ratio);
1250 * Set the font weight
1251 * @param weight the font weight
1253 public void setFontWeight(UITools.FontWeight weight) throws UIWidgetException {
1254 if (weight != null) {
1257 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1258 map.put(TextAttribute.WEIGHT, weight.value());
1259 setFont(new Font(map));
1265 * Get the font weight
1266 * @return the font weight
1268 public String getFontWeight() throws UIWidgetException {
1271 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1272 Float fl = (Float) map.get(TextAttribute.WEIGHT);
1273 return UITools.mapTextAttribute.get(fl);
1280 * Set the font angle
1281 * @param angle the font angle
1283 public void setFontAngle(String angle) throws UIWidgetException {
1284 if (angle != null && !angle.isEmpty()) {
1285 angle = angle.toLowerCase();
1286 boolean italic = angle.equals("italic") || angle.equals("oblique") || angle.equals("it");
1289 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1290 map.put(TextAttribute.POSTURE, italic ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR);
1291 setFont(new Font(map));
1297 * Get the font angle
1298 * @return the font angle
1300 public String getFontAngle() throws UIWidgetException {
1303 Map<TextAttribute, Object> map = (Map<TextAttribute, Object>) f.getAttributes();
1304 Float fl = (Float) map.get(TextAttribute.POSTURE);
1305 if (fl == null || fl == TextAttribute.POSTURE_REGULAR) {
1316 * Set the component relief
1317 * @param relief a string which represents the relief
1319 public void setRelief(String relief) throws UIWidgetException {
1320 if (relief != null) {
1321 getJComponent().setBorder(ScilabRelief.getBorderFromRelief(relief));
1322 this.relief = relief;
1328 * @return the relief
1330 public String getRelief() {
1338 public void setFont(Font f) throws UIWidgetException {
1339 getModifiableJComponent().setFont(f);
1340 getJComponent().revalidate();
1347 public Font getFont() throws UIWidgetException {
1348 return getModifiableJComponent().getFont();
1351 public void setUnits(String[] unit) throws UIWidgetException {
1353 boolean mustLayout = false;
1354 if (unit.length >= 4) {
1355 final int[] units = new int[4];
1356 for (int i = 0; i < 4; i++) {
1357 if (unit[i].equals("%") || unit[i].equalsIgnoreCase("n") || unit[i].equalsIgnoreCase("normalized")) {
1359 } else if (unit[i].equals("pt") || unit[i].equalsIgnoreCase("points")) {
1366 if (this.nolayoutconstraint == null) {
1367 this.nolayoutconstraint = new NoLayout.NoLayoutConstraint();
1370 this.nolayoutconstraint.setUnit(units[0], units[1], units[2], units[3]);
1372 } else if (unit.length == 1) {
1374 if (!unit[0].isEmpty()) {
1375 if (unit[0].equalsIgnoreCase("points")) {
1377 } else if (unit[0].equalsIgnoreCase("normalized")) {
1382 if (this.nolayoutconstraint == null) {
1383 this.nolayoutconstraint = new NoLayout.NoLayoutConstraint();
1386 this.nolayoutconstraint.setUnit(u, u, u, u);
1391 Container p = getJComponent().getParent();
1392 if (p != null && p.getLayout() instanceof NoLayout) {
1402 * Get the position unit (when NoLayout has been set)
1405 public String[] getUnits() throws UIWidgetException {
1407 boolean allTheSame = true;
1408 for (int i = 1; i < 4; i++) {
1409 if (nolayoutconstraint.unit[i] != nolayoutconstraint.unit[0]) {
1416 ret = new String[1];
1417 switch (nolayoutconstraint.unit[0]) {
1419 ret[0] = "normalized";
1429 ret = new String[4];
1430 for (int i = 0; i < 4; i++) {
1431 switch (nolayoutconstraint.unit[i]) {
1449 * Get the constraint when no layout
1450 * @return the constraint
1452 public NoLayout.NoLayoutConstraint getNoLayoutConstraint() {
1453 return nolayoutconstraint;
1456 private void setNoLayoutConstraint(Rectangle2D.Double r) {
1457 if (this.nolayoutconstraint == null) {
1458 this.nolayoutconstraint = new NoLayout.NoLayoutConstraint();
1461 this.nolayoutconstraint.setPoint(r.x, r.y);
1462 this.nolayoutconstraint.setDims(r.width, r.height);
1463 this.nolayoutconstraint.setUnit(0, 0, 0, 0);
1468 * @param p the location
1470 public void setLocation(Point p) throws UIWidgetException {
1472 Rectangle2D.Double pos = getPosition();
1474 setPosition(new Rectangle2D.Double(p.x, p.y, pos.width, pos.height));
1480 * Set the position (when NoLayout has been set)
1481 * @param r the position
1483 public void setPosition(Rectangle2D.Double r) throws UIWidgetException {
1485 if (getComponent() == null) {
1486 setNoLayoutConstraint(r);
1490 if (getComponent() instanceof Component) {
1491 Component c = (Component) getComponent();
1492 Container p = c.getParent();
1493 if (c instanceof JFrame) {
1494 JFrame win = (JFrame) c;
1495 win.setLocation((int) r.x, (int) r.y);
1496 Dimension dim = win.getSize();
1497 if (dim.width != (int) r.width || dim.height != (int) r.height) {
1498 win.setPreferredSize(new Dimension((int) r.width, (int) r.height));
1499 if (win.isVisible()) {
1505 } else if (p == null || p.getLayout() instanceof NoLayout) {
1506 if (this.nolayoutconstraint == null) {
1507 this.nolayoutconstraint = new NoLayout.NoLayoutConstraint();
1510 this.nolayoutconstraint.setPoint(r.x, r.y);
1511 if (this.nolayoutconstraint.bounds.width != r.width || this.nolayoutconstraint.bounds.height != r.height) {
1512 this.nolayoutconstraint.setDims(r.width, r.height);
1514 c.setSize((int) r.width, (int) r.height);
1519 if (p != null && p.isVisible()) {
1524 } else if (p.getLayout() == null) {
1525 c.setSize((int) r.width, (int) r.height);
1535 * @return the position
1537 public Rectangle2D.Double getPosition() throws UIWidgetException {
1538 if (getComponent() instanceof Component) {
1539 Component c = (Component) getComponent();
1540 Container p = c.getParent();
1541 if (p != null && p.getLayout() instanceof NoLayout) {
1542 return this.nolayoutconstraint.bounds;
1545 Dimension d = c.getSize();
1546 Point pt = c.getLocation();
1548 return new Rectangle2D.Double(pt.x, pt.y, d.width, d.height);
1555 * Set the foreground color
1556 * @param c the color
1558 public void setForegroundColor(Color c) throws UIWidgetException {
1560 getModifiableJComponent().setForeground(c);
1565 * Get the foreground color
1566 * @return the foreground color
1568 public Color getForegroundColor() throws UIWidgetException {
1569 return getModifiableJComponent().getForeground();
1573 * Set the background color
1574 * @param c the color
1576 public void setBackgroundColor(Color c) throws UIWidgetException {
1578 getModifiableJComponent().setBackground(c);
1583 * Get the background color
1584 * @return the background color
1586 public Color getBackgroundColor() throws UIWidgetException {
1587 return getModifiableJComponent().getBackground();
1591 * Set the component scrollable or not
1592 * @param b if true the component will be scrollable
1594 public void setScrollable(boolean b) {
1595 if (component instanceof JScrollPane) {
1596 JScrollPane scroll = (JScrollPane) component;
1598 scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
1599 scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
1601 scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
1602 scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
1604 } else if (component instanceof JComponent && b) {
1605 JComponent c = (JComponent) component;
1606 JScrollPane scroll = new JScrollPane();
1608 scroll.getViewport().setView(c);
1609 modifiableComponent = c;
1614 * Check if the component is scrollable
1615 * @return true if the component is scrollable
1617 public boolean getScrollable() {
1618 return component instanceof JScrollPane && ((JScrollPane) component).getHorizontalScrollBarPolicy() == ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
1622 * Set the horizontal block increment (pgup, pgdown in scroll)
1623 * @param blockIncrement the block increment
1625 public void setHorizontalBlockIncrement(int blockIncrement) {
1626 if (component instanceof JScrollPane) {
1627 JScrollPane scroll = (JScrollPane) component;
1628 JScrollBar bar = scroll.getHorizontalScrollBar();
1630 bar.setBlockIncrement(blockIncrement);
1636 * Get the horizontal block increment
1637 * @return block increment
1639 public int getHorizontalBlockIncrement() {
1640 if (component instanceof JScrollPane) {
1641 JScrollPane scroll = (JScrollPane) component;
1642 JScrollBar bar = scroll.getHorizontalScrollBar();
1644 return bar.getBlockIncrement();
1652 * Set the vertical block increment (pgup, pgdown in scroll)
1653 * @param blockIncrement the block increment
1655 public void setVerticalBlockIncrement(int blockIncrement) {
1656 if (component instanceof JScrollPane) {
1657 JScrollPane scroll = (JScrollPane) component;
1658 JScrollBar bar = scroll.getVerticalScrollBar();
1660 bar.setBlockIncrement(blockIncrement);
1666 * Get the vertical block increment
1667 * @return block increment
1669 public int getVerticalBlockIncrement() {
1670 if (component instanceof JScrollPane) {
1671 JScrollPane scroll = (JScrollPane) component;
1672 JScrollBar bar = scroll.getVerticalScrollBar();
1674 return bar.getBlockIncrement();
1682 * Set the horizontal unit increment (wheel in scroll)
1683 * @param unitIncrement the unit increment
1685 public void setHorizontalUnitIncrement(int unitIncrement) {
1686 if (component instanceof JScrollPane) {
1687 JScrollPane scroll = (JScrollPane) component;
1688 JScrollBar bar = scroll.getHorizontalScrollBar();
1690 bar.setUnitIncrement(unitIncrement);
1696 * Get the horizontal unit increment
1697 * @return unit increment
1699 public int getHorizontalUnitIncrement() {
1700 if (component instanceof JScrollPane) {
1701 JScrollPane scroll = (JScrollPane) component;
1702 JScrollBar bar = scroll.getHorizontalScrollBar();
1704 return bar.getUnitIncrement();
1712 * Set the vertical unit increment (wheel in scroll)
1713 * @param unitIncrement the unit increment
1715 public void setVerticalUnitIncrement(int unitIncrement) {
1716 if (component instanceof JScrollPane) {
1717 JScrollPane scroll = (JScrollPane) component;
1718 JScrollBar bar = scroll.getVerticalScrollBar();
1720 bar.setUnitIncrement(unitIncrement);
1726 * Get the vertical unit increment
1727 * @return unit increment
1729 public int getVerticalUnitIncrement() {
1730 if (component instanceof JScrollPane) {
1731 JScrollPane scroll = (JScrollPane) component;
1732 JScrollBar bar = scroll.getVerticalScrollBar();
1734 return bar.getUnitIncrement();
1742 * Set the horizontal scroll value
1743 * @param value the value
1745 public void setHorizontalScrollValue(int value) {
1746 if (component instanceof JScrollPane) {
1747 JScrollPane scroll = (JScrollPane) component;
1748 JScrollBar bar = scroll.getHorizontalScrollBar();
1750 bar.setValue(value);
1756 * Get the horizontal scroll value
1757 * @return the scroll value
1759 public int getHorizontalScrollValue() {
1760 if (component instanceof JScrollPane) {
1761 JScrollPane scroll = (JScrollPane) component;
1762 JScrollBar bar = scroll.getHorizontalScrollBar();
1764 return bar.getValue();
1772 * Set the vertical scroll value
1773 * @param value the value
1775 public void setVerticalScrollValue(int value) {
1776 if (component instanceof JScrollPane) {
1777 JScrollPane scroll = (JScrollPane) component;
1778 JScrollBar bar = scroll.getVerticalScrollBar();
1780 bar.setValue(value);
1786 * Get the vertical scroll value
1787 * @return the scroll value
1789 public int getVerticalScrollValue() {
1790 if (component instanceof JScrollPane) {
1791 JScrollPane scroll = (JScrollPane) component;
1792 JScrollBar bar = scroll.getVerticalScrollBar();
1794 return bar.getValue();
1802 * Set the component constraints
1803 * @param constraint a string containing layout constraints (CSS style)
1805 public void setConstraint(String constraint) throws UIWidgetException {
1806 setConstraint(StyleParser.parseLine(constraint));
1810 * Set the component constraints
1811 * @param constraint a map containing layout constraints
1813 public void setConstraint(Map<String, String> constraint) throws UIWidgetException {
1814 this.constraint = constraint;
1815 Container p = getJComponent().getParent();
1816 if (p != null && !(p.getLayout() instanceof NoLayout) && getParent() != null) {
1817 getParent().add(this);
1824 * Get the constraint
1825 * @return constraint as String
1827 public String getConstraint() {
1828 if (constraint != null) {
1830 for (Map.Entry<String, String> entry : constraint.entrySet()) {
1831 str += entry.getKey() + ":" + entry.getValue();
1842 * @param layout the layout informations (CSS style)
1844 public void setLayout(String layout) throws UIWidgetException {
1845 getModifiableContainer().setLayout(UILayoutFactory.getLayout(getModifiableJComponent(), layout));
1849 * Enable or not the component
1850 * @param enable if true, the component is enabled
1852 public void setEnable(boolean enable) throws UIWidgetException {
1853 getJComponent().setEnabled(enable);
1857 * Check if the component is enabled
1858 * @return true if the component is enabled
1860 public boolean getEnable() throws UIWidgetException {
1861 return getJComponent().isEnabled();
1865 * Create a MouseListener on this component
1867 private final void createMouseListener() throws UIWidgetException {
1868 if (mouseListener == null) {
1869 mouseListener = new UIMouseListener(this);
1870 mouseListener.newInstance();
1871 mouseListener.addListenerToComponent(getModifiableJComponent());
1876 * Set onmouseclick action
1877 * @param command the command to execute
1879 public void setOnmouseclick(String command) throws UIWidgetException {
1880 if (command != null && !command.isEmpty()) {
1881 createMouseListener();
1882 mouseListener.setOnmouseclick(command);
1887 * Set onmouseover action
1888 * @param command the command to execute
1890 public void setOnmouseover(String command) throws UIWidgetException {
1891 if (command != null && !command.isEmpty()) {
1892 createMouseListener();
1893 mouseListener.setOnmouseover(command);
1898 * Set onmouseenter action
1899 * @param command the command to execute
1901 public void setOnmouseenter(String command) throws UIWidgetException {
1902 if (command != null && !command.isEmpty()) {
1903 createMouseListener();
1904 mouseListener.setOnmouseenter(command);
1909 * Set onmouseexit action
1910 * @param command the command to execute
1912 public void setOnmouseexit(String command) throws UIWidgetException {
1913 if (command != null && !command.isEmpty()) {
1914 createMouseListener();
1915 mouseListener.setOnmouseexit(command);
1920 * Set onmousepress action
1921 * @param command the command to execute
1923 public void setOnmousepress(String command) throws UIWidgetException {
1924 if (command != null && !command.isEmpty()) {
1925 createMouseListener();
1926 mouseListener.setOnmousepress(command);
1931 * Set onmouserelease action
1932 * @param command the command to execute
1934 public void setOnmouserelease(String command) throws UIWidgetException {
1935 if (command != null && !command.isEmpty()) {
1936 createMouseListener();
1937 mouseListener.setOnmouserelease(command);
1942 * Set onmousewheel action
1943 * @param command the command to execute
1945 public void setOnmousewheel(String command) throws UIWidgetException {
1946 if (command != null && !command.isEmpty()) {
1947 createMouseListener();
1948 mouseListener.setOnmousewheel(command);
1953 * Set onmousedrag action
1954 * @param command the command to execute
1956 public void setOnmousedrag(String command) throws UIWidgetException {
1957 if (command != null && !command.isEmpty()) {
1958 createMouseListener();
1959 mouseListener.setOnmousedrag(command);
1964 * Get onmouseclick action
1965 * @return the command to execute
1967 public UICallback getOnmouseclick() {
1968 if (mouseListener != null) {
1969 return mouseListener.getOnmouseclick();
1976 * Get onmouseover action
1977 * @return the command to execute
1979 public UICallback getOnmouseover() {
1980 if (mouseListener != null) {
1981 return mouseListener.getOnmouseover();
1988 * Get onmouseenter action
1989 * @return the command to execute
1991 public UICallback getOnmouseenter() {
1992 if (mouseListener != null) {
1993 return mouseListener.getOnmouseenter();
2000 * Get onmouseexit action
2001 * @return the command to execute
2003 public UICallback getOnmouseexit() {
2004 if (mouseListener != null) {
2005 return mouseListener.getOnmouseexit();
2012 * Get onmousepress action
2013 * @return the command to execute
2015 public UICallback getOnmousepress() {
2016 if (mouseListener != null) {
2017 return mouseListener.getOnmousepress();
2024 * Get onmouserelease action
2025 * @return the command to execute
2027 public UICallback getOnmouserelease() {
2028 if (mouseListener != null) {
2029 return mouseListener.getOnmouserelease();
2036 * Get onmousedrag action
2037 * @return the command to execute
2039 public UICallback getOnmousedrag() {
2040 if (mouseListener != null) {
2041 return mouseListener.getOnmousedrag();
2048 * Get onmousewheel action
2049 * @return the command to execute
2051 public UICallback getOnmousewheel() {
2052 if (mouseListener != null) {
2053 return mouseListener.getOnmousewheel();
2060 * Enable onmouseclick
2061 * @param b, if true the action is enabled
2063 public void setOnmouseclickEnable(boolean b) {
2064 if (mouseListener != null) {
2065 mouseListener.setOnmouseclickEnable(b);
2070 * Check if onmouseclick is enabled
2071 * @return true the action is enabled
2073 public boolean getOnmouseclickEnable() {
2074 if (mouseListener != null) {
2075 return mouseListener.getOnmouseclickEnable();
2082 * Enable onmouseover
2083 * @param b, if true the action is enabled
2085 public void setOnmouseoverEnable(boolean b) {
2086 if (mouseListener != null) {
2087 mouseListener.setOnmouseoverEnable(b);
2092 * Check if onmouseover is enabled
2093 * @return true the action is enabled
2095 public boolean getOnmouseoverEnable() {
2096 if (mouseListener != null) {
2097 return mouseListener.getOnmouseoverEnable();
2104 * Enable onmouseenter
2105 * @param b, if true the action is enabled
2107 public void setOnmouseenterEnable(boolean b) {
2108 if (mouseListener != null) {
2109 mouseListener.setOnmouseenterEnable(b);
2114 * Check if onmouseenter is enabled
2115 * @return true the action is enabled
2117 public boolean getOnmouseenterEnable() {
2118 if (mouseListener != null) {
2119 return mouseListener.getOnmouseenterEnable();
2126 * Enable onmouseexit
2127 * @param b, if true the action is enabled
2129 public void setOnmouseexitEnable(boolean b) {
2130 if (mouseListener != null) {
2131 mouseListener.setOnmouseexitEnable(b);
2136 * Check if onmouseexit is enabled
2137 * @return true the action is enabled
2139 public boolean getOnmouseexitEnable() {
2140 if (mouseListener != null) {
2141 return mouseListener.getOnmouseexitEnable();
2148 * Enable onmousepress
2149 * @param b, if true the action is enabled
2151 public void setOnmousepressEnable(boolean b) {
2152 if (mouseListener != null) {
2153 mouseListener.setOnmousepressEnable(b);
2158 * Check if onmousepress is enabled
2159 * @return true the action is enabled
2161 public boolean getOnmousepressEnable() {
2162 if (mouseListener != null) {
2163 return mouseListener.getOnmousepressEnable();
2170 * Enable onmouserelease
2171 * @param b, if true the action is enabled
2173 public void setOnmousereleaseEnable(boolean b) {
2174 if (mouseListener != null) {
2175 mouseListener.setOnmousereleaseEnable(b);
2180 * Check if onmouserelease is enabled
2181 * @return true the action is enabled
2183 public boolean getOnmousereleaseEnable() {
2184 if (mouseListener != null) {
2185 return mouseListener.getOnmousereleaseEnable();
2192 * Enable onmousewheel
2193 * @param b, if true the action is enabled
2195 public void setOnmousewheelEnable(boolean b) {
2196 if (mouseListener != null) {
2197 mouseListener.setOnmousewheelEnable(b);
2202 * Check if onmousewheel is enabled
2203 * @return true the action is enabled
2205 public boolean getOnmousewheelEnable() {
2206 if (mouseListener != null) {
2207 return mouseListener.getOnmousewheelEnable();
2214 * Enable onmousedrag
2215 * @param b, if true the action is enabled
2217 public void setOnmousedragEnable(boolean b) {
2218 if (mouseListener != null) {
2219 mouseListener.setOnmousedragEnable(b);
2224 * Check if onmousedrag is enabled
2225 * @return true the action is enabled
2227 public boolean getOnmousedragEnable() {
2228 if (mouseListener != null) {
2229 return mouseListener.getOnmousedragEnable();
2236 * Create a FocusListener on this component
2238 private final void createFocusListener() throws UIWidgetException {
2239 if (focusListener == null) {
2240 focusListener = new UIFocusListener(this);
2241 focusListener.newInstance();
2242 focusListener.addListenerToComponent(getModifiableJComponent());
2247 * Set onfocusgain action
2248 * @param command the command to execute
2250 public void setOnfocusgain(String command) throws UIWidgetException {
2251 if (command != null && !command.isEmpty()) {
2252 createFocusListener();
2253 focusListener.setOnfocusgain(command);
2258 * Set onfocusloss action
2259 * @param command the command to execute
2261 public void setOnfocusloss(String command) throws UIWidgetException {
2262 if (command != null && !command.isEmpty()) {
2263 createFocusListener();
2264 focusListener.setOnfocusloss(command);
2269 * Get onfocusgain action
2270 * @return the command to execute
2272 public UICallback getOnfocusgain() {
2273 if (focusListener != null) {
2274 return focusListener.getOnfocusgain();
2281 * Get onfocusloss action
2282 * @return the command to execute
2284 public UICallback getOnfocusloss() {
2285 if (focusListener != null) {
2286 return focusListener.getOnfocusloss();
2293 * Enable onfocusgain
2294 * @param b, if true the action is enabled
2296 public void setOnfocusgainEnable(boolean b) {
2297 if (focusListener != null) {
2298 focusListener.setOnfocusgainEnable(b);
2303 * Check if onfocusgain is enabled
2304 * @return true the action is enabled
2306 public boolean getOnfocusgainEnable() {
2307 if (focusListener != null) {
2308 return focusListener.getOnfocusgainEnable();
2315 * Enable onfocusloss
2316 * @param b, if true the action is enabled
2318 public void setOnfocuslossEnable(boolean b) {
2319 if (focusListener != null) {
2320 focusListener.setOnfocuslossEnable(b);
2325 * Check if onfocusloss is enabled
2326 * @return true the action is enabled
2328 public boolean getOnfocuslossEnable() {
2329 if (focusListener != null) {
2330 return focusListener.getOnfocuslossEnable();
2337 * Change the parent and update the dependencies
2338 * @param parent the parent
2340 public void updateDependencies(UIComponent parent) throws UIWidgetException {
2345 * Add a list of children
2346 * @param list the children
2348 public void add(List<UIComponent> list) throws UIWidgetException {
2349 for (UIComponent uicomp : list) {
2355 * Add an UIComponent to the children list
2356 * @parent uicomp the child to add
2358 public void add(final UIComponent uicomp) throws UIWidgetException {
2359 UIAccessTools.add(this, uicomp);
2364 * @parent uicomp the listener to add
2366 public void addListener(final UIListener uicomp) throws UIWidgetException {
2367 uicomp.addListenerToComponent(getModifiableJComponent());
2372 * @param popup the popup menu
2374 public void addPopupMenu(final JPopupMenu popup) {
2375 UIAccessTools.execOnEDT(new Runnable() {
2378 getModifiableJComponent().setComponentPopupMenu(popup);
2379 } catch (Exception e) {
2387 * Set a property of this component
2388 * @param name the property name
2389 * @param value the property value
2391 public void setProperty(final String name, final String value) throws UIWidgetException {
2393 if (thisOrComponent) {
2394 setPropertyViaReflectionInThis(name, value);
2396 setPropertyViaReflectionInComponent(name, value);
2398 } catch (Exception e) {
2399 if (thisOrComponent) {
2400 setPropertyViaReflectionInComponent(name, value);
2402 setPropertyViaReflectionInThis(name, value);
2408 * Set a property (via relection) of this component
2409 * @param name the property name
2410 * @param value the property value
2412 protected final void setPropertyViaReflectionInThis(final String name, final String value) throws UIWidgetException {
2413 UIAccessTools.setPropertyViaReflection(this, name, value);
2417 * Set a property (via relection) of the modifiable component
2418 * @param name the property name
2419 * @param value the property value
2421 protected final void setPropertyViaReflectionInComponent(final String name, final String value) throws UIWidgetException {
2422 UIAccessTools.setPropertyViaReflection(getModifiableComponent(), name, value);
2426 * Set a property of this component
2427 * @param name the property name
2428 * @param value the property value
2430 public void setProperty(final String name, final ScilabType value) throws UIWidgetException {
2432 if (thisOrComponent) {
2433 setPropertyViaReflectionInThis(name, value);
2435 setPropertyViaReflectionInComponent(name, value);
2437 } catch (Exception e) {
2438 if (thisOrComponent) {
2439 setPropertyViaReflectionInComponent(name, value);
2441 setPropertyViaReflectionInThis(name, value);
2447 * Set a property of this component
2448 * @param name the property name
2449 * @param value the property value
2451 public void setProperty(final List<String> names, final List<ScilabType> values) throws UIWidgetException {
2452 if (names != null && values != null) {
2453 final Object modifiableComponent = getModifiableComponent();
2454 final Class clazzThis = this.getClass();
2455 final Class clazzComp = modifiableComponent.getClass();
2456 final List<Object> objs = new ArrayList<Object>(names.size());
2457 final List<Method> methods = new ArrayList<Method>(names.size());
2459 for (String name : names) {
2460 String methodName = UIAccessTools.getSetterName(name);
2462 if (thisOrComponent) {
2463 method = UIMethodFinder.findSetter(methodName, clazzThis);
2464 if (method == null) {
2465 method = UIMethodFinder.findSetter(methodName, clazzComp);
2466 if (method == null) {
2467 throw new UIWidgetException("No attribute " + name + " in " + clazzThis.getSimpleName());
2469 objs.add(modifiableComponent);
2474 method = UIMethodFinder.findSetter(methodName, clazzComp);
2475 if (method == null) {
2476 method = UIMethodFinder.findSetter(methodName, clazzThis);
2477 if (method == null) {
2478 throw new UIWidgetException("No attribute " + name + " in " + clazzThis.getSimpleName());
2482 objs.add(modifiableComponent);
2485 methods.add(method);
2488 UIAccessTools.execOnEDT(new Runnable() {
2491 for (int i = 0; i < objs.size(); i++) {
2492 UIAccessTools.invokeSetter(methods.get(i), objs.get(i), values.get(i));
2494 } catch (Exception e) {
2495 System.err.println(e);
2503 * Set a property (via relection) of this component
2504 * @param name the property name
2505 * @param value the property value
2507 protected final void setPropertyViaReflectionInThis(final String name, final ScilabType value) throws UIWidgetException {
2508 UIAccessTools.setPropertyViaReflection(this, name, value);
2512 * Set a property (via relection) of the modifiable component
2513 * @param name the property name
2514 * @param value the property value
2516 protected final void setPropertyViaReflectionInComponent(final String name, final ScilabType value) throws UIWidgetException {
2517 UIAccessTools.setPropertyViaReflection(getModifiableComponent(), name, value);
2521 * Get the pairs property name -- method name
2524 public String[][] getPropertiesPairs() {
2525 Map<String, Method> map = new TreeMap<String, Method>();
2526 if (thisOrComponent) {
2527 UIMethodFinder.getSetter(getModifiableComponent().getClass(), map);
2528 UIMethodFinder.getSetter(this.getClass(), map);
2530 UIMethodFinder.getSetter(this.getClass(), map);
2531 UIMethodFinder.getSetter(getModifiableComponent().getClass(), map);
2534 System.out.println(this.getClass() + ": (" + map.size() + " entries)");
2535 for (Map.Entry<String, Method> entry : map.entrySet()) {
2536 System.out.println(entry.getKey() + " --> " + entry.getValue());
2543 * Get a property value
2544 * @param name the property name
2546 public Object getProperty(final String name) throws UIWidgetException {
2548 if (thisOrComponent) {
2549 return getPropertyViaReflectionInThis(name);
2551 return getPropertyViaReflectionInComponent(name);
2553 } catch (Exception e) {
2554 if (thisOrComponent) {
2555 return getPropertyViaReflectionInComponent(name);
2557 return getPropertyViaReflectionInThis(name);
2563 * Get a property value (via reflection)
2564 * @param name the property name
2566 protected Object getPropertyViaReflectionInThis(final String name) throws UIWidgetException {
2567 return UIAccessTools.getPropertyViaReflection(this, name);
2571 * Get a property value (via reflection) in the modifiable component
2572 * @param name the property name
2574 protected Object getPropertyViaReflectionInComponent(final String name) throws UIWidgetException {
2575 return UIAccessTools.getPropertyViaReflection(getModifiableComponent(), name);
2579 * Create a new instance
2580 * @param attributes the attributes
2582 private void createNewInstance(final ConvertableMap attributes) throws UIWidgetException {
2583 Set<String> uselessAttrs = UIAccessTools.createNewInstance(this, attributes);
2584 if (uselessAttrs.contains("scrollable")) {
2585 boolean scrollable = (Boolean) attributes.get(boolean.class, "scrollable", false);
2586 if (scrollable && component instanceof JComponent) {
2587 modifiableComponent = component;
2588 if (SwingUtilities.isEventDispatchThread()) {
2589 component = new JScrollPane((JComponent) modifiableComponent);
2592 SwingUtilities.invokeAndWait(new Runnable() {
2595 component = new JScrollPane((JComponent) modifiableComponent);
2596 } catch (Exception e) {
2597 System.err.println(e);
2598 e.printStackTrace();
2602 } catch (Exception e) {
2603 System.err.println(e);
2604 e.printStackTrace();
2607 uselessAttrs.remove("scrollable");
2611 if (uselessAttrs.contains("position")) {
2612 if (attributes instanceof StringMap) {
2613 setNoLayoutConstraint(StringConverters.getObjectFromValue(Rectangle2D.Double.class, (String) attributes.get("position")));
2615 setNoLayoutConstraint(ScilabTypeConverters.getObjectFromValue(Rectangle2D.Double.class, (ScilabType) attributes.get("position")));
2617 uselessAttrs.remove("position");
2620 setAttributesAndStyle(attributes, uselessAttrs);
2624 * Set the attributes and style
2625 * @param attributes the attributes
2626 * @param uselessAttrs the useless attributes
2628 private void setAttributesAndStyle(final ConvertableMap attributes, final Set<String> uselessAttrs) {
2629 if (!uselessAttrs.isEmpty()) {
2630 UIAccessTools.execOnEDT(new Runnable() {
2632 if (attributes instanceof StringMap) {
2633 for (String attr : uselessAttrs) {
2635 setProperty(attr, (String) attributes.get(attr));
2636 } catch (UIWidgetException e) { }
2639 for (String attr : uselessAttrs) {
2641 setProperty(attr, (ScilabType) attributes.get(attr));
2642 } catch (UIWidgetException e) { }
2646 if (component instanceof JComponent && nolayoutconstraint == null) {
2647 JComponent jc = (JComponent) component;
2648 Dimension d = jc.getPreferredSize();
2649 setNoLayoutConstraint(0, 0, (double) d.width, (double) d.height);
2650 nolayoutconstraint.setUnit(0, 0, 0, 0);
2655 if (component instanceof JComponent && nolayoutconstraint == null) {
2656 UIAccessTools.execOnEDT(new Runnable() {
2658 JComponent jc = (JComponent) component;
2659 Dimension d = jc.getPreferredSize();
2660 setNoLayoutConstraint(0, 0, (double) d.width, (double) d.height);
2661 nolayoutconstraint.setUnit(0, 0, 0, 0);
2667 if (style != null) {
2668 Map<String, String> elementStyle = null;
2670 elementStyle = style.get("#" + id);
2673 if (elementStyle == null) {
2674 String styleClass = (String) attributes.get(String.class, "class", null);
2675 if (styleClass != null) {
2676 elementStyle = style.get(styleClass);
2679 if (elementStyle == null) {
2680 elementStyle = style.get("." + this.getClass().getSimpleName());
2683 final Map<String, String> es = elementStyle;
2684 if (elementStyle != null) {
2685 UIAccessTools.execOnEDT(new Runnable() {
2688 setUiStyle(new HashMap<String, String>(es));
2689 } catch (UIWidgetException e) {
2690 System.err.println(e);
2691 e.printStackTrace();
2703 protected void finalize() throws Throwable {
2704 UserData.removeUIWidgetUserData(uid);