swingjs/api, javajs/async
[jalview.git] / src / javajs / async / AsyncColorChooser.java
1 package javajs.async;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.beans.PropertyChangeEvent;
8 import java.beans.PropertyChangeListener;
9
10 import javax.swing.JColorChooser;
11 import javax.swing.plaf.UIResource;
12
13 /**
14  * A simple Asynchronous file chooser for JavaScript; synchronous with Java.
15  * 
16  * Allows two modes -- using an ActionListener (setAction(ActionListener) or constructor(ActionListener))
17  * 
18  * @author Bob Hanson
19  */
20
21 public class AsyncColorChooser implements PropertyChangeListener {
22
23         private ActionListener listener;
24         private Color selectedColor;
25
26         public void showDialog(Component component, String title, Color initialColor, ActionListener listener) {
27                 setListener(listener);
28                 process(JColorChooser.showDialog(component, title, initialColor));
29                 unsetListener();
30         }
31
32         public Color getSelectedColor() {
33                 return selectedColor;
34         }
35
36
37         @Override
38         public void propertyChange(PropertyChangeEvent evt) {
39                 // JavaScript only
40                 Color c = (Color) evt.getNewValue();
41                 
42                 switch (evt.getPropertyName()) {
43                 case "SelectedColor":
44                         process(c);
45                         break;
46                 }
47         }
48
49         private void setListener(ActionListener a) {
50                 listener = a;
51                 /** @j2sNative Clazz.load("javax.swing.JColorChooser");javax.swing.JColorChooser.listener = this */
52         }
53
54         private void unsetListener() {
55                 /** @j2sNative javax.swing.JColorChooser.listener = null */
56         }
57
58         
59         
60         private void process(Color c) {
61                 if (c instanceof UIResource)
62                         return;
63                 selectedColor = c;
64                 listener.actionPerformed(new ActionEvent(this, c == null ? 0 : c.getRGB(), c == null ? null : c.toString()));
65         }
66         
67 }