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.lang.reflect.InvocationTargetException;
  30 import java.util.ArrayList;
  31 
  32 import javax.swing.*;
  33 import javax.swing.border.*;
  34 
  35 import static sun.tools.jconsole.JConsole.*;
  36 import static sun.tools.jconsole.Resources.*;
  37 
  38 
  39 @SuppressWarnings("serial")
  40 class OverviewTab extends Tab {
  41     JPanel gridPanel;
  42     TimeComboBox timeComboBox;
  43 
  44     public static String getTabName() {
  45         return getText("Overview");
  46     }
  47 
  48     public OverviewTab(VMPanel vmPanel) {
  49         super(vmPanel, getTabName());
  50 
  51         setBorder(new EmptyBorder(4, 4, 3, 4));
  52         setLayout(new BorderLayout());
  53 
  54         JPanel topPanel     = new JPanel(new BorderLayout());
  55         add(topPanel, BorderLayout.NORTH);
  56 
  57         JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
  58         topPanel.add(controlPanel, BorderLayout.CENTER);
  59 
  60         timeComboBox = new TimeComboBox();
  61         LabeledComponent lc = new LabeledComponent(Resources.getText("Time Range:"),
  62                                                    getMnemonicInt("Time Range:"),
  63                                                    timeComboBox);
  64         controlPanel.add(lc);
  65 
  66         gridPanel = new JPanel(new AutoGridLayout(10, 6));
  67         gridPanel.setBorder(null);
  68         JScrollPane sp = new JScrollPane(gridPanel);
  69         sp.setBorder(null);
  70         sp.setViewportBorder(null);
  71         add(sp, BorderLayout.CENTER);
  72 
  73         // Note that panels are added on first update
  74     }
  75 
  76 
  77     public SwingWorker<?, ?> newSwingWorker() {
  78         return new SwingWorker<Object, Object>() {
  79             public Object doInBackground() {
  80                 return null;
  81             }
  82 
  83             protected void done() {
  84                 if (gridPanel.getComponentCount() == 0) {
  85                     final ArrayList<Plotter> plotters = new ArrayList<Plotter>();
  86                     for (Tab tab : vmPanel.getTabs()) {
  87                         OverviewPanel[] ops = tab.getOverviewPanels();
  88                         if (ops != null) {
  89                             for (OverviewPanel op : ops) {
  90                                 gridPanel.add(op);
  91                                 Plotter plotter = op.getPlotter();
  92                                 if (plotter != null) {
  93                                     plotters.add(plotter);
  94                                     timeComboBox.addPlotter(plotter);
  95                                 }
  96                             }
  97                         }
  98                     }
  99                     if (plotters.size() > 0) {
 100                         workerAdd(new Runnable() {
 101                             public void run() {
 102                                 ProxyClient proxyClient = vmPanel.getProxyClient();
 103                                 for (Plotter plotter : plotters) {
 104                                     proxyClient.addWeakPropertyChangeListener(plotter);
 105                                 }
 106                             }
 107                         });
 108                     }
 109                     if (getParent() instanceof JTabbedPane) {
 110                         Utilities.updateTransparency((JTabbedPane)getParent());
 111                     }
 112                 }
 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.getPreferredSize().width);
 141             }
 142 
 143             int n = parent.getComponentCount();
 144             int maxCols = Math.min(n, parentWidth / columnWidth);
 145 
 146             for (int columns = maxCols; columns >= 1; columns--) {
 147                 if (columns == 1) {
 148                     setColumns(maxCols);
 149                 } else if ((n % columns) == 0) {
 150                     setColumns(columns);
 151                     break;
 152                 }
 153             }
 154         }
 155     }
 156 }