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 */
021package org.biojava.nbio.structure.align.ce;
022
023import org.biojava.nbio.structure.Atom;
024import org.biojava.nbio.structure.Group;
025import org.biojava.nbio.structure.Structure;
026import org.biojava.nbio.structure.align.model.AFPChain;
027import org.biojava.nbio.structure.align.util.UserConfiguration;
028import org.biojava.nbio.structure.jama.Matrix;
029
030import javax.swing.*;
031import java.io.File;
032import java.lang.reflect.InvocationTargetException;
033import java.lang.reflect.Method;
034import java.util.List;
035
036/** A class to wrap some of the strucutre.gui classes using Reflection
037 *
038 * @author Andreas Prlic
039 *
040 */
041
042public class GuiWrapper {
043
044        static final String guiPackage = "org.biojava.nbio.structure.gui";
045
046        static final String strucAlignmentDisplay = "org.biojava.nbio.structure.align.gui.StructureAlignmentDisplay";
047        static final String displayAFP   = "org.biojava.nbio.structure.align.gui.DisplayAFP" ;
048        static final String alignmentGUI = "org.biojava.nbio.structure.align.gui.AlignmentGui";
049        static final String strucAligJmol = "org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol";
050        static final String abstractAligJmol = "org.biojava.nbio.structure.align.gui.jmol.AbstractAlignmentJmol";
051
052        static final String scaleMatrixPanel = "org.biojava.nbio.structure.gui.ScaleableMatrixPanel";
053
054        @SuppressWarnings("rawtypes")
055        public static boolean isGuiModuleInstalled(){
056                String className = displayAFP;
057                try {
058                        @SuppressWarnings("unused")
059                        Class c = Class.forName(className);
060                } catch (ClassNotFoundException ex){
061                        return false;
062                }
063                return true;
064        }
065
066        @SuppressWarnings({ "rawtypes", "unchecked" })
067        public static Object display(AFPChain afpChain, Atom[] ca1, Atom[] ca2)
068                        throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException{
069
070                Class c = Class.forName(strucAlignmentDisplay);
071
072                Method display = c.getMethod("display", new Class[]{AFPChain.class, Atom[].class,
073                                Atom[].class});
074
075                Object structureAlignmentJmol = display.invoke(null, afpChain,ca1,ca2);
076
077                return structureAlignmentJmol;
078        }
079
080
081
082        @SuppressWarnings({ "rawtypes", "unchecked" })
083        public static void showAlignmentImage(AFPChain afpChain, Atom[] ca1,
084                        Atom[] ca2, Object jmol)
085                                        throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException{
086
087                Class abstractAligJmolC = Class.forName(abstractAligJmol);
088
089                Class c = Class.forName(displayAFP);
090                Method show = c.getMethod("showAlignmentPanel", new Class[] {AFPChain.class, Atom[].class, Atom[].class, abstractAligJmolC});
091
092                show.invoke(null,afpChain, ca1, ca2, jmol);
093        }
094
095
096        /** Shows a structure in Jmol
097         * @since 3.0.5
098         */
099        public static void showStructure(Structure structure)
100                        throws ClassNotFoundException, NoSuchMethodException,
101                        InvocationTargetException, IllegalAccessException, InstantiationException{
102
103                Class<?> structureAlignmentJmol = Class.forName(strucAligJmol);
104
105                Object strucAligJ = structureAlignmentJmol.newInstance();
106
107                Method setS = structureAlignmentJmol.getMethod("setStructure", new Class[] {Structure.class});
108
109                setS.invoke(strucAligJ,structure);
110        }
111
112
113        @SuppressWarnings({ "rawtypes", "unchecked" })
114        public static void showAlignmentGUI()
115                        throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
116                // proxy for AlignmentGui.getInstance();
117
118
119                Class c = Class.forName(alignmentGUI);
120                Method m = c.getMethod("getInstance", (Class[])null);
121                m.invoke(c,(Object[])null);
122        }
123
124        @SuppressWarnings({"unused" })
125        public static Structure getAlignedStructure(Atom[] ca1, Atom[] ca2)
126                        throws ClassNotFoundException, NoSuchMethodException,
127                        InvocationTargetException, IllegalAccessException{
128
129                Class<?> structureAlignmentJmol = Class.forName(strucAligJmol);
130
131                Class<?> c = Class.forName(displayAFP);
132                Method show = c.getMethod("getAlignedStructure", new Class[] { Atom[].class, Atom[].class});
133
134                Structure s = (Structure) show.invoke(null, ca1, ca2);
135
136                return s;
137
138        }
139
140        public static JPanel getScaleableMatrixPanel(Matrix m)
141                        throws ClassNotFoundException, NoSuchMethodException,
142                        InvocationTargetException, IllegalAccessException, InstantiationException{
143
144                Class<?> scaleMatrixPanelC = Class.forName(scaleMatrixPanel);
145
146                Method setMatrix = scaleMatrixPanelC.getMethod("setMatrix", new Class[] { Matrix.class});
147
148                JPanel panel = (JPanel) scaleMatrixPanelC.newInstance();
149
150                setMatrix.invoke(panel, m);
151
152                return panel;
153
154        }
155
156        /**
157         *
158         * @param afpChain
159         * @param ca1
160         * @param ca2
161         * @return
162         * @throws ClassNotFoundException If an error occurs when invoking jmol
163         * @throws NoSuchMethodException If an error occurs when invoking jmol
164         * @throws InvocationTargetException If an error occurs when invoking jmol
165         * @throws IllegalAccessException If an error occurs when invoking jmol
166         */
167        @SuppressWarnings({ "rawtypes", "unchecked" })
168        public static Group[] prepareGroupsForDisplay(AFPChain afpChain, Atom[] ca1,
169                        Atom[] ca2)
170                                        throws ClassNotFoundException, NoSuchMethodException,
171                                        InvocationTargetException, IllegalAccessException{
172                Class c = Class.forName(strucAlignmentDisplay);
173
174                Method display = c.getMethod("prepareGroupsForDisplay", new Class[]{AFPChain.class, Atom[].class,
175                                Atom[].class});
176
177                Object groups = display.invoke(null, afpChain,ca1,ca2);
178
179                return (Group[]) groups;
180        }
181
182        @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
183        public static Atom[] getAtomArray(Atom[] ca, List<Group> hetatoms, List<Group> nucs)
184                        throws ClassNotFoundException, NoSuchMethodException,
185                        InvocationTargetException, IllegalAccessException{
186
187                Class structureAlignmentJmol = Class.forName(strucAligJmol);
188
189                Class c = Class.forName(displayAFP);
190                Method show = c.getMethod("getAtomArray", new Class[] { Atom[].class, List.class, List.class});
191
192                Atom[] atoms = (Atom[]) show.invoke(null, ca, hetatoms, nucs);
193
194                return atoms;
195
196        }
197
198        /**
199         * @since 3.0.5
200         */
201        public static void showDBResults(StartupParameters params) {
202                //System.err.println("not implemented full yet");
203
204                // We want to do this, but because we don't know if structure-gui.jar is in the classpath we use reflection to hide the calls
205
206                UserConfiguration config = UserConfiguration.fromStartupParams(params);
207
208                String tableClass = "org.biojava.nbio.structure.align.gui.DBResultTable";
209
210                try {
211                        Class<?> c = Class.forName(tableClass);
212                        Object table = c.newInstance();
213
214                        Method show = c.getMethod("show", new Class[]{File.class, UserConfiguration.class });
215
216                        show.invoke(table, new File(params.getShowDBresult()),config);
217
218                } catch (Exception e){
219                        e.printStackTrace();
220
221                        System.err.println("Probably structure-gui.jar is not in the classpath, can't show results...");
222                }
223
224                //DBResultTable table = new DBResultTable();
225
226                //table.show(new File(params.getShowDBresult()),config);
227
228        }
229
230}