]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/gui/pixx/AWTStyleSelector.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / gui / pixx / AWTStyleSelector.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2004 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
10 /*  PJIRC is free software; you can redistribute     */
11 /*  it and/or modify it under the terms of the GNU   */
12 /*  General Public License as published by the       */
13 /*  Free Software Foundation; version 2 or later of  */
14 /*  the License.                                     */
15 /*                                                   */
16 /*  PJIRC is distributed in the hope that it will    */
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */
18 /*  even the implied warranty of MERCHANTABILITY or  */
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */
20 /*  General Public License for more details.         */
21 /*                                                   */
22 /*  You should have received a copy of the GNU       */
23 /*  General Public License along with PJIRC; if      */
24 /*  not, write to the Free Software Foundation,      */
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */
26 /*  MA  02111-1307  USA                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc.gui.pixx;
31
32 import irc.StyleContext;
33
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.Graphics;
37 import java.awt.Image;
38 import java.awt.Panel;
39 import java.awt.event.InputEvent;
40 import java.awt.event.MouseEvent;
41 import java.awt.event.MouseListener;
42
43 /**
44  * The style selector.
45  */
46 public class AWTStyleSelector extends Panel implements MouseListener {
47         private PixxConfiguration _pixxConfiguration;
48         private StyleContext _ct;
49         private int _color;
50         private int _backColor;
51         private boolean _bold;
52         private boolean _underline;
53
54         /**
55          * Create a new AWTStyleSelector.
56          * 
57          * @param config
58          *          global irc configuration.
59          */
60         public AWTStyleSelector(PixxConfiguration config) {
61                 super();
62                 _pixxConfiguration = config;
63                 _color = 1;
64                 _backColor = 0;
65                 _bold = false;
66                 _underline = false;
67                 addMouseListener(this);
68                 _ct = _pixxConfiguration.getIRCConfiguration().getDefaultStyleContext();
69         }
70
71         /**
72          * Release this object.
73          */
74         public void release() {
75                 removeMouseListener(this);
76                 _pixxConfiguration = null;
77         }
78
79         /**
80          * Set the style context.
81          * 
82          * @param ct
83          *          style context.
84          */
85         public void setStyleContext(StyleContext ct) {
86                 _ct = ct;
87         }
88
89         @Override
90         public void paint(Graphics g) {
91                 update(g);
92         }
93
94         private void drawSelect(Graphics g, int i, int j) {
95                 int x = (int) (i * getItemWidth());
96                 int y = (int) (j * getItemHeight());
97                 int w = (int) getItemWidth();
98                 int h = (int) getItemHeight();
99                 g.setColor(Color.black);
100                 g.drawRect(x + 1, y + 1, w - 2, h - 2);
101                 g.setColor(Color.white);
102                 g.drawRect(x, y, w - 2, h - 2);
103         }
104
105         private void drawColor(Graphics g, int i, int j, Color c) {
106                 int x = (int) (i * getItemWidth());
107                 int y = (int) (j * getItemHeight());
108                 int w = (int) getItemWidth();
109                 int h = (int) getItemHeight();
110                 g.setColor(c);
111                 g.fillRect(x, y, w, h);
112         }
113
114         private void drawBold(Graphics g, int i, int j) {
115                 int x = (int) (i * getItemWidth());
116                 int y = (int) (j * getItemHeight());
117                 int w = (int) getItemWidth();
118                 int h = (int) getItemHeight();
119                 int tw = g.getFontMetrics().stringWidth("a");
120                 g.drawString("a", x + (w - tw) / 2, y + h - (h - g.getFont().getSize()) / 2 - 2);
121                 g.drawString("a", x + (w - tw) / 2 + 1, y + h - (h - g.getFont().getSize()) / 2 - 2);
122         }
123
124         private void drawUnderline(Graphics g, int i, int j) {
125                 int x = (int) (i * getItemWidth());
126                 int y = (int) (j * getItemHeight());
127                 int w = (int) getItemWidth();
128                 int h = (int) getItemHeight();
129                 int tw = g.getFontMetrics().stringWidth("a");
130                 g.drawString("a", x + (w - tw) / 2, y + h - (h - g.getFont().getSize()) / 2 - 2);
131
132                 g.drawLine(x + w / 2 - tw / 2, y + h - 1, x + w / 2 + tw / 2, y + h - 1);
133         }
134
135         @Override
136         public void update(Graphics g) {
137                 int w = getSize().width;
138                 int h = getSize().height;
139
140                 Image buffer;
141                 Graphics gra;
142                 try {
143                         buffer = createImage(w, h);
144                         gra = buffer.getGraphics();
145                 } catch (Throwable e) {
146                         return;
147                 }
148
149                 Color[] cols = _pixxConfiguration.getIRCConfiguration().getStyleColors(_ct);
150                 int c = 0;
151                 for (int y = 0; y < 2; y++) {
152                         for (int x = 1; x < 9; x++) {
153                                 drawColor(gra, x, y, cols[c]);
154                                 if (c == _color)
155                                         drawSelect(gra, x, y);
156                                 c++;
157                         }
158                 }
159
160                 gra.setColor(cols[_backColor]);
161                 gra.fillRect(0, 0, (int) getItemWidth(), h);
162
163                 gra.setColor(cols[_color]);
164                 drawBold(gra, 0, 0);
165                 drawUnderline(gra, 0, 1);
166
167                 if (_bold)
168                         drawSelect(gra, 0, 0);
169                 if (_underline)
170                         drawSelect(gra, 0, 1);
171
172                 g.drawImage(buffer, 0, 0, this);
173         }
174
175         @Override
176         public Dimension getPreferredSize() {
177                 return new Dimension(9 * 10, 10);
178         }
179
180         private double getItemWidth() {
181                 return getSize().width / 9.0;
182         }
183
184         private double getItemHeight() {
185                 return getSize().height / 2.0;
186         }
187
188         /**
189          * Set the front color.
190          * 
191          * @param c
192          *          the new front color.
193          */
194         public void setFrontColor(int c) {
195                 _color = c;
196                 repaint();
197         }
198
199         /**
200          * Set the back color.
201          * 
202          * @param c
203          *          the new back color.
204          */
205         public void setBackColor(int c) {
206                 _backColor = c;
207                 repaint();
208         }
209
210         /**
211          * Set the new bold status.
212          * 
213          * @param bold
214          *          the new bold status.
215          */
216         public void setBold(boolean bold) {
217                 _bold = bold;
218                 repaint();
219         }
220
221         /**
222          * Set the new underline status.
223          * 
224          * @param underline
225          *          the new underline statu.
226          */
227         public void setUnderline(boolean underline) {
228                 _underline = underline;
229                 repaint();
230         }
231
232         /**
233          * Get prefix to be used for text styling.
234          * 
235          * @return text style prefix.
236          */
237         public String getPrefix() {
238                 String pre = "";
239                 if (_color != 1 || _backColor != 0) {
240                         String c1 = "";
241                         if (_color != 1) {
242                                 c1 = c1 + _color;
243                                 if (_color < 10)
244                                         c1 = "0" + c1;
245                         }
246                         String c2 = "";
247                         if (_backColor != 0) {
248                                 c2 = "" + _backColor;
249                                 if (_backColor < 10)
250                                         c2 = "0" + c2;
251                                 c2 = "," + c2;
252                         }
253                         if (c2.length() > 0 && c1.length() == 0)
254                                 c1 = "1";
255                         pre = pre + (char) 3 + c1 + c2;
256                 }
257                 if (_bold)
258                         pre += (char) 2;
259                 if (_underline)
260                         pre += (char) 31;
261                 return pre;
262         }
263
264         @Override
265         public void mouseClicked(MouseEvent e) {
266         }
267
268         @Override
269         public void mouseEntered(MouseEvent e) {
270         }
271
272         @Override
273         public void mouseExited(MouseEvent e) {
274         }
275
276         @Override
277         public void mousePressed(MouseEvent e) {
278                 int x = (int) (e.getX() / getItemWidth());
279                 int y = (int) (e.getY() / getItemHeight());
280                 if (x == 0) {
281                         if (y == 0)
282                                 _bold = !_bold;
283                         if (y == 1)
284                                 _underline = !_underline;
285                 } else {
286                         x--;
287                         if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
288                                 _color = x + y * 8;
289                         else
290                                 _backColor = x + y * 8;
291                 }
292                 repaint();
293         }
294
295         @Override
296         public void mouseReleased(MouseEvent e) {
297         }
298
299 }