1 package net.sf.openrocket.gui.dialogs.preset;
4 import java.awt.Dialog;
5 import java.awt.Window;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.ItemEvent;
9 import java.awt.event.ItemListener;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
14 import javax.swing.JButton;
15 import javax.swing.JCheckBox;
16 import javax.swing.JDialog;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JScrollPane;
20 import javax.swing.JTextField;
21 import javax.swing.RowFilter;
22 import javax.swing.event.DocumentEvent;
23 import javax.swing.event.DocumentListener;
24 import javax.swing.table.TableColumn;
25 import javax.swing.table.TableModel;
27 import net.miginfocom.swing.MigLayout;
28 import net.sf.openrocket.gui.util.GUIUtil;
29 import net.sf.openrocket.l10n.Translator;
30 import net.sf.openrocket.preset.ComponentPreset;
31 import net.sf.openrocket.preset.TypedKey;
32 import net.sf.openrocket.rocketcomponent.RocketComponent;
33 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
34 import net.sf.openrocket.startup.Application;
37 * Dialog shown for selecting a preset component.
39 public class ComponentPresetChooserDialog extends JDialog {
41 private static final Translator trans = Application.getTranslator();
43 private final RocketComponent component;
45 private ComponentPresetTable componentSelectionTable;
46 private JTextField filterText;
47 private JCheckBox foreDiameterFilterCheckBox;
48 private JCheckBox aftDiameterFilterCheckBox;
50 private ComponentPresetRowFilter foreDiameterFilter;
51 private ComponentPresetRowFilter aftDiameterFilter;
55 * outerDiamtereColumnIndex is the index of the column associated with the OUTER_DIAMETER
56 * field. This index is needed by the matchOuterDiameterCheckBox to implement filtering.
58 int aftDiameterColumnIndex = -1;
59 int foreDiameterColumnIndex = -1;
61 private List<ComponentPreset> presets;
63 private boolean okClicked = false;
66 public ComponentPresetChooserDialog(Window owner, RocketComponent component) {
67 super(owner, trans.get("title"), Dialog.ModalityType.APPLICATION_MODAL);
68 this.component = component;
69 this.presets = Application.getComponentPresetDao().listForType(component.getPresetType());
71 List<TypedKey<?>> displayedColumnKeys = Arrays.asList(component.getPresetType().getDisplayedColumns());
74 final List<TypedKey<?>> columnKeys = ComponentPreset.ORDERED_KEY_LIST;
75 int i = 0; // We start at 0 but use preincrement because the first column is favorite.
76 for (final TypedKey<?> key : columnKeys) {
77 // Note the increment early in the loop. This really means that initial loop i=1
78 // we do it here so the continue below doesn't mess up the counting.
80 // Don't allow the matching filters if the column is not part of the default set for
81 // this kind of preset.
82 if (!displayedColumnKeys.contains(key)) {
85 if (key == ComponentPreset.OUTER_DIAMETER || key == ComponentPreset.AFT_OUTER_DIAMETER) {
86 aftDiameterColumnIndex = i;
88 if (key == ComponentPreset.OUTER_DIAMETER || key == ComponentPreset.FORE_OUTER_DIAMETER) {
89 foreDiameterColumnIndex = i;
95 JPanel panel = new JPanel(new MigLayout("fill, ins para"));
100 JPanel sub = new JPanel(new MigLayout("fill, ins 0"));
101 JLabel filterLabel = new JLabel(trans.get("ComponentPresetChooserDialog.filter.label"));
102 sub.add(filterLabel, "gapright para");
104 filterText = new JTextField();
105 sub.add(filterText, "growx");
106 filterText.getDocument().addDocumentListener(new DocumentListener() {
108 public void changedUpdate(DocumentEvent e) {
113 public void insertUpdate(DocumentEvent e) {
118 public void removeUpdate(DocumentEvent e) {
123 panel.add(sub, "growx, ay 0, gapright para");
126 panel.add(getFilterCheckboxes(), "wrap para");
129 componentSelectionTable = new ComponentPresetTable(presets, displayedColumnKeys);
130 // GUIUtil.setAutomaticColumnTableWidths(componentSelectionTable, 20);
131 int w = componentSelectionTable.getRowHeight() + 4;
132 TableColumn tc = componentSelectionTable.getColumnModel().getColumn(0);
133 tc.setPreferredWidth(w);
137 JScrollPane scrollpane = new JScrollPane();
138 scrollpane.setViewportView(componentSelectionTable);
139 panel.add(scrollpane, "grow, width 700lp, height 300lp, spanx, wrap para");
142 // OK / Cancel buttons
143 JButton okButton = new JButton(trans.get("dlg.but.ok"));
144 okButton.addActionListener(new ActionListener() {
146 public void actionPerformed(ActionEvent e) {
150 panel.add(okButton, "tag ok, spanx, split");
153 JButton cancelButton = new JButton(trans.get("dlg.but.cancel"));
154 cancelButton.addActionListener(new ActionListener() {
156 public void actionPerformed(ActionEvent e) {
160 panel.add(cancelButton, "tag cancel");
164 GUIUtil.rememberWindowSize(this);
165 GUIUtil.setDisposableDialogOptions(this, okButton);
169 private JPanel getFilterCheckboxes() {
170 SymmetricComponent sc;
172 JPanel panel = new JPanel(new MigLayout("fill, ins 0"));
175 * Add show all compatible check box.
177 final List<ComponentPreset.Type> compatibleTypes = component.getPresetType().getCompatibleTypes();
178 final ComponentPreset.Type nativeType = component.getPresetType();
179 if (compatibleTypes != null && compatibleTypes.size() > 0) {
180 JCheckBox showAll = new JCheckBox();
181 showAll.setText(trans.get("ComponentPresetChooserDialog.checkbox.showAllCompatible"));
182 panel.add(showAll, "wrap");
183 showAll.addItemListener(new ItemListener() {
185 public void itemStateChanged(ItemEvent e) {
186 if (((JCheckBox) e.getItem()).isSelected()) {
187 presets = Application.getComponentPresetDao().listForTypes(compatibleTypes);
189 presets = Application.getComponentPresetDao().listForType(nativeType);
191 componentSelectionTable.updateData(presets);
197 * Add filter by fore diameter
199 foreDiameterFilterCheckBox = new JCheckBox(trans.get("ComponentPresetChooserDialog.checkbox.filterForeDiameter"));
200 sc = getPreviousSymmetricComponent();
201 if (sc != null && foreDiameterColumnIndex >= 0) {
202 foreDiameterFilter = new ComponentPresetRowFilter(sc.getAftRadius() * 2.0, foreDiameterColumnIndex);
203 panel.add(foreDiameterFilterCheckBox, "wrap");
204 foreDiameterFilterCheckBox.addItemListener(new ItemListener() {
206 public void itemStateChanged(ItemEvent e) {
213 * Add filter by aft diameter
215 aftDiameterFilterCheckBox = new JCheckBox(trans.get("ComponentPresetChooserDialog.checkbox.filterAftDiameter"));
216 sc = getNextSymmetricComponent();
217 if (sc != null && aftDiameterColumnIndex >= 0) {
218 aftDiameterFilter = new ComponentPresetRowFilter(sc.getForeRadius() * 2.0, aftDiameterColumnIndex);
219 panel.add(aftDiameterFilterCheckBox, "wrap");
220 aftDiameterFilterCheckBox.addItemListener(new ItemListener() {
222 public void itemStateChanged(ItemEvent e) {
232 * Return the motor selected by this chooser dialog, or <code>null</code> if the selection has been aborted.
234 * @return the selected motor, or <code>null</code> if no motor has been selected or the selection was canceled.
236 public ComponentPreset getSelectedComponentPreset() {
239 int row = componentSelectionTable.getSelectedRow();
240 row = componentSelectionTable.convertRowIndexToModel(row);
241 return presets.get(row);
244 public void close(boolean ok) {
246 this.setVisible(false);
249 private void updateFilters() {
250 List<RowFilter<TableModel, Object>> filters = new ArrayList<RowFilter<TableModel, Object>>(2);
251 String filterTextRegex = filterText.getText();
252 if (filterTextRegex != null) {
254 // The "(?iu)" magic turns on case insensitivity with unicode chars
255 RowFilter<TableModel, Object> regexFilter = RowFilter.regexFilter("(?iu)" + filterTextRegex);
256 filters.add(regexFilter);
257 } catch (java.util.regex.PatternSyntaxException e) {
260 if (aftDiameterFilterCheckBox.isSelected()) {
261 filters.add(aftDiameterFilter);
263 if (foreDiameterFilterCheckBox.isSelected()) {
264 filters.add(foreDiameterFilter);
267 componentSelectionTable.setRowFilter(RowFilter.andFilter(filters));
271 private SymmetricComponent getPreviousSymmetricComponent() {
272 RocketComponent c = component;
274 c = c.getPreviousComponent();
275 if (c instanceof SymmetricComponent) {
276 return (SymmetricComponent) c;
283 private SymmetricComponent getNextSymmetricComponent() {
284 RocketComponent c = component;
286 c = c.getNextComponent();
287 if (c instanceof SymmetricComponent) {
288 return (SymmetricComponent) c;