3bfb27a3099c2c0da7a4e894ad8629d177f07f53
[jalview.git] / srcjar / fr / orsay / lri / varna / views / VueLoadColorMapValues.java
1 package fr.orsay.lri.varna.views;
2
3 import java.awt.CardLayout;
4 import java.awt.Dimension;
5 import java.awt.GridLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.io.FileNotFoundException;
9 import java.io.FileReader;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.io.Reader;
14 import java.net.MalformedURLException;
15 import java.net.URL;
16 import java.net.URLConnection;
17
18 import javax.swing.ButtonGroup;
19 import javax.swing.JButton;
20 import javax.swing.JCheckBox;
21 import javax.swing.JFileChooser;
22 import javax.swing.JLabel;
23 import javax.swing.JPanel;
24 import javax.swing.JRadioButton;
25 import javax.swing.JTextField;
26
27 import fr.orsay.lri.varna.VARNAPanel;
28
29 public class VueLoadColorMapValues extends JPanel implements ActionListener {
30         /**
31          * 
32          */
33         private static final long serialVersionUID = -1648400107478203724L;
34         VARNAPanel _vp;
35         
36   public VueLoadColorMapValues(VARNAPanel vp)
37   {
38           _vp = vp;
39           init();
40   }
41   JRadioButton urlCB = new JRadioButton("URL"); 
42   JRadioButton fileCB = new JRadioButton("File");
43   JPanel urlAux = new JPanel();
44   JPanel fileAux = new JPanel();
45   CardLayout l = new CardLayout();
46   JPanel input = new JPanel();
47   JTextField urlTxt = new JTextField(); 
48   JTextField fileTxt = new JTextField(); 
49   JButton load = new JButton("Choose file");
50   
51   private void init()
52   {
53           setLayout(new GridLayout(2,1));
54           JPanel choice = new JPanel();
55           urlCB.addActionListener(this);
56           fileCB.addActionListener(this);
57           ButtonGroup group = new ButtonGroup();
58           group.add(urlCB);
59           group.add(fileCB);
60           choice.add(new JLabel("Choose input source:"));
61           choice.add(urlCB);
62           choice.add(fileCB);
63           input.setLayout(l);
64           urlTxt.setPreferredSize(new Dimension(300,30));
65           fileTxt.setPreferredSize(new Dimension(300,30));
66           urlAux.add(urlTxt);
67           fileAux.add(fileTxt);
68           fileAux.add(load);
69           input.add(fileAux,"file");
70           input.add(urlAux,"url");
71           group.setSelected(fileCB.getModel(), true);
72           load.addActionListener(this);
73           this.add(choice);
74           this.add(input);
75   }
76
77 public void actionPerformed(ActionEvent e) {
78         if (e.getSource() instanceof JRadioButton)
79         {
80                 if (urlCB.isSelected())
81                 {
82                         l.show(input, "url");
83                 }
84                 else
85                 {
86                         l.show(input, "file");
87                 }
88         }
89         else if (e.getSource() instanceof JButton)
90         {
91                 JFileChooser fc = new JFileChooser();
92                 if (fc.showOpenDialog(_vp) == JFileChooser.APPROVE_OPTION)
93                 {
94                         this.fileTxt.setText(fc.getSelectedFile().getAbsolutePath());
95                 }
96         }
97 }
98
99 public Reader getReader() throws IOException
100 {
101         if (urlCB.isSelected())
102         {
103                 URL url = new URL(urlTxt.getText());
104                 URLConnection connexion = url.openConnection();
105                 connexion.setUseCaches(false);
106                 InputStream r = connexion.getInputStream();
107                 return new InputStreamReader(r);
108         }
109         else
110         {
111           return new FileReader(fileTxt.getText());
112         }
113         
114 }
115
116 }