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 * Copyright (C) 2010-2011 - DIGITEO - Vincent COUVERT
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
14 package org.scilab.modules.gui.bridge.label;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_ICON__;
18 import java.awt.Color;
19 import java.awt.Dimension;
21 import java.awt.Graphics;
22 import java.awt.Graphics2D;
23 import java.awt.GraphicsConfiguration;
24 import java.awt.GraphicsDevice;
25 import java.awt.GraphicsEnvironment;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.Image;
29 import java.awt.image.BufferedImage;
31 import java.io.IOException;
33 import javax.imageio.ImageIO;
34 import javax.swing.BorderFactory;
35 import javax.swing.GrayFilter;
36 import javax.swing.Icon;
37 import javax.swing.ImageIcon;
38 import javax.swing.JComponent;
39 import javax.swing.JEditorPane;
40 import javax.swing.JLabel;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTextPane;
44 import javax.swing.SwingConstants;
45 import javax.swing.UIManager;
46 import javax.swing.border.Border;
47 import javax.swing.event.HyperlinkEvent;
48 import javax.swing.event.HyperlinkListener;
49 import javax.swing.text.html.HTMLDocument;
50 import javax.swing.text.html.StyleSheet;
52 import org.scilab.forge.jlatexmath.TeXConstants;
53 import org.scilab.forge.jlatexmath.TeXFormula;
54 import org.scilab.forge.jlatexmath.TeXIcon;
55 import org.scilab.modules.commons.gui.FindIconHelper;
56 import org.scilab.modules.console.utils.ScilabSpecialTextUtilities;
57 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
58 import org.scilab.modules.gui.SwingViewObject;
59 import org.scilab.modules.gui.SwingViewWidget;
60 import org.scilab.modules.gui.bridge.label.SwingScilabLabel.IconLabel.IconType;
61 import org.scilab.modules.gui.events.callback.CommonCallBack;
62 import org.scilab.modules.gui.label.SimpleLabel;
63 import org.scilab.modules.gui.menubar.MenuBar;
64 import org.scilab.modules.gui.textbox.TextBox;
65 import org.scilab.modules.gui.toolbar.ToolBar;
66 import org.scilab.modules.gui.utils.Position;
67 import org.scilab.modules.gui.utils.PositionConverter;
68 import org.scilab.modules.gui.utils.ScilabAlignment;
69 import org.scilab.modules.gui.utils.ScilabRelief;
70 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
71 import org.scilab.modules.gui.utils.Size;
72 import org.scilab.modules.gui.utils.WebBrowser;
75 * Swing implementation for Scilab Labels in GUIs
76 * @author Vincent COUVERT
77 * @author Marouane BEN JELLOUL
79 public class SwingScilabLabel extends JScrollPane implements SwingViewObject, SimpleLabel {
81 private enum LabelStyle {
82 TEXT, LATEX, MATHML, HTML
85 private static final long serialVersionUID = 7177323379068859441L;
89 private JComponent label = new JLabel(" ");
91 private Border defaultBorder = null;
93 private LabelStyle labelStyle = null;
95 private String horizontalAlignment = "left"; //Horizontal alignment property
97 private String verticalAlignment = "middle"; // Vertical alignment property
99 private final JPanel alignmentPanel = new JPanel(); // Used for alignment
101 private final GridBagLayout alignmentLayout = new GridBagLayout();
103 private String labelText = ""; // Used to save user given text
105 private static HyperlinkListener urlOpener = new HyperlinkListener() {
106 public void hyperlinkUpdate(HyperlinkEvent event) {
107 if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
108 WebBrowser.openUrl(event.getURL(), event.getDescription());
116 public SwingScilabLabel() {
118 alignmentPanel.setLayout(alignmentLayout);
119 setViewportView(label);
120 setBorder(BorderFactory.createEmptyBorder());
121 setViewportBorder(BorderFactory.createEmptyBorder());
122 setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
123 // Initialize display
128 * Apply a new font for the label.
129 * @param font new font to use.
131 public void setFont(Font font) {
135 if (labelStyle == LabelStyle.HTML) {
136 StyleSheet styleSheet = ((HTMLDocument) ((JTextPane) label).getDocument()).getStyleSheet();
137 styleSheet.addRule("body {font-family:" + font.getName() + ";}");
138 styleSheet.addRule("body {font-size:" + font.getSize() + "pt;}");
140 styleSheet.addRule("body {font-weight:bold;}");
142 styleSheet.addRule("body {font-weight:normal;}");
144 if (font.isItalic()) {
145 styleSheet.addRule("body {font-style:italic;}");
147 styleSheet.addRule("body {font-style:normal;}");
154 * To set the Foreground color of the element.
155 * @param color the Color
157 public void setForeground(Color color) {
158 super.setForeground(color);
160 label.setForeground(color);
164 public void setEnabled(boolean enabled) {
165 super.setEnabled(enabled);
167 label.setEnabled(enabled);
172 * To set the Background color of the element.
173 * @param color the Color
175 public void setBackground(Color color) {
176 super.setBackground(color);
177 if (alignmentPanel != null) {
178 alignmentPanel.setBackground(color);
181 if (label instanceof JLabel) {
182 ((JLabel) label).setOpaque(true);
184 label.setBackground(color);
189 * Draws a swing Scilab PushButton
190 * @see org.scilab.modules.gui.uielement.UIElement#draw()
193 this.setVisible(true);
198 * Gets the dimensions (width and height) of a swing Scilab element
199 * @return the dimensions of the element
200 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
202 public Size getDims() {
203 return new Size(getWidth(), getHeight());
207 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab
209 * @return the position of the element
210 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
212 public Position getPosition() {
213 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
217 * Sets the dimensions (width and height) of a swing Scilab element
218 * @param newSize the dimensions to set to the element
219 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
221 public void setDims(Size newSize) {
222 setSize(newSize.getWidth(), newSize.getHeight());
223 // Need validate to force alignement to be applyed
224 setMinimumSize(new Dimension(Math.max((int) label.getMinimumSize().getWidth(), newSize.getWidth()), (int) label.getMinimumSize().getHeight()));
228 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab
230 * @param newPosition the position to set to the element
231 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
233 public void setPosition(Position newPosition) {
234 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
235 setLocation(javaPosition.getX(), javaPosition.getY());
239 * Add a callback to the Label
240 * @param callback the callback to set.
242 public void setCallback(CommonCallBack callback) {
248 * @param menuBarToAdd the MenuBar associated to the Label.
250 public void addMenuBar(MenuBar menuBarToAdd) {
251 /* Unimplemented for Labels */
252 throw new UnsupportedOperationException();
257 * @param toolBarToAdd the ToolBar associated to the Label.
259 public void addToolBar(ToolBar toolBarToAdd) {
260 /* Unimplemented for Labels */
261 throw new UnsupportedOperationException();
266 * @return MenuBar: the MenuBar associated to the Label.
268 public MenuBar getMenuBar() {
269 /* Unimplemented for Labels */
270 throw new UnsupportedOperationException();
275 * @return ToolBar: the ToolBar associated to the Label.
277 public ToolBar getToolBar() {
278 /* Unimplemented for Labels */
279 throw new UnsupportedOperationException();
283 * Set the horizontal alignment for the Label text
284 * @param alignment the value for the alignment (See ScilabAlignment.java)
286 public void setHorizontalAlignment(String alignment) {
287 horizontalAlignment = alignment;
292 * Set the vertical alignment for the Label text
293 * @param alignment the value for the alignment (See ScilabAlignment.java)
295 public void setVerticalAlignment(String alignment) {
296 verticalAlignment = alignment;
302 * Set the Relief of the Label
303 * @param reliefType the type of the relief to set (See ScilabRelief.java)
305 public void setRelief(String reliefType) {
306 if (defaultBorder == null) {
307 defaultBorder = getBorder();
309 setBorder(ScilabRelief.getBorderFromRelief(reliefType, defaultBorder));
315 public void destroy() {
316 ScilabSwingUtilities.removeFromParent(this);
321 * @param infoBarToAdd the InfoBar associated to the Label.
323 public void addInfoBar(TextBox infoBarToAdd) {
324 /* Unimplemented for Labels */
325 throw new UnsupportedOperationException();
330 * @return the InfoBar associated to the Label.
332 public TextBox getInfoBar() {
333 /* Unimplemented for Labels */
334 throw new UnsupportedOperationException();
338 * get the text displayed in the label
340 * @see org.scilab.modules.gui.text.SimpleText#getText()
342 public String getText() {
347 * Set the text displayed in the label
348 * @param newText the text
349 * @see org.scilab.modules.gui.text.SimpleText#setText(java.lang.String)
351 public void setText(String newText) {
352 // Save the data given by the user so that it can be retrieved
353 // (Java adds HTML tags in the getlabel().getText() returned value)
355 // <math></math> : LateXLabel ( MathML )
356 // <html></html> : JTextPane for clickable links
357 // $...$ : LateXLabel ( LateX )
361 if (labelText.startsWith("<math>") && labelText.endsWith("</math>")) {
362 boolean mathML = ScilabSpecialTextUtilities.setText(new JLabel(), labelText);
364 //if MAthML rendering failed use normal renderer ( JLabel)
366 changeLabelType(LabelStyle.MATHML);
367 ((IconLabel) label).setText(labelText);
369 changeLabelType(LabelStyle.TEXT);
370 ((JLabel) label).setText(labelText);
375 if (labelText.startsWith("<html>") && labelText.endsWith("</html>")) {
376 changeLabelType(LabelStyle.HTML);
377 ((JEditorPane) label).setText(labelText);
381 if (labelText.startsWith("$") && labelText.endsWith("$")) {
382 boolean latex = ScilabSpecialTextUtilities.setText(new JLabel(), labelText);
384 //if MAthML rendering failed use normal renderer ( JLabel)
386 changeLabelType(LabelStyle.LATEX);
387 ((IconLabel) label).setText(labelText);
389 changeLabelType(LabelStyle.TEXT);
390 ((JLabel) label).setText(labelText);
395 changeLabelType(LabelStyle.TEXT);
396 ((JLabel) label).setText(labelText);
398 ((JLabel) label).invalidate();
399 ((JLabel) label).validate();
400 ((JLabel) label).repaint();
404 * Change Label type to switch between JLabel abd JEditorPane JLabel is
405 * quicker on simple text JEditorPane can enable HyperLinks
408 private void changeLabelType(LabelStyle style) {
410 if (labelStyle == style) {
415 Color bgColor = label.getBackground();
416 Color fgColor = label.getForeground();
417 Font font = label.getFont();
418 Dimension dims = label.getSize();
419 label.setVisible(false);
421 switch (labelStyle) {
423 alignmentPanel.remove(label);
424 label = new IconLabel(IconType.LATEX);
425 setViewportView(label);
428 alignmentPanel.remove(label);
429 label = new IconLabel(IconType.MATHML);
430 setViewportView(label);
433 JTextPane newLabel = new JTextPane();
434 newLabel.addHyperlinkListener(urlOpener);
435 newLabel.setContentType("text/html");
436 newLabel.setEditable(false);
437 StyleSheet styleSheet = ((HTMLDocument) newLabel.getDocument()).getStyleSheet();
438 styleSheet.addRule("body {font-family:" + font.getName() + ";}");
439 styleSheet.addRule("body {font-size:" + font.getSize() + "pt;}");
441 styleSheet.addRule("body {font-weight:bold;}");
443 styleSheet.addRule("body {font-weight:normal;}");
445 if (font.isItalic()) {
446 styleSheet.addRule("body {font-style:italic;}");
448 styleSheet.addRule("body {font-style:normal;}");
452 alignmentPanel.add(label);
453 setViewportView(alignmentPanel);
457 alignmentPanel.remove(label);
458 label = new JLabel();
459 setViewportView(label);
461 update(__GO_UI_ICON__, GraphicController.getController().getProperty(getId(), __GO_UI_ICON__));
462 ((JLabel) label).setOpaque(false);
467 setBackground(bgColor);
468 label.setForeground(fgColor);
471 label.setVisible(true);
476 * Set alignment of the text component
478 private void setAlignment() {
480 if (labelStyle != LabelStyle.HTML) {
481 ((JLabel) label).setVerticalAlignment(ScilabAlignment.toSwingAlignment(verticalAlignment));
482 ((JLabel) label).setHorizontalAlignment(ScilabAlignment.toSwingAlignment(horizontalAlignment));
486 GridBagConstraints gbc = new GridBagConstraints();
495 switch (ScilabAlignment.toSwingAlignment(horizontalAlignment)) {
496 case SwingConstants.LEFT:
497 switch (ScilabAlignment.toSwingAlignment(verticalAlignment)) {
498 case SwingConstants.TOP:
499 gbc.anchor = GridBagConstraints.NORTHWEST;
501 case SwingConstants.CENTER:
502 gbc.anchor = GridBagConstraints.WEST;
504 default: // SwingConstants.BOTTOM
505 gbc.anchor = GridBagConstraints.SOUTHWEST;
509 case SwingConstants.CENTER:
510 switch (ScilabAlignment.toSwingAlignment(verticalAlignment)) {
511 case SwingConstants.TOP:
512 gbc.anchor = GridBagConstraints.NORTH;
514 case SwingConstants.CENTER:
515 gbc.anchor = GridBagConstraints.CENTER;
517 default: // SwingConstants.BOTTOM
518 gbc.anchor = GridBagConstraints.SOUTH;
522 default: // SwingConstants.RIGHT
523 switch (ScilabAlignment.toSwingAlignment(verticalAlignment)) {
524 case SwingConstants.TOP:
525 gbc.anchor = GridBagConstraints.NORTHEAST;
527 case SwingConstants.CENTER:
528 gbc.anchor = GridBagConstraints.EAST;
530 default: // SwingConstants.BOTTOM
531 gbc.anchor = GridBagConstraints.SOUTHEAST;
537 alignmentLayout.setConstraints(label, gbc);
544 public void setId(Integer id) {
552 public Integer getId() {
557 * Generic update method
558 * @param property property name
559 * @param value property value
561 public void update(int property, Object value) {
563 case __GO_UI_ICON__: {
564 if (labelStyle == LabelStyle.TEXT) {
565 String icon = (String) value;
566 if (icon != null && icon.equals("") == false) {
567 File file = new File((String) value);
568 if (file.exists() == false) {
569 String filename = FindIconHelper.findImage((String) value);
570 file = new File(filename);
574 ((JLabel) label).setIcon(new ImageIcon(ImageIO.read(file)));
575 } catch (IOException e) {
578 ((JLabel) label).setIcon(null);
584 SwingViewWidget.update(this, property, value);
589 public void resetBackground() {
590 Color color = (Color) UIManager.getLookAndFeelDefaults().get("Label.background");
592 //bypass setBackground
593 if (label instanceof JLabel) {
594 ((JLabel) label).setOpaque(true);
596 label.setBackground(color);
600 public void resetForeground() {
601 Color color = (Color) UIManager.getLookAndFeelDefaults().get("Label.foreground");
603 label.setForeground(color);
608 * Component to display LaTeX Thank you Calixte
610 public static class IconLabel extends JLabel {
611 public enum IconType {
615 private static final long serialVersionUID = 3885565301114930030L;
616 private final static Font DEFAULTFONT = new Font("serif", Font.PLAIN, 12);
617 private final static boolean isWindows = System.getProperty("os.name").toLowerCase().indexOf("win") != -1;
620 private Icon iconDisable;
621 private float fontSize;
623 private Dimension preferred;
624 IconType type = IconType.MATHML;
626 public IconLabel(IconType type) {
631 preferred = new Dimension(0, 0);
636 * Set the LaTeX content
637 * @param text the LaTeX
639 public void setText(String text) {
645 * Get the LaTeX content
646 * @return the LaTeX string
648 public String getText() {
652 public Dimension getPreferredSize() {
656 public Dimension getMinimumSize() {
657 return getPreferredSize();
660 protected void paintComponent(Graphics g) {
661 super.paintComponent(g);
662 if (isOpaque() && getBackground() != null) {
663 final Color old = g.getColor();
664 g.setColor(getBackground());
665 g.fillRect(0, 0, getWidth(), getHeight());
669 int h = getHorizontalAlignment();
670 int v = getVerticalAlignment();
675 if (h == JLabel.CENTER) {
676 x = (getSize().width - icon.getIconWidth()) / 2;
677 } else if (h == JLabel.RIGHT) {
678 x = getSize().width - icon.getIconWidth();
681 if (v == JLabel.CENTER) {
682 y = (getSize().height - icon.getIconHeight()) / 2;
683 } else if (v == JLabel.BOTTOM) {
684 y = getSize().height - icon.getIconHeight();
687 if (type == IconType.LATEX) {
688 TeXIcon latex = (TeXIcon) icon;
690 latex.setForeground(getForeground());
691 latex.paintIcon(this, g, x, y);
693 if (isWindows && (UIManager.getColor("Label.disabledForeground") instanceof Color) && (UIManager.getColor("Label.disabledShadow") instanceof Color)) {
694 latex.setForeground(UIManager.getColor("Label.disabledShadow"));
695 latex.paintIcon(this, g, x + 1, y + 1);
696 latex.setForeground(UIManager.getColor("Label.disabledForeground"));
697 latex.paintIcon(this, g, x, y);
699 latex.setForeground(getBackground().brighter());
700 latex.paintIcon(this, g, x + 1, y + 1);
701 latex.setForeground(getBackground().darker());
702 latex.paintIcon(this, g, x, y);
706 //enable/disable is made @ icon generation in ScilabSpecialTextUtilities
708 icon.paintIcon(this, g, x, y);
710 icon.paintIcon(this, g, x + 1, y + 1);
711 iconDisable.paintIcon(this, g, x, y);
716 public void setFont(Font f) {
717 this.fontSize = f.getSize2D();
721 public Font getFont() {
726 * Update the component
728 private void update() {
729 if (text.equals("") == false) {
730 if (type == IconType.LATEX) {
731 icon = ScilabSpecialTextUtilities.compileLaTeXExpression(text, (int) fontSize);
734 icon = ScilabSpecialTextUtilities.compileMathMLExpression(text, (int) fontSize);
736 if (isWindows && (UIManager.getColor("Label.disabledForeground") instanceof Color) && (UIManager.getColor("Label.disabledShadow") instanceof Color)) {
737 icon = ScilabSpecialTextUtilities.compileMathMLExpression(text, (int) fontSize, UIManager.getColor("Label.disabledShadow"));
738 iconDisable = ScilabSpecialTextUtilities.compileMathMLExpression(text, (int) fontSize, UIManager.getColor("Label.disabledForeground"));
740 icon = ScilabSpecialTextUtilities.compileMathMLExpression(text, (int) fontSize, getBackground().brighter());
741 iconDisable = ScilabSpecialTextUtilities.compileMathMLExpression(text, (int) fontSize, getBackground().darker());
745 preferred = new Dimension(icon.getIconWidth(), icon.getIconHeight());
750 public void setEnabled(boolean enabled) {
751 super.setEnabled(enabled);