1 package fr.orsay.lri.varna.views;
3 import java.awt.BorderLayout;
5 import java.awt.Cursor;
6 import java.awt.Dimension;
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;
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;
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;
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;
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);
63 case JOptionPane.YES_OPTION:
64 super.approveSelection();
66 case JOptionPane.NO_OPTION:
68 case JOptionPane.CLOSED_OPTION:
70 case JOptionPane.CANCEL_OPTION:
75 super.approveSelection();
79 public VueColorMapStyle(VARNAPanel vp)
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);
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(){
102 public void focusGained(FocusEvent arg0) {
103 _code.setSelectionStart(0);
104 _code.setSelectionEnd(_code.getText().length());
107 public void focusLost(FocusEvent arg0) {
110 _code.setVisible(false);
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());
118 Object[] finalArray = new Object[palettes.length+1];
120 for (int i=0;i<palettes.length;i++)
122 if (palettes[i].getColorMap().equals(_vp.getColorMap()))
125 //System.out.println(selected);
127 finalArray[i] = palettes[i];
129 String custom = new String("Custom...");
130 finalArray[palettes.length] = custom;
131 _cb = new JComboBox(finalArray);
134 _cb.setSelectedIndex(selected);
138 _cb.setSelectedItem(finalArray.length-1);
140 _cb.addItemListener(this);
142 _code.setText(getTextRepresentation());
146 FileFilter CMSFiles = new FileFilter(){
147 public boolean accept(File f) {
148 return f.getName().toLowerCase().endsWith(".cms") || f.isDirectory();
151 public String getDescription() {
152 return "Color Map (*.cms) Files";
156 fc.addChoosableFileFilter(CMSFiles);
157 fc.setFileFilter(CMSFiles);
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)
167 File file = fc.getSelectedFile();
169 FileInputStream fis = new FileInputStream(file);
170 byte[] data = new byte[(int) file.length()];
173 String str = new String(data).trim();
174 ModeleColorMap ns = ModeleColorMap.parseColorMap(str);
177 } catch (FileNotFoundException e1) {
178 e1.printStackTrace();
179 } catch (IOException e1) {
180 e1.printStackTrace();
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)
192 PrintWriter out = new PrintWriter(fc.getSelectedFile());
193 out.println(_gp.getColorMap().getParamEncoding());
195 } catch (FileNotFoundException e1) {
196 e1.printStackTrace();
197 } catch (IOException e1) {
198 e1.printStackTrace();
204 saveStyleButton.setAlignmentX(CENTER_ALIGNMENT);
205 loadStyleButton.setAlignmentX(CENTER_ALIGNMENT);
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);
217 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
219 add(Box.createVerticalStrut(10));
221 add(gradientCaption);
222 //add(Box.createVerticalStrut(10));
226 public String getTextRepresentation()
228 int selected = _cb.getSelectedIndex();
229 if ((selected!=-1) && (selected<_cb.getItemCount()-1))
231 return ((NamedColorMapTypes) _cb.getSelectedItem()).getId();
235 return _gp.getColorMap().getParamEncoding();
239 public void cancelChanges()
241 _vp.setColorMap(_backup);
244 public ModeleColorMap getColorMap()
246 return _gp.getColorMap();
249 public void actionPerformed(ActionEvent e) {
250 // TODO Auto-generated method stub
255 private void refreshCode()
258 NamedColorMapTypes n = null;
259 for (int i=0;i<_cb.getItemCount()-1;i++)
261 Object o = _cb.getItemAt(i);
262 if (o instanceof NamedColorMapTypes)
264 NamedColorMapTypes ni = (NamedColorMapTypes) o;
265 if (ni.getColorMap().equals(_gp.getColorMap()))
267 selected = i; n = ni;
273 _code.setText(n.getId());
275 _code.setText(getTextRepresentation());
276 _vp.setColorMap(_gp.getColorMap());
280 public void itemStateChanged(ItemEvent arg0) {
281 if (arg0.getStateChange()==ItemEvent.SELECTED)
283 Object o = arg0.getItem();
284 if (o instanceof ModeleColorMap.NamedColorMapTypes)
286 ModeleColorMap.NamedColorMapTypes n = ((ModeleColorMap.NamedColorMapTypes) o);
287 ModeleColorMap m = n.getColorMap().clone();
288 m.setMinValue(_backup.getMinValue());
289 m.setMaxValue(_backup.getMaxValue());
296 public void propertyChange(PropertyChangeEvent arg0) {
297 if (arg0.getPropertyName().equals("PaletteChanged"))
299 _cb.setSelectedIndex(_cb.getItemCount()-1);