1 |
iliev |
1286 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
|
|
* |
6 |
|
|
* This file is part of JSampler. |
7 |
|
|
* |
8 |
|
|
* JSampler is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License version 2 |
10 |
|
|
* as published by the Free Software Foundation. |
11 |
|
|
* |
12 |
|
|
* JSampler is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with JSampler; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
|
|
* MA 02111-1307 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
package org.jsampler.view.std; |
24 |
|
|
|
25 |
|
|
import java.awt.Color; |
26 |
|
|
import java.awt.Cursor; |
27 |
|
|
import java.awt.Dialog; |
28 |
|
|
import java.awt.Dimension; |
29 |
|
|
|
30 |
|
|
import java.awt.event.ActionEvent; |
31 |
|
|
import java.awt.event.ActionListener; |
32 |
|
|
import java.awt.event.MouseAdapter; |
33 |
|
|
import java.awt.event.MouseEvent; |
34 |
|
|
|
35 |
|
|
import java.util.Vector; |
36 |
|
|
|
37 |
|
|
import javax.swing.BorderFactory; |
38 |
|
|
import javax.swing.Box; |
39 |
|
|
import javax.swing.BoxLayout; |
40 |
|
|
import javax.swing.JColorChooser; |
41 |
|
|
import javax.swing.JPanel; |
42 |
|
|
|
43 |
|
|
import javax.swing.event.ChangeEvent; |
44 |
|
|
import javax.swing.event.ChangeListener; |
45 |
|
|
|
46 |
|
|
import net.sf.juife.JuifeUtils; |
47 |
|
|
import net.sf.juife.OkCancelDialog; |
48 |
|
|
|
49 |
|
|
/** |
50 |
|
|
* |
51 |
|
|
* @author Grigor Iliev |
52 |
|
|
*/ |
53 |
|
|
public class JSColorButton extends JPanel { |
54 |
|
|
private Color color; |
55 |
|
|
private final Vector<ActionListener> listeners = new Vector<ActionListener>(); |
56 |
|
|
|
57 |
|
|
/** Creates a new instance of <code>JSColorButton</code> */ |
58 |
|
|
public |
59 |
|
|
JSColorButton() { this(Color.WHITE); } |
60 |
|
|
|
61 |
|
|
/** Creates a new instance of <code>JSColorButton</code> */ |
62 |
|
|
public |
63 |
|
|
JSColorButton(Color c) { |
64 |
|
|
color = c; |
65 |
|
|
|
66 |
|
|
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
67 |
|
|
setPreferredSize(new Dimension(42, 16)); |
68 |
|
|
setMaximumSize(new Dimension(42, 16)); |
69 |
|
|
setBorder(BorderFactory.createLineBorder(Color.BLACK)); |
70 |
|
|
|
71 |
|
|
addMouseListener(new MouseAdapter() { |
72 |
|
|
public void |
73 |
|
|
mouseClicked(MouseEvent e) { |
74 |
|
|
if(!isEnabled()) return; |
75 |
|
|
if(e.getButton() == e.BUTTON1) showColorChooser(); |
76 |
|
|
} |
77 |
|
|
}); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
|
|
* Registers the specified listener to be |
82 |
|
|
* notified when the current color is changed. |
83 |
|
|
* @param l The <code>ActionListener</code> to register. |
84 |
|
|
*/ |
85 |
|
|
public void |
86 |
|
|
addActionListener(ActionListener l) { listeners.add(l); } |
87 |
|
|
|
88 |
|
|
/** |
89 |
|
|
* Removes the specified listener. |
90 |
|
|
* @param l The <code>ActionListener</code> to remove. |
91 |
|
|
*/ |
92 |
|
|
public void |
93 |
|
|
removeActionListener(ActionListener l) { listeners.remove(l); } |
94 |
|
|
|
95 |
|
|
/** Notifies listeners that the current color is changed. */ |
96 |
|
|
private void |
97 |
|
|
fireActionPerformed() { |
98 |
|
|
ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null); |
99 |
|
|
for(ActionListener l : listeners) l.actionPerformed(e); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
public void |
103 |
|
|
setEnabled(boolean b) { |
104 |
|
|
setOpaque(b); |
105 |
|
|
if(b) setBorder(BorderFactory.createLineBorder(Color.BLACK)); |
106 |
|
|
else setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); |
107 |
|
|
//setBorderPainted(!b); |
108 |
|
|
super.setEnabled(b); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
private void |
112 |
|
|
showColorChooser() { |
113 |
|
|
ColorDlg dlg = new ColorDlg ( |
114 |
|
|
(Dialog)JuifeUtils.getWindow(this), getColor() |
115 |
|
|
); |
116 |
|
|
|
117 |
|
|
dlg.setVisible(true); |
118 |
|
|
if(!dlg.isCancelled()) { |
119 |
|
|
setColor(dlg.getColor()); |
120 |
|
|
fireActionPerformed(); |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
public Color |
125 |
|
|
getColor() { return color; } |
126 |
|
|
|
127 |
|
|
public void |
128 |
|
|
setColor(Color c) { |
129 |
|
|
color = c; |
130 |
|
|
setBackground(color); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
protected static class ColorDlg extends OkCancelDialog { |
135 |
|
|
private final JColorChooser colorChooser = new JColorChooser(); |
136 |
|
|
|
137 |
|
|
ColorDlg(Dialog owner) { this(owner, Color.WHITE); } |
138 |
|
|
|
139 |
|
|
ColorDlg(Dialog owner, Color c) { |
140 |
|
|
super(owner); |
141 |
|
|
|
142 |
|
|
colorChooser.setPreviewPanel(new JPanel()); |
143 |
|
|
colorChooser.setColor(c); |
144 |
|
|
|
145 |
|
|
JPanel mainPane = new JPanel(); |
146 |
|
|
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
147 |
|
|
mainPane.add(colorChooser); |
148 |
|
|
|
149 |
|
|
mainPane.add(Box.createRigidArea(new Dimension(0, 6))); |
150 |
|
|
|
151 |
|
|
final JPanel p = new JPanel(); |
152 |
|
|
p.setBackground(c); |
153 |
|
|
p.setBorder(BorderFactory.createLineBorder(Color.BLACK)); |
154 |
|
|
mainPane.add(p); |
155 |
|
|
|
156 |
|
|
p.setPreferredSize(new Dimension(48, 8)); |
157 |
|
|
p.setMaximumSize(new Dimension(Short.MAX_VALUE, 8)); |
158 |
|
|
|
159 |
|
|
setMainPane(mainPane); |
160 |
|
|
|
161 |
|
|
colorChooser.getSelectionModel().addChangeListener(new ChangeListener() { |
162 |
|
|
public void |
163 |
|
|
stateChanged(ChangeEvent e) { p.setBackground(getColor()); } |
164 |
|
|
}); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
protected void |
168 |
|
|
onOk() { setVisible(false); } |
169 |
|
|
|
170 |
|
|
protected void |
171 |
|
|
onCancel() { setVisible(false); } |
172 |
|
|
|
173 |
|
|
public Color |
174 |
|
|
getColor() { return colorChooser.getColor(); } |
175 |
|
|
} |
176 |
|
|
} |