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.statusbar;
025    
026    import java.awt.Dimension;
027    import java.awt.GridBagConstraints;
028    import java.awt.GridBagLayout;
029    import java.awt.Insets;
030    import java.util.TimerTask;
031    
032    import javax.swing.BorderFactory;
033    import javax.swing.JLabel;
034    import javax.swing.JPanel;
035    import javax.swing.JProgressBar;
036    
037    import org.ujmp.core.interfaces.GUIObject;
038    import org.ujmp.gui.MatrixGUIObject;
039    import org.ujmp.gui.util.TaskQueue;
040    
041    public class StatusBar extends JPanel {
042            private static final long serialVersionUID = -92341245296146976L;
043    
044            private final JLabel taskStatus = new JLabel();
045    
046            private JLabel objectStatus = null;
047    
048            private GUIObject object = null;
049    
050            private final JProgressBar jProgressBar = new JProgressBar();
051    
052            // private Timer timer = null;
053    
054            public StatusBar(GUIObject o) {
055                    this.object = o;
056                    if (o instanceof MatrixGUIObject) {
057                            this.objectStatus = new MatrixStatisticsBar((MatrixGUIObject) o);
058                    } else {
059                            this.objectStatus = new JLabel();
060                    }
061                    this.setPreferredSize(new Dimension(1000, 30));
062                    this.setBorder(BorderFactory.createEtchedBorder());
063                    this.setLayout(new GridBagLayout());
064    
065                    taskStatus.setPreferredSize(new Dimension(200, 30));
066                    taskStatus.setMinimumSize(new Dimension(200, 30));
067    
068                    add(objectStatus, new GridBagConstraints(0, 0, 1, 1, 0.2, 1.0,
069                                    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(2,
070                                                    2, 2, 2), 0, 0));
071    
072                    add(taskStatus, new GridBagConstraints(2, 0, 1, 1, 0.0, 1.0,
073                                    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(2,
074                                                    2, 2, 2), 0, 0));
075    
076                    add(new MemoryUsage(), new GridBagConstraints(3, 0, 1, 1, 0.0, 1.0,
077                                    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(2,
078                                                    2, 2, 2), 0, 0));
079    
080                    jProgressBar.setStringPainted(false);
081                    jProgressBar.setMinimum(0);
082                    jProgressBar.setMaximum(1000);
083                    jProgressBar.setValue(1000);
084                    jProgressBar.setVisible(false);
085    
086                    objectStatus.setBorder(BorderFactory.createEtchedBorder());
087                    taskStatus.setBorder(BorderFactory.createEtchedBorder());
088                    jProgressBar.setBorder(BorderFactory.createEtchedBorder());
089    
090                    add(jProgressBar, new GridBagConstraints(1, 0, 1, 1, 0.8, 1.0,
091                                    GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(2,
092                                                    2, 2, 2), 0, 0));
093    
094            }
095    
096            public void start() {
097                    stop();
098                    // timer = new Timer("Toolbar Timer for " + object.getLabel());
099                    // timer.schedule(new UpdateTask(this), 1000, // initial delay
100                    // 1000); // subsequent rate
101            }
102    
103            public void stop() {
104                    // if (timer != null) {
105                    // timer.cancel();
106                    // timer = null;
107                    // }
108            }
109    
110            public void setTaskString(String s) {
111                    taskStatus.setText(s);
112            }
113    
114            public void setObjectString(String s) {
115                    objectStatus.setText(s);
116            }
117    
118            public void setProgress(Double progress) {
119                    if (progress == null) {
120                            jProgressBar.setValue(0);
121                            jProgressBar.setIndeterminate(true);
122                            jProgressBar.setVisible(true);
123                    } else if (progress == 1.0) {
124                            jProgressBar.setValue(1000);
125                            jProgressBar.setVisible(false);
126                    } else {
127                            int value = (int) (progress * jProgressBar.getMaximum());
128                            jProgressBar.setIndeterminate(false);
129                            jProgressBar.setValue(value);
130                            jProgressBar.setVisible(true);
131                    }
132            }
133    
134            public GUIObject getObject() {
135                    return object;
136            }
137    
138            class UpdateTask extends TimerTask {
139    
140                    private StatusBar statusBar = null;
141    
142                    public UpdateTask(StatusBar statusBar) {
143                            this.statusBar = statusBar;
144                    }
145    
146                    
147                    public void run() {
148                            statusBar.setTaskString(TaskQueue.getStatus());
149                            // statusBar.setToolTipText(getObject().getToolTipText());
150                            statusBar.setObjectString("" + getObject());
151                            statusBar.setProgress(TaskQueue.getProgress());
152                    }
153    
154            }
155    
156    }