1 package org.lsst.ccs.plugin.jas3.trending.timeselection;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.List;
8 import javax.swing.DefaultComboBoxModel;
9 import javax.swing.JComboBox;
10 import javax.swing.JList;
11 import javax.swing.JPanel;
12 import javax.swing.JSeparator;
13 import javax.swing.ListCellRenderer;
14 import org.freehep.application.studio.Studio;
15
16
17
18
19
20
21
22
23 final public class TimeSelectionComboBox extends JComboBox {
24
25
26
27 static private final String CUSTOM_BUTTON = "Choose...";
28 static private final String PRESETS_BUTTON = "Presets...";
29 static private final String DEFAULT_NAME = "custom";
30 static private final int MAX_PRESETS = 10;
31
32 private final PresetList timeWindowList;
33 private volatile TimeSelection selection;
34
35
36
37
38 public TimeSelectionComboBox(Studio application) {
39
40 timeWindowList = new PresetList(application);
41
42 setRenderer(new Renderer(getRenderer()));
43 setMaximumRowCount(MAX_PRESETS + 5);
44
45 Model model = new Model(timeWindowList);
46 setModel(model);
47 setSelectedIndex(1);
48 selection = model.getSelectedItem();
49
50 addActionListener(new ActionListener() {
51 public void actionPerformed(ActionEvent e) {
52 TimeSelection ts = myModel().getSelectedItem();
53
54 switch (ts.name) {
55
56 case CUSTOM_BUTTON:
57
58 ts = TimeSelectionPanel.editTimeWindow(TimeSelectionComboBox.this, null);
59 if (ts == null) {
60 setSelectedItem(selection);
61 } else {
62 if (ts.name.isEmpty()) {
63 ts.name = DEFAULT_NAME;
64 if (DEFAULT_NAME.equals(selection.name)) {
65 myModel().removeElementAt(4);
66 }
67 myModel().insertElementAt(ts, 4);
68 setSelectedIndex(4);
69 } else {
70 timeWindowList.insert(ts);
71 Model model = new Model(timeWindowList);
72 model.setSelectedItem(ts);
73 setModel(model);
74 }
75 selection = ts;
76 }
77 break;
78
79 case PRESETS_BUTTON:
80
81 ts = PresetsDialog.managePresets(TimeSelectionComboBox.this, timeWindowList);
82 if (ts == null) ts = selection;
83 ts.touch();
84 Model model = new Model(timeWindowList);
85 int index = model.getIndexOf(ts);
86 if (index == -1) index = 1;
87 selection = model.getElementAt(index);
88 model.setSelectedItem(selection);
89 setModel(model);
90 break;
91
92 default:
93
94 if (!DEFAULT_NAME.equals(ts.name)) {
95 ts.touch();
96 if (DEFAULT_NAME.equals(selection.name)) {
97 myModel().removeElementAt(4);
98 }
99 }
100 selection = ts;
101
102 }
103 }
104 });
105
106 }
107
108
109
110 private Model myModel() {
111 return (Model) super.getModel();
112 }
113
114
115 public TimeSelection getSelectedTimeWindow() {
116 return selection;
117 }
118
119
120
121
122
123 public PresetList getPresetList() {
124 return timeWindowList;
125 }
126
127
128
129 private class Renderer implements ListCellRenderer {
130
131 private final ListCellRenderer horse;
132 private final JPanel separatorPanel = new JPanel(new BorderLayout());
133 private final JSeparator separator = new JSeparator();
134
135 Renderer(ListCellRenderer renderer) {
136 horse = renderer;
137 }
138
139 @Override
140 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
141 Component comp = horse.getListCellRendererComponent(list, ((TimeSelection)value).name, index, isSelected, cellHasFocus);
142 if (index == 0 || index == 3) {
143 separatorPanel.removeAll();
144 separatorPanel.add(comp, BorderLayout.CENTER);
145 separatorPanel.add(separator, BorderLayout.SOUTH);
146 comp = separatorPanel;
147 } else if (index > 4 && index == TimeSelectionComboBox.this.getItemCount()-1) {
148 separatorPanel.removeAll();
149 separatorPanel.add(comp, BorderLayout.CENTER);
150 separatorPanel.add(separator, BorderLayout.NORTH);
151 comp = separatorPanel;
152 }
153 return comp;
154 }
155
156 }
157
158
159
160 private class Model extends DefaultComboBoxModel<TimeSelection> {
161
162 Model(PresetList tsList) {
163
164
165
166 addElement(new TimeSelection(CUSTOM_BUTTON, "", "", false));
167
168
169
170 TimeSelection selectedItem = new TimeSelection("Last Hour", "now-3600", "now", false);
171 addElement(selectedItem);
172 addElement(new TimeSelection("Last 6 Hours", "now-21600", "now", false));
173 addElement(new TimeSelection("Last 24 Hours", "now-86400", "now", false));
174
175
176
177 List<TimeSelection> customRanges = tsList.getRecent(MAX_PRESETS);
178
179 for (TimeSelection ts : customRanges) {
180 addElement(ts);
181 }
182
183
184
185 addElement(new TimeSelection(PRESETS_BUTTON, "", "", false));
186
187
188
189 setSelectedItem(selectedItem);
190 }
191
192 @Override
193 public TimeSelection getSelectedItem() {
194 return (TimeSelection) super.getSelectedItem();
195 }
196
197 }
198
199 }