1 /*
2  * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.tools.jconsole;
27 
28 import java.awt.*;
29 
30 import javax.swing.*;
31 
32 
33 import static javax.swing.SwingConstants.*;
34 import static sun.tools.jconsole.JConsole.*;
35 import static sun.tools.jconsole.Utilities.*;
36 
37 
38 @SuppressWarnings("serial")
39 abstract class OverviewPanel extends PlotterPanel {
40     private static final Dimension PREFERRED_PLOTTER_SIZE = new Dimension(300, 200);
41     private static final Dimension MINIMUM_PLOTTER_SIZE = new Dimension(200, 150);
42 
43     // This is the default view range for all the overview plotters
44     static final int VIEW_RANGE = -1;   // Show all data
45 
46     static Color PLOTTER_COLOR = IS_GTK ? new Color(231, 111, 80) : null;
47 
48     private JLabel infoLabel;
49 
OverviewPanel(String title)50     public OverviewPanel(String title) {
51         this(title, null, null, null);
52     }
53 
OverviewPanel(String title, String plotterKey, String plotterName, Plotter.Unit plotterUnit)54     public OverviewPanel(String title, String plotterKey,
55                          String plotterName, Plotter.Unit plotterUnit) {
56         super(title);
57         setLayout(new BorderLayout(0, 0));
58 
59         if (plotterKey != null && plotterName != null) {
60             Plotter plotter = new Plotter();
61             plotter.setPreferredSize(PREFERRED_PLOTTER_SIZE);
62             plotter.setMinimumSize(MINIMUM_PLOTTER_SIZE);
63             plotter.setViewRange(VIEW_RANGE);
64             if (plotterUnit != null) {
65                 plotter.setUnit(plotterUnit);
66             }
67             plotter.createSequence(plotterKey, plotterName, PLOTTER_COLOR, true);
68             setAccessibleName(plotter,
69                               Resources.format(Messages.OVERVIEW_PANEL_PLOTTER_ACCESSIBLE_NAME,
70                                       title));
71             setPlotter(plotter);
72         }
73     }
74 
75 
getInfoLabel()76     public JLabel getInfoLabel() {
77         if (infoLabel == null) {
78             infoLabel = new JLabel("", CENTER) {
79                 @Override
80                 public void setText(String text) {
81                     if (text.startsWith("<html>")) {
82                         // Replace spaces with nbsp, except the
83                         // last one of two or more (to allow wrapping)
84                         StringBuilder buf = new StringBuilder();
85                         char[] chars = text.toCharArray();
86                         int n = chars.length;
87                         for (int i = 0; i < n; i++) {
88                             if (chars[i] == ' '
89                                 && ((i < n-1 && chars[i+1] == ' ')
90                                     || ((i == 0 || chars[i-1] != ' ')
91                                         && (i == n-1 || chars[i+1] != ' ')))) {
92                                 buf.append("&nbsp;");
93                             } else {
94                                 buf.append(chars[i]);
95                             }
96                         }
97                         text = buf.toString();
98                     }
99                     super.setText(text);
100                 }
101             };
102 
103             if (IS_GTK) {
104                 JPanel southPanel = new JPanel(new BorderLayout());
105                 JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
106                 southPanel.add(separator, BorderLayout.NORTH);
107                 southPanel.add(infoLabel, BorderLayout.SOUTH);
108                 add(southPanel, BorderLayout.SOUTH);
109             } else {
110                 add(infoLabel, BorderLayout.SOUTH);
111             }
112         }
113         return infoLabel;
114     }
115 }
116