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 24, 2008
021 */
022package org.biojava.nbio.structure.gui.util;
023
024import org.biojava.nbio.structure.Structure;
025import org.biojava.nbio.structure.align.gui.AlignmentGui;
026import org.biojava.nbio.structure.gui.BiojavaJmol;
027import org.biojava.nbio.structure.io.PDBFileReader;
028
029import javax.swing.*;
030import java.awt.*;
031import java.awt.event.ActionEvent;
032import java.awt.event.ActionListener;
033import java.awt.event.KeyEvent;
034import java.io.File;
035
036
037/**
038 *  Create the menu for BiojavaJmol
039 * @author Andreas Prlic
040 * @since 1.7
041 */
042public class MenuCreator {
043
044        /** provide a JMenuBar that can be added to a JFrame
045         *
046         * @return a JMenuBar
047         */
048        public static JMenuBar initMenu(){
049
050                // show a menu
051
052                JMenuBar menu = new JMenuBar();
053
054                JMenu file= new JMenu("File");
055                file.getAccessibleContext().setAccessibleDescription("File Menu");
056
057                JMenuItem openI = new JMenuItem("Open");
058                openI.setMnemonic(KeyEvent.VK_O);
059                openI.addActionListener(new ActionListener(){
060                        @Override
061                        public void actionPerformed(ActionEvent e) {
062                                String cmd = e.getActionCommand();
063                                if ( cmd.equals("Open")){
064                                        final JFileChooser fc = new JFileChooser();
065
066//                                      In response to a button click:
067                                        int returnVal = fc.showOpenDialog(null);
068                                        if ( returnVal == JFileChooser.APPROVE_OPTION) {
069                                                File file = fc.getSelectedFile();
070
071                                                PDBFileReader reader = new PDBFileReader();
072                                                try {
073                                                        Structure s = reader.getStructure(file);
074                                                        BiojavaJmol jmol = new BiojavaJmol();
075                                                        jmol.setStructure(s);
076                                                        jmol.evalString("select * ; color chain;");
077                                                        jmol.evalString("select *; spacefill off; wireframe off; backbone 0.4;  ");
078
079                                                } catch (Exception ex){
080                                                        ex.printStackTrace();
081                                                }
082
083
084                                        }
085                                }
086                        }
087                });
088                file.add(openI);
089
090                JMenuItem exitI = new JMenuItem("Exit");
091                exitI.setMnemonic(KeyEvent.VK_X);
092                exitI.addActionListener(new ActionListener(){
093
094                        @Override
095                        public void actionPerformed(ActionEvent e) {
096                                String cmd = e.getActionCommand();
097
098                                if ( cmd.equals("Exit")){
099                                        System.exit(0);
100                                }
101                        }
102                });
103
104                file.add(exitI);
105                menu.add(file);
106
107                JMenu align = new JMenu("Align");
108                JMenuItem pairI = new JMenuItem("2 protein structures");
109                pairI.addActionListener(new ActionListener() {
110                        @Override
111                        public void actionPerformed(ActionEvent e) {
112                                String cmd = e.getActionCommand();
113
114                                if ( cmd.equals("2 protein structures")){
115                                        MenuCreator.showPairDialog();
116                                }
117                        }
118                });
119
120                align.add(pairI);
121
122                menu.add(align);
123
124                JMenu about = new JMenu("About");
125                JMenuItem aboutI = new JMenuItem("PDBview");
126                aboutI.addActionListener(new ActionListener(){
127
128                        @Override
129                        public void actionPerformed(ActionEvent e) {
130                                String cmd = e.getActionCommand();
131
132                                if ( cmd.equals("PDBview")){
133                                        MenuCreator.showAboutDialog();
134                                }
135                        }
136                });
137
138                about.add(aboutI);
139
140                menu.add(Box.createGlue());
141                menu.add(about);
142
143                return menu;
144
145        }
146
147
148        /** provide a display for the pairwise structure alignment
149         *
150         */
151        private static void showPairDialog(){
152                AlignmentGui gui = AlignmentGui.getInstance();
153                gui.setVisible(true);
154        }
155
156        /** show some info about this gui
157         *
158         */
159        private static void showAboutDialog(){
160
161                JDialog dialog = new JDialog();
162
163                dialog.setSize(new Dimension(300,300));
164
165                String msg = "This viewer is based on <b>BioJava</b> and <b>Jmol</>. <br>Author: Andreas Prlic <br> ";
166                msg += "Structure Alignment algorithm based on a variation of the PSC++ algorithm by Peter Lackner.";
167
168
169                JEditorPane txt = new JEditorPane("text/html", msg);
170                txt.setEditable(false);
171
172
173                JScrollPane scroll = new JScrollPane(txt);
174
175                Box vBox = Box.createVerticalBox();
176                vBox.add(scroll);
177
178                JButton close = new JButton("Close");
179
180                close.addActionListener(new ActionListener(){
181                        @Override
182                        public void actionPerformed(ActionEvent event) {
183                                Object source = event.getSource();
184
185                                JButton but = (JButton)source;
186                                Container parent = but.getParent().getParent().getParent().getParent().getParent().getParent() ;
187
188                                JDialog dia = (JDialog) parent;
189                                dia.dispose();
190                        }
191                });
192
193                Box hBoxb = Box.createHorizontalBox();
194                hBoxb.add(Box.createGlue());
195                hBoxb.add(close,BorderLayout.EAST);
196
197                vBox.add(hBoxb);
198
199                dialog.getContentPane().add(vBox);
200                dialog.setVisible(true);
201
202
203        }
204
205
206}