source: 3thparty/jmessenger/src/nu/fw/jeti/plugins/xhtml/fontchooser/ColorPanel.java @ 3952

Revision 3952, 9.6 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1710 - Adicao do codigo fonte java do componente jmessenger(jabberit_messenger)

  • Property svn:executable set to *
Line 
1/*
2 * SimplyHTML, a word processor based on Java, HTML and CSS
3 * Copyright (C) 2002 Ulrich Hilger
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20package nu.fw.jeti.plugins.xhtml.fontchooser;
21
22import java.awt.BorderLayout;
23import java.awt.Color;
24import java.awt.Dimension;
25import java.awt.FlowLayout;
26import java.awt.event.ActionEvent;
27import java.awt.event.ActionListener;
28import java.util.Enumeration;
29import java.util.EventListener;
30import java.util.EventObject;
31import java.util.Vector;
32
33import javax.swing.*;
34import javax.swing.text.AttributeSet;
35import javax.swing.text.SimpleAttributeSet;
36import javax.swing.text.StyleConstants;
37
38import nu.fw.jeti.util.I18N;
39
40/**
41 * a panel to display and change a color setting
42 *
43 * @author Ulrich Hilger
44 * @author Light Development
45 * @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
46 * @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
47 * @author published under the terms and conditions of the
48 *      GNU General Public License,
49 *      for details see file gpl.txt in the distribution
50 *      package of this software
51 *
52 * @version stage 9, release 4, January 12, 2003
53 */
54public class ColorPanel extends JPanel implements ActionListener, AttributeComponent
55{
56
57        /** the component showing the chosen color */
58        JTextField colorDisplay = new JTextField();
59
60        /** default color */
61        private Color defaultColor;
62
63        /** the attribute key, this component returns values for */
64        private Object attributeKey;
65
66        /** value to compare for determining changes */
67        private Color originalColor;
68
69        /** indicates if setValue is called initially */
70        private int setValCount = 0;
71
72        /**
73         * construct a color panel
74         *
75         * @param title  the title of the color panel
76         * @param col  the color to be displayed first
77         * @param titleFont  font for title
78         * @param key  the attribute key this component shall return color values for
79         */
80        public ColorPanel(String title, Color col)
81        {
82                super(new BorderLayout(5, 5));
83
84                this.defaultColor = col;
85                this.attributeKey = title;
86
87                /** adjust the color display */
88                colorDisplay.setBackground(col);
89                Dimension dim = new Dimension(20, 15);
90                colorDisplay.setMinimumSize(dim);
91                colorDisplay.setMaximumSize(dim);
92                colorDisplay.setPreferredSize(dim);
93                colorDisplay.setEditable(false);
94
95                /** a button to open a color chooser window */
96                JButton browseButton = new JButton();
97                browseButton.setText("...");
98                dim = new Dimension(20, 15);
99                browseButton.setMinimumSize(dim);
100                browseButton.setMaximumSize(dim);
101                browseButton.setPreferredSize(dim);
102                browseButton.addActionListener(this);
103
104                /** a helper panel for proper component placement */
105                JPanel eastPanel = new JPanel(new FlowLayout());
106                eastPanel.add(colorDisplay);
107                eastPanel.add(browseButton);
108
109                /** set the title */
110                if ((title != null) && (title.length() > 0))
111                {
112                        JLabel titleLabel = new JLabel(title);
113                        titleLabel.setFont(UIManager.getFont("TextField.font"));
114                        add(titleLabel, BorderLayout.WEST);
115                        add(eastPanel, BorderLayout.EAST);
116                }
117                else
118                {
119                        add(eastPanel, BorderLayout.WEST);
120                }
121
122        }
123
124        /**
125         * get the selected color
126         *
127         * @return the selected color
128         */
129        public Color getColor()
130        {
131                return colorDisplay.getBackground();
132        }
133
134        /**
135         * set the selected color
136         *
137         * @param color the selected color
138         */
139        private void setColor(Color color)
140        {
141                //System.out.println("ColorPanel setColor attributeKey=" + attributeKey + ", color=" + color);
142                colorDisplay.setBackground(color);
143                if (++setValCount < 2)
144                {
145                        originalColor = color;
146                }
147                fireColorChanged();
148        }
149
150        /**
151         * open a color chooser when a 'Browse' button
152         * is clicked and change the associated color
153         * display accordingly, when another color
154         * is selected from the color chooser
155         */
156        public void actionPerformed(ActionEvent e)
157        {
158                Color color = JColorChooser.showDialog(this, I18N.gettext("xhtml.Select_Color"), colorDisplay.getBackground());
159                if (color != null)
160                {
161                        setColor(color);
162                }
163        }
164
165        /**
166         * set the value of this <code>AttributeComponent</code>
167         *
168         * @param a  the set of attributes possibly having an
169         *          attribute this component can display
170         *
171         * @return true, if the set of attributes had a matching attribute,
172         *            false if not
173         */
174        public boolean setValue(AttributeSet a)
175        {
176                //System.out.println(a);
177                boolean success = false;
178                if (defaultColor == Color.black)
179                {
180                        //if( a.isDefined(StyleConstants.Foreground)) {
181                        Color c = StyleConstants.getForeground(a);
182                        if (c != null)
183                        {
184                                //System.out.println("ColorPanel setValue attributeKey=" + attributeKey + ", a=" + a.getAttribute(attributeKey));
185                                //String value = a.getAttribute(getAttributeKey()).toString();
186                                //setValue(value);
187                                setColor(c);
188                                success = true;
189                        }
190                        else
191                                setColor(defaultColor);
192                }
193                else
194                {
195                        Color c = StyleConstants.getBackground(a);
196                        if (c != null)
197                        {
198                                //if( a.isDefined(StyleConstants.Background)) {
199                                //System.out.println("ColorPanel setValue attributeKey=" + attributeKey + ", a=" + a.getAttribute(attributeKey));
200                                //String value = a.getAttribute(getAttributeKey()).toString();
201                                //setValue(value);
202                                setColor(c);
203                                success = true;
204                        }
205                        else
206                                setColor(defaultColor);
207                }
208
209                return success;
210        }
211
212        //  public void setValue(String value) {
213        //    //System.out.println("ColorPanel setValue value=" + value);
214        //    try {
215        //      setColor(new Color(Integer.parseInt(
216        //          value.toString().substring(1).toUpperCase(), 16)));
217        //    }
218        //    catch(Exception e) {
219        //      try {
220        //        //setColor(Util.styleSheet().getForeground(a));
221        //        //System.out.println("ColorPanel setValue value=" + value + "=" + Color.getColor(value));
222        //        setColor(Color.getColor(value));
223        //      }
224        //      catch(Exception e2) {
225        //        Util.errMsg(null, null, e2);
226        //      }
227        //    }
228        //  }
229
230        public String getAttr()
231        {
232                String color = "#" + Integer.toHexString(getColor().getRGB()).substring(2);
233                return color;
234        }
235
236        /**
237         * get the value of this <code>AttributeComponent</code>
238         *
239         * @return the value selected from this component
240         */
241        public AttributeSet getValue()
242        {
243                SimpleAttributeSet set = new SimpleAttributeSet();
244                Color value = getColor();
245                if (value != originalColor)
246                {
247                        //      String color = "#" + Integer.toHexString(
248                        //          value.getRGB()).substring(2);
249                        //      try {
250                        //        Util.styleSheet().addCSSAttribute(set,
251                        //            (CSS.Attribute) getAttributeKey(), color);
252                        //      }
253                        //      catch(Exception e) {
254                        //        set.addAttribute(getAttributeKey(), color);
255                        //      }
256                        //if (attributeKey.equals("foreground"))        StyleConstants.setForeground(set, value);
257                        if(defaultColor==Color.BLACK) StyleConstants.setForeground(set, value);
258                        else StyleConstants.setBackground(set, value);
259                        //System.out.println("ColorPanel getValue color=" + color);
260                }
261                return set;
262        }
263
264        public AttributeSet getValue(boolean includeUnchanged)
265        {
266                if (includeUnchanged)
267                {
268                        SimpleAttributeSet set = new SimpleAttributeSet();
269                        Color value = getColor();
270                        //      String color = "#" + Integer.toHexString(
271                        //          value.getRGB()).substring(2);
272                        //      try {
273                        //        Util.styleSheet().addCSSAttribute(set,
274                        //            (CSS.Attribute) getAttributeKey(), color);
275                        //      }
276                        //      catch(Exception e) {
277                        //        set.addAttribute(getAttributeKey(), color);
278                        //      }
279                        //if (attributeKey.equals("foreground"))
280                        if(defaultColor==Color.BLACK) StyleConstants.setForeground(set, value);
281                        else StyleConstants.setBackground(set, value);
282
283                        //System.out.println("ColorPanel getValue color=" + color);
284                        return set;
285                }
286                else
287                {
288                        return getValue();
289                }
290        }
291
292        /**
293         * get the attribute key this object was created for
294         *
295         * @return the attribute key this ColorPanel returns values for
296         */
297        public Object getAttributeKey()
298        {
299                return attributeKey;
300        }
301
302        /* -------------- event listener implementation start ----------- */
303
304        /** the listeners for ColorPanelEvents */
305        private Vector listeners = new Vector(0);
306
307        /**
308         * add an event listener.
309         *
310         * @param  listener  the event listener to add
311         */
312        public void addColorPanelListener(ColorPanelListener listener)
313        {
314                listeners.addElement(listener);
315        }
316
317        /**
318         * remove an event listener.
319         *
320         * @param  listener  the event listener to remove
321         */
322        public void removeColorPanelListener(ColorPanelListener listener)
323        {
324                listeners.removeElement(listener);
325        }
326
327        /** fire a color changed event to all registered listeners */
328        void fireColorChanged()
329        {
330                Enumeration listenerList = listeners.elements();
331                while (listenerList.hasMoreElements())
332                {
333                        ((ColorPanelListener) listenerList.nextElement()).colorChanged(new ColorPanelEvent(this));
334                }
335        }
336
337        /** the event object definition for ColorPanels */
338        static class ColorPanelEvent extends EventObject
339        {
340                public ColorPanelEvent(Object source)
341                {
342                        super(source);
343                }
344        }
345
346        /** the event listener definition for ColorPanels */
347        interface ColorPanelListener extends EventListener
348        {
349                public void colorChanged(ColorPanelEvent e);
350        }
351
352        /* -------------- event listener implementation end ----------- */
353
354}
355/*
356 * Overrides for emacs
357 * Local variables:
358 * tab-width: 4
359 * End:
360 */
Note: See TracBrowser for help on using the repository browser.