6a583352630bae06512f9fd51bdda8840e311a64
[jalview.git] / src / jalview / jbgui / SearchPanel.java
1 package jalview.jbgui;
2
3 import jalview.gui.AnnotationColumnChooser;
4 import jalview.gui.JvSwingUtils;
5
6 import java.awt.Color;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9
10 import javax.swing.JCheckBox;
11 import javax.swing.JPanel;
12 import javax.swing.JTextField;
13 import javax.swing.border.TitledBorder;
14 import javax.swing.event.DocumentEvent;
15 import javax.swing.event.DocumentListener;
16
17 @SuppressWarnings("serial")
18 public class SearchPanel extends JPanel
19 {
20
21   private AnnotationColumnChooser aColChooser;
22
23   private JCheckBox displayName = new JCheckBox();
24
25   private JCheckBox description = new JCheckBox();
26
27   private JTextField searchBox = new JTextField(10);
28
29   private JCheckBox structuresFilter = new JCheckBox();
30
31   public SearchPanel(AnnotationColumnChooser aColChooser)
32   {
33
34     this.aColChooser = aColChooser;
35     this.setBorder(new TitledBorder("Search Filter"));
36     this.setBackground(Color.white);
37     this.setFont(JvSwingUtils.getLabelFont());
38
39
40     getSearchBox().setBackground(Color.white);
41     getSearchBox().setFont(JvSwingUtils.getLabelFont());
42     getSearchBox().getDocument().addDocumentListener(new DocumentListener()
43     {
44       @Override
45       public void insertUpdate(DocumentEvent e)
46       {
47         searchStringAction();
48       }
49
50       @Override
51       public void removeUpdate(DocumentEvent e)
52       {
53         searchStringAction();
54       }
55
56       @Override
57       public void changedUpdate(DocumentEvent e)
58       {
59         searchStringAction();
60       }
61     });
62
63     getDisplayName().setBackground(Color.white);
64     getDisplayName().setFont(JvSwingUtils.getLabelFont());
65     getDisplayName().setText("Display Name");
66     getDisplayName().setEnabled(false);
67     getDisplayName().addActionListener(new ActionListener()
68     {
69       @Override
70       public void actionPerformed(ActionEvent actionEvent)
71       {
72         displayNameCheckboxAction();
73       }
74     });
75
76     getDescription().setBackground(Color.white);
77     getDescription().setFont(JvSwingUtils.getLabelFont());
78     getDescription().setText("Description");
79     getDescription().setEnabled(false);
80     getDescription().addActionListener(new ActionListener()
81     {
82       @Override
83       public void actionPerformed(ActionEvent actionEvent)
84       {
85         discriptionCheckboxAction();
86       }
87     });
88
89     syncState();
90     this.add(getSearchBox());
91     this.add(getDisplayName());
92     this.add(getDescription());
93   }
94
95   public boolean isDescriptionChecked()
96   {
97     return getDescription().isSelected();
98   }
99
100   public boolean isDisplayNameChecked()
101   {
102     return getDisplayName().isSelected();
103   }
104
105   public String getSearchString()
106   {
107     return getSearchBox().getText();
108   }
109
110   public void displayNameCheckboxAction()
111   {
112     aColChooser.setCurrentSearchPanel(this);
113     aColChooser.updateView();
114   }
115
116   public void discriptionCheckboxAction()
117   {
118     aColChooser.setCurrentSearchPanel(this);
119     aColChooser.updateView();
120   }
121
122   public void searchStringAction()
123   {
124     aColChooser.setCurrentSearchPanel(this);
125     aColChooser.updateView();
126   }
127
128   public JCheckBox getDisplayName()
129   {
130     return displayName;
131   }
132
133   public void setDisplayName(JCheckBox displayName)
134   {
135     this.displayName = displayName;
136   }
137
138   public JCheckBox getDescription()
139   {
140     return description;
141   }
142
143   public void setDescription(JCheckBox description)
144   {
145     this.description = description;
146   }
147
148   public JTextField getSearchBox()
149   {
150     return searchBox;
151   }
152
153   public void setSearchBox(JTextField searchBox)
154   {
155     this.searchBox = searchBox;
156   }
157
158   public JCheckBox getStructuresFilter()
159   {
160     return structuresFilter;
161   }
162
163   public void setStructuresFilter(JCheckBox structuresFilter)
164   {
165     this.structuresFilter = structuresFilter;
166   }
167
168   public void syncState()
169   {
170     SearchPanel sp = aColChooser.getCurrentSearchPanel();
171     if (sp != null)
172     {
173       description.setEnabled(sp.getDescription().isEnabled());
174       description.setSelected(sp.getDescription().isSelected());
175
176       displayName.setEnabled(sp.getDisplayName().isEnabled());
177       displayName.setSelected(sp.getDisplayName().isSelected());
178
179       searchBox.setText(sp.getSearchBox().getText());
180     }
181   }
182 }