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
26package sun.tools.jconsole;
27
28import java.awt.*;
29import java.util.ArrayList;
30
31import javax.swing.*;
32import javax.swing.border.*;
33
34
35@SuppressWarnings("serial")
36class OverviewTab extends Tab {
37    JPanel gridPanel;
38    TimeComboBox timeComboBox;
39
40    public static String getTabName() {
41        return Messages.OVERVIEW;
42    }
43
44    public OverviewTab(VMPanel vmPanel) {
45        super(vmPanel, getTabName());
46
47        setBorder(new EmptyBorder(4, 4, 3, 4));
48        setLayout(new BorderLayout());
49
50        JPanel topPanel     = new JPanel(new BorderLayout());
51        add(topPanel, BorderLayout.NORTH);
52
53        JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
54        topPanel.add(controlPanel, BorderLayout.CENTER);
55
56        timeComboBox = new TimeComboBox();
57        LabeledComponent lc = new LabeledComponent(Messages.TIME_RANGE_COLON,
58                                                   Resources.getMnemonicInt(Messages.TIME_RANGE_COLON),
59                                                   timeComboBox);
60        controlPanel.add(lc);
61
62        gridPanel = new JPanel(new AutoGridLayout(10, 6));
63        gridPanel.setBorder(null);
64        JScrollPane sp = new JScrollPane(gridPanel);
65        sp.setBorder(null);
66        sp.setViewportBorder(null);
67        add(sp, BorderLayout.CENTER);
68
69        // Note that panels are added on first update
70    }
71
72
73    public SwingWorker<?, ?> newSwingWorker() {
74        return new SwingWorker<Object, Object>() {
75            public Object doInBackground() {
76                return null;
77            }
78
79            protected void done() {
80                if (gridPanel.getComponentCount() == 0) {
81                    final ArrayList<Plotter> plotters = new ArrayList<Plotter>();
82                    for (Tab tab : vmPanel.getTabs()) {
83                        OverviewPanel[] ops = tab.getOverviewPanels();
84                        if (ops != null) {
85                            for (OverviewPanel op : ops) {
86                                gridPanel.add(op);
87                                Plotter plotter = op.getPlotter();
88                                if (plotter != null) {
89                                    plotters.add(plotter);
90                                    timeComboBox.addPlotter(plotter);
91                                }
92                            }
93                        }
94                    }
95                    if (plotters.size() > 0) {
96                        workerAdd(new Runnable() {
97                            public void run() {
98                                ProxyClient proxyClient = vmPanel.getProxyClient();
99                                for (Plotter plotter : plotters) {
100                                    proxyClient.addWeakPropertyChangeListener(plotter);
101                                }
102                            }
103                        });
104                    }
105                    if (getParent() instanceof JTabbedPane) {
106                        Utilities.updateTransparency((JTabbedPane)getParent());
107                    }
108                }
109            }
110        };
111    }
112
113
114
115    private class AutoGridLayout extends GridLayout {
116        public AutoGridLayout(int hGap, int vGap) {
117            super(0, 1, hGap, vGap);
118        }
119
120        public Dimension preferredLayoutSize(Container parent) {
121            return minimumLayoutSize(parent);
122        }
123
124        public Dimension minimumLayoutSize(Container parent) {
125            updateColumns(parent);
126            return super.minimumLayoutSize(parent);
127        }
128
129        private void updateColumns(Container parent) {
130            // Use the outer panel width, not the scrolling gridPanel
131            int parentWidth = OverviewTab.this.getWidth();
132
133            int columnWidth = 1;
134
135            for (Component c : parent.getComponents()) {
136                columnWidth = Math.max(columnWidth, c.getPreferredSize().width);
137            }
138
139            int n = parent.getComponentCount();
140            int maxCols = Math.min(n, parentWidth / columnWidth);
141
142            for (int columns = maxCols; columns >= 1; columns--) {
143                if (columns == 1) {
144                    setColumns(maxCols);
145                } else if ((n % columns) == 0) {
146                    setColumns(columns);
147                    break;
148                }
149            }
150        }
151    }
152}
153