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