01: /*
02: * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package sun.tools.jconsole;
27:
28: import java.awt.event.*;
29: import java.beans.*;
30: import java.util.*;
31:
32: import 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")
40: public class TimeComboBox extends JComboBox implements ItemListener,
41: PropertyChangeListener {
42: private ArrayList<Plotter> plotters = new ArrayList<Plotter>();
43:
44: public TimeComboBox(Plotter... plotterArray) {
45: super (Plotter.rangeNames);
46:
47: addItemListener(this );
48:
49: if (plotterArray != null && plotterArray.length > 0) {
50: plotters.addAll(Arrays.asList(plotterArray));
51: selectValue(plotterArray[0].getViewRange());
52: for (Plotter plotter : plotters) {
53: plotter.addPropertyChangeListener(this );
54: }
55: }
56: }
57:
58: public void addPlotter(Plotter plotter) {
59: plotters.add(plotter);
60: if (plotters.size() == 1) {
61: selectValue(plotter.getViewRange());
62: }
63: plotter.addPropertyChangeListener(this );
64: }
65:
66: public void itemStateChanged(ItemEvent ev) {
67: for (Plotter plotter : plotters) {
68: plotter
69: .setViewRange(Plotter.rangeValues[getSelectedIndex()]);
70: }
71: }
72:
73: private void selectValue(int value) {
74: // Set the selected value
75: for (int i = 0; i < Plotter.rangeValues.length; i++) {
76: if (Plotter.rangeValues[i] == value) {
77: setSelectedItem(Plotter.rangeNames[i]);
78: }
79: }
80: // Make sure all plotters show this value
81: if (plotters.size() > 1) {
82: for (Plotter plotter : plotters) {
83: plotter.setViewRange(value);
84: }
85: }
86: }
87:
88: public void propertyChange(PropertyChangeEvent ev) {
89: if (ev.getPropertyName() == "viewRange") {
90: selectValue((Integer) ev.getNewValue());
91: }
92: }
93: }
|