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