~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/common/org/crosswire/common/config/swing/fields/ColorField.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.common.config.swing.fields;
3
 
 
4
 
import java.awt.BorderLayout;
5
 
import java.awt.Color;
6
 
import java.awt.Component;
7
 
import java.awt.Graphics;
8
 
import java.awt.Insets;
9
 
import java.awt.event.ActionEvent;
10
 
import java.awt.event.ActionListener;
11
 
 
12
 
import javax.swing.Icon;
13
 
import javax.swing.JButton;
14
 
import javax.swing.JColorChooser;
15
 
import javax.swing.JComponent;
16
 
import javax.swing.JPanel;
17
 
 
18
 
import org.crosswire.common.config.swing.Field;
19
 
import org.crosswire.common.swing.GuiConvert;
20
 
 
21
 
/**
22
 
* A Filename selection.
23
 
*
24
 
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
25
 
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
26
 
* Distribution Licence:<br />
27
 
* Project B is free software; you can redistribute it
28
 
* and/or modify it under the terms of the GNU General Public License,
29
 
* version 2 as published by the Free Software Foundation.<br />
30
 
* This program is distributed in the hope that it will be useful,
31
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33
 
* General Public License for more details.<br />
34
 
* The License is available on the internet
35
 
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
36
 
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37
 
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
38
 
* The copyright to this program is held by it's authors.
39
 
* </font></td></tr></table>
40
 
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
41
 
* @see <{docs.Licence}>
42
 
* @author Joe Walker
43
 
*/
44
 
public class ColorField extends JPanel implements Field
45
 
{
46
 
    /**
47
 
    * Create a new FileField
48
 
    */
49
 
    public ColorField()
50
 
    {
51
 
        edit.setIcon(new CustomIcon());
52
 
        edit.setMargin(new Insets(1, 2, 1, 1));
53
 
        edit.addActionListener(new ActionListener()
54
 
        {
55
 
            public void actionPerformed(ActionEvent ex)
56
 
            {
57
 
                color = JColorChooser.showDialog(ColorField.this, name, color);
58
 
                //text.setText(Convert.color2String(color));
59
 
                edit.repaint();
60
 
            }
61
 
        });
62
 
 
63
 
        setLayout(new BorderLayout());
64
 
        add(edit, BorderLayout.WEST);
65
 
        //add(text, BorderLayout.EAST);
66
 
    }
67
 
 
68
 
    /**
69
 
    * Some fields will need some extra info to display properly
70
 
    * like the options in an options field. FieldMap calls this
71
 
    * method with options provided by the choice.
72
 
    * @param param The options provided by the Choice
73
 
    */
74
 
    public void setOptions(Object param)
75
 
    {
76
 
        name = (String) param;
77
 
        edit.setText(name);
78
 
    }
79
 
 
80
 
    /**
81
 
    * Return a string version of the current value
82
 
    * @return The current value
83
 
    */
84
 
    public String getValue()
85
 
    {
86
 
        return GuiConvert.color2String(color);
87
 
    }
88
 
 
89
 
    /**
90
 
    * Set the current value
91
 
    * @param value The new text
92
 
    */
93
 
    public void setValue(String value)
94
 
    {
95
 
        color = GuiConvert.string2Color(value);
96
 
        //text.setText(value);
97
 
        edit.repaint();
98
 
    }
99
 
 
100
 
    /**
101
 
    * Get the actual component that we can add to a Panel.
102
 
    * (This can well be this in an implementation).
103
 
    */
104
 
    public JComponent getComponent()
105
 
    {
106
 
        return this;
107
 
    }
108
 
 
109
 
    /** The name of the Color selection */
110
 
    protected String name = "Edit";
111
 
 
112
 
    /** What to do when we are clicked */
113
 
    private Runnable runner;
114
 
 
115
 
    /** The browse button */
116
 
    protected JButton edit = new JButton(name);
117
 
 
118
 
    /** Some feedback on the color */
119
 
    //private JLabel text = new JLabel();
120
 
 
121
 
    /** The current Color */
122
 
    protected Color color = Color.white;
123
 
 
124
 
    /** The icon square size */
125
 
    private static final int SIZE = 16;
126
 
 
127
 
    /**
128
 
    * The CustomIcon that shows the selected color
129
 
    */
130
 
    class CustomIcon implements Icon
131
 
    {
132
 
        /**
133
 
        * Returns the icon's height.
134
 
        */
135
 
        public int getIconHeight()
136
 
        {
137
 
            return SIZE;
138
 
        }
139
 
 
140
 
        /**
141
 
        * Returns the icon's width.
142
 
        */
143
 
        public int getIconWidth()
144
 
        {
145
 
            return SIZE;
146
 
        }
147
 
 
148
 
        /**
149
 
        * Draw the icon at the specified location.
150
 
        */
151
 
        public void paintIcon(Component c, Graphics g, int x, int y)
152
 
        {
153
 
            if (color == null)
154
 
            {
155
 
                g.setColor(Color.black);
156
 
                g.drawRect(x, y, SIZE, SIZE);
157
 
                g.drawLine(x, y, x+SIZE, y+SIZE);
158
 
                g.drawLine(x+SIZE, y, x, y+SIZE);
159
 
            }
160
 
            else
161
 
            {
162
 
                g.setColor(color);
163
 
                g.fillRect(x, y, SIZE, SIZE);
164
 
                g.setColor(Color.black);
165
 
                g.drawRect(x, y, SIZE, SIZE);
166
 
            }
167
 
        }
168
 
    }
169
 
}
170