001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import java.awt.*;
029: import java.lang.reflect.InvocationTargetException;
030: import java.util.ArrayList;
031:
032: import javax.swing.*;
033: import javax.swing.border.*;
034:
035: import static sun.tools.jconsole.JConsole.*;
036: import static sun.tools.jconsole.Resources.*;
037:
038: @SuppressWarnings("serial")
039: class OverviewTab extends Tab {
040: JPanel gridPanel;
041: TimeComboBox timeComboBox;
042:
043: public static String getTabName() {
044: return getText("Overview");
045: }
046:
047: public OverviewTab(VMPanel vmPanel) {
048: super (vmPanel, getTabName());
049:
050: setBorder(new EmptyBorder(4, 4, 3, 4));
051: setLayout(new BorderLayout());
052:
053: JPanel topPanel = new JPanel(new BorderLayout());
054: add(topPanel, BorderLayout.NORTH);
055:
056: JPanel controlPanel = new JPanel(new FlowLayout(
057: FlowLayout.CENTER, 20, 5));
058: topPanel.add(controlPanel, BorderLayout.CENTER);
059:
060: timeComboBox = new TimeComboBox();
061: LabeledComponent lc = new LabeledComponent(Resources
062: .getText("Time Range:"), getMnemonicInt("Time Range:"),
063: timeComboBox);
064: controlPanel.add(lc);
065:
066: gridPanel = new JPanel(new AutoGridLayout(10, 6));
067: gridPanel.setBorder(null);
068: JScrollPane sp = new JScrollPane(gridPanel);
069: sp.setBorder(null);
070: sp.setViewportBorder(null);
071: add(sp, BorderLayout.CENTER);
072:
073: // Note that panels are added on first update
074: }
075:
076: public SwingWorker<?, ?> newSwingWorker() {
077: return new SwingWorker<Object, Object>() {
078: public Object doInBackground() {
079: return null;
080: }
081:
082: protected void done() {
083: if (gridPanel.getComponentCount() == 0) {
084: final ArrayList<Plotter> plotters = new ArrayList<Plotter>();
085: for (Tab tab : vmPanel.getTabs()) {
086: OverviewPanel[] ops = tab.getOverviewPanels();
087: if (ops != null) {
088: for (OverviewPanel op : ops) {
089: gridPanel.add(op);
090: Plotter plotter = op.getPlotter();
091: if (plotter != null) {
092: plotters.add(plotter);
093: timeComboBox.addPlotter(plotter);
094: }
095: }
096: }
097: }
098: if (plotters.size() > 0) {
099: workerAdd(new Runnable() {
100: public void run() {
101: ProxyClient proxyClient = vmPanel
102: .getProxyClient();
103: for (Plotter plotter : plotters) {
104: proxyClient
105: .addWeakPropertyChangeListener(plotter);
106: }
107: }
108: });
109: }
110: if (getParent() instanceof JTabbedPane) {
111: Utilities
112: .updateTransparency((JTabbedPane) getParent());
113: }
114: }
115: }
116: };
117: }
118:
119: private class AutoGridLayout extends GridLayout {
120: public AutoGridLayout(int hGap, int vGap) {
121: super (0, 1, hGap, vGap);
122: }
123:
124: public Dimension preferredLayoutSize(Container parent) {
125: return minimumLayoutSize(parent);
126: }
127:
128: public Dimension minimumLayoutSize(Container parent) {
129: updateColumns(parent);
130: return super .minimumLayoutSize(parent);
131: }
132:
133: private void updateColumns(Container parent) {
134: // Use the outer panel width, not the scrolling gridPanel
135: int parentWidth = OverviewTab.this .getWidth();
136:
137: int columnWidth = 1;
138:
139: for (Component c : parent.getComponents()) {
140: columnWidth = Math.max(columnWidth, c
141: .getPreferredSize().width);
142: }
143:
144: int n = parent.getComponentCount();
145: int maxCols = Math.min(n, parentWidth / columnWidth);
146:
147: for (int columns = maxCols; columns >= 1; columns--) {
148: if (columns == 1) {
149: setColumns(maxCols);
150: } else if ((n % columns) == 0) {
151: setColumns(columns);
152: break;
153: }
154: }
155: }
156: }
157: }
|