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 Jun 30, 2010
021 * Author: ap3
022 *
023 */
024package org.biojava.nbio.structure.gui.util;
025
026import java.awt.BorderLayout;
027import java.awt.Dimension;
028import java.util.ArrayList;
029import java.util.List;
030
031import javax.swing.Box;
032import javax.swing.JLabel;
033import javax.swing.JPanel;
034import javax.swing.JTextField;
035
036import org.biojava.nbio.structure.Structure;
037import org.biojava.nbio.structure.StructureException;
038import org.biojava.nbio.structure.StructureIdentifier;
039import org.biojava.nbio.structure.align.client.StructureName;
040import org.biojava.nbio.structure.align.util.AtomCache;
041import org.biojava.nbio.structure.align.util.UserConfiguration;
042import org.biojava.nbio.structure.align.webstart.WebStartMain;
043
044/**
045 * A Text Panel that allows the user to specify multiple structure
046 * identifiers, space separated.
047 *
048 * @author Aleix Lafita
049 * @since 4.1.1
050 *
051 */
052public class SelectMultiplePanel extends JPanel {
053
054        private static final long serialVersionUID = 757947454156959178L;
055
056        JTextField input;
057
058        public SelectMultiplePanel(){
059                this(true);
060        }
061
062        public SelectMultiplePanel(boolean show2boxes){
063
064                Box vBox = Box.createVerticalBox();
065
066                input = new JTextField("1mbc 1hlb 1dlw 1ith.A 1thb.A 1kr7.A_0-109");
067                Box b = getDomainPanel(input);
068                vBox.add(b);
069
070                this.add(vBox);
071        }
072
073        private Box getDomainPanel(JTextField f){
074
075                JLabel l01 = new JLabel("Input structures:");
076
077                Box hBox = Box.createHorizontalBox();
078                hBox.add(Box.createGlue());
079                hBox.add(l01);
080
081                f.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
082                f.setToolTipText("Provide structure identifiers space separated.");
083
084                hBox.add(Box.createVerticalGlue());
085                hBox.add(f, BorderLayout.CENTER);
086                hBox.add(Box.createGlue());
087
088                return hBox;
089        }
090
091        public List<Structure> getStructures() throws StructureException {
092
093                List<Structure> structures = new ArrayList<Structure>();
094
095                for (StructureIdentifier name:getNames()){
096                        structures.add(getStructure(name));
097                }
098                return structures;
099        }
100
101        public List<StructureIdentifier> getNames() {
102
103                List<StructureIdentifier> names = new ArrayList<StructureIdentifier>();
104
105                String raw = input.getText().trim();
106                String[] split = raw.split(" ");
107                for (String name:split){
108                        if (name != null && !name.isEmpty())
109                                names.add(new StructureName(name.trim()));
110                }
111                return names;
112        }
113
114        private Structure getStructure(StructureIdentifier name) throws StructureException{
115
116                UserConfiguration config = WebStartMain.getWebStartConfig();
117
118                AtomCache cache = new AtomCache(config);
119
120                Structure s = null;
121                try {
122                        s = cache.getStructure(name);
123                        s.setName(name.getIdentifier());
124                } catch (Exception e){
125                        e.printStackTrace();
126                }
127                return s;
128        }
129}