Path: blob/master/src/jdk.jconsole/share/classes/sun/tools/jconsole/TimeComboBox.java
40948 views
/*1* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jconsole;2627import java.awt.event.*;28import java.beans.*;29import java.util.*;3031import javax.swing.*;3233/**34* A combo box to control the visible time range for one or more Plotter components.35* When used with two or more Plotters, it also acts to coordinate the range between36* them.37*/38@SuppressWarnings("serial")39public class TimeComboBox extends JComboBox<String> implements ItemListener, PropertyChangeListener {40private ArrayList<Plotter> plotters = new ArrayList<Plotter>();4142public TimeComboBox(Plotter... plotterArray) {43super(Plotter.rangeNames);4445addItemListener(this);4647if (plotterArray != null && plotterArray.length > 0) {48plotters.addAll(Arrays.asList(plotterArray));49selectValue(plotterArray[0].getViewRange());50for (Plotter plotter : plotters) {51plotter.addPropertyChangeListener(this);52}53}54}5556public void addPlotter(Plotter plotter) {57plotters.add(plotter);58if (plotters.size() == 1) {59selectValue(plotter.getViewRange());60}61plotter.addPropertyChangeListener(this);62}6364public void itemStateChanged(ItemEvent ev) {65for (Plotter plotter : plotters) {66plotter.setViewRange(Plotter.rangeValues[getSelectedIndex()]);67}68}6970private void selectValue(int value) {71// Set the selected value72for (int i = 0; i < Plotter.rangeValues.length; i++) {73if (Plotter.rangeValues[i] == value) {74setSelectedItem(Plotter.rangeNames[i]);75}76}77// Make sure all plotters show this value78if (plotters.size() > 1) {79for (Plotter plotter : plotters) {80plotter.setViewRange(value);81}82}83}8485public void propertyChange(PropertyChangeEvent ev) {86if (ev.getPropertyName() == "viewRange") {87selectValue((Integer)ev.getNewValue());88}89}90}919293