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 at May 25, 2008
021 */
022package org.biojava.nbio.structure.gui.util;
023
024
025import org.biojava.nbio.structure.Structure;
026import org.biojava.nbio.structure.StructureException;
027import org.biojava.nbio.structure.StructureTools;
028import org.biojava.nbio.structure.align.util.UserConfiguration;
029import org.biojava.nbio.structure.io.MMCIFFileReader;
030import org.biojava.nbio.structure.io.PDBFileReader;
031import org.biojava.nbio.structure.io.StructureIOFile;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035import javax.swing.*;
036import java.awt.*;
037import java.awt.event.ActionEvent;
038import java.io.File;
039import java.io.IOException;
040import java.net.URL;
041
042/** A JPanel to upload 2 custom PDB files.
043 *
044 * @author Andreas Prlic
045 * @since 1.7
046 */
047public class PDBUploadPanel
048extends JPanel
049implements StructurePairSelector {
050
051        /**
052         *
053         */
054        private static final long serialVersionUID = 1L;
055
056
057
058        private static final Logger logger = LoggerFactory.getLogger(PDBUploadPanel.class);
059
060
061
062        private JComboBox fileType ;
063
064        JTextField filePath1;
065        JTextField filePath2;
066        JTextField chain1;
067        JTextField chain2;
068
069        public static JComboBox getFileFormatSelect(){
070                JComboBox fileType = new JComboBox();
071                        fileType = new JComboBox(new String[] {UserConfiguration.PDB_FORMAT,UserConfiguration.MMCIF_FORMAT});
072                        fileType.setSelectedIndex(0);
073                        fileType.setMaximumSize(new Dimension(10,50));
074
075                return fileType;
076        }
077        public PDBUploadPanel(){
078                this(true);
079        }
080        public PDBUploadPanel(boolean show2boxes){
081                Box vBox = Box.createVerticalBox();
082
083                filePath1 = new JTextField(20);
084                filePath2 = new JTextField(20);
085                chain1 = new JTextField(1);
086                chain2 = new JTextField(1);
087
088                JPanel p1 = getLocalFilePanel(1,filePath1,chain1);
089                JPanel p2 = getLocalFilePanel(2,filePath2,chain2);
090
091                vBox.add(p1);
092                if ( show2boxes)
093                        vBox.add(p2);
094
095                JLabel ftype = new JLabel("File format:");
096                Box hBox = Box.createHorizontalBox();
097                hBox.add(Box.createGlue());
098                hBox.add(ftype);
099                fileType = getFileFormatSelect();
100                hBox.add(fileType);
101                hBox.add(Box.createGlue());
102
103                vBox.add(hBox);
104
105                this.add(vBox);
106        }
107
108
109        public String getFilePath1(){
110                return filePath1.getText();
111        }
112
113        public String getChain1(){
114                return chain1.getText();
115        }
116
117        @Override
118        public Structure getStructure1() throws StructureException{
119
120                return getStructure(filePath1,chain1);
121        }
122
123        @Override
124        public Structure getStructure2() throws StructureException{
125
126                return getStructure(filePath2,chain2);
127        }
128
129        private Structure getStructure(JTextField filePath,JTextField chainId) throws StructureException{
130                //PDBFileReader reader = new PDBFileReader();
131
132                StructureIOFile reader = null;
133                String fileFormat = (String)fileType.getSelectedItem();
134                if ( fileFormat.equals(UserConfiguration.PDB_FORMAT)){
135                        reader = new PDBFileReader();
136                } else if ( fileFormat.equals(UserConfiguration.MMCIF_FORMAT)){
137                        reader = new MMCIFFileReader();
138                } else {
139                        throw new StructureException("Unkown file format " + fileFormat);
140                }
141
142                String path = filePath.getText();
143                File f = new File(path);
144                Structure s = null;
145                try {
146                        s = reader.getStructure(f);
147                } catch (IOException  e){
148                        logger.warn(e.getMessage());
149                        //e.printStackTrace();
150                        throw new StructureException(e);
151                }
152
153                Structure reduced = StructureTools.getReducedStructure(s, chainId.getText());
154
155                String fileURL = "";
156                try {
157
158                        URL u ;
159
160                        if ( chainId.getText() == null || chainId.getText().equals("")){
161
162                                u = f.toURI().toURL();
163                        } else {
164                                u = new URL(f.toURI().toURL().toString() + "?chainId=" + chainId.getText());
165                        }
166                        fileURL = u.toString() ;
167
168                } catch (Exception e){
169                        e.printStackTrace();
170                }
171
172                reduced.setPDBCode(fileURL);
173                reduced.setName(fileURL);
174                return reduced;
175
176        }
177
178
179
180        private JPanel getLocalFilePanel(int pos ,JTextField filePath, JTextField  chainId){
181
182                JPanel panel = new JPanel();
183                //panel.setBorder(BorderFactory.createLineBorder(Color.black));
184
185                JLabel l01 = new JLabel("File "+pos+":");
186                panel.add(l01);
187
188                panel.add(filePath);
189                Action action3 = new ChooseAction(filePath);
190                JButton chooser = new JButton(action3);
191                panel.add(chooser);
192
193                JLabel chainLabel = new JLabel("Chain "+pos+": (optional)");
194                panel.add(chainLabel);
195                panel.add(chainId);
196
197                return panel;
198
199        }
200}
201
202class ChooseAction extends AbstractAction{
203
204        JTextField textField;
205        public ChooseAction (JTextField textField){
206                super("Choose");
207                this.textField = textField;
208        }
209        public static final long serialVersionUID = 0l;
210        // This method is called when the button is pressed
211        @Override
212        public void actionPerformed(ActionEvent evt) {
213                // Perform action...
214                final JFileChooser fc = new JFileChooser();
215
216                //              In response to a button click:
217                int returnVal = fc.showOpenDialog(null);
218                if ( returnVal == JFileChooser.APPROVE_OPTION) {
219                        File file = fc.getSelectedFile();
220                        textField.setText(file.getAbsolutePath());
221                        textField.repaint();
222
223
224                }
225
226        }
227}