001/*
002 *                  BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Feb 9, 2007
021 *
022 */
023package org.biojava.nbio.structure.gui.util;
024
025import org.biojava.nbio.structure.Chain;
026import org.biojava.nbio.structure.Structure;
027import org.biojava.nbio.structure.StructureException;
028import org.biojava.nbio.structure.StructureImpl;
029import org.biojava.nbio.structure.align.util.UserConfiguration;
030import org.biojava.nbio.structure.io.PDBFileReader;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import javax.swing.*;
035import java.awt.*;
036import java.awt.event.ActionEvent;
037import java.io.File;
038import java.io.IOException;
039
040/** A class to define where a structure for the alignment is coming from
041 *
042 * @author Andreas Prlic
043 * @since 1.7
044 * @version %I% %G%
045 */
046public class PDBDirPanel
047extends JPanel
048implements StructurePairSelector{
049
050        /**
051         *
052         */
053        private static final long serialVersionUID = -5682120627824627408L;
054
055        boolean debug = true;
056        JTextField pdbDir;
057        JTextField f1;
058        JTextField f2;
059        JTextField c1;
060        JTextField c2;
061
062        private static final Logger logger = LoggerFactory.getLogger(StructurePairSelector.class);
063
064        /** load the PDB files from a local directory
065         *
066         */
067        public PDBDirPanel() {
068
069                Box vBox = Box.createVerticalBox();
070
071                pdbDir = new JTextField(20);
072
073                String conf = System.getProperty(UserConfiguration.PDB_DIR);
074                if ( conf != null){
075                        pdbDir.setText(conf);
076                }
077                JPanel dir = getPDBDirPanel(pdbDir);
078                vBox.add(dir);
079
080
081                int pdbfSize = 4;
082
083                f1 = new JTextField(pdbfSize);
084                c1 = new JTextField(1);
085                JPanel p1 = getPDBFilePanel(1,f1,c1);
086                vBox.add(p1);
087
088                f2 = new JTextField(pdbfSize);
089                c2 = new JTextField(1);
090                JPanel p2 = getPDBFilePanel(2, f2,c2);
091                vBox.add(p2);
092
093
094                this.add(vBox);
095
096        }
097
098
099        private Structure fromPDB(JTextField f, JTextField c) throws StructureException{
100                String pdb = f.getText();
101
102
103                if ( pdb.length() < 4) {
104                        f.setText("!!!");
105                        return null;
106                }
107
108                String chain = c.getText();
109                if ( debug )
110                        System.out.println("file :" + pdb + " " +  chain);
111                /// prepare structures
112
113                // load them from the file system
114
115
116
117                String dir = pdbDir.getText();
118                PDBFileReader reader = new PDBFileReader(dir);
119
120                if ( debug )
121                        System.out.println("dir: " + dir);
122
123                Structure tmp1 = new StructureImpl();
124
125                try {
126                        Structure structure1 = reader.getStructureById(pdb);
127
128                        // no chain has been specified
129                        // return whole structure
130                        if (( chain == null) || (chain.length()==0)){
131                                return structure1;
132                        }
133                        if ( debug)
134                                System.out.println("using chain " + chain +  " for structure " + structure1.getPDBCode());
135                        Chain c1 = structure1.findChain(chain);
136                        tmp1.setPDBCode(structure1.getPDBCode());
137                        tmp1.setPDBHeader(structure1.getPDBHeader());
138                        tmp1.setPDBCode(structure1.getPDBCode());
139                        tmp1.addChain(c1);
140                        System.out.println("ok");
141
142                } catch (IOException e){
143                        logger.warn(e.getMessage());
144                        throw new StructureException(e);
145                }
146                return tmp1;
147        }
148
149
150
151        @Override
152        public Structure getStructure1() throws StructureException{
153                return fromPDB(f1,c1);
154        }
155
156        @Override
157        public Structure getStructure2() throws StructureException{
158                return fromPDB(f2,c2);
159        }
160
161
162        private JPanel getPDBDirPanel(JTextField f){
163                JPanel panel = new JPanel();
164                panel.setBorder(BorderFactory.createLineBorder(Color.black));
165
166
167                JLabel l01 = new JLabel("Select PDB directory");
168                panel.add(l01);
169
170                panel.add(f);
171
172                Action action = new ChooseDirAction(pdbDir);
173
174                JButton chooser = new JButton(action);
175                panel.add(chooser);
176                return panel;
177        }
178
179        private JPanel getPDBFilePanel(int pos ,JTextField f, JTextField c){
180
181                JPanel panel = new JPanel();
182                panel.setBorder(BorderFactory.createLineBorder(Color.black));
183
184
185
186                JLabel l01 = new JLabel("PDB code ");
187
188                panel.add(l01);
189                Box hBox11 = Box.createHorizontalBox();
190
191                JLabel l11 = new JLabel(pos + ":");
192
193
194
195                f.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
196
197
198                hBox11.add(l11);
199                hBox11.add(Box.createVerticalGlue());
200                hBox11.add(f, BorderLayout.CENTER);
201                hBox11.add(Box.createVerticalGlue());
202
203                panel.add(hBox11);
204
205                Box hBox21 = Box.createHorizontalBox();
206                JLabel l21 = new JLabel("Chain" + pos + ":");
207
208                c.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
209                hBox21.add(l21);
210                hBox21.add(Box.createGlue());
211                hBox21.add(c, BorderLayout.CENTER);
212                hBox21.add(Box.createGlue());
213
214                panel.add(hBox21);
215
216                return panel;
217        }
218
219}
220
221class ChooseDirAction extends AbstractAction{
222
223        JTextField textField;
224        public ChooseDirAction (JTextField textField){
225                super("Choose");
226                this.textField = textField;
227        }
228        public static final long serialVersionUID = 0l;
229        // This method is called when the button is pressed
230        @Override
231        public void actionPerformed(ActionEvent evt) {
232                // Perform action...
233                JFileChooser chooser = new JFileChooser();
234                String txt = textField.getText();
235                if ( txt != null){
236                        chooser.setCurrentDirectory(new java.io.File(txt));
237                }
238                chooser.setDialogTitle("Choose directory that contains your PDB files");
239                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
240                //
241                // disable the "All files" option.
242                //
243                chooser.setAcceptAllFileFilterUsed(false);
244                //
245
246
247
248//              In response to a button click:
249                int returnVal = chooser.showOpenDialog(null);
250                if ( returnVal == JFileChooser.APPROVE_OPTION) {
251                        File file = chooser.getSelectedFile();
252                        textField.setText(file.getAbsolutePath());
253                        textField.repaint();
254                }
255
256        }
257}
258
259