1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Vincent Couvert
4  * Copyright (C) 2007 - INRIA - Marouane BEN JELLOUL
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16 
17 package org.scilab.modules.gui.menu;
18 
19 import java.awt.Color;
20 import java.awt.Font;
21 
22 import org.scilab.modules.gui.bridge.menu.SwingScilabMenu;
23 import org.scilab.modules.gui.checkboxmenuitem.CheckBoxMenuItem;
24 import org.scilab.modules.gui.events.callback.CommonCallBack;
25 import org.scilab.modules.gui.menuitem.MenuItem;
26 
27 /**
28  * Bridge for Scilab Menus in GUIs
29  * @author Vincent COUVERT
30  * @author Marouane BEN JELLOUL
31  */
32 public class ScilabMenuBridge {
33 
34     /**
35      * Constructor
36      */
ScilabMenuBridge()37     protected ScilabMenuBridge() {
38         throw new UnsupportedOperationException(); /* Prevents calls from subclass */
39     }
40 
41     /**
42      * Creates a Scilab Menu
43      * @return the created Menu
44      */
createMenu()45     public static SimpleMenu createMenu() {
46         return new SwingScilabMenu();
47     }
48 
49     /**
50      * set a text to a menu
51      * @param menu the Menu which we want to set the text to
52      * @param newText the new text to set to the menu
53      */
setText(Menu menu, String newText)54     public static void setText(Menu menu, String newText) {
55         String label = newText;
56 
57         // Try to set a mnemonic according to text (character preeceded by a &)
58         for (int charIndex = 0; charIndex < newText.length(); charIndex++) {
59             if (newText.charAt(charIndex) == '&') {
60 
61                 boolean canBeAMnemonic = true;
62 
63                 // Previous char must not be a &
64                 if ((charIndex != 0) && (newText.charAt(charIndex - 1) == '&')) {
65                     canBeAMnemonic = false;
66                 }
67 
68                 if (canBeAMnemonic && newText.charAt(charIndex + 1) != '&') {
69                     // A mnemonic
70                     menu.getAsSimpleMenu().setMnemonic(newText.charAt(charIndex + 1));
71 
72                     // Have to remove the & used to set a Mnemonic
73                     String firstPart = newText.substring(0, Math.max(charIndex, 0)); // Before &
74                     String secondPart = newText.substring(Math.min(charIndex + 1, newText.length()), newText.length()); // After &
75                     label = firstPart + secondPart;
76                     break;
77                 }
78 
79             }
80         }
81 
82         // Set the text after relacing all && (display a & in the label) by &
83         menu.getAsSimpleMenu().setText(label.replaceAll("&&", "&"));
84     }
85 
86     /**
87      * Get a text of a menu
88      * @param menu the Menu which we want to set the text to
89      * @return the text of the menu
90      */
getText(Menu menu)91     public static String getText(Menu menu) {
92         return menu.getAsSimpleMenu().getBaseText();
93     }
94 
95     /**
96      * Append a MenuItem to a Scilab Menu
97      * @param menu the Menu which we want to add the MenuItem to
98      * @param newMenuItem the MenuItem to add to the Menu
99      */
add(Menu menu, MenuItem newMenuItem)100     public static void add(Menu menu, MenuItem newMenuItem) {
101         menu.getAsSimpleMenu().add(newMenuItem);
102     }
103 
104     /**
105      * Append a CheckBoxMenuItem to a Scilab Menu
106      * @param menu the Menu which we want to add the CheckBoxMenuItem to
107      * @param newCheckBoxMenuItem the CheckBoxMenuItem to add to the Menu
108      */
add(Menu menu, CheckBoxMenuItem newCheckBoxMenuItem)109     public static void add(Menu menu, CheckBoxMenuItem newCheckBoxMenuItem) {
110         menu.getAsSimpleMenu().add(newCheckBoxMenuItem);
111     }
112 
113     /**
114      * Append a subMenu to a Scilab Menu
115      * @param menu the Menu which we want to append the subMenu to
116      * @param newSubMenu the subMenu to append to the Menu
117      */
add(Menu menu, Menu newSubMenu)118     public static void add(Menu menu, Menu newSubMenu) {
119         menu.getAsSimpleMenu().add(newSubMenu);
120     }
121 
122     /**
123      * set a mnemonic to a Menu
124      * @param menu the Menu which we want to set the mnemonic to
125      * @param mnemonic the mnemonic to set to the menu
126      */
setMnemonic(Menu menu, int mnemonic)127     public static void setMnemonic(Menu menu, int mnemonic) {
128         menu.getAsSimpleMenu().setMnemonic(mnemonic);
129     }
130 
131     /**
132      * Add a Separator to a Menu
133      * @param menu the Menu which we want to add the Separator to
134      */
addSeparator(Menu menu)135     public static void addSeparator(Menu menu) {
136         menu.getAsSimpleMenu().addSeparator();
137     }
138 
139     /**
140      * Set if the menu is enabled or not
141      * @param menu the Menu which we want to set the status of
142      * @param status true if the menu is enabled
143      */
setEnabled(Menu menu, boolean status)144     public static void setEnabled(Menu menu, boolean status) {
145         menu.getAsSimpleMenu().setEnabled(status);
146     }
147 
148     /**
149      * Gets the enable status of a Menu
150      * @param menu the menu which we want to get the status of
151      * @return the enable status of the Menu (true if the Menu is enabled, false if not)
152      */
isEnabled(Menu menu)153     public static boolean isEnabled(Menu menu) {
154         return menu.getAsSimpleMenu().isEnabled();
155     }
156 
157     /**
158      * Gets the visibility status of a Scilab menu
159      * @param menu the menu we want to get the visiblity status of
160      * @return the visibility status of the menu (true if the menu is visible, false if not)
161      * @see org.scilab.modules.gui.UIElement#isVisible()
162      */
isVisible(Menu menu)163     public static boolean isVisible(Menu menu) {
164         return menu.getAsSimpleMenu().isVisible();
165     }
166 
167     /**
168      * Sets the visibility status of a Scilab menu
169      * @param menu the menu we want to set the visiblity status of
170      * @param newVisibleState the visibility status we want to set to the menu (true to set the menu visible, false else)
171      * @see org.scilab.modules.gui.UIElement#setVisible(boolean)
172      */
setVisible(Menu menu, boolean newVisibleState)173     public static void setVisible(Menu menu, boolean newVisibleState) {
174         menu.getAsSimpleMenu().setVisible(newVisibleState);
175     }
176 
177     /**
178      * Set the Background color of the menu
179      * @param menu the menu we want to set the background of
180      * @param color the Color
181      */
setBackground(Menu menu, Color color)182     public static void setBackground(Menu menu, Color color) {
183         menu.getAsSimpleMenu().setBackground(color);
184     }
185 
186     /**
187      * Get the Background color of the menu
188      * @param menu the menu we want to get the background of
189      * @return the Color
190      */
getBackground(Menu menu)191     public static Color getBackground(Menu menu) {
192         return menu.getAsSimpleMenu().getBackground();
193     }
194 
195     /**
196      * Set the Foreground color of the menu
197      * @param menu the menu we want to set the foreground of
198      * @param color the Color
199      */
setForeground(Menu menu, Color color)200     public static void setForeground(Menu menu, Color color) {
201         menu.getAsSimpleMenu().setForeground(color);
202     }
203 
204     /**
205      * Get the Foreground color of the menu
206      * @param menu the menu we want to get the foreground of
207      * @return the Color
208      */
getForeground(Menu menu)209     public static Color getForeground(Menu menu) {
210         return menu.getAsSimpleMenu().getForeground();
211     }
212 
213     /**
214      * Set the font of the menu.
215      * @param menu the menu we want to set the font of
216      * @param font the font
217      */
setFont(Menu menu, Font font)218     public static void setFont(Menu menu, Font font) {
219         menu.getAsSimpleMenu().setFont(font);
220     }
221 
222     /**
223      * Get the font of the menu.
224      * @param menu the menu we want to get the font of
225      * @return the font
226      */
getFont(Menu menu)227     public static Font getFont(Menu menu) {
228         return menu.getAsSimpleMenu().getFont();
229     }
230 
231     /**
232      * Add a callback to the menu, this callback is a Scilab command
233      * @param menu the menu we want to set the callback of
234      * @param callback the CallBack to set.
235      */
setCallback(Menu menu, CommonCallBack callback)236     public static void setCallback(Menu menu, CommonCallBack callback) {
237         menu.getAsSimpleMenu().setCallback(callback);
238     }
239 
240     /**
241      * Set the horizontal alignment for the Menu text
242      * @param menu the Menu we want to set the alignment of
243      * @param alignment the value for the alignment (See ScilabAlignment.java)
244      */
setHorizontalAlignment(Menu menu, String alignment)245     public static void setHorizontalAlignment(Menu menu, String alignment) {
246         menu.getAsSimpleMenu().setHorizontalAlignment(alignment);
247     }
248 
249     /**
250      * Set the vertical alignment for the Menu text
251      * @param menu the Menu we want to set the alignment of
252      * @param alignment the value for the alignment (See ScilabAlignment.java)
253      */
setVerticalAlignment(Menu menu, String alignment)254     public static void setVerticalAlignment(Menu menu, String alignment) {
255         menu.getAsSimpleMenu().setVerticalAlignment(alignment);
256     }
257 
258     /**
259      * Set the Relief of the Menu
260      * @param menu the Menu which we want to set the Relief of
261      * @param reliefType the type of the relief to set (See ScilabRelief.java)
262      */
setRelief(Menu menu, String reliefType)263     public static void setRelief(Menu menu, String reliefType) {
264         menu.getAsSimpleMenu().setRelief(reliefType);
265     }
266 
267     /**
268      * Delete the Menu
269      * @param menu the Menu to be destroyed
270      */
destroy(Menu menu)271     public static void destroy(Menu menu) {
272         menu.getAsSimpleMenu().destroy();
273     }
274 
275     /**
276      * Give the focus to the Menu
277      * @param menu the Menu
278      */
requestFocus(Menu menu)279     public static void requestFocus(Menu menu) {
280         menu.getAsSimpleMenu().requestFocus();
281     }
282 
283     /**
284      * Retrieve the CallBack associated to this Menu
285      * @param menuItem the Menu
286      * @return the CallBack
287      */
getCallback(Menu menu)288     public static CommonCallBack getCallback(Menu menu) {
289         return menu.getAsSimpleMenu().getCallback();
290     }
291 
292     /**
293      * Set if the Menu is checked or not
294      * @param menu the Menu
295      * @param status true if the Menu is checked
296      */
setChecked(Menu menu, boolean status)297     public static void setChecked(Menu menu, boolean status) {
298         menu.getAsSimpleMenu().setChecked(status);
299     }
300 
301     /**
302      * Get if the Menu is checked or not
303      * @return true if the Menu is checked
304      */
isChecked(Menu menu)305     public static boolean isChecked(Menu menu) {
306         return menu.getAsSimpleMenu().isChecked();
307     }
308 
309 }
310