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