JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / views / VueHighlightRegionEdit.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.util.ArrayList;
11
12 import javax.swing.JButton;
13 import javax.swing.JColorChooser;
14         import javax.swing.JLabel;
15 import javax.swing.JOptionPane;
16         import javax.swing.JPanel;
17         import javax.swing.JSlider;
18 import javax.swing.JSpinner;
19 import javax.swing.SpinnerNumberModel;
20 import javax.swing.event.ChangeEvent;
21 import javax.swing.event.ChangeListener;
22
23
24 import fr.orsay.lri.varna.VARNAPanel;
25 import fr.orsay.lri.varna.models.annotations.HighlightRegionAnnotation;
26 import fr.orsay.lri.varna.models.rna.ModeleBase;
27
28         public class VueHighlightRegionEdit implements ChangeListener, ActionListener {
29
30                 private VARNAPanel _vp;
31                 private JSlider _fromSlider;
32                 private JSlider _toSlider;
33                 private JPanel panel;
34                 private HighlightRegionAnnotation _an;
35                 private static int CONTROL_HEIGHT = 50;
36                 private static int TITLE_WIDTH = 70;
37                 private static int CONTROL_WIDTH = 200;
38                 private JButton fillShow  = new JButton();
39                 private JButton outlineShow  = new JButton();
40                 JSpinner rad;
41
42                 public VueHighlightRegionEdit(VARNAPanel vp, HighlightRegionAnnotation an) {
43                         _an = an;
44                         _vp = vp;
45                         _toSlider = new JSlider(JSlider.HORIZONTAL, 0,vp.getRNA().getSize()-1,0);
46                         _toSlider.setMajorTickSpacing(10);
47                         _toSlider.setPaintTicks(true);
48                         _toSlider.setPaintLabels(true);
49                         _toSlider.setPreferredSize(new Dimension(CONTROL_WIDTH, CONTROL_HEIGHT));
50
51                         _fromSlider = new JSlider(JSlider.HORIZONTAL, 0,vp.getRNA().getSize()-1,0);
52                         _fromSlider.setMajorTickSpacing(10);
53                         _fromSlider.setPaintTicks(true);
54                         _fromSlider.setPaintLabels(true);
55                         _fromSlider.setPreferredSize(new Dimension(CONTROL_WIDTH, CONTROL_HEIGHT));
56
57                         _fromSlider.addChangeListener(this);
58                         _toSlider.addChangeListener(this);
59
60                         panel = new JPanel();
61                         panel.setLayout(new FlowLayout(FlowLayout.LEFT));
62
63                         JPanel fromp = new JPanel();
64                         JLabel l1 = new JLabel("From: ");
65                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
66                         fromp.add(l1);
67                         fromp.add(_fromSlider);
68
69                         JPanel top = new JPanel();
70                         l1 = new JLabel("To: ");
71                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
72                         top.add(l1);
73                         top.add(_toSlider);
74
75                         JPanel outlinep = new JPanel();
76                         l1 = new JLabel("Outline color: ");
77                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
78                         outlineShow.setContentAreaFilled(false);
79                         outlineShow.setOpaque(true);
80                         outlineShow.setPreferredSize(new Dimension(CONTROL_WIDTH,CONTROL_HEIGHT));
81                         outlineShow.setBackground(an.getOutlineColor());
82                         outlineShow.addActionListener(this);
83                         outlineShow.setActionCommand("outline");
84                         outlinep.add(l1);
85                         outlinep.add(outlineShow);
86                         
87                         JPanel fillp = new JPanel();
88                         l1 = new JLabel("Fill color: ");
89                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
90                         fillShow.setContentAreaFilled(false);
91                         fillShow.setOpaque(true);
92                         fillShow.setPreferredSize(new Dimension(CONTROL_WIDTH,CONTROL_HEIGHT));
93                         fillShow.setBackground(an.getFillColor());
94                         fillShow.addActionListener(this);
95                         fillShow.setActionCommand("fill");
96                         fillp.add(l1);
97                         fillp.add(fillShow);
98                         
99                         JPanel radiusp = new JPanel();
100                         l1 = new JLabel("Radius: ");
101                         l1.setPreferredSize(new Dimension(TITLE_WIDTH,CONTROL_HEIGHT));
102                         SpinnerNumberModel jm = new SpinnerNumberModel(_an.getRadius(),1.0,50.0,0.1);
103                         rad = new JSpinner(jm);
104                         rad.setPreferredSize(new Dimension(CONTROL_WIDTH,CONTROL_HEIGHT));
105                         radiusp.add(l1);
106                         radiusp.add(rad);
107                         rad.addChangeListener(this);
108                         
109                         JPanel jp = new JPanel();
110                         jp.setLayout(new GridLayout(5,1));
111                         jp.add(fromp);
112                         jp.add(top);
113                         jp.add(outlinep);
114                         jp.add(fillp);
115                         jp.add(radiusp);
116                         panel.add(jp);
117                 }
118
119                 public JPanel getPanel() {
120                         return panel;
121                 }
122
123                 public double getAngle() {
124                         return _toSlider.getValue();
125                 }
126
127                 public VARNAPanel get_vp() {
128                         return _vp;
129                 }
130                 
131                 HighlightRegionAnnotation _backup = null;
132                 
133                 public boolean show() {
134                         boolean accept = false;
135                         int from = _an.getMinIndex();
136                         int to = _an.getMaxIndex();
137                         _fromSlider.setValue(from);
138                         _toSlider.setValue(to );
139                         if (JOptionPane.showConfirmDialog(_vp, getPanel(),
140                                         "Edit region annotation", JOptionPane.OK_CANCEL_OPTION,
141                                         JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) 
142                         {
143                                 accept = true;
144                         } 
145                         _vp.repaint();
146                         return accept;
147                 }
148
149                 public void stateChanged(ChangeEvent e) {
150                         if ((e.getSource()==_toSlider)||(e.getSource()==_fromSlider))
151                         {
152                         int from  = _fromSlider.getValue(); 
153                         int to  = _toSlider.getValue(); 
154                         if (from>to)
155                         {
156                                 if (e.getSource().equals(_fromSlider))
157                                 {
158                                         _toSlider.setValue(from);
159                                 }
160                                 else if (e.getSource().equals(_toSlider))
161                                 {
162                                         _fromSlider.setValue(to);
163                                 }
164                         }
165                         from  = _fromSlider.getValue(); 
166                         to  = _toSlider.getValue(); 
167                         _an.setBases(_vp.getRNA().getBasesBetween(from, to));
168                         _vp.repaint();
169                         }
170                         else if (e.getSource().equals(rad))
171                         {
172                                 Object val = rad.getValue();
173                                 if (val instanceof Double)
174                                 {                                       
175                                         _an.setRadius(((Double)val).doubleValue());
176                                 }
177                         }
178                         
179                 }
180
181                 public void actionPerformed(ActionEvent e) {
182                         if (e.getActionCommand().equals("outline"))
183                         {
184                           Color c = JColorChooser.showDialog(getPanel(), "Choose new outline color", _an.getOutlineColor());
185                           if (c!= null)
186                           {   _an.setOutlineColor(c);  }
187                         }
188                         else if (e.getActionCommand().equals("fill"))
189                         {
190                                   Color c = JColorChooser.showDialog(getPanel(), "Choose new fill color", _an.getFillColor());
191                                   if (c!= null)
192                                   {  _an.setFillColor(c); }
193                         }
194                         outlineShow.setBackground(_an.getOutlineColor());
195                         fillShow.setBackground(_an.getFillColor());
196                         _vp.repaint();
197                         
198                 }
199         }
200