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 */
021package org.biojava.nbio.structure.align.gui;
022
023import java.awt.BorderLayout;
024import java.awt.Dimension;
025import java.io.IOException;
026import java.util.ArrayList;
027
028import javax.swing.Box;
029import javax.swing.JLabel;
030import javax.swing.JPanel;
031import javax.swing.JTabbedPane;
032import javax.swing.JTextField;
033
034import org.biojava.nbio.structure.PdbId;
035import org.biojava.nbio.structure.ResidueRange;
036import org.biojava.nbio.structure.Structure;
037import org.biojava.nbio.structure.StructureException;
038import org.biojava.nbio.structure.StructureIdentifier;
039import org.biojava.nbio.structure.SubstructureIdentifier;
040import org.biojava.nbio.structure.align.util.AtomCache;
041import org.biojava.nbio.structure.align.util.UserConfiguration;
042import org.biojava.nbio.structure.align.webstart.WebStartMain;
043import org.biojava.nbio.structure.gui.util.StructurePairSelector;
044
045
046/**
047 * A Panel that allows user to specify PDB and chain ID, as well as sub-ranges
048 *
049 * @author Andreas
050 *
051 */
052public class SelectPDBPanel
053extends JPanel
054implements StructurePairSelector{
055
056        boolean debug = true;
057
058        JTextField f1;
059        JTextField f2;
060        JTextField c1;
061        JTextField c2;
062        JTextField r1;
063        JTextField r2;
064
065        UserConfiguration config;
066        JTabbedPane configPane;
067
068        /**
069         *
070         */
071        private static final long serialVersionUID = 4002475313717172193L;
072
073
074
075        public SelectPDBPanel(){
076                this(true);
077        }
078        public SelectPDBPanel(boolean show2PDBs) {
079
080                Box vBox = Box.createVerticalBox();
081
082                JLabel help = new JLabel("Optional: specify chain ID or range.");
083                Box hBox1 = Box.createHorizontalBox();
084                hBox1.add(Box.createGlue());
085                hBox1.add(help);
086                vBox.add(hBox1);
087
088
089                //pdbDir = new JTextField(20);
090
091                int pdbfSize = 4;
092
093                f1 = new JTextField(pdbfSize);
094                c1 = new JTextField(1);
095                r1 = new JTextField(5);
096                Box p1 = getPDBFilePanel(1,f1,c1,r1);
097                vBox.add(p1);
098
099                f2 = new JTextField(pdbfSize);
100                c2 = new JTextField(1);
101                r2 = new JTextField(5);
102                Box p2 = getPDBFilePanel(2, f2,c2,r2);
103
104                if ( show2PDBs)
105                        vBox.add(p2);
106
107                //vBox.setBorder(BorderFactory.createLineBorder(Color.black));
108                this.add(vBox);
109        }
110
111        public StructureIdentifier getName1() {
112                String id = f1.getText().trim();
113                String chainId = c1.getText().trim();
114                String range = r1.getText().trim();
115                
116                PdbId pdbId = new PdbId(id);
117                // Prefer range over chain
118                if( ! range.isEmpty() ) {
119                        return new SubstructureIdentifier(pdbId, ResidueRange.parseMultiple(range));
120                } else if ( ! chainId.isEmpty() ){
121                        return new SubstructureIdentifier(pdbId, ResidueRange.parseMultiple(chainId));
122                }
123                return new SubstructureIdentifier(pdbId, new ArrayList<ResidueRange>());
124        }
125        public StructureIdentifier getName2() {
126                String id = f2.getText().trim();
127                String chainId = c2.getText().trim();
128                String range = r2.getText().trim();
129                
130                PdbId pdbId = new PdbId(id);
131                // Prefer range over chain
132                if( ! range.isEmpty() ) {
133                        return new SubstructureIdentifier(pdbId, ResidueRange.parseMultiple(range));
134                } else if ( ! chainId.isEmpty() ){
135                        return new SubstructureIdentifier(pdbId, ResidueRange.parseMultiple(chainId));
136                }
137                return new SubstructureIdentifier(pdbId, new ArrayList<ResidueRange>());
138        }
139        @Override
140        public Structure getStructure1() throws StructureException, IOException{
141                return getStructure(getName1());
142        }
143
144        @Override
145        public Structure getStructure2() throws StructureException, IOException{
146                return getStructure(getName2());
147        }
148
149        private Structure getStructure(StructureIdentifier name) throws IOException, StructureException {
150                UserConfiguration config = WebStartMain.getWebStartConfig();
151                AtomCache cache = new AtomCache(config);
152                return cache.getStructure(name);
153        }
154
155        private Box getPDBFilePanel(int pos ,JTextField f, JTextField c, JTextField r){
156
157                //JPanel panel = new JPanel();
158                //panel.setBorder(BorderFactory.createLineBorder(Color.black));
159
160                JLabel l01 = new JLabel("PDB code ");
161
162                //panel.add(l01);
163                Box hBox = Box.createHorizontalBox();
164                hBox.add(Box.createGlue());
165                hBox.add(l01);
166
167                JLabel l11 = new JLabel(pos + ":");
168                f.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
169                f.setToolTipText("Provide 4-character PDB code here. Example: 4hhb");
170                hBox.add(l11);
171                hBox.add(Box.createVerticalGlue());
172                hBox.add(f, BorderLayout.CENTER);
173                hBox.add(Box.createGlue());
174
175                //panel.add(hBox11);
176
177                //Box hBox21 = Box.createHorizontalBox();
178                JLabel l21 = new JLabel("Chain" + pos + ":");
179                hBox.add(l21);
180
181                c.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
182                //hBox.add(Box.createGlue());
183                hBox.add(c, BorderLayout.CENTER);
184
185                String msg1 = "Both chainID and range specification are optional. If both are provided, range has preference.";
186                l21.setToolTipText(msg1);
187                c.setToolTipText(msg1);
188
189                JLabel rangeL = new JLabel(" Range " + pos + ":");
190                hBox.add(Box.createGlue());
191                hBox.add(rangeL);
192                r.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
193
194                // set help text:
195                String msg ="Syntax example: A:407-495,A:582-686";
196                rangeL.setToolTipText(msg);
197                r.setToolTipText(msg);
198
199                //hBox.add(Box.createGlue());
200                hBox.add(r,BorderLayout.CENTER);
201
202                //hBox21.add(Box.createGlue());
203
204                //panel.add(hBox21);
205
206
207
208                return hBox;
209        }
210
211
212
213
214
215}