1 /********************************************************************
2 *
3 *  This library is free software; you can redistribute it and/or
4 *  modify it under the terms of the GNU Library General Public
5 *  License as published by the Free Software Foundation; either
6 *  version 2 of the License, or (at your option) any later version.
7 *
8 *  This library is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 *  Library General Public License for more details.
12 *
13 *  You should have received a copy of the GNU Library General Public
14 *  License along with this library; if not, write to the
15 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 *  Boston, MA  02111-1307, USA.
17 *
18 *  Copyright (C) Genome Research Limited
19 *
20 ********************************************************************/
21 package uk.ac.sanger.artemis.components.filetree;
22 
23 import java.awt.Dimension;
24 import java.awt.Font;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 
30 import javax.swing.Box;
31 import javax.swing.JButton;
32 import javax.swing.JComboBox;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JSeparator;
36 import javax.swing.JTextField;
37 
38 import uk.ac.sanger.artemis.components.KeyChoice;
39 import uk.ac.sanger.artemis.io.GFFEntryInformation;
40 import uk.ac.sanger.artemis.util.DatabaseDocument;
41 
42 /**
43  * This displays the feature key information used to split the data read
44  * from the database into separate Artemis entries. The name of each entry
45  * is displayed along with an editable list of feature keys that belong
46  * to that entry.
47  */
48 public class DatabaseEntryFilterPanel extends JPanel
49 {
50   private static final long serialVersionUID = 1L;
51   private JTextField[] nameField;
52   private JComboBox[] keyList;
53 
DatabaseEntryFilterPanel()54   public DatabaseEntryFilterPanel()
55   {
56     super(new GridBagLayout());
57 
58     final GridBagConstraints c = new GridBagConstraints();
59 
60     // current feature types in each entry
61     String types[][][] = DatabaseDocument.getTYPES();
62     nameField = new JTextField[types.length];
63     keyList = new JComboBox[types.length];
64 
65     c.gridx = 1;
66     c.gridy = 0;
67     c.anchor = GridBagConstraints.WEST;
68     JLabel nameLabel = new JLabel("Entry Name");
69     Font fontBold = nameLabel.getFont().deriveFont(Font.BOLD);
70     nameLabel.setFont(fontBold);
71     add(nameLabel, c);
72     c.gridx = 2;
73     JLabel featureKeyLabel = new JLabel("Feature Key List");
74     featureKeyLabel.setFont(fontBold);
75     add(featureKeyLabel, c);
76     c.gridx = 3;
77     c.gridwidth = 3;
78     JLabel featureKeyEditLabel = new JLabel("Edit Feature Key List");
79     featureKeyEditLabel.setFont(fontBold);
80     add(featureKeyEditLabel, c);
81     c.gridwidth = 1;
82 
83     c.gridy = 1;
84     c.gridx = 0;
85     c.fill  = GridBagConstraints.BOTH;
86     c.gridwidth = 6;
87     add(new JSeparator(), c);
88     c.gridwidth = 1;
89     c.gridy = 2;
90     add(Box.createVerticalStrut(10), c);
91     c.fill  = GridBagConstraints.NONE;
92 
93     c.gridx = 0;
94     c.gridy = 3;
95     add(new JLabel("1 "), c);
96     c.gridx = 1;
97     add(new JLabel("Default"), c);
98 
99     for(int i = 0; i < types.length; i++)
100     {
101       final KeyChoice keyChoice = new KeyChoice(new GFFEntryInformation());
102       int gridx = 0;
103 
104       c.gridx = gridx;
105       c.gridy = i + 4;
106 
107       add(new JLabel(Integer.toString(i+2)), c);
108 
109       c.gridx = ++gridx;
110       nameField[i] = new JTextField(types[i][0][0], 20);
111       add(nameField[i], c);
112 
113       keyList[i] = new JComboBox(types[i][1]);
114       keyList[i].setPreferredSize(new Dimension(
115           keyChoice.getPreferredSize().width, keyList[i].getPreferredSize().height));
116       final int MAX_VISIBLE_ROWS = 30;
117       keyList[i].setMaximumRowCount (MAX_VISIBLE_ROWS);
118       keyList[i].setEditable(false);
119       final JComboBox thiskeyList = keyList[i];
120 
121       c.gridx = ++gridx;
122       add(keyList[i], c);
123       c.gridx = ++gridx;
124       JButton deleteKey = new JButton("Delete");
125       add(deleteKey, c);
126       deleteKey.addActionListener(new ActionListener()
127       {
128         public void actionPerformed(ActionEvent arg0)
129         {
130           thiskeyList.removeItem(thiskeyList.getSelectedItem());
131           thiskeyList.revalidate();
132         }
133       });
134 
135       c.gridx = ++gridx;
136       JButton addKey = new JButton("Add :");
137       add(addKey, c);
138       c.gridx = ++gridx;
139 
140       add(keyChoice, c);
141 
142 
143       addKey.addActionListener(new ActionListener()
144       {
145         public void actionPerformed(ActionEvent arg0)
146         {
147           for(int i=0; i<thiskeyList.getItemCount(); i++)
148             if(thiskeyList.getItemAt(i).equals(keyChoice.getSelectedItem()))
149               return;
150           thiskeyList.addItem(keyChoice.getSelectedItem().getKeyString());
151           thiskeyList.setSelectedItem(keyChoice.getSelectedItem().getKeyString());
152         }
153       });
154     }
155   }
156 
157   /**
158    * Method to set the Entry names (from the nameField text fields) and
159    * set the feature types in each entry (based on keyList values).
160    */
setTypesForEntries()161   protected void setTypesForEntries()
162   {
163     String newTypes[][][] = new String[nameField.length][2][];
164     for(int i=0; i<nameField.length; i++)
165     {
166       newTypes[i][0] = new String[1];
167       newTypes[i][0][0] = nameField[i].getText().trim() ;
168 
169       String[] keys = new String[keyList[i].getItemCount()];
170       //System.out.print(newTypes[i][0][0]);
171 
172       for(int j=0; j<keyList[i].getItemCount(); j++)
173       {
174         keys[j] = (String) keyList[i].getItemAt(j);
175         //System.out.print(" "+keys[j]);
176       }
177       //System.out.println();
178       newTypes[i][1] = keys;
179     }
180     DatabaseDocument.setTYPES(newTypes);
181   }
182 
183 }