jdk/src/jdk.jconsole/share/classes/sun/tools/jconsole/Plotter.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19566
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
     2
 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.filechooser.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.filechooser.FileFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.jconsole.JConsoleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import static sun.tools.jconsole.Formatter.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import static sun.tools.jconsole.ProxyClient.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
@SuppressWarnings("serial")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class Plotter extends JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                     implements Accessible, ActionListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static enum Unit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        NONE, BYTES, PERCENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static final String[] rangeNames = {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    56
        Messages.ONE_MIN,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    57
        Messages.FIVE_MIN,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    58
        Messages.TEN_MIN,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    59
        Messages.THIRTY_MIN,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    60
        Messages.ONE_HOUR,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    61
        Messages.TWO_HOURS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    62
        Messages.THREE_HOURS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    63
        Messages.SIX_HOURS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    64
        Messages.TWELVE_HOURS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    65
        Messages.ONE_DAY,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    66
        Messages.SEVEN_DAYS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    67
        Messages.ONE_MONTH,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    68
        Messages.THREE_MONTHS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    69
        Messages.SIX_MONTHS,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    70
        Messages.ONE_YEAR,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
    71
        Messages.ALL
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final int[] rangeValues = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        5,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        30,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        1 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        2 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        3 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        6 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        12 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        1 * 24 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        7 * 24 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        1 * 31 * 24 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        3 * 31 * 24 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        6 * 31 * 24 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        366 * 24 * 60,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    final static long SECOND = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    final static long MINUTE = 60 * SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    final static long HOUR   = 60 * MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    final static long DAY    = 24 * HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    final static Color bgColor = new Color(250, 250, 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    final static Color defaultColor = Color.blue.darker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    final static int ARRAY_SIZE_INCREMENT = 4000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static Stroke dashedStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private TimeStamps times = new TimeStamps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private ArrayList<Sequence> seqs = new ArrayList<Sequence>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private JPopupMenu popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private JMenu timeRangeMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private JRadioButtonMenuItem[] menuRBs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private JMenuItem saveAsMI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private JFileChooser saveFC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int viewRange = -1; // Minutes (value <= 0 means full range)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private Unit unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int decimals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private double decimalsMultiplier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private Border border = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private Rectangle r = new Rectangle(1, 1, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Font smallFont = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // Initial margins, may be recalculated as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private int topMargin = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private int bottomMargin = 45;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private int leftMargin = 65;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private int rightMargin = 70;
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   127
    private final boolean displayLegend;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public Plotter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this(Unit.NONE, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public Plotter(Unit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        this(unit, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   137
    public Plotter(Unit unit, int decimals) {
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   138
        this(unit,decimals,true);
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   139
    }
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   140
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // Note: If decimals > 0 then values must be decimally shifted left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // that many places, i.e. multiplied by Math.pow(10.0, decimals).
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   143
    public Plotter(Unit unit, int decimals, boolean displayLegend) {
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   144
        this.displayLegend = displayLegend;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        setUnit(unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        setDecimals(decimals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        addMouseListener(new MouseAdapter() {
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   151
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (getParent() instanceof PlotterPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    getParent().requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void setUnit(Unit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.unit = unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void setDecimals(int decimals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this.decimals = decimals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        this.decimalsMultiplier = Math.pow(10.0, decimals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void createSequence(String key, String name, Color color, boolean isPlotted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        Sequence seq = getSequence(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            seq = new Sequence(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        seq.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        seq.color = (color != null) ? color : defaultColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        seq.isPlotted = isPlotted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        seqs.add(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void setUseDashedTransitions(String key, boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Sequence seq = getSequence(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (seq != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            seq.transitionStroke = b ? getDashedStroke() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public void setIsPlotted(String key, boolean isPlotted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Sequence seq = getSequence(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (seq != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            seq.isPlotted = isPlotted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // Note: If decimals > 0 then values must be decimally shifted left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // that many places, i.e. multiplied by Math.pow(10.0, decimals).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public synchronized void addValues(long time, long... values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        assert (values.length == seqs.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        times.add(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i = 0; i < values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            seqs.get(i).add(values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private Sequence getSequence(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        for (Sequence seq : seqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (seq.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return the displayed time range in minutes, or -1 for all data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int getViewRange() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return viewRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param minutes the displayed time range in minutes, or -1 to diaplay all data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void setViewRange(int minutes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (minutes != viewRange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int oldValue = viewRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            viewRange = minutes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            /* Do not i18n this string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            firePropertyChange("viewRange", oldValue, viewRange);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (popupMenu != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                for (int i = 0; i < menuRBs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    if (rangeValues[i] == viewRange) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        menuRBs[i].setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   244
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public JPopupMenu getComponentPopupMenu() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (popupMenu == null) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   247
            popupMenu = new JPopupMenu(Messages.CHART_COLON);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   248
            timeRangeMenu = new JMenu(Messages.PLOTTER_TIME_RANGE_MENU);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   249
            timeRangeMenu.setMnemonic(Resources.getMnemonicInt(Messages.PLOTTER_TIME_RANGE_MENU));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            popupMenu.add(timeRangeMenu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            menuRBs = new JRadioButtonMenuItem[rangeNames.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            ButtonGroup rbGroup = new ButtonGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            for (int i = 0; i < rangeNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                menuRBs[i] = new JRadioButtonMenuItem(rangeNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                rbGroup.add(menuRBs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                menuRBs[i].addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (viewRange == rangeValues[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    menuRBs[i].setSelected(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                timeRangeMenu.add(menuRBs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            popupMenu.addSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   265
            saveAsMI = new JMenuItem(Messages.PLOTTER_SAVE_AS_MENU_ITEM);
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   266
            saveAsMI.setMnemonic(Resources.getMnemonicInt(Messages.PLOTTER_SAVE_AS_MENU_ITEM));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            saveAsMI.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            popupMenu.add(saveAsMI);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return popupMenu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void actionPerformed(ActionEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        JComponent src = (JComponent)ev.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (src == saveAsMI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            saveAs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int index = timeRangeMenu.getPopupMenu().getComponentIndex(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            setViewRange(rangeValues[index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private void saveAs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (saveFC == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            saveFC = new SaveDataFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int ret = saveFC.showSaveDialog(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (ret == JFileChooser.APPROVE_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            saveDataToFile(saveFC.getSelectedFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private void saveDataToFile(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            PrintStream out = new PrintStream(new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // Print header line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            out.print("Time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (Sequence seq : seqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                out.print(","+seq.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // Print data lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (seqs.size() > 0 && seqs.get(0).size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                for (int i = 0; i < seqs.get(0).size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    double excelTime = toExcelTime(times.time(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    out.print(String.format(Locale.ENGLISH, "%.6f", excelTime));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    for (Sequence seq : seqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        out.print("," + getFormattedValue(seq.value(i), false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            JOptionPane.showMessageDialog(this,
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   318
                                          Resources.format(Messages.FILE_CHOOSER_SAVED_FILE,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   319
                                                           file.getAbsolutePath(),
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   320
                                                           file.length()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            String msg = ex.getLocalizedMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            String path = file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (msg.startsWith(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                msg = msg.substring(path.length()).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            JOptionPane.showMessageDialog(this,
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   328
                                          Resources.format(Messages.FILE_CHOOSER_SAVE_FAILED_MESSAGE,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   329
                                                           path,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   330
                                                           msg),
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
   331
                                          Messages.FILE_CHOOSER_SAVE_FAILED_TITLE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                          JOptionPane.ERROR_MESSAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   336
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void paintComponent(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        super.paintComponent(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
19566
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   340
        int width = getWidth()-rightMargin-leftMargin-10;
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   341
        int height = getHeight()-topMargin-bottomMargin;
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   342
        if (width <= 0 || height <= 0) {
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   343
            // not enough room to paint anything
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   344
            return;
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   345
        }
20347ff1b3c2 6358357: Division by zero in Threads tab when shrinking jconsole window
egahlin
parents: 14342
diff changeset
   346
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        Color oldColor = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        Font  oldFont  = g.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Color fg = getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Color bg = getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        boolean bgIsLight = (bg.getRed() > 200 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                             bg.getGreen() > 200 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                             bg.getBlue() > 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                         RenderingHints.VALUE_ANTIALIAS_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (smallFont == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            smallFont = oldFont.deriveFont(9.0F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        r.x = leftMargin - 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        r.y = topMargin  - 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        r.width  = getWidth()-leftMargin-rightMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        r.height = getHeight()-topMargin-bottomMargin+16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (border == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // By setting colors here, we avoid recalculating them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            // over and over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            border = new BevelBorder(BevelBorder.LOWERED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                     getBackground().brighter().brighter(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                                     getBackground().brighter(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                     getBackground().darker().darker(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                     getBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        border.paintBorder(this, g, r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // Fill background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        g.setColor(bgColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        g.fillRect(r.x+2, r.y+2, r.width-4, r.height-4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        long tMin = Long.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        long tMax = Long.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        long vMin = Long.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        long vMax = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int w = getWidth()-rightMargin-leftMargin-10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int h = getHeight()-topMargin-bottomMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (times.size > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            tMin = Math.min(tMin, times.time(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            tMax = Math.max(tMax, times.time(times.size-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        long viewRangeMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (viewRange > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            viewRangeMS = viewRange * MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // Display full time range, but no less than a minute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            viewRangeMS = Math.max(tMax - tMin, 1 * MINUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // Calculate min/max values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        for (Sequence seq : seqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (seq.size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                for (int i = 0; i < seq.size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    if (seq.size == 1 || times.time(i) >= tMax - viewRangeMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        long val = seq.value(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        if (val > Long.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            vMax = Math.max(vMax, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            vMin = Math.min(vMin, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                vMin = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (unit == Unit.BYTES || !seq.isPlotted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                // We'll scale only to the first (main) value set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                // TODO: Use a separate property for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // Normalize scale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        vMax = normalizeMax(vMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (vMin > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (vMax / vMin > 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                vMin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                vMin = normalizeMin(vMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        // Axes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // Draw vertical axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        int x = leftMargin - 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        int y = topMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        g.drawLine(x,   y,   x,   y+h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        int n = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if ((""+vMax).startsWith("2")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            n = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } else if ((""+vMax).startsWith("3")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            n = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        } else if ((""+vMax).startsWith("4")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            n = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        } else if ((""+vMax).startsWith("6")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            n = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } else if ((""+vMax).startsWith("7")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            n = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } else if ((""+vMax).startsWith("8")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            n = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        } else if ((""+vMax).startsWith("9")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            n = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        // Ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        ArrayList<Long> tickValues = new ArrayList<Long>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        tickValues.add(vMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            long v = i * vMax / n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (v > vMin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                tickValues.add(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        tickValues.add(vMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        n = tickValues.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        String[] tickStrings = new String[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            long v = tickValues.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            tickStrings[i] = getSizeString(v, vMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // Trim trailing decimal zeroes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (decimals > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            boolean trimLast = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            boolean removedDecimalPoint = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                for (String str : tickStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    if (!(str.endsWith("0") || str.endsWith("."))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        trimLast = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                if (trimLast) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    if (tickStrings[0].endsWith(".")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        removedDecimalPoint = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                        String str = tickStrings[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        tickStrings[i] = str.substring(0, str.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            } while (trimLast && !removedDecimalPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        // Draw ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        int lastY = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            long v = tickValues.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            y = topMargin+h-(int)(h * (v-vMin) / (vMax-vMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            g.drawLine(x-2, y, x+2, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            String s = tickStrings[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (unit == Unit.PERCENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                s += "%";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            int sx = x-6-fm.stringWidth(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (y < lastY-13) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                if (checkLeftMargin(sx)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    // Wait for next repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                g.drawString(s, sx, y+4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // Draw horizontal grid line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            g.setColor(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            g.drawLine(r.x + 4, y, r.x + r.width - 4, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            lastY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // Draw horizontal axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        x = leftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        y = topMargin + h + 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        g.drawLine(x,   y,   x+w, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        long t1 = tMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (t1 <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            // No data yet, so draw current time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            t1 = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        long tz = timeDF.getTimeZone().getOffset(t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        long tickInterval = calculateTickInterval(w, 40, viewRangeMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (tickInterval > 3 * HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            tickInterval = calculateTickInterval(w, 80, viewRangeMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        long t0 = tickInterval - (t1 - viewRangeMS + tz) % tickInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        while (t0 < viewRangeMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            x = leftMargin + (int)(w * t0 / viewRangeMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            g.drawLine(x, y-2, x, y+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            long t = t1 - viewRangeMS + t0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            String str = formatClockTime(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            g.drawString(str, x, y+16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            //if (tickInterval > (1 * HOUR) && t % (1 * DAY) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if ((t + tz) % (1 * DAY) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                str = formatDate(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                g.drawString(str, x, y+27);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            // Draw vertical grid line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            g.setColor(Color.lightGray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            g.drawLine(x, topMargin, x, topMargin + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            t0 += tickInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        // Plot values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        int start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        int nValues = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int nLists = seqs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (nLists > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            nValues = seqs.get(0).size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (nValues == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            Sequence seq = seqs.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            // Find starting point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            for (int p = 0; p < seq.size; p++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                if (times.time(p) >= tMax - viewRangeMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    start = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        //Optimization: collapse plot of more than four values per pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        int pointsPerPixel = (nValues - start) / w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (pointsPerPixel < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            pointsPerPixel = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Draw graphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        // Loop backwards over sequences because the first needs to be painted on top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        for (int i = nLists-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            int x0 = leftMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            int y0 = topMargin + h + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            Sequence seq = seqs.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (seq.isPlotted && seq.size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                // Paint twice, with white and with color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                for (int pass = 0; pass < 2; pass++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    g.setColor((pass == 0) ? Color.white : seq.color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    int x1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    long v1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    for (int p = start; p < nValues; p += pointsPerPixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        // Make sure we get the last value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        if (pointsPerPixel > 1 && p >= nValues - pointsPerPixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                            p = nValues - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        int x2 = (int)(w * (times.time(p)-(t1-viewRangeMS)) / viewRangeMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        long v2 = seq.value(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        if (v2 >= vMin && v2 <= vMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                            int y2  = (int)(h * (v2 -vMin) / (vMax-vMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                            if (x1 >= 0 && v1 >= vMin && v1 <= vMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                int y1 = (int)(h * (v1-vMin) / (vMax-vMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                if (y1 == y2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                    // fillrect is much faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                    g.fillRect(x0+x1, y0-y1-pass, x2-x1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                    Graphics2D g2d = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                    Stroke oldStroke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                    if (seq.transitionStroke != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                        oldStroke = g2d.getStroke();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                        g2d.setStroke(seq.transitionStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                    g.drawLine(x0+x1, y0-y1-pass, x0+x2, y0-y2-pass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                    if (oldStroke != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                        g2d.setStroke(oldStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                        x1 = x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        v1 = v2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                // Current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                long v = seq.value(seq.size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                if (v >= vMin && v <= vMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    if (bgIsLight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        g.setColor(seq.color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    x = r.x + r.width + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    y = topMargin+h-(int)(h * (v-vMin) / (vMax-vMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    // a small triangle/arrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    g.fillPolygon(new int[] { x+2, x+6, x+6 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                  new int[] { y,   y+3, y-3 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                  3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        int[] valueStringSlots = new int[nLists];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        for (int i = 0; i < nLists; i++) valueStringSlots[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        for (int i = 0; i < nLists; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            Sequence seq = seqs.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (seq.isPlotted && seq.size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                // Draw current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                // TODO: collapse values if pointsPerPixel >= 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                long v = seq.value(seq.size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                if (v >= vMin && v <= vMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    x = r.x + r.width + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    y = topMargin+h-(int)(h * (v-vMin) / (vMax-vMin));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    int y2 = getValueStringSlot(valueStringSlots, y, 2*10, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    g.setFont(smallFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    if (bgIsLight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        g.setColor(seq.color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    String curValue = getFormattedValue(v, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    if (unit == Unit.PERCENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        curValue += "%";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    int valWidth = fm.stringWidth(curValue);
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
   684
                    String legend = (displayLegend?seq.name:"");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    int legendWidth = fm.stringWidth(legend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    if (checkRightMargin(valWidth) || checkRightMargin(legendWidth)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        // Wait for next repaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    g.drawString(legend  , x + 17, Math.min(topMargin+h,      y2 + 3 - 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    g.drawString(curValue, x + 17, Math.min(topMargin+h + 10, y2 + 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    // Maybe draw a short line to value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    if (y2 > y + 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                        g.drawLine(x + 9, y + 2, x + 14, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    } else if (y2 < y - 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        g.drawLine(x + 9, y - 2, x + 14, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                g.setFont(oldFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                g.setColor(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    private boolean checkLeftMargin(int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // Make sure leftMargin has at least 2 pixels over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (x < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            leftMargin += (2 - x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            // Repaint from top (above any cell renderers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            SwingUtilities.getWindowAncestor(this).repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    private boolean checkRightMargin(int w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        // Make sure rightMargin has at least 2 pixels over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (w + 2 > rightMargin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            rightMargin = (w + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            // Repaint from top (above any cell renderers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            SwingUtilities.getWindowAncestor(this).repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    private int getValueStringSlot(int[] slots, int y, int h, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        for (int s = 0; s < slots.length; s++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (slots[s] >= y && slots[s] < y + h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                // collide below us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                if (slots[s] > h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    return getValueStringSlot(slots, slots[s]-h, h, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    return getValueStringSlot(slots, slots[s]+h, h, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            } else if (y >= h && slots[s] > y - h && slots[s] < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                // collide above us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                return getValueStringSlot(slots, slots[s]+h, h, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        slots[i] = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    private long calculateTickInterval(int w, int hGap, long viewRangeMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        long tickInterval = viewRangeMS * hGap / w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (tickInterval < 1 * MINUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            tickInterval = 1 * MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        } else if (tickInterval < 5 * MINUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            tickInterval = 5 * MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } else if (tickInterval < 10 * MINUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            tickInterval = 10 * MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        } else if (tickInterval < 30 * MINUTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            tickInterval = 30 * MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        } else if (tickInterval < 1 * HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            tickInterval = 1 * HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        } else if (tickInterval < 3 * HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            tickInterval = 3 * HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        } else if (tickInterval < 6 * HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            tickInterval = 6 * HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        } else if (tickInterval < 12 * HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            tickInterval = 12 * HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        } else if (tickInterval < 1 * DAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            tickInterval = 1 * DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            tickInterval = normalizeMax(tickInterval / DAY) * DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return tickInterval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    private long normalizeMin(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        int exp = (int)Math.log10((double)l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        long multiple = (long)Math.pow(10.0, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        int i = (int)(l / multiple);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        return i * multiple;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    private long normalizeMax(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        int exp = (int)Math.log10((double)l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        long multiple = (long)Math.pow(10.0, exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        int i = (int)(l / multiple);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        l = (i+1)*multiple;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private String getFormattedValue(long v, boolean groupDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        String fmt = "%";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (groupDigits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            fmt += ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if (decimals > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            fmt += "." + decimals + "f";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            str = String.format(fmt, v / decimalsMultiplier);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            fmt += "d";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            str = String.format(fmt, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    private String getSizeString(long v, long vMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (unit == Unit.BYTES && decimals == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            s = formatBytes(v, vMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            s = getFormattedValue(v, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    private static synchronized Stroke getDashedStroke() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (dashedStroke == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            dashedStroke = new BasicStroke(1.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                                           BasicStroke.CAP_BUTT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                                           BasicStroke.JOIN_MITER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                           10.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                           new float[] { 2.0f, 3.0f },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                           0.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return dashedStroke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    private static Object extendArray(Object a1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        int n = Array.getLength(a1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        Object a2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            Array.newInstance(a1.getClass().getComponentType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                              n + ARRAY_SIZE_INCREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        System.arraycopy(a1, 0, a2, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        return a2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    private static class TimeStamps {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // Time stamps (long) are split into offsets (long) and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // series of times from the offsets (int). A new offset is
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   841
        // stored when the time value doesn't fit in an int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // (approx every 24 days).  An array of indices is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        // define the starting point for each offset in the times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        // array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        long[] offsets = new long[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        int[] indices = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        int[] rtimes = new int[ARRAY_SIZE_INCREMENT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        // Number of stored timestamps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * Returns the time stamp for index i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        public long time(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            long offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            for (int j = indices.length - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                if (i >= indices[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    offset = offsets[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return offset + rtimes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        public void add(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            // May need to store a new time offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            int n = offsets.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            if (n == 0 || time - offsets[n - 1] > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                // Grow offset and indices arrays and store new offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                offsets = Arrays.copyOf(offsets, n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                offsets[n] = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                indices = Arrays.copyOf(indices, n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                indices[n] = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // May need to extend the array size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            if (rtimes.length == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                rtimes = (int[])extendArray(rtimes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            // Store the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            rtimes[size]  = (int)(time - offsets[offsets.length - 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    private static class Sequence {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        boolean isPlotted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        Stroke transitionStroke = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // Values are stored in an int[] if all values will fit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // otherwise in a long[]. An int can represent up to 2 GB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // Use a random start size, so all arrays won't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        // be grown during the same update interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        Object values =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            new byte[ARRAY_SIZE_INCREMENT + (int)(Math.random() * 100)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        // Number of stored values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public Sequence(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
         * Returns the value at index i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        public long value(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            return Array.getLong(values, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        public void add(long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            // May need to switch to a larger array type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            if ((values instanceof byte[] ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                 values instanceof short[] ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                 values instanceof int[]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                       value > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                long[] la = new long[Array.getLength(values)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    la[i] = Array.getLong(values, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                values = la;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            } else if ((values instanceof byte[] ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        values instanceof short[]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                       value > Short.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                int[] ia = new int[Array.getLength(values)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    ia[i] = Array.getInt(values, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                values = ia;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            } else if (values instanceof byte[] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                       value > Byte.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                short[] sa = new short[Array.getLength(values)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                    sa[i] = Array.getShort(values, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                values = sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            // May need to extend the array size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            if (Array.getLength(values) == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                values = extendArray(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            // Store the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            if (values instanceof long[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                ((long[])values)[size] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            } else if (values instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                ((int[])values)[size] = (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            } else if (values instanceof short[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                ((short[])values)[size] = (short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                ((byte[])values)[size] = (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    // Can be overridden by subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    long getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    long getLastTimeStamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return times.time(times.size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    long getLastValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        Sequence seq = getSequence(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        return (seq != null && seq.size > 0) ? seq.value(seq.size - 1) : 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    // Called on EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    public void propertyChange(PropertyChangeEvent ev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        String prop = ev.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (prop == JConsoleContext.CONNECTION_STATE_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            ConnectionState newState = (ConnectionState)ev.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            switch (newState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
              case DISCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                    long time = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                    times.add(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    for (Sequence seq : seqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                        seq.add(Long.MIN_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    private static class SaveDataFileChooser extends JFileChooser {
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
  1000
        private static final long serialVersionUID = -5182890922369369669L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        SaveDataFileChooser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            setFileFilter(new FileNameExtensionFilter("CSV file", "csv"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
  1005
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        public void approveSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            File file = getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (file != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                FileFilter filter = getFileFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                if (filter != null && filter instanceof FileNameExtensionFilter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    String[] extensions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                        ((FileNameExtensionFilter)filter).getExtensions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    boolean goodExt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                    for (String ext : extensions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                        if (file.getName().toLowerCase().endsWith("." + ext.toLowerCase())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                            goodExt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    if (!goodExt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                        file = new File(file.getParent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                                        file.getName() + "." + extensions[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                if (file.exists()) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1028
                    String okStr = Messages.FILE_CHOOSER_FILE_EXISTS_OK_OPTION;
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1029
                    String cancelStr = Messages.FILE_CHOOSER_FILE_EXISTS_CANCEL_OPTION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    int ret =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                        JOptionPane.showOptionDialog(this,
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1032
                                                     Resources.format(Messages.FILE_CHOOSER_FILE_EXISTS_MESSAGE,
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1033
                                                                      file.getName()),
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1034
                                                     Messages.FILE_CHOOSER_FILE_EXISTS_TITLE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                                                     JOptionPane.OK_CANCEL_OPTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                                                     JOptionPane.WARNING_MESSAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                                                     null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                                                     new Object[] { okStr, cancelStr },
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                                                     okStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    if (ret != JOptionPane.OK_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                setSelectedFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            super.approveSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
  1050
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            accessibleContext = new AccessiblePlotter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    protected class AccessiblePlotter extends AccessibleJComponent {
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
  1059
        private static final long serialVersionUID = -3847205410473510922L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        protected AccessiblePlotter() {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1061
            setAccessibleName(Messages.PLOTTER_ACCESSIBLE_NAME);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
  1064
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            String name = super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            if (seqs.size() > 0 && seqs.get(0).size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                String keyValueList = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                for (Sequence seq : seqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    if (seq.isPlotted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                        String value = "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                        if (seq.size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                            if (unit == Unit.BYTES) {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1075
                                value = Resources.format(Messages.SIZE_BYTES, seq.value(seq.size - 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                                value =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                                    getFormattedValue(seq.value(seq.size - 1), false) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                                    ((unit == Unit.PERCENT) ? "%" : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                        // Assume format string ends with newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                        keyValueList +=
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1084
                            Resources.format(Messages.PLOTTER_ACCESSIBLE_NAME_KEY_AND_VALUE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                                    seq.key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                name += "\n" + keyValueList + ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            } else {
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 5506
diff changeset
  1090
                name += "\n" + Messages.PLOTTER_ACCESSIBLE_NAME_NO_DATA;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
1233
3b08cf0cab53 6748745: JConsole: plotters don't resize well when the window is resized
dfuchs
parents: 2
diff changeset
  1095
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            return AccessibleRole.CANVAS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
}