JAL-1668 added filtering and sorting capabilites
[jalview.git] / src / jalview / jbgui / GStructureChooser.java
1 package jalview.jbgui;
2
3 import jalview.gui.AlignmentPanel;
4 import jalview.gui.Desktop;
5 import jalview.util.MessageManager;
6 import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary;
7
8 import java.awt.BorderLayout;
9 import java.awt.CardLayout;
10 import java.awt.Dimension;
11 import java.awt.FlowLayout;
12 import java.awt.Image;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ItemEvent;
15 import java.awt.event.ItemListener;
16
17 import javax.swing.ImageIcon;
18 import javax.swing.JButton;
19 import javax.swing.JCheckBox;
20 import javax.swing.JComboBox;
21 import javax.swing.JFrame;
22 import javax.swing.JInternalFrame;
23 import javax.swing.JLabel;
24 import javax.swing.JList;
25 import javax.swing.JPanel;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTable;
28 import javax.swing.JTextField;
29 import javax.swing.ListSelectionModel;
30
31 @SuppressWarnings("serial")
32 public abstract class GStructureChooser extends JPanel implements
33         ItemListener
34 {
35   protected String frameTitle = "Structure Chooser";
36
37   protected JInternalFrame mainFrame = new JInternalFrame(frameTitle);
38
39
40
41   protected JComboBox<FilterOptions> filterOptionsComboBox = new JComboBox<FilterOptions>();
42
43   protected AlignmentPanel ap;
44
45   protected JLabel resultLabel = new JLabel("Select : ");
46
47   protected JButton ok = new JButton();
48
49   protected JButton cancel = new JButton();
50
51   protected JButton pdbFromFile = new JButton();
52
53   protected JTextField search = new JTextField(16);
54
55   protected JPanel actionPanel = new JPanel();
56
57   protected JPanel filterPanel = new JPanel();
58
59   protected JPanel idInputPanel = new JPanel();
60
61   protected JPanel fileChooserPanel = new JPanel();
62
63   protected JPanel switchableViewsPanel = new JPanel(new CardLayout());
64
65   protected CardLayout switchableViewsLayout = (CardLayout) (switchableViewsPanel
66           .getLayout());
67
68   protected BorderLayout mainLayout = new BorderLayout();
69
70   protected BorderLayout idInputPanelLayout = new BorderLayout();
71
72   protected BorderLayout fileChooserPanelLayout = new BorderLayout();
73
74   protected JCheckBox rememberSettings = new JCheckBox("Don't ask me again");
75
76   protected JCheckBox invertFilter = new JCheckBox("Invert");
77
78   protected ImageIcon loadingImage = new ImageIcon(getClass().getResource(
79           "/images/loading.gif"));
80
81   protected JLabel loadingImageLabel = new JLabel(loadingImage);
82
83   protected static final String VIEWS_FILTER = "VIEWS_FILTER";
84
85   protected static final String VIEWS_FROM_FILE = "VIEWS_FROM_FILE";
86
87   protected static final String VIEWS_ENTER_ID = "VIEWS_ENTER_ID";
88
89   protected JList<PDBResponseSummary> jListFoundStructures = new JList<PDBResponseSummary>();
90
91   // protected JScrollPane foundStructuresScroller = new JScrollPane(
92   // jListFoundStructures);
93
94   protected JTable summaryTable = new JTable();
95   protected JScrollPane foundStructuresScroller = new JScrollPane(
96           summaryTable);
97
98   public GStructureChooser()
99   {
100     try
101     {
102       jbInit();
103       mainFrame.setVisible(false);
104       mainFrame.invalidate();
105       mainFrame.pack();
106     } catch (Exception e)
107     {
108       e.printStackTrace();
109     }
110   }
111
112   private void jbInit() throws Exception
113   {
114     ok.setFont(new java.awt.Font("Verdana", 0, 12));
115     ok.setText(MessageManager.getString("action.view"));
116     ok.addActionListener(new java.awt.event.ActionListener()
117     {
118       public void actionPerformed(ActionEvent e)
119       {
120         ok_ActionPerformed();
121       }
122     });
123     cancel.setFont(new java.awt.Font("Verdana", 0, 12));
124     cancel.setText(MessageManager.getString("action.cancel"));
125     cancel.addActionListener(new java.awt.event.ActionListener()
126     {
127       public void actionPerformed(ActionEvent e)
128       {
129         mainFrame.dispose();
130       }
131     });
132
133     pdbFromFile.setFont(new java.awt.Font("Verdana", 0, 12));
134     pdbFromFile.setText("             Select PDB File             ");
135     pdbFromFile.addActionListener(new java.awt.event.ActionListener()
136     {
137       public void actionPerformed(ActionEvent e)
138       {
139         pdbFromFile_actionPerformed();
140       }
141     });
142
143     jListFoundStructures
144             .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
145     jListFoundStructures.setLayoutOrientation(JList.VERTICAL);
146     jListFoundStructures.setVisibleRowCount(-1);
147     foundStructuresScroller.setPreferredSize(new Dimension(500, 300));
148     foundStructuresScroller
149             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
150
151     filterOptionsComboBox.setFont(new java.awt.Font("Verdana", 0, 12));
152     invertFilter.setFont(new java.awt.Font("Verdana", 0, 12));
153     rememberSettings.setFont(new java.awt.Font("Verdana", 0, 12));
154
155     filterOptionsComboBox.addItemListener(this);
156     invertFilter.addItemListener(this);
157
158     actionPanel.add(rememberSettings);
159     actionPanel.add(ok);
160     actionPanel.add(cancel);
161
162
163     filterPanel.add(resultLabel);
164     filterPanel.add(filterOptionsComboBox);
165     loadingImageLabel.setVisible(false);
166
167     filterPanel.add(loadingImageLabel);
168
169     filterPanel.add(invertFilter);
170
171     idInputPanel.setLayout(new FlowLayout());
172     idInputPanel.add(search);
173
174
175     fileChooserPanel.setLayout(new FlowLayout());
176     fileChooserPanel.add(pdbFromFile);
177
178     switchableViewsPanel.add(fileChooserPanel, VIEWS_FROM_FILE);
179     switchableViewsPanel.add(idInputPanel, VIEWS_ENTER_ID);
180     switchableViewsPanel.add(foundStructuresScroller, VIEWS_FILTER);
181
182     
183     this.setLayout(mainLayout);
184     this.add(filterPanel, java.awt.BorderLayout.NORTH);
185     this.add(switchableViewsPanel, java.awt.BorderLayout.CENTER);
186     this.add(actionPanel, java.awt.BorderLayout.SOUTH);
187
188     mainFrame.setVisible(true);
189     mainFrame.setContentPane(this);
190     mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
191     Desktop.addInternalFrame(mainFrame, frameTitle, 800, 400);
192   }
193
194   
195   private ImageIcon scaleImageIcone(ImageIcon imageIcon, int width, int height)
196   {
197     Image image = imageIcon.getImage(); // transform it
198
199     Image newimg = image.getScaledInstance(width, height,
200             java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
201
202     return new ImageIcon(newimg);
203   }
204   
205   @Override
206   public void itemStateChanged(ItemEvent e)
207   {
208     stateChanged(e);
209   }
210
211   public class FilterOptions
212   {
213     private String name;
214
215     private String value;
216
217     private String view;
218
219     public FilterOptions(String name, String value, String view)
220     {
221       this.name = name;
222       this.value = value;
223       this.view = view;
224     }
225
226     // public FilterOptions(PDBDocField field, String view)
227     // {
228     // this.name = "Best " + field.getName();
229     // this.value = field.getCode();
230     // this.view = view;
231     // }
232
233     public String getName()
234     {
235       return name;
236     }
237
238     public void setName(String name)
239     {
240       this.name = name;
241     }
242
243     public String getValue()
244     {
245       return value;
246     }
247
248     public void setValue(String value)
249     {
250       this.value = value;
251     }
252
253     public String getView()
254     {
255       return view;
256     }
257
258     public void setView(String view)
259     {
260       this.view = view;
261     }
262
263     public String toString()
264     {
265       return this.name;
266     }
267   }
268
269   protected abstract void stateChanged(ItemEvent e);
270
271   protected abstract void updateCurrentView();
272
273   protected abstract void ok_ActionPerformed();
274
275   protected abstract void populateFilterOptions();
276
277   protected abstract void pdbFromFile_actionPerformed();
278 }