KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > gui > ColorButton


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.whiteboard.gui;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.awt.event.MouseListener JavaDoc;
26
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JColorChooser JavaDoc;
29
30 import org.lucane.applications.whiteboard.WhiteBoard;
31 import org.lucane.applications.whiteboard.graph.MyGraph;
32
33 public class ColorButton extends JButton JavaDoc
34 implements MouseListener JavaDoc
35 {
36     private static final Rectangle JavaDoc BORDER = new Rectangle JavaDoc(5, 5, 12, 12);
37     private static final Rectangle JavaDoc BACKGROUND = new Rectangle JavaDoc(12, 12, 12, 12);
38     
39     private WhiteBoard plugin;
40     
41     public ColorButton(WhiteBoard plugin)
42     {
43         super(plugin.getImageIcon("blank.png"));
44         setToolTipText(plugin.tr("tip.chooseColors"));
45         setFocusable(false);
46         setBorderPainted(false);
47         addMouseListener(this);
48         
49         this.plugin = plugin;
50     }
51     
52     protected void paintChildren(Graphics JavaDoc g)
53     {
54         Color JavaDoc back = plugin.getGraph().getCellBackground();
55         g.setColor(back == null ? Color.WHITE : back);
56         if(back != null)
57             g.fillRect(BACKGROUND.x, BACKGROUND.y, BACKGROUND.width, BACKGROUND.height);
58         else
59             g.drawRect(BACKGROUND.x, BACKGROUND.y, BACKGROUND.width, BACKGROUND.height);
60         
61         g.setColor(plugin.getGraph().getCellBorder());
62         g.drawRect(BORDER.x, BORDER.y, BORDER.width, BORDER.height);
63     }
64
65     public void mouseEntered(MouseEvent JavaDoc e) {}
66     public void mouseExited(MouseEvent JavaDoc e) {}
67     public void mouseReleased(MouseEvent JavaDoc e) {}
68     public void mousePressed(MouseEvent JavaDoc e) {}
69     public void mouseClicked(MouseEvent JavaDoc e)
70     {
71         if(!isEnabled())
72             return;
73             
74         if(e.getButton() == MouseEvent.BUTTON3)
75         {
76             plugin.getGraph().setCellBackground(null);
77             plugin.getGraph().setCellBorder(null);
78             return;
79         }
80         
81         if(BORDER.contains(e.getX(), e.getY()))
82             chooseColor(false);
83         else if(BACKGROUND.contains(e.getX(), e.getY()))
84             chooseColor(true);
85     }
86     
87     private void chooseColor(boolean background)
88     {
89         MyGraph graph = plugin.getGraph();
90         Color JavaDoc color = background ? graph.getCellBackground() : graph.getCellBorder();
91         String JavaDoc msg = background ? "back" : "border";
92         
93         color = JColorChooser.showDialog(null, msg, color);
94         if(color == null)
95             return;
96         
97         if(background)
98             graph.setCellBackground(color);
99         else
100             graph.setCellBorder(color);
101         
102         repaint();
103     }
104 }
Popular Tags