JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / views / VueColorMapStyle.java
1 package fr.orsay.lri.varna.views;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Cursor;
6 import java.awt.Dimension;
7 import java.awt.Font;
8 import java.awt.Graphics;
9 import java.awt.Graphics2D;
10 import java.awt.RenderingHints;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.event.FocusAdapter;
14 import java.awt.event.FocusEvent;
15 import java.awt.event.FocusListener;
16 import java.awt.event.ItemEvent;
17 import java.awt.event.ItemListener;
18 import java.awt.event.MouseEvent;
19 import java.awt.event.MouseListener;
20 import java.awt.event.MouseMotionListener;
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.PrintWriter;
28 import java.util.Arrays;
29 import java.util.Comparator;
30
31 import javax.swing.Box;
32 import javax.swing.BoxLayout;
33 import javax.swing.JButton;
34 import javax.swing.JColorChooser;
35 import javax.swing.JComboBox;
36 import javax.swing.JDialog;
37 import javax.swing.JFileChooser;
38 import javax.swing.JLabel;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JTextField;
42 import javax.swing.filechooser.FileFilter;
43
44 import fr.orsay.lri.varna.VARNAPanel;
45 import fr.orsay.lri.varna.components.GradientEditorPanel;
46 import fr.orsay.lri.varna.models.VARNAConfig;
47 import fr.orsay.lri.varna.models.rna.ModeleColorMap;
48 import fr.orsay.lri.varna.models.rna.ModeleColorMap.NamedColorMapTypes;
49
50 public class VueColorMapStyle extends JPanel implements ActionListener, ItemListener, PropertyChangeListener {
51         private VARNAPanel _vp;
52         private GradientEditorPanel _gp;
53         private JComboBox _cb; 
54         private JTextField _code; 
55         private ModeleColorMap _backup;
56         // TODO BH SwingJS note that the save dialog is never used in JavaScript 
57         private static JFileChooser fc = new JFileChooser(){
58             public void approveSelection(){
59                 File f = getSelectedFile();
60                 if(f.exists() && getDialogType() == SAVE_DIALOG){
61                     int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_OPTION);
62                     switch(result){
63                         case JOptionPane.YES_OPTION:
64                             super.approveSelection();
65                             return;
66                         case JOptionPane.NO_OPTION:
67                             return;
68                         case JOptionPane.CLOSED_OPTION:
69                             return;
70                         case JOptionPane.CANCEL_OPTION:
71                             cancelSelection();
72                             return;
73                     }
74                 }
75                 super.approveSelection();
76             }        
77         };
78         
79         public VueColorMapStyle(VARNAPanel vp)
80         {
81                 super();
82                 _vp = vp;
83                 init();
84         }
85
86         private void init()
87         {
88                 JLabel gradientCaption = new JLabel("Click gradient to add new color...");
89                 _gp = new GradientEditorPanel(_vp.getColorMap().clone());
90                 _backup = _vp.getColorMap();
91                 _gp.setPreferredSize(new Dimension(300,70));
92                 _gp.addPropertyChangeListener(this);
93
94                 JPanel codePanel = new JPanel();
95                 JLabel codeCaption = new JLabel("Param. code: ");
96                 codeCaption.setVisible(true);
97                 _code = new JTextField("");
98                 _code.setFont(Font.decode("Monospaced-PLAIN-12"));
99                 _code.setEditable(true);
100                 _code.addFocusListener(new FocusListener(){
101
102                         public void focusGained(FocusEvent arg0) {
103                                                 _code.setSelectionStart(0);
104                                                 _code.setSelectionEnd(_code.getText().length());
105                         }
106
107                         public void focusLost(FocusEvent arg0) {
108                         }                       
109                 });             
110                 _code.setVisible(false);
111                 
112                 NamedColorMapTypes[] palettes =  ModeleColorMap.NamedColorMapTypes.values();
113                 Arrays.sort(palettes,new Comparator<ModeleColorMap.NamedColorMapTypes>(){
114                         public int compare(ModeleColorMap.NamedColorMapTypes arg0, ModeleColorMap.NamedColorMapTypes arg1) {
115                                 return arg0.getId().compareTo(arg1.getId());
116                         }                       
117                 });
118                 Object[] finalArray = new Object[palettes.length+1];
119                 int selected = -1;
120                 for (int i=0;i<palettes.length;i++)
121                 { 
122                         if (palettes[i].getColorMap().equals(_vp.getColorMap()))
123                         {
124                                 selected = i; 
125                                 //System.out.println(selected);
126                         }
127                         finalArray[i] = palettes[i]; 
128                 }
129                 String custom = new String("Custom...");
130                 finalArray[palettes.length] = custom;
131                 _cb = new JComboBox(finalArray);
132                 if (selected!=-1)
133                 {
134                         _cb.setSelectedIndex(selected);
135                 }
136                 else
137                 {
138                         _cb.setSelectedItem(finalArray.length-1);
139                 }
140                 _cb.addItemListener(this);
141                 
142                 _code.setText(getTextRepresentation());
143                 
144                 
145                 
146                 FileFilter CMSFiles = new FileFilter(){
147                         public boolean accept(File f) {
148                                 return f.getName().toLowerCase().endsWith(".cms") || f.isDirectory();
149                         }
150
151                         public String getDescription() {
152                                 return "Color Map (*.cms) Files";
153                         }
154                         
155                 };
156                 fc.addChoosableFileFilter(CMSFiles);
157                 fc.setFileFilter(CMSFiles);
158                 
159                 codePanel.setLayout(new BoxLayout(codePanel,BoxLayout.LINE_AXIS));
160                 codePanel.add(codeCaption);
161                 codePanel.add(_code);
162                 JButton loadStyleButton = new JButton("Load");
163                 loadStyleButton.addActionListener(new ActionListener(){
164                         public void actionPerformed(ActionEvent e) {
165                                 if (fc.showOpenDialog(VueColorMapStyle.this)==JFileChooser.APPROVE_OPTION)
166                                 {
167                                         File file = fc.getSelectedFile();
168                                         try {
169                                                 FileInputStream fis = new FileInputStream(file);
170                                                 byte[] data = new byte[(int) file.length()];
171                                                 fis.read(data);
172                                                 fis.close();
173                                                 String str = new String(data).trim();
174                                                 ModeleColorMap ns = ModeleColorMap.parseColorMap(str);
175                                                 _gp.setColorMap(ns);
176                                                 refreshCode();
177                                         } catch (FileNotFoundException e1) {
178                                                 e1.printStackTrace();
179                                         } catch (IOException e1) {
180                                                 e1.printStackTrace();
181                                         }
182                                 }
183                         }
184                         
185                 });
186                 JButton saveStyleButton = new JButton("Save");
187                 saveStyleButton.addActionListener(new ActionListener(){
188                         public void actionPerformed(ActionEvent e) {
189                                 if (fc.showSaveDialog(VueColorMapStyle.this)==JFileChooser.APPROVE_OPTION)
190                                 {
191                                         try {
192                                                 PrintWriter out = new PrintWriter(fc.getSelectedFile());
193                                                 out.println(_gp.getColorMap().getParamEncoding());
194                                                 out.close();
195                                         } catch (FileNotFoundException e1) {
196                                                 e1.printStackTrace();
197                                         } catch (IOException e1) {
198                                                 e1.printStackTrace();
199                                         }
200                                 }
201                         }
202                         
203                 });
204                 saveStyleButton.setAlignmentX(CENTER_ALIGNMENT);
205                 loadStyleButton.setAlignmentX(CENTER_ALIGNMENT);
206
207                 JPanel jp = new JPanel(new BorderLayout());
208                 jp.add(_cb,BorderLayout.CENTER);
209                 JPanel jp2 = new JPanel();
210                 jp2.setLayout(new BoxLayout(jp2,BoxLayout.X_AXIS));
211                 jp2.add(Box.createRigidArea(new Dimension(5,0)));
212                 jp2.add(loadStyleButton);
213                 jp2.add(Box.createRigidArea(new Dimension(5,0)));
214                 jp2.add(saveStyleButton);
215                 jp.add(jp2,BorderLayout.EAST);
216                 
217                 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
218                 add(jp);
219                 add(Box.createVerticalStrut(10));
220                 add(_gp);
221                 add(gradientCaption);
222                 //add(Box.createVerticalStrut(10));
223                 //add(codePanel);
224         }
225         
226         public String getTextRepresentation()
227         {
228                 int selected = _cb.getSelectedIndex();
229                 if ((selected!=-1) && (selected<_cb.getItemCount()-1))
230                 {
231                         return ((NamedColorMapTypes) _cb.getSelectedItem()).getId(); 
232                 }
233                 else
234                 {
235                         return _gp.getColorMap().getParamEncoding();
236                 }
237         }
238         
239         public void cancelChanges()
240         {
241                 _vp.setColorMap(_backup);
242         }
243         
244         public ModeleColorMap getColorMap()
245         {
246                 return _gp.getColorMap();
247         }
248         
249         public void actionPerformed(ActionEvent e) {
250                 // TODO Auto-generated method stub
251                 
252         }
253         
254         
255         private void refreshCode()
256         {
257                 int selected = -1;
258                 NamedColorMapTypes n = null;
259                 for (int i=0;i<_cb.getItemCount()-1;i++)
260                 { 
261                         Object o = _cb.getItemAt(i);
262                         if (o instanceof NamedColorMapTypes)
263                         {
264                                 NamedColorMapTypes ni = (NamedColorMapTypes) o;
265                                 if (ni.getColorMap().equals(_gp.getColorMap()))
266                                 { 
267                                         selected = i;   n = ni;
268                                 }
269                         }
270                 }
271                 if (selected!=-1)
272                 {
273                         _code.setText(n.getId());
274                 }
275                 _code.setText(getTextRepresentation());
276                 _vp.setColorMap(_gp.getColorMap());
277                 _gp.repaint();
278         }
279
280         public void itemStateChanged(ItemEvent arg0) {
281                 if (arg0.getStateChange()==ItemEvent.SELECTED)
282                 {
283                 Object o = arg0.getItem();
284                 if (o instanceof ModeleColorMap.NamedColorMapTypes)
285                 {
286                         ModeleColorMap.NamedColorMapTypes n = ((ModeleColorMap.NamedColorMapTypes) o);
287                         ModeleColorMap m = n.getColorMap().clone();
288                         m.setMinValue(_backup.getMinValue());
289                         m.setMaxValue(_backup.getMaxValue());
290                         _gp.setColorMap(m);
291                         refreshCode();
292                 }
293                 }
294         }
295
296         public void propertyChange(PropertyChangeEvent arg0) {
297                 if (arg0.getPropertyName().equals("PaletteChanged"))
298                 {
299                         _cb.setSelectedIndex(_cb.getItemCount()-1);
300                         refreshCode();
301                 };
302         }
303
304
305 }