OSDN Git Service

761193ad6780c7bb55fdbdb22c34a8f8e3885fb0
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / actions / ChangeColorAction.java
1 package jp.sourceforge.stigmata.ui.swing.actions;
2
3 /*
4  * $Id$
5  */
6
7 import java.awt.Color;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10
11 import javax.swing.AbstractAction;
12 import javax.swing.JColorChooser;
13 import javax.swing.JDialog;
14
15 import jp.sourceforge.stigmata.ui.swing.GUIUtility;
16 import jp.sourceforge.stigmata.ui.swing.StigmataFrame;
17
18 /**
19  * 
20  * @author Haruaki TAMADA
21  * @version $Revision$ 
22  */
23 public class ChangeColorAction extends AbstractAction{
24     private static final long serialVersionUID = -7617597154707466764L;
25
26     private StigmataFrame frame;
27     private Color currentColor = Color.RED;
28     private JColorChooser chooser;
29     private boolean colorSelected = false;
30     private ActionListener listener;
31
32     public ChangeColorAction(String label, StigmataFrame frame, 
33                               Color initialColor, ActionListener listener){
34         super(frame.getMessages().get(label + ".label"), GUIUtility.getIcon(frame.getMessages(), label + ".icon"));
35         this.frame = frame;
36         this.listener = listener;
37         this.currentColor = initialColor;
38
39         chooser = new JColorChooser();
40         chooser.setColor(initialColor);
41     }
42
43     public ChangeColorAction(StigmataFrame frame, Color color, ActionListener listener){
44         this("changecolor", frame, color, listener);
45     }
46
47     public ChangeColorAction(StigmataFrame frame, ActionListener listener){
48         this(frame, Color.RED, listener);
49     }
50
51     public boolean isColorSelected(){
52         return colorSelected;
53     }
54
55     public Color getColor(){
56         return currentColor;
57     }
58
59     @Override
60     public void actionPerformed(ActionEvent e){
61         chooser.setColor(currentColor);
62         JDialog dialog = JColorChooser.createDialog(
63             frame, frame.getMessages().get("changecolor.title"), 
64             true, chooser,
65             new ActionListener(){ // ok
66                 @Override
67                 public void actionPerformed(ActionEvent e){
68                     currentColor = chooser.getColor();
69                     colorSelected = true;
70                     listener.actionPerformed(new ActionEvent(ChangeColorAction.this, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers()));
71                 }
72             },
73             new ActionListener(){ // cancel
74                 @Override
75                 public void actionPerformed(ActionEvent e){
76                     colorSelected = false;
77                 }
78             }
79         );
80         dialog.setVisible(true);
81     }
82 }