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.frame;
025    
026    import java.awt.BorderLayout;
027    import java.awt.Dimension;
028    import java.awt.Toolkit;
029    import java.io.File;
030    import java.util.TimerTask;
031    
032    import javax.swing.JComponent;
033    import javax.swing.JFrame;
034    
035    import org.ujmp.core.Coordinates;
036    import org.ujmp.core.Matrix;
037    import org.ujmp.core.interfaces.GUIObject;
038    import org.ujmp.gui.MatrixGUIObject;
039    import org.ujmp.gui.io.ExportJPEG;
040    import org.ujmp.gui.io.ExportPDF;
041    import org.ujmp.gui.io.ExportPNG;
042    import org.ujmp.gui.statusbar.StatusBar;
043    import org.ujmp.gui.util.FrameManager;
044    import org.ujmp.gui.util.GlobalTimer;
045    import org.ujmp.gui.util.GraphicsExecutor;
046    import org.ujmp.gui.util.UIDefaults;
047    
048    public abstract class AbstractFrame extends JFrame {
049            private static final long serialVersionUID = -4656308453503586700L;
050    
051            private int modCount = -1;
052    
053            private GUIObject object = null;
054    
055            private StatusBar statusBar = null;
056    
057            private static int frameCount = 0;
058    
059            private TimerTask updateTask = null;
060    
061            public AbstractFrame(GUIObject o, JComponent component) {
062                    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
063                    UIDefaults.setDefaults();
064                    FrameManager.registerFrame(o, this);
065                    this.object = o;
066                    String label = o.getLabel() == null ? "no label" : o.getLabel();
067                    if (o instanceof MatrixGUIObject) {
068                            MatrixGUIObject mgui = (MatrixGUIObject) o;
069                            Matrix m = mgui.getMatrix();
070                            String size = Coordinates.toString(m.getSize())
071                                            .replaceAll(",", "x");
072                            setTitle("[" + size + "] " + m.getClass().getSimpleName() + " ["
073                                            + label + "]");
074                    } else {
075                            setTitle(o.toString());
076                    }
077    
078                    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
079                    if (d.getHeight() < 800) {
080                            setPreferredSize(new Dimension(700, 500));
081                            setSize(new Dimension(700, 500));
082                            setExtendedState(MAXIMIZED_BOTH);
083                    } else if (d.getHeight() < 1024) {
084                            setPreferredSize(new Dimension(1000, 600));
085                            setSize(new Dimension(1000, 600));
086                            setExtendedState(MAXIMIZED_BOTH);
087                    } else {
088                            setPreferredSize(new Dimension(1280, 800));
089                            setSize(new Dimension(1280, 800));
090                    }
091    
092                    statusBar = new StatusBar(object);
093                    getContentPane().add(statusBar, BorderLayout.SOUTH);
094    
095                    getContentPane().add(component, BorderLayout.CENTER);
096    
097                    // DefaultToolbar toolbar = new DefaultToolbar(component, o);
098                    // getContentPane().add(toolbar, BorderLayout.NORTH);
099    
100                    final GUIObject go = object;
101                    updateTask = new TimerTask() {
102    
103                            public void run() {
104                                    if (modCount != go.getModCount()) {
105                                            modCount = go.getModCount();
106                                            repaint(1000);
107                                    }
108    
109                            }
110                    };
111                    GlobalTimer.getInstance().scheduleAtFixedRate(updateTask, 1000, 1000);
112            }
113    
114            public final void setVisible(boolean state) {
115                    if (state == true && isVisible()) {
116                            return;
117                    }
118                    if (state == false && !isVisible()) {
119                            return;
120                    }
121    
122                    super.setVisible(state);
123                    if (state) {
124                            frameCount++;
125                            statusBar.start();
126                    } else {
127                            frameCount--;
128                            statusBar.stop();
129                    }
130    
131                    if (frameCount == 0) {
132                            GraphicsExecutor.shutDown();
133                            GlobalTimer.shutDown();
134                    }
135            }
136    
137            public final GUIObject getObject() {
138                    return object;
139            }
140    
141            public final void exportToPDF(File file) {
142                    ExportPDF.save(file, this);
143            }
144    
145            public final void exportToPNG(File file) {
146                    ExportPNG.save(file, this);
147            }
148    
149            public final void exportToJPEG(File file) {
150                    ExportJPEG.save(file, this);
151            }
152    
153    }