001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.panels;
025    
026    import java.awt.BorderLayout;
027    import java.awt.Dimension;
028    import java.lang.reflect.Constructor;
029    
030    import javax.swing.JPanel;
031    import javax.swing.JSplitPane;
032    import javax.swing.JTabbedPane;
033    import javax.swing.tree.TreeModel;
034    
035    import org.ujmp.core.Matrix;
036    import org.ujmp.core.util.GnuPlot;
037    import org.ujmp.core.util.Matlab;
038    import org.ujmp.core.util.Octave;
039    import org.ujmp.core.util.R;
040    import org.ujmp.gui.MatrixGUIObject;
041    import org.ujmp.gui.plot.MatrixPlot;
042    
043    public class MatrixPanel extends AbstractPanel {
044            private static final long serialVersionUID = 3912987239953510584L;
045    
046            public MatrixPanel(MatrixGUIObject m) {
047                    super(m);
048    
049                    JSplitPane splitPane1 = new JSplitPane();
050                    JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
051    
052                    JTabbedPane tabbedPane = new JTabbedPane();
053                    tabbedPane.setMinimumSize(new Dimension(10, 10));
054    
055                    if (m.getMatrix() instanceof TreeModel) {
056                            tabbedPane.add("Tree View", new MatrixTreePanel(m));
057                    }
058    
059                    tabbedPane.add("2D Visualization", new MatrixPaintPanel(m, false));
060    
061                    if (false) {
062                            try {
063                                    // tabbedPane.add("3D Visualization", new Matrix3DPanel(m,
064                                    // false));
065                            } catch (Throwable t) {
066                            }
067                    }
068    
069                    try {
070                            Class.forName("org.ujmp.jung.MatrixGraphPanel");
071                            Class.forName("edu.uci.ics.jung.graph.Graph");
072                            tabbedPane.add("Graph", new GraphPanel(m.getMatrix()));
073                    } catch (Throwable e) {
074                    }
075    
076                    try {
077                            Class.forName("org.math.plot.PlotPanel");
078                            Class<?> c = Class.forName("org.ujmp.jmathplot.JMathPlotLinePanel");
079                            Constructor<?> con = c.getConstructor(Matrix.class);
080                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
081                            tabbedPane.add("Line Plot", panel);
082                    } catch (Throwable e) {
083                    }
084    
085                    try {
086                            Class.forName("org.math.plot.PlotPanel");
087                            Class<?> c = Class.forName("org.ujmp.jmathplot.JMathPlotXYPanel");
088                            Constructor<?> con = c.getConstructor(Matrix.class);
089                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
090                            tabbedPane.add("XY Plot", panel);
091                    } catch (Throwable e) {
092                    }
093    
094                    try {
095                            Class.forName("org.math.plot.PlotPanel");
096                            Class<?> c = Class
097                                            .forName("org.ujmp.jmathplot.JMathPlotScatterPanel");
098                            Constructor<?> con = c.getConstructor(Matrix.class);
099                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
100                            tabbedPane.add("Scatter Plot", panel);
101                    } catch (Throwable e) {
102                    }
103    
104                    try {
105                            Class.forName("org.math.plot.PlotPanel");
106                            Class<?> c = Class.forName("org.ujmp.jmathplot.JMathPlotBarPanel");
107                            Constructor<?> con = c.getConstructor(Matrix.class);
108                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
109                            tabbedPane.add("Bar Plot", panel);
110                    } catch (Throwable e) {
111                    }
112    
113                    try {
114                            Class.forName("org.math.plot.PlotPanel");
115                            Class<?> c = Class
116                                            .forName("org.ujmp.jmathplot.JMathPlotHistogramPanel");
117                            Constructor<?> con = c.getConstructor(Matrix.class);
118                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
119                            tabbedPane.add("Histogram", panel);
120                    } catch (Throwable e) {
121                    }
122    
123                    try {
124                            Class.forName("org.math.plot.PlotPanel");
125                            Class<?> c = Class
126                                            .forName("org.ujmp.jmathplot.JMathPlotStaircasePanel");
127                            Constructor<?> con = c.getConstructor(Matrix.class);
128                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
129                            tabbedPane.add("Staircase Plot", panel);
130                    } catch (Throwable e) {
131                    }
132    
133                    try {
134                            Class.forName("org.math.plot.PlotPanel");
135                            Class<?> c = Class.forName("org.ujmp.jmathplot.JMathPlotGridPanel");
136                            Constructor<?> con = c.getConstructor(Matrix.class);
137                            JPanel panel = (JPanel) con.newInstance(m.getMatrix());
138                            tabbedPane.add("Grid Plot", panel);
139                    } catch (Throwable e) {
140                    }
141    
142                    try {
143                            if (m.getColumnCount() >= 3) {
144                                    Class.forName("org.math.plot.PlotPanel");
145                                    Class<?> c = Class
146                                                    .forName("org.ujmp.jmathplot.JMathPlotBar3DPanel");
147                                    Constructor<?> con = c.getConstructor(Matrix.class);
148                                    JPanel panel = (JPanel) con.newInstance(m.getMatrix());
149                                    tabbedPane.add("Bar Plot 3D", panel);
150                            }
151                    } catch (Throwable e) {
152                    }
153    
154                    try {
155                            if (m.getColumnCount() >= 3) {
156                                    Class<?> c = Class
157                                                    .forName("org.ujmp.jmathplot.JMathPlotScatter3DPanel");
158                                    Constructor<?> con = c.getConstructor(Matrix.class);
159                                    JPanel panel = (JPanel) con.newInstance(m.getMatrix());
160                                    tabbedPane.add("Scatter Plot 3D", panel);
161                            }
162                    } catch (Throwable e) {
163                    }
164    
165                    try {
166                            if (m.getColumnCount() >= 3) {
167                                    Class.forName("org.math.plot.PlotPanel");
168                                    Class<?> c = Class
169                                                    .forName("org.ujmp.jmathplot.JMathPlotLine3DPanel");
170                                    Constructor<?> con = c.getConstructor(Matrix.class);
171                                    JPanel panel = (JPanel) con.newInstance(m.getMatrix());
172                                    tabbedPane.add("Line Plot 3D", panel);
173                            }
174                    } catch (Throwable e) {
175                    }
176    
177                    if (GnuPlot.isAvailable()) {
178                            GnuPlotPanel gnuPlotPanel = new GnuPlotPanel(m);
179                            tabbedPane.add("GnuPlot", gnuPlotPanel);
180                    }
181    
182                    if (Matlab.isAvailable()) {
183                            MatlabPanel matlabPanel = new MatlabPanel(m);
184                            tabbedPane.add("Matlab", matlabPanel);
185                    }
186    
187                    if (Octave.isAvailable()) {
188                            OctavePanel octavePanel = new OctavePanel(m);
189                            tabbedPane.add("Octave", octavePanel);
190                    }
191    
192                    if (R.isAvailable()) {
193                            RPanel rPanel = new RPanel(m);
194                            tabbedPane.add("R", rPanel);
195                    }
196    
197                    // try {
198                    // Class<?> jfreechart = Class
199                    // .forName("org.ujmp.jfreechart.MatrixChartPanel");
200                    // JPanel panel = (JPanel) jfreechart.getConstructor(
201                    // MatrixGUIObject.class).newInstance(m);
202                    // splitPane2.setTopComponent(new BufferedPanel(panel));
203                    // } catch (Throwable t) {
204                    splitPane2.setTopComponent(new BufferedPanel(new MatrixPlot(m, true)));
205                    // }
206    
207                    splitPane2.setBottomComponent(tabbedPane);
208                    splitPane1.setLeftComponent(splitPane2);
209    
210                    splitPane1.setRightComponent(new MatrixEditorPanel(m));
211    
212                    add(splitPane1, BorderLayout.CENTER);
213    
214                    splitPane1.setDividerLocation(600);
215                    splitPane2.setDividerLocation(200);
216            }
217    
218    }