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                UIManager.LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
125                JPanel panel = new JPanel();
126
127                for(int i = 0; i < lookAndFeels.length; i++){
128                  JButton button = new JButton(lookAndFeels[i].getName());
129                  button.addActionListener(new MyAction(dialog));
130                  panel.add(button);
131                }
132
133                JTabbedPane jTab = new JTabbedPane();
134                jTab.addTab("Select User Interface", null, panel, "Select the Look and Feel of the application.");
135
136                vBox.add(jTab);
137                vBox.add(Box.createGlue());
138
139                JButton apply = new JButton("Apply");
140                apply.addActionListener(new ActionListener(){
141                        @Override
142                public void actionPerformed(ActionEvent event) {
143                                instance.applyValues();
144                                dialog.dispose();
145                        }
146                });
147
148                JButton close = new JButton("Cancel");
149
150                close.addActionListener(new ActionListener(){
151                        @Override
152                public void actionPerformed(ActionEvent event) {
153                          dialog.dispose();
154                        }
155                });
156
157                Box hBoxb = Box.createHorizontalBox();
158                hBoxb.add(Box.createGlue());
159                hBoxb.add(close,BorderLayout.EAST);
160                hBoxb.add(apply,BorderLayout.EAST);
161                vBox.add(hBoxb);
162
163                dialog.getContentPane().add(vBox);
164                dialog.setVisible(true);
165
166        }
167
168        protected void applyValues()
169        {
170                UserConfiguration config = WebStartMain.getWebStartConfig();
171
172                String dir = pdbDir.getText();
173                config.setPdbFilePath(dir);
174                boolean fromFtpF = fromFtp.isSelected();
175                if(fromFtpF) {
176                        config.setFetchBehavior(FetchBehavior.FETCH_REMEDIATED);
177                } else {
178                        config.setFetchBehavior(FetchBehavior.LOCAL_ONLY);
179                }
180                String fileFormat = (String)fileType.getSelectedItem();
181                config.setFileFormat(fileFormat);
182
183
184
185        }
186
187        public JCheckBox getFromFtp()
188        {
189                return fromFtp;
190        }
191        public void setFromFtp(JCheckBox fromFtp)
192        {
193                System.out.println(fromFtp);
194                this.fromFtp = fromFtp;
195        }
196        public JTextField getPDBDirField(){
197                return pdbDir;
198        }
199        public void setPDBDirField(JTextField dir){
200                pdbDir = dir;
201        }
202
203
204
205
206}
207
208class MyAction implements ActionListener{
209        JDialog dialog;
210
211        public MyAction(JDialog dialog) {
212                this.dialog= dialog;
213        }
214
215
216        @Override
217        @SuppressWarnings("unused")
218        public void actionPerformed(ActionEvent ae){
219                Object EventSource = ae.getSource();
220                String lookAndFeelClassName = null;
221                UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
222                for(int i = 0; i < looks.length; i++){
223                        if(ae.getActionCommand().equals(looks[i].getName())){
224                                lookAndFeelClassName = looks[i].getClassName();
225                                break;
226                        }
227                }
228                try{
229                        UIManager.setLookAndFeel(lookAndFeelClassName);
230
231                        SwingUtilities.updateComponentTreeUI(dialog);
232
233                        SwingUtilities.updateComponentTreeUI( AlignmentGui.getInstanceNoVisibilityChange());
234                }
235                catch(Exception e){
236                        JOptionPane.showMessageDialog(dialog, "Can't change look and feel:"
237                                        + lookAndFeelClassName, "Invalid PLAF", JOptionPane.ERROR_MESSAGE);
238                }
239        }
240}