-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOSPCombo.java
More file actions
164 lines (147 loc) · 4.88 KB
/
OSPCombo.java
File metadata and controls
164 lines (147 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
/**
* A JPopupMenu with String items from which to choose.
*
* @author Doug Brown
*/
public class OSPCombo extends JPopupMenu {
// instance fields
protected String[] items;
protected int selected;
//protected JTextField display;
int row, column;
/**
* Constructor that specifies initial selected index.
*
* @param choices an array of string items
* @param initial the initial selected index
*/
public OSPCombo(String[] choices, int initial) {
items = choices;
selected = initial;
}
/**
* Constructor that selects index 0.
*
* @param choices an array of string items
*/
public OSPCombo(String[] choices) {
this(choices, 0);
}
/**
* Returns the selected index.
*
* @return the selected index
*/
public int getSelectedIndex() {
return selected;
}
/**
* Returns the items String[].
*
* @return the items
*/
public String[] getItems() {
return items;
}
/**
* Shows the popup immediately below the specified field. If item is selected,
* sets the field text and fires property change.
*
* @param field the field that displays the selected string
*/
public void showPopup(final JTextField display) {
//display = field;
Action selectAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
int prev = selected;
selected = Integer.parseInt(e.getActionCommand());
display.setText(items[selected]);
OSPCombo.this.firePropertyChange("index", prev, -1); //$NON-NLS-1$
}
};
removeAll();
for(int i = 0; i<items.length; i++) {
String next = items[i].toString();
JMenuItem item = new JMenuItem(next);
item.setFont(display.getFont());
item.addActionListener(selectAction);
item.setActionCommand(String.valueOf(i));
add(item);
}
int popupHeight = 8+getComponentCount()*display.getHeight();
setPopupSize(display.getWidth(), popupHeight);
show(display, 0, display.getHeight());
}
/**
* Returns the selected String.
*
* @return the currently selected String
*/
public String toString() {
return items[selected];
}
/**
* Returns an ObjectLoader to save and load data for this class.
*
* @return the object loader
*/
public static XML.ObjectLoader getLoader() {
return new Loader();
}
/**
* A class to save and load XML data for this class.
*/
static class Loader implements XML.ObjectLoader {
public void saveObject(XMLControl control, Object obj) {
OSPCombo combo = (OSPCombo) obj;
control.setValue("items", combo.items); //$NON-NLS-1$
control.setValue("index", combo.selected); //$NON-NLS-1$
}
public Object createObject(XMLControl control) {
String[] items = (String[]) control.getObject("items"); //$NON-NLS-1$
int index = control.getInt("index"); //$NON-NLS-1$
return new OSPCombo(items, index);
}
public Object loadObject(XMLControl control, Object obj) {
//OSPCombo combo =(OSPCombo)obj;
//combo.selected=control.getInt("index");//$NON-NLS-1$
//combo.items=(String[])control.getObject("items"); //$NON-NLS-1$
return obj;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/