1 /* 2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 3 * Copyright (C) 2011 - Pierre GRADIT 4 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET 5 * 6 * 7 * Copyright (C) 2012 - 2016 - Scilab Enterprises 8 * 9 * This file is hereby licensed under the terms of the GNU GPL v2.0, 10 * pursuant to article 5.3.4 of the CeCILL v.2.1. 11 * This file was originally licensed under the terms of the CeCILL v2.1, 12 * and continues to be available under such terms. 13 * For more information, see the COPYING file which you should have received 14 * along with this program. 15 * 16 */ 17 18 package org.scilab.modules.preferences.Component; 19 20 import java.awt.Component; 21 import java.awt.Dimension; 22 import java.awt.Graphics; 23 import java.awt.event.ActionEvent; 24 import java.awt.event.ActionListener; 25 26 import javax.swing.Icon; 27 import javax.swing.JButton; 28 import javax.swing.JFrame; 29 import javax.swing.SwingUtilities; 30 31 import org.w3c.dom.Node; 32 33 import org.scilab.modules.gui.bridge.colorchooser.SwingScilabColorChooser; 34 import org.scilab.modules.preferences.XChooser; 35 import org.scilab.modules.preferences.XComponent; 36 import org.scilab.modules.preferences.XCommonManager; 37 import org.scilab.modules.preferences.XConfigManager; 38 import org.scilab.modules.preferences.XSentinel; 39 40 /** 41 * Implementation of Label compliant with extended management. 42 * 43 * @author Pierre GRADIT 44 * @author Calixte DENIZET 45 */ 46 public class Color extends JButton implements XComponent, XChooser { 47 48 /** 49 * 50 */ 51 private static final long serialVersionUID = 5598263085800128888L; 52 private static final int ICONDIM = 16; 53 54 private ActionListener actionListener = null; 55 private SwingScilabColorChooser colorChooser; 56 57 /** 58 * Define the set of actuators. 59 * @return array of actuator names. 60 */ actuators()61 public final String[] actuators() { 62 String[] actuators = {"enable", "color"}; 63 return actuators; 64 } 65 66 /** 67 * Constructor. 68 * @param peer : associated view DOM node. 69 */ Color(final Node peer)70 public Color(final Node peer) { 71 super(new Icon() { 72 public final int getIconHeight() { 73 return ICONDIM; 74 } 75 76 public final int getIconWidth() { 77 return ICONDIM * 2; 78 } 79 80 public void paintIcon(Component c, Graphics g, int x, int y) { 81 if (c.isEnabled()) { 82 g.setColor(c.getForeground()); 83 g.fillRect(x, y, getIconWidth() - 1, getIconHeight() - 1); 84 g.setColor(java.awt.Color.BLACK); 85 g.drawRect(x, y, getIconWidth() - 1, getIconHeight() - 1); 86 } else { 87 java.awt.Color color = c.getForeground(); 88 float hsb[] = new float[3]; 89 java.awt.Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb); 90 g.setColor(java.awt.Color.getHSBColor(hsb[0], hsb[1] * 0.2f, hsb[2] * 0.95f)); 91 g.fillRect(x, y, getIconWidth(), getIconHeight()); 92 } 93 } 94 }); 95 96 String color = XCommonManager.getAttribute(peer , "color", "000000"); 97 color(color); 98 setOpaque(true); 99 addActionListener(new ActionListener() { 100 public void actionPerformed(ActionEvent e) { 101 java.awt.Color jColor = XCommonManager.getColor(color()); 102 ActionEvent transmit = new ActionEvent(Color.this, e.getID(), "Color change", e.getWhen() + 1, e.getModifiers()); 103 colorChooser = new SwingScilabColorChooser(jColor); 104 JFrame frame = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, Color.this); 105 colorChooser.setLocationRelativeTo(frame); 106 colorChooser.displayAndWait(); 107 if (actionListener != null) { 108 actionListener.actionPerformed(transmit); 109 } 110 } 111 }); 112 113 setRequestFocusEnabled(true); 114 setFocusable(true); 115 116 String enable = XConfigManager.getAttribute(peer , "enable", "true"); 117 setEnabled(enable.equals("true")); 118 } 119 120 /** 121 * Refresh the component by the use of actuators. 122 * @param peer the corresponding view DOM node 123 */ refresh(final Node peer)124 public final void refresh(final Node peer) { 125 String color = XCommonManager.getAttribute(peer , "color", "000000"); 126 if (!color.equals(color())) { 127 color(color); 128 } 129 130 String enable = XConfigManager.getAttribute(peer , "enable", "true"); 131 setEnabled(enable.equals("true")); 132 } 133 134 /** 135 * Sensor for 'color' attribute. 136 * @return the attribute value. 137 */ color()138 public final String color() { 139 java.awt.Color color = getForeground(); 140 return XCommonManager.getColor(color); 141 } 142 143 /** 144 * Actuator for 'color' attribute. 145 * @param text : the attribute value. 146 */ color(final String color)147 public final void color(final String color) { 148 java.awt.Color jColor = XCommonManager.getColor(color); 149 setForeground(jColor); 150 } 151 152 /** 153 * Registration of a single listener. 154 * @param listener 155 */ addActionListener(ActionListener listener)156 public void addActionListener(ActionListener listener) { 157 if (listener instanceof XSentinel) { 158 actionListener = listener; 159 } else { 160 super.addActionListener(listener); 161 } 162 } 163 164 /** 165 * External consultation 166 */ choose()167 public Object choose() { 168 java.awt.Color jColor = colorChooser.getSelectedColor(); 169 if (jColor != null) { 170 return XCommonManager.getColor(jColor); 171 } else { 172 return color(); 173 } 174 } 175 176 /** 177 * Developer serialization method. 178 * @return equivalent signature. 179 */ toString()180 public final String toString() { 181 String signature = "Color"; 182 signature += " color='" + color() + "'"; 183 return signature; 184 } 185 } 186 187