1/* 2 * Copyright (c) 2004, 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 26package sun.tools.jconsole; 27 28import java.awt.event.*; 29import java.beans.*; 30import java.util.*; 31 32import javax.swing.*; 33 34/** 35 * A combo box to control the visible time range for one or more Plotter components. 36 * When used with two or more Plotters, it also acts to coordinate the range between 37 * them. 38 */ 39@SuppressWarnings("serial") 40public class TimeComboBox extends JComboBox<String> implements ItemListener, PropertyChangeListener { 41 private ArrayList<Plotter> plotters = new ArrayList<Plotter>(); 42 43 public TimeComboBox(Plotter... plotterArray) { 44 super(Plotter.rangeNames); 45 46 addItemListener(this); 47 48 if (plotterArray != null && plotterArray.length > 0) { 49 plotters.addAll(Arrays.asList(plotterArray)); 50 selectValue(plotterArray[0].getViewRange()); 51 for (Plotter plotter : plotters) { 52 plotter.addPropertyChangeListener(this); 53 } 54 } 55 } 56 57 public void addPlotter(Plotter plotter) { 58 plotters.add(plotter); 59 if (plotters.size() == 1) { 60 selectValue(plotter.getViewRange()); 61 } 62 plotter.addPropertyChangeListener(this); 63 } 64 65 public void itemStateChanged(ItemEvent ev) { 66 for (Plotter plotter : plotters) { 67 plotter.setViewRange(Plotter.rangeValues[getSelectedIndex()]); 68 } 69 } 70 71 private void selectValue(int value) { 72 // Set the selected value 73 for (int i = 0; i < Plotter.rangeValues.length; i++) { 74 if (Plotter.rangeValues[i] == value) { 75 setSelectedItem(Plotter.rangeNames[i]); 76 } 77 } 78 // Make sure all plotters show this value 79 if (plotters.size() > 1) { 80 for (Plotter plotter : plotters) { 81 plotter.setViewRange(value); 82 } 83 } 84 } 85 86 public void propertyChange(PropertyChangeEvent ev) { 87 if (ev.getPropertyName() == "viewRange") { 88 selectValue((Integer)ev.getNewValue()); 89 } 90 } 91} 92