JAL-3032 adds Java 8 functionality (2/2)
[jalview.git] / src2 / fr / orsay / lri / varna / views / VueChemProbAnnotation.java
1         package fr.orsay.lri.varna.views;
2
3
4         import java.awt.Color;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.GridLayout;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.ItemEvent;
11 import java.awt.event.ItemListener;
12
13 import javax.swing.JButton;
14 import javax.swing.JComboBox;
15 import javax.swing.JLabel;
16 import javax.swing.JOptionPane;
17 import javax.swing.JPanel;
18 import javax.swing.JSpinner;
19 import javax.swing.SpinnerNumberModel;
20 import javax.swing.event.ChangeEvent;
21 import javax.swing.event.ChangeListener;
22
23 import fr.orsay.lri.varna.VARNAPanel;
24 import fr.orsay.lri.varna.models.annotations.ChemProbAnnotation;
25 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
26
27
28 public class VueChemProbAnnotation implements ChangeListener, ActionListener, ItemListener {
29
30                 protected VARNAPanel _vp;
31                 private JPanel panel;
32                 protected ChemProbAnnotation _an;
33                 private static int CONTROL_HEIGHT = 50;
34                 private static int TITLE_WIDTH = 70;
35                 private static int CONTROL_WIDTH = 200;
36                 protected JButton color  = new JButton();
37                 JSpinner intensity;
38                 JComboBox outward = new JComboBox(new String[]{"Inward","Outward"});
39                 JComboBox type = new JComboBox(ChemProbAnnotation.ChemProbAnnotationType.values());
40
41                 public VueChemProbAnnotation(VARNAPanel vp, ChemProbAnnotation an) {
42                         _an = an;
43                         _vp = vp;
44
45                         panel = new JPanel();
46                         panel.setLayout(new FlowLayout(FlowLayout.LEFT));
47
48
49                         JPanel outlinep = new JPanel();
50                         JLabel l1 = new JLabel("Color: ");
51                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
52                         color.setContentAreaFilled(false);
53                         color.setOpaque(true);
54                         color.setPreferredSize(new Dimension(CONTROL_WIDTH,CONTROL_HEIGHT));
55                         color.setBackground(_an.getColor());
56                         color.addActionListener(this);
57                         color.setActionCommand("outline");
58                         outlinep.add(l1);
59                         outlinep.add(color);
60                         
61                         
62                         JPanel radiusp = new JPanel();
63                         l1 = new JLabel("Intensity: ");
64                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
65                         SpinnerNumberModel jm = new SpinnerNumberModel(_an.getIntensity(),0.01,10.0,0.01);
66                         intensity = new JSpinner(jm);
67                         radiusp.add(l1);
68                         radiusp.add(intensity);
69                         intensity.addChangeListener(this);
70
71                         JPanel dirp = new JPanel();
72                         l1 = new JLabel("Direction: ");
73                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
74                         outward.addItemListener(this);
75                         dirp.add(l1);
76                         dirp.add(outward);
77
78                         JPanel typep = new JPanel();
79                         l1 = new JLabel("Type: ");
80                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
81                         type.addItemListener(this);
82                         typep.add(l1);
83                         typep.add(type);
84                         
85                         
86                         
87                         JPanel jp = new JPanel();
88                         jp.setLayout(new GridLayout(4,1));
89                         jp.add(outlinep);
90                         jp.add(radiusp);
91                         jp.add(dirp);
92                         jp.add(typep);
93                         panel.add(jp);
94                 }
95
96                 public JPanel getPanel() {
97                         return panel;
98                 }
99
100                 public VARNAPanel get_vp() {
101                         return _vp;
102                 }
103                 
104                 HighlightRegionAnnotation _backup = null;
105                 
106                 public boolean show() {
107                         boolean accept = false;
108                         intensity.setValue(_an.getIntensity());
109                         color.setBackground(_an.getColor());
110                         type.setSelectedItem(_an.getType());
111                         outward.setSelectedItem((_an.isOut()?"Inward":"Outward"));
112                         
113                         if (JOptionPane.showConfirmDialog(_vp, getPanel(),
114                                         "Edit chemical probing annotation", JOptionPane.OK_CANCEL_OPTION,
115                                         JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) 
116                         {
117                                 accept = true;
118                         } 
119                         _vp.repaint();
120                         return accept;
121                 }
122
123                 public void stateChanged(ChangeEvent e) {
124                         if (e.getSource().equals(intensity))
125                         {
126                                 Object val = intensity.getValue();
127                                 if (val instanceof Double)
128                                 {                                       
129                                         _an.setIntensity(((Double)val).doubleValue());
130                                         _vp.repaint();
131                                 }
132                         }
133                         
134                 }
135
136         public void actionPerformed(ActionEvent e) {
137                 if (e.getActionCommand().equals("outline")) {
138                         // BH j2s SwingJS asynchronous for JavaScript; synchronous for Java
139                         _vp.getVARNAUI().showColorDialog("Choose new outline color", _an.getColor(), new Runnable() {
140
141                                 @Override
142                                 public void run() {
143                                         Color c = (Color) _vp.getVARNAUI().dialogReturnValue;
144                                         if (c != null) {
145                                                 _an.setColor(c);
146                                                 color.setBackground(_an.getColor());
147                                                 _vp.repaint();
148                                         }
149                                 }
150
151                         });
152                 }
153
154         }
155
156                 public void itemStateChanged(ItemEvent e) {
157                         if (e.getSource()==outward)
158                         {
159                                 _an.setOut(!e.getItem().equals("Outward"));
160                                 _vp.repaint();
161                         }
162                         else if ((e.getSource()==type)&&(e.getItem() instanceof ChemProbAnnotation.ChemProbAnnotationType))
163                         {
164                                 ChemProbAnnotation.ChemProbAnnotationType t = (ChemProbAnnotation.ChemProbAnnotationType) e.getItem();
165                                 _an.setType(t);
166                                 _vp.repaint();
167                         }
168                         
169                 }
170         }
171
172