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        @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
157        public static Atom[] getAtomArray(Atom[] ca, List<Group> hetatoms, List<Group> nucs)
158                        throws ClassNotFoundException, NoSuchMethodException,
159                        InvocationTargetException, IllegalAccessException{
160
161                Class structureAlignmentJmol = Class.forName(strucAligJmol);
162
163                Class c = Class.forName(displayAFP);
164                Method show = c.getMethod("getAtomArray", new Class[] { Atom[].class, List.class, List.class});
165
166                Atom[] atoms = (Atom[]) show.invoke(null, ca, hetatoms, nucs);
167
168                return atoms;
169
170        }
171
172        /**
173         * @since 3.0.5
174         */
175        public static void showDBResults(StartupParameters params) {
176                //System.err.println("not implemented full yet");
177
178                // 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
179
180                UserConfiguration config = UserConfiguration.fromStartupParams(params);
181
182                String tableClass = "org.biojava.nbio.structure.align.gui.DBResultTable";
183
184                try {
185                        Class<?> c = Class.forName(tableClass);
186                        Object table = c.newInstance();
187
188                        Method show = c.getMethod("show", new Class[]{File.class, UserConfiguration.class });
189
190                        show.invoke(table, new File(params.getShowDBresult()),config);
191
192                } catch (Exception e){
193                        e.printStackTrace();
194
195                        System.err.println("Probably structure-gui.jar is not in the classpath, can't show results...");
196                }
197
198                //DBResultTable table = new DBResultTable();
199
200                //table.show(new File(params.getShowDBresult()),config);
201
202        }
203
204}