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 Apr 6, 2010
021 * Author: Andreas Prlic
022 *
023 */
024
025package org.biojava.nbio.structure.align.gui;
026
027import org.biojava.nbio.structure.align.util.UserConfiguration;
028import org.biojava.nbio.structure.align.webstart.WebStartMain;
029import org.biojava.nbio.structure.gui.util.PDBUploadPanel;
030import org.biojava.nbio.structure.io.LocalPDBDirectory.FetchBehavior;
031
032import javax.swing.*;
033import java.awt.*;
034import java.awt.event.ActionEvent;
035import java.awt.event.ActionListener;
036import java.awt.event.KeyEvent;
037
038public class ConfigPDBInstallPanel extends JPanel
039{
040
041        /**
042         *
043         */
044        private static final long serialVersionUID = -1055193854675583808L;
045
046        JCheckBox fromFtp;
047        JComboBox fileType;
048
049        JTextField pdbDir;
050
051        private static ConfigPDBInstallPanel instance = new ConfigPDBInstallPanel();
052
053        static JDialog dialog;
054
055        private ConfigPDBInstallPanel(){
056
057                UserConfiguration   config = WebStartMain.getWebStartConfig();
058
059                fileType = PDBUploadPanel.getFileFormatSelect();
060
061                Box vBox = Box.createVerticalBox();
062                Box hBox = Box.createHorizontalBox();
063
064                pdbDir = new JTextField(20);
065
066                String conf = System.getProperty(UserConfiguration.PDB_DIR);
067                if ( conf != null){
068                        pdbDir.setText(conf);
069                }
070
071                JLabel l01 = new JLabel("Directory containing PDBs");
072                //panel.add(l01);
073                //panel.add(f);
074
075                hBox.add(l01);
076                hBox.add(pdbDir);
077                vBox.add(hBox);
078
079                if ( config != null) {
080                        pdbDir.setText( config.getPdbFilePath() );
081                }
082
083                Action action = new ChooseDirAction(pdbDir, config);
084
085                JButton chooser = new JButton(action);
086                hBox.add(chooser);
087
088                Box hBox3 = Box.createHorizontalBox();
089                JLabel label2 = new JLabel("Fetch missing PDBs from ftp site:");
090                fromFtp = new JCheckBox();
091                fromFtp.setMnemonic(KeyEvent.VK_F);
092                fromFtp.setSelected(true);
093                if ( config != null)
094                        fromFtp.setSelected(config.getFetchBehavior() != FetchBehavior.LOCAL_ONLY);
095
096                JLabel ftype = new JLabel("File format:");
097
098                hBox3.add(Box.createGlue());
099                hBox3.add(ftype);
100                hBox3.add(fileType);
101                hBox3.add(Box.createGlue());
102                hBox3.add(label2);
103                hBox3.add(fromFtp);
104
105                vBox.add(hBox3);
106                this.add(vBox);
107
108        }
109
110        public static void showDialog(){
111                if ( dialog != null) {
112
113                        dialog.setVisible(true);
114                        return;
115                }
116
117                dialog = new JDialog();
118                dialog.setSize(new Dimension(600,300));
119                Box vBox = Box.createVerticalBox();
120
121                vBox.add(instance);
122
123
124
125                UIManager.LookAndFeelInfo lookAndFeels[] = UIManager.getInstalledLookAndFeels();
126                JPanel panel = new JPanel();
127
128                for(int i = 0; i < lookAndFeels.length; i++){
129                  JButton button = new JButton(lookAndFeels[i].getName());
130                  button.addActionListener(new MyAction(dialog));
131                  panel.add(button);
132                }
133
134                JTabbedPane jTab = new JTabbedPane();
135                jTab.addTab("Select User Interface", null, panel, "Select the Look and Feel of the application.");
136
137                vBox.add(jTab);
138                vBox.add(Box.createGlue());
139
140                JButton apply = new JButton("Apply");
141                apply.addActionListener(new ActionListener(){
142                        @Override
143                public void actionPerformed(ActionEvent event) {
144                                instance.applyValues();
145                                dialog.dispose();
146                        }
147                });
148
149                JButton close = new JButton("Cancel");
150
151                close.addActionListener(new ActionListener(){
152                        @Override
153                public void actionPerformed(ActionEvent event) {
154                          dialog.dispose();
155                        }
156                });
157
158                Box hBoxb = Box.createHorizontalBox();
159                hBoxb.add(Box.createGlue());
160                hBoxb.add(close,BorderLayout.EAST);
161                hBoxb.add(apply,BorderLayout.EAST);
162                vBox.add(hBoxb);
163
164                dialog.getContentPane().add(vBox);
165                dialog.setVisible(true);
166
167        }
168
169        protected void applyValues()
170        {
171                UserConfiguration config = WebStartMain.getWebStartConfig();
172
173                String dir = pdbDir.getText();
174                config.setPdbFilePath(dir);
175                boolean fromFtpF = fromFtp.isSelected();
176                if(fromFtpF) {
177                        config.setFetchBehavior(FetchBehavior.FETCH_REMEDIATED);
178                } else {
179                        config.setFetchBehavior(FetchBehavior.LOCAL_ONLY);
180                }
181                String fileFormat = (String)fileType.getSelectedItem();
182                config.setFileFormat(fileFormat);
183
184                
185
186        }
187
188        public JCheckBox getFromFtp()
189        {
190                return fromFtp;
191        }
192        public void setFromFtp(JCheckBox fromFtp)
193        {
194                System.out.println(fromFtp);
195                this.fromFtp = fromFtp;
196        }
197        public JTextField getPDBDirField(){
198                return pdbDir;
199        }
200        public void setPDBDirField(JTextField dir){
201                pdbDir = dir;
202        }
203
204
205
206
207}
208
209class MyAction implements ActionListener{
210        JDialog dialog;
211
212        public MyAction(JDialog dialog) {
213                this.dialog= dialog;
214        }
215
216
217        @Override
218        @SuppressWarnings("unused")
219        public void actionPerformed(ActionEvent ae){
220                Object EventSource = ae.getSource();
221                String lookAndFeelClassName = null;
222                UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
223                for(int i = 0; i < looks.length; i++){
224                        if(ae.getActionCommand().equals(looks[i].getName())){
225                                lookAndFeelClassName = looks[i].getClassName();
226                                break;
227                        }
228                }
229                try{
230                        UIManager.setLookAndFeel(lookAndFeelClassName);
231
232                        SwingUtilities.updateComponentTreeUI(dialog);
233
234                        SwingUtilities.updateComponentTreeUI( AlignmentGui.getInstanceNoVisibilityChange());
235                }
236                catch(Exception e){
237                        JOptionPane.showMessageDialog(dialog, "Can't change look and feel:"
238                                        + lookAndFeelClassName, "Invalid PLAF", JOptionPane.ERROR_MESSAGE);
239                }
240        }
241}