Display computer motor class in motor selection dialog
[debian/openrocket] / core / src / net / sf / openrocket / gui / dialogs / motor / thrustcurve / ThrustCurveMotorSelectionPanel.java
1 package net.sf.openrocket.gui.dialogs.motor.thrustcurve;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.Cursor;
7 import java.awt.Font;
8 import java.awt.Paint;
9 import java.awt.Rectangle;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.awt.event.MouseAdapter;
13 import java.awt.event.MouseEvent;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.prefs.Preferences;
18
19 import javax.swing.BorderFactory;
20 import javax.swing.DefaultComboBoxModel;
21 import javax.swing.JCheckBox;
22 import javax.swing.JComboBox;
23 import javax.swing.JComponent;
24 import javax.swing.JLabel;
25 import javax.swing.JLayeredPane;
26 import javax.swing.JList;
27 import javax.swing.JPanel;
28 import javax.swing.JScrollPane;
29 import javax.swing.JSeparator;
30 import javax.swing.JTable;
31 import javax.swing.JTextArea;
32 import javax.swing.JTextField;
33 import javax.swing.ListCellRenderer;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.RowFilter;
36 import javax.swing.SwingUtilities;
37 import javax.swing.event.DocumentEvent;
38 import javax.swing.event.DocumentListener;
39 import javax.swing.event.ListSelectionEvent;
40 import javax.swing.event.ListSelectionListener;
41 import javax.swing.table.TableModel;
42 import javax.swing.table.TableRowSorter;
43
44 import net.miginfocom.swing.MigLayout;
45 import net.sf.openrocket.database.ThrustCurveMotorSet;
46 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
47 import net.sf.openrocket.gui.components.StyledLabel;
48 import net.sf.openrocket.gui.components.StyledLabel.Style;
49 import net.sf.openrocket.gui.dialogs.motor.CloseableDialog;
50 import net.sf.openrocket.gui.dialogs.motor.MotorSelector;
51 import net.sf.openrocket.gui.util.GUIUtil;
52 import net.sf.openrocket.gui.util.Icons;
53 import net.sf.openrocket.gui.util.SwingPreferences;
54 import net.sf.openrocket.l10n.Translator;
55 import net.sf.openrocket.logging.LogHelper;
56 import net.sf.openrocket.motor.Motor;
57 import net.sf.openrocket.motor.ThrustCurveMotor;
58 import net.sf.openrocket.startup.Application;
59 import net.sf.openrocket.unit.UnitGroup;
60 import net.sf.openrocket.util.BugException;
61 import net.sf.openrocket.utils.MotorCorrelation;
62
63 import org.jfree.chart.ChartColor;
64 import org.jfree.chart.ChartFactory;
65 import org.jfree.chart.ChartPanel;
66 import org.jfree.chart.JFreeChart;
67 import org.jfree.chart.axis.ValueAxis;
68 import org.jfree.chart.plot.PlotOrientation;
69 import org.jfree.chart.plot.XYPlot;
70 import org.jfree.chart.title.TextTitle;
71 import org.jfree.data.xy.XYSeries;
72 import org.jfree.data.xy.XYSeriesCollection;
73
74 public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelector {
75         private static final LogHelper log = Application.getLogger();
76         private static final Translator trans = Application.getTranslator();
77         
78         private static final double MOTOR_SIMILARITY_THRESHOLD = 0.95;
79         
80         private static final int SHOW_ALL = 0;
81         private static final int SHOW_SMALLER = 1;
82         private static final int SHOW_EXACT = 2;
83         private static final String[] SHOW_DESCRIPTIONS = {
84                         //// Show all motors
85                         trans.get("TCMotorSelPan.SHOW_DESCRIPTIONS.desc1"),
86                         //// Show motors with diameter less than that of the motor mount
87                         trans.get("TCMotorSelPan.SHOW_DESCRIPTIONS.desc2"),
88                         //// Show motors with diameter equal to that of the motor mount
89                         trans.get("TCMotorSelPan.SHOW_DESCRIPTIONS.desc3")
90         };
91         private static final int SHOW_MAX = 2;
92         
93         private static final int ZOOM_ICON_POSITION_NEGATIVE_X = 50;
94         private static final int ZOOM_ICON_POSITION_POSITIVE_Y = 12;
95         
96         private static final Paint[] CURVE_COLORS = ChartColor.createDefaultPaintArray();
97         
98         private static final Color NO_COMMENT_COLOR = Color.GRAY;
99         private static final Color WITH_COMMENT_COLOR = Color.BLACK;
100         
101         private static final ThrustCurveMotorComparator MOTOR_COMPARATOR = new ThrustCurveMotorComparator();
102         
103         
104         
105         private final List<ThrustCurveMotorSet> database;
106         
107         private final double diameter;
108         private CloseableDialog dialog = null;
109         
110         
111         private final ThrustCurveMotorDatabaseModel model;
112         private final JTable table;
113         private final TableRowSorter<TableModel> sorter;
114         
115         private final JCheckBox hideSimilarBox;
116         
117         private final JTextField searchField;
118         private String[] searchTerms = new String[0];
119         
120         
121         private final JLabel curveSelectionLabel;
122         private final JComboBox curveSelectionBox;
123         private final DefaultComboBoxModel curveSelectionModel;
124         
125         private final JLabel totalImpulseLabel;
126         private final JLabel classificationLabel;
127         private final JLabel avgThrustLabel;
128         private final JLabel maxThrustLabel;
129         private final JLabel burnTimeLabel;
130         private final JLabel launchMassLabel;
131         private final JLabel emptyMassLabel;
132         private final JLabel dataPointsLabel;
133         private final JLabel digestLabel;
134         
135         private final JTextArea comment;
136         private final Font noCommentFont;
137         private final Font withCommentFont;
138         
139         private final JFreeChart chart;
140         private final ChartPanel chartPanel;
141         private final JLabel zoomIcon;
142         
143         private final JComboBox delayBox;
144         
145         private ThrustCurveMotor selectedMotor;
146         private ThrustCurveMotorSet selectedMotorSet;
147         private double selectedDelay;
148         
149         
150         /**
151          * Sole constructor.
152          * 
153          * @param current       the currently selected ThrustCurveMotor, or <code>null</code> for none.
154          * @param delay         the currently selected ejection charge delay.
155          * @param diameter      the diameter of the motor mount.
156          */
157         public ThrustCurveMotorSelectionPanel(ThrustCurveMotor current, double delay, double diameter) {
158                 super(new MigLayout("fill", "[grow][]"));
159                 
160                 this.diameter = diameter;
161                 
162                 
163                 // Construct the database (adding the current motor if not in the db already)
164                 List<ThrustCurveMotorSet> db;
165                 // TODO - ugly blind cast.
166                 db = ((ThrustCurveMotorSetDatabase) Application.getMotorSetDatabase()).getMotorSets();
167                 
168                 // If current motor is not found in db, add a new ThrustCurveMotorSet containing it
169                 if (current != null) {
170                         selectedMotor = current;
171                         for (ThrustCurveMotorSet motorSet : db) {
172                                 if (motorSet.getMotors().contains(current)) {
173                                         selectedMotorSet = motorSet;
174                                         break;
175                                 }
176                         }
177                         if (selectedMotorSet == null) {
178                                 db = new ArrayList<ThrustCurveMotorSet>(db);
179                                 ThrustCurveMotorSet extra = new ThrustCurveMotorSet();
180                                 extra.addMotor(current);
181                                 selectedMotorSet = extra;
182                                 db.add(extra);
183                                 Collections.sort(db);
184                         }
185                 }
186                 database = db;
187                 
188                 
189                 
190                 ////  GUI
191                 
192                 JPanel panel;
193                 JLabel label;
194                 
195                 panel = new JPanel(new MigLayout("fill"));
196                 this.add(panel, "grow");
197                 
198                 
199                 
200                 // Selection label
201                 //// Select rocket motor:
202                 label = new StyledLabel(trans.get("TCMotorSelPan.lbl.Selrocketmotor"), Style.BOLD);
203                 panel.add(label, "spanx, wrap para");
204                 
205                 // Diameter selection
206                 JComboBox filterComboBox = new JComboBox(SHOW_DESCRIPTIONS);
207                 filterComboBox.addActionListener(new ActionListener() {
208                         @Override
209                         public void actionPerformed(ActionEvent e) {
210                                 JComboBox cb = (JComboBox) e.getSource();
211                                 int sel = cb.getSelectedIndex();
212                                 if ((sel < 0) || (sel > SHOW_MAX))
213                                         sel = SHOW_ALL;
214                                 switch (sel) {
215                                 case SHOW_ALL:
216                                         sorter.setRowFilter(new MotorRowFilterAll());
217                                         break;
218                                 
219                                 case SHOW_SMALLER:
220                                         sorter.setRowFilter(new MotorRowFilterSmaller());
221                                         break;
222                                 
223                                 case SHOW_EXACT:
224                                         sorter.setRowFilter(new MotorRowFilterExact());
225                                         break;
226                                 
227                                 default:
228                                         throw new BugException("Invalid selection mode sel=" + sel);
229                                 }
230                                 Application.getPreferences().putChoice("MotorDiameterMatch", sel);
231                                 scrollSelectionVisible();
232                         }
233                 });
234                 panel.add(filterComboBox, "spanx, growx, wrap rel");
235                 
236                 //// Hide very similar thrust curves
237                 hideSimilarBox = new JCheckBox(trans.get("TCMotorSelPan.checkbox.hideSimilar"));
238                 GUIUtil.changeFontSize(hideSimilarBox, -1);
239                 hideSimilarBox.setSelected(Application.getPreferences().getBoolean(net.sf.openrocket.startup.Preferences.MOTOR_HIDE_SIMILAR, true));
240                 hideSimilarBox.addActionListener(new ActionListener() {
241                         @Override
242                         public void actionPerformed(ActionEvent e) {
243                                 Application.getPreferences().putBoolean(net.sf.openrocket.startup.Preferences.MOTOR_HIDE_SIMILAR, hideSimilarBox.isSelected());
244                                 updateData();
245                         }
246                 });
247                 panel.add(hideSimilarBox, "gapleft para, spanx, growx, wrap para");
248                 
249                 
250                 // Motor selection table
251                 model = new ThrustCurveMotorDatabaseModel(database);
252                 table = new JTable(model);
253                 
254                 
255                 // Set comparators and widths
256                 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
257                 sorter = new TableRowSorter<TableModel>(model);
258                 for (int i = 0; i < ThrustCurveMotorColumns.values().length; i++) {
259                         ThrustCurveMotorColumns column = ThrustCurveMotorColumns.values()[i];
260                         sorter.setComparator(i, column.getComparator());
261                         table.getColumnModel().getColumn(i).setPreferredWidth(column.getWidth());
262                 }
263                 table.setRowSorter(sorter);
264                 
265                 // Set selection and double-click listeners
266                 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
267                         @Override
268                         public void valueChanged(ListSelectionEvent e) {
269                                 int row = table.getSelectedRow();
270                                 if (row >= 0) {
271                                         row = table.convertRowIndexToModel(row);
272                                         ThrustCurveMotorSet motorSet = model.getMotorSet(row);
273                                         log.user("Selected table row " + row + ": " + motorSet);
274                                         if (motorSet != selectedMotorSet) {
275                                                 select(selectMotor(motorSet));
276                                         }
277                                 } else {
278                                         log.user("Selected table row " + row + ", nothing selected");
279                                 }
280                         }
281                 });
282                 table.addMouseListener(new MouseAdapter() {
283                         @Override
284                         public void mouseClicked(MouseEvent e) {
285                                 if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
286                                         if (dialog != null) {
287                                                 dialog.close(true);
288                                         }
289                                 }
290                         }
291                 });
292                 
293                 
294                 JScrollPane scrollpane = new JScrollPane();
295                 scrollpane.setViewportView(table);
296                 panel.add(scrollpane, "grow, width :500:, height :300:, spanx, wrap para");
297                 
298                 
299                 
300                 
301                 // Motor mount diameter label
302                 //// Motor mount diameter: 
303                 label = new StyledLabel(trans.get("TCMotorSelPan.lbl.Motormountdia") + " " +
304                                 UnitGroup.UNITS_MOTOR_DIMENSIONS.getDefaultUnit().toStringUnit(diameter));
305                 panel.add(label, "gapright 30lp, spanx, split");
306                 
307                 
308                 
309                 // Search field
310                 //// Search:
311                 label = new StyledLabel(trans.get("TCMotorSelPan.lbl.Search"));
312                 panel.add(label, "");
313                 
314                 searchField = new JTextField();
315                 searchField.getDocument().addDocumentListener(new DocumentListener() {
316                         @Override
317                         public void changedUpdate(DocumentEvent e) {
318                                 update();
319                         }
320                         
321                         @Override
322                         public void insertUpdate(DocumentEvent e) {
323                                 update();
324                         }
325                         
326                         @Override
327                         public void removeUpdate(DocumentEvent e) {
328                                 update();
329                         }
330                         
331                         private void update() {
332                                 String text = searchField.getText().trim();
333                                 String[] split = text.split("\\s+");
334                                 ArrayList<String> list = new ArrayList<String>();
335                                 for (String s : split) {
336                                         s = s.trim().toLowerCase();
337                                         if (s.length() > 0) {
338                                                 list.add(s);
339                                         }
340                                 }
341                                 searchTerms = list.toArray(new String[0]);
342                                 sorter.sort();
343                                 scrollSelectionVisible();
344                         }
345                 });
346                 panel.add(searchField, "growx, wrap");
347                 
348                 
349                 
350                 // Vertical split
351                 this.add(panel, "grow");
352                 this.add(new JSeparator(JSeparator.VERTICAL), "growy, gap para para");
353                 panel = new JPanel(new MigLayout("fill"));
354                 
355                 
356                 
357                 // Thrust curve selection
358                 //// Select thrust curve:
359                 curveSelectionLabel = new JLabel(trans.get("TCMotorSelPan.lbl.Selectthrustcurve"));
360                 panel.add(curveSelectionLabel);
361                 
362                 curveSelectionModel = new DefaultComboBoxModel();
363                 curveSelectionBox = new JComboBox(curveSelectionModel);
364                 curveSelectionBox.setRenderer(new CurveSelectionRenderer(curveSelectionBox.getRenderer()));
365                 curveSelectionBox.addActionListener(new ActionListener() {
366                         @Override
367                         public void actionPerformed(ActionEvent e) {
368                                 Object value = curveSelectionBox.getSelectedItem();
369                                 if (value != null) {
370                                         select(((MotorHolder) value).getMotor());
371                                 }
372                         }
373                 });
374                 panel.add(curveSelectionBox, "growx, wrap para");
375                 
376                 
377                 
378                 
379                 
380                 // Ejection charge delay:
381                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Ejectionchargedelay")));
382                 
383                 delayBox = new JComboBox();
384                 delayBox.setEditable(true);
385                 delayBox.addActionListener(new ActionListener() {
386                         @Override
387                         public void actionPerformed(ActionEvent e) {
388                                 JComboBox cb = (JComboBox) e.getSource();
389                                 String sel = (String) cb.getSelectedItem();
390                                 //// None
391                                 if (sel.equalsIgnoreCase(trans.get("TCMotorSelPan.equalsIgnoreCase.None"))) {
392                                         selectedDelay = Motor.PLUGGED;
393                                 } else {
394                                         try {
395                                                 selectedDelay = Double.parseDouble(sel);
396                                         } catch (NumberFormatException ignore) {
397                                         }
398                                 }
399                                 setDelays(false);
400                         }
401                 });
402                 panel.add(delayBox, "growx, wrap rel");
403                 //// (Number of seconds or \"None\")
404                 panel.add(new StyledLabel(trans.get("TCMotorSelPan.lbl.NumberofsecondsorNone"), -3), "skip, wrap para");
405                 setDelays(false);
406                 
407                 
408                 panel.add(new JSeparator(), "spanx, growx, wrap para");
409                 
410                 
411                 
412                 // Thrust curve info
413                 //// Total impulse:
414                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Totalimpulse")));
415                 totalImpulseLabel = new JLabel();
416                 panel.add(totalImpulseLabel, "split");
417                 classificationLabel = new JLabel();
418                 classificationLabel.setEnabled(false); // Gray
419                 //              classificationLabel.setForeground(Color.GRAY);
420                 panel.add(classificationLabel, "gapleft unrel, wrap");
421                 
422                 //// Avg. thrust:
423                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Avgthrust")));
424                 avgThrustLabel = new JLabel();
425                 panel.add(avgThrustLabel, "wrap");
426                 
427                 //// Max. thrust:
428                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Maxthrust")));
429                 maxThrustLabel = new JLabel();
430                 panel.add(maxThrustLabel, "wrap");
431                 
432                 //// Burn time:
433                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Burntime")));
434                 burnTimeLabel = new JLabel();
435                 panel.add(burnTimeLabel, "wrap");
436                 
437                 //// Launch mass:
438                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Launchmass")));
439                 launchMassLabel = new JLabel();
440                 panel.add(launchMassLabel, "wrap");
441                 
442                 //// Empty mass:
443                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Emptymass")));
444                 emptyMassLabel = new JLabel();
445                 panel.add(emptyMassLabel, "wrap");
446                 
447                 //// Data points:
448                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Datapoints")));
449                 dataPointsLabel = new JLabel();
450                 panel.add(dataPointsLabel, "wrap para");
451                 
452                 if (System.getProperty("openrocket.debug.motordigest") != null) {
453                         //// Digest:
454                         panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Digest")));
455                         digestLabel = new JLabel();
456                         panel.add(digestLabel, "w :300:, wrap para");
457                 } else {
458                         digestLabel = null;
459                 }
460                 
461                 
462                 comment = new JTextArea(5, 5);
463                 GUIUtil.changeFontSize(comment, -2);
464                 withCommentFont = comment.getFont();
465                 noCommentFont = withCommentFont.deriveFont(Font.ITALIC);
466                 comment.setLineWrap(true);
467                 comment.setWrapStyleWord(true);
468                 comment.setEditable(false);
469                 scrollpane = new JScrollPane(comment);
470                 panel.add(scrollpane, "spanx, growx, wrap para");
471                 
472                 
473                 
474                 
475                 // Thrust curve plot
476                 chart = ChartFactory.createXYLineChart(
477                                 null, // title
478                                 null, // xAxisLabel
479                                 null, // yAxisLabel
480                                 null, // dataset
481                                 PlotOrientation.VERTICAL,
482                                 false, // legend
483                                 false, // tooltips
484                                 false // urls
485                                 );
486                 
487                 
488                 // Add the data and formatting to the plot
489                 XYPlot plot = chart.getXYPlot();
490                 
491                 changeLabelFont(plot.getRangeAxis(), -2);
492                 changeLabelFont(plot.getDomainAxis(), -2);
493                 
494                 //// Thrust curve:
495                 chart.setTitle(new TextTitle(trans.get("TCMotorSelPan.title.Thrustcurve"), this.getFont()));
496                 chart.setBackgroundPaint(this.getBackground());
497                 plot.setBackgroundPaint(Color.WHITE);
498                 plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
499                 plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
500                 
501                 chartPanel = new ChartPanel(chart,
502                                 false, // properties
503                                 false, // save
504                                 false, // print
505                                 false, // zoom
506                                 false); // tooltips
507                 chartPanel.setMouseZoomable(false);
508                 chartPanel.setPopupMenu(null);
509                 chartPanel.setMouseWheelEnabled(false);
510                 chartPanel.setRangeZoomable(false);
511                 chartPanel.setDomainZoomable(false);
512                 
513                 chartPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
514                 chartPanel.addMouseListener(new MouseAdapter() {
515                         @Override
516                         public void mouseClicked(MouseEvent e) {
517                                 if (selectedMotor == null || selectedMotorSet == null)
518                                         return;
519                                 if (e.getButton() == MouseEvent.BUTTON1) {
520                                         // Open plot dialog
521                                         List<ThrustCurveMotor> motors = getFilteredCurves();
522                                         ThrustCurveMotorPlotDialog plotDialog = new ThrustCurveMotorPlotDialog(motors,
523                                                         motors.indexOf(selectedMotor),
524                                                         SwingUtilities.getWindowAncestor(ThrustCurveMotorSelectionPanel.this));
525                                         plotDialog.setVisible(true);
526                                 }
527                         }
528                 });
529                 
530                 JLayeredPane layer = new CustomLayeredPane();
531                 
532                 layer.setBorder(BorderFactory.createLineBorder(Color.BLUE));
533                 
534                 layer.add(chartPanel, (Integer) 0);
535                 
536                 zoomIcon = new JLabel(Icons.ZOOM_IN);
537                 zoomIcon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
538                 layer.add(zoomIcon, (Integer) 1);
539                 
540                 
541                 panel.add(layer, "width 300:300:, height 180:180:, grow, spanx");
542                 
543                 
544                 
545                 this.add(panel, "grow");
546                 
547                 
548                 
549                 // Sets the filter:
550                 int showMode = Application.getPreferences().getChoice(net.sf.openrocket.startup.Preferences.MOTOR_DIAMETER_FILTER, SHOW_MAX, SHOW_EXACT);
551                 filterComboBox.setSelectedIndex(showMode);
552                 
553                 
554                 // Update the panel data
555                 updateData();
556                 selectedDelay = delay;
557                 setDelays(false);
558                 
559         }
560         
561         
562         @Override
563         public Motor getSelectedMotor() {
564                 return selectedMotor;
565         }
566         
567         
568         @Override
569         public double getSelectedDelay() {
570                 return selectedDelay;
571         }
572         
573         
574         @Override
575         public JComponent getDefaultFocus() {
576                 return searchField;
577         }
578         
579         @Override
580         public void selectedMotor(Motor motorSelection) {
581                 if (!(motorSelection instanceof ThrustCurveMotor)) {
582                         log.error("Received argument that was not ThrustCurveMotor: " + motorSelection);
583                         return;
584                 }
585                 
586                 ThrustCurveMotor motor = (ThrustCurveMotor) motorSelection;
587                 ThrustCurveMotorSet set = findMotorSet(motor);
588                 if (set == null) {
589                         log.error("Could not find set for motor:" + motorSelection);
590                         return;
591                 }
592                 
593                 // Store selected motor in preferences node, set all others to false
594                 Preferences prefs = ((SwingPreferences) Application.getPreferences()).getNode(net.sf.openrocket.startup.Preferences.PREFERRED_THRUST_CURVE_MOTOR_NODE);
595                 for (ThrustCurveMotor m : set.getMotors()) {
596                         String digest = m.getDigest();
597                         prefs.putBoolean(digest, m == motor);
598                 }
599         }
600         
601         public void setCloseableDialog(CloseableDialog dialog) {
602                 this.dialog = dialog;
603         }
604         
605         
606         
607         private void changeLabelFont(ValueAxis axis, float size) {
608                 Font font = axis.getTickLabelFont();
609                 font = font.deriveFont(font.getSize2D() + size);
610                 axis.setTickLabelFont(font);
611         }
612         
613         
614         /**
615          * Called when a different motor is selected from within the panel.
616          */
617         private void select(ThrustCurveMotor motor) {
618                 if (selectedMotor == motor)
619                         return;
620                 
621                 ThrustCurveMotorSet set = findMotorSet(motor);
622                 if (set == null) {
623                         throw new BugException("Could not find motor from database, motor=" + motor);
624                 }
625                 
626                 boolean updateDelays = (selectedMotorSet != set);
627                 
628                 selectedMotor = motor;
629                 selectedMotorSet = set;
630                 updateData();
631                 if (updateDelays) {
632                         setDelays(true);
633                 }
634         }
635         
636         
637         private void updateData() {
638                 
639                 if (selectedMotorSet == null) {
640                         // No motor selected
641                         curveSelectionModel.removeAllElements();
642                         curveSelectionBox.setEnabled(false);
643                         curveSelectionLabel.setEnabled(false);
644                         totalImpulseLabel.setText("");
645                         totalImpulseLabel.setToolTipText(null);
646                         classificationLabel.setText("");
647                         classificationLabel.setToolTipText(null);
648                         avgThrustLabel.setText("");
649                         maxThrustLabel.setText("");
650                         burnTimeLabel.setText("");
651                         launchMassLabel.setText("");
652                         emptyMassLabel.setText("");
653                         dataPointsLabel.setText("");
654                         if (digestLabel != null) {
655                                 digestLabel.setText("");
656                         }
657                         setComment("");
658                         chart.getXYPlot().setDataset(new XYSeriesCollection());
659                         return;
660                 }
661                 
662                 
663                 // Check which thrust curves to display
664                 List<ThrustCurveMotor> motors = getFilteredCurves();
665                 final int index = motors.indexOf(selectedMotor);
666                 
667                 
668                 // Update the thrust curve selection box
669                 curveSelectionModel.removeAllElements();
670                 for (int i = 0; i < motors.size(); i++) {
671                         curveSelectionModel.addElement(new MotorHolder(motors.get(i), i));
672                 }
673                 curveSelectionBox.setSelectedIndex(index);
674                 
675                 if (motors.size() > 1) {
676                         curveSelectionBox.setEnabled(true);
677                         curveSelectionLabel.setEnabled(true);
678                 } else {
679                         curveSelectionBox.setEnabled(false);
680                         curveSelectionLabel.setEnabled(false);
681                 }
682                 
683                 
684                 // Update thrust curve data
685                 double impulse = selectedMotor.getTotalImpulseEstimate();
686                 MotorClass mc = MotorClass.getMotorClass(impulse);
687                 totalImpulseLabel.setText(UnitGroup.UNITS_IMPULSE.getDefaultUnit().toStringUnit(impulse));
688                 classificationLabel.setText("(" + mc.getDescription(impulse) + ")");
689                 totalImpulseLabel.setToolTipText(mc.getClassDescription());
690                 classificationLabel.setToolTipText(mc.getClassDescription());
691                 
692                 avgThrustLabel.setText(UnitGroup.UNITS_FORCE.getDefaultUnit().toStringUnit(
693                                 selectedMotor.getAverageThrustEstimate()));
694                 maxThrustLabel.setText(UnitGroup.UNITS_FORCE.getDefaultUnit().toStringUnit(
695                                 selectedMotor.getMaxThrustEstimate()));
696                 burnTimeLabel.setText(UnitGroup.UNITS_SHORT_TIME.getDefaultUnit().toStringUnit(
697                                 selectedMotor.getBurnTimeEstimate()));
698                 launchMassLabel.setText(UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(
699                                 selectedMotor.getLaunchCG().weight));
700                 emptyMassLabel.setText(UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(
701                                 selectedMotor.getEmptyCG().weight));
702                 dataPointsLabel.setText("" + (selectedMotor.getTimePoints().length - 1));
703                 if (digestLabel != null) {
704                         digestLabel.setText(selectedMotor.getDigest());
705                 }
706                 
707                 setComment(selectedMotor.getDescription());
708                 
709                 
710                 // Update the plot
711                 XYPlot plot = chart.getXYPlot();
712                 
713                 XYSeriesCollection dataset = new XYSeriesCollection();
714                 for (int i = 0; i < motors.size(); i++) {
715                         ThrustCurveMotor m = motors.get(i);
716                         
717                         //// Thrust
718                         XYSeries series = new XYSeries(trans.get("TCMotorSelPan.title.Thrust"));
719                         double[] time = m.getTimePoints();
720                         double[] thrust = m.getThrustPoints();
721                         
722                         for (int j = 0; j < time.length; j++) {
723                                 series.add(time[j], thrust[j]);
724                         }
725                         
726                         dataset.addSeries(series);
727                         
728                         boolean selected = (i == index);
729                         plot.getRenderer().setSeriesStroke(i, new BasicStroke(selected ? 3 : 1));
730                         plot.getRenderer().setSeriesPaint(i, getColor(i));
731                 }
732                 
733                 plot.setDataset(dataset);
734         }
735         
736         private List<ThrustCurveMotor> getFilteredCurves() {
737                 List<ThrustCurveMotor> motors = selectedMotorSet.getMotors();
738                 if (hideSimilarBox.isSelected()) {
739                         List<ThrustCurveMotor> filtered = new ArrayList<ThrustCurveMotor>(motors.size());
740                         for (int i = 0; i < motors.size(); i++) {
741                                 ThrustCurveMotor m = motors.get(i);
742                                 if (m.equals(selectedMotor)) {
743                                         filtered.add(m);
744                                         continue;
745                                 }
746                                 
747                                 double similarity = MotorCorrelation.similarity(selectedMotor, m);
748                                 log.debug("Motor similarity: " + similarity);
749                                 if (similarity < MOTOR_SIMILARITY_THRESHOLD) {
750                                         filtered.add(m);
751                                 }
752                         }
753                         motors = filtered;
754                 }
755                 
756                 Collections.sort(motors, MOTOR_COMPARATOR);
757                 
758                 return motors;
759         }
760         
761         
762         private void setComment(String s) {
763                 s = s.trim();
764                 if (s.length() == 0) {
765                         //// No description available.
766                         comment.setText("No description available.");
767                         comment.setFont(noCommentFont);
768                         comment.setForeground(NO_COMMENT_COLOR);
769                 } else {
770                         comment.setText(s);
771                         comment.setFont(withCommentFont);
772                         comment.setForeground(WITH_COMMENT_COLOR);
773                 }
774                 comment.setCaretPosition(0);
775         }
776         
777         private void scrollSelectionVisible() {
778                 if (selectedMotorSet != null) {
779                         int index = table.convertRowIndexToView(model.getIndex(selectedMotorSet));
780                         System.out.println("index=" + index);
781                         table.getSelectionModel().setSelectionInterval(index, index);
782                         Rectangle rect = table.getCellRect(index, 0, true);
783                         rect = new Rectangle(rect.x, rect.y - 100, rect.width, rect.height + 200);
784                         table.scrollRectToVisible(rect);
785                 }
786         }
787         
788         
789         public static Color getColor(int index) {
790                 return (Color) CURVE_COLORS[index % CURVE_COLORS.length];
791         }
792         
793         
794         /**
795          * Find the ThrustCurveMotorSet that contains a motor.
796          * 
797          * @param motor         the motor to look for.
798          * @return                      the ThrustCurveMotorSet, or null if not found.
799          */
800         private ThrustCurveMotorSet findMotorSet(ThrustCurveMotor motor) {
801                 for (ThrustCurveMotorSet set : database) {
802                         if (set.getMotors().contains(motor)) {
803                                 return set;
804                         }
805                 }
806                 
807                 return null;
808         }
809         
810         
811         
812         /**
813          * Select the default motor from this ThrustCurveMotorSet.  This uses primarily motors
814          * that the user has previously used, and secondarily a heuristic method of selecting which
815          * thrust curve seems to be better or more reliable.
816          * 
817          * @param set   the motor set
818          * @return              the default motor in this set
819          */
820         private ThrustCurveMotor selectMotor(ThrustCurveMotorSet set) {
821                 if (set.getMotorCount() == 0) {
822                         throw new BugException("Attempting to select motor from empty ThrustCurveMotorSet: " + set);
823                 }
824                 if (set.getMotorCount() == 1) {
825                         return set.getMotors().get(0);
826                 }
827                 
828                 
829                 // Find which motor has been used the most recently
830                 List<ThrustCurveMotor> list = set.getMotors();
831                 Preferences prefs = ((SwingPreferences) Application.getPreferences()).getNode(net.sf.openrocket.startup.Preferences.PREFERRED_THRUST_CURVE_MOTOR_NODE);
832                 for (ThrustCurveMotor m : list) {
833                         String digest = m.getDigest();
834                         if (prefs.getBoolean(digest, false)) {
835                                 return m;
836                         }
837                 }
838                 
839                 // No motor has been used
840                 Collections.sort(list, MOTOR_COMPARATOR);
841                 return list.get(0);
842         }
843         
844         
845         /**
846          * Set the values in the delay combo box.  If <code>reset</code> is <code>true</code>
847          * then sets the selected value as the value closest to selectedDelay, otherwise
848          * leaves selection alone.
849          */
850         private void setDelays(boolean reset) {
851                 if (selectedMotor == null) {
852                         
853                         //// None
854                         delayBox.setModel(new DefaultComboBoxModel(new String[] { trans.get("TCMotorSelPan.delayBox.None") }));
855                         delayBox.setSelectedIndex(0);
856                         
857                 } else {
858                         
859                         List<Double> delays = selectedMotorSet.getDelays();
860                         String[] delayStrings = new String[delays.size()];
861                         double currentDelay = selectedDelay; // Store current setting locally
862                         
863                         for (int i = 0; i < delays.size(); i++) {
864                                 //// None
865                                 delayStrings[i] = ThrustCurveMotor.getDelayString(delays.get(i), trans.get("TCMotorSelPan.delayBox.None"));
866                         }
867                         delayBox.setModel(new DefaultComboBoxModel(delayStrings));
868                         
869                         if (reset) {
870                                 
871                                 // Find and set the closest value
872                                 double closest = Double.NaN;
873                                 for (int i = 0; i < delays.size(); i++) {
874                                         // if-condition to always become true for NaN
875                                         if (!(Math.abs(delays.get(i) - currentDelay) > Math.abs(closest - currentDelay))) {
876                                                 closest = delays.get(i);
877                                         }
878                                 }
879                                 if (!Double.isNaN(closest)) {
880                                         selectedDelay = closest;
881                                         //// None
882                                         delayBox.setSelectedItem(ThrustCurveMotor.getDelayString(closest, trans.get("TCMotorSelPan.delayBox.None")));
883                                 } else {
884                                         delayBox.setSelectedItem("None");
885                                 }
886                                 
887                         } else {
888                                 
889                                 selectedDelay = currentDelay;
890                                 //// None
891                                 delayBox.setSelectedItem(ThrustCurveMotor.getDelayString(currentDelay, trans.get("TCMotorSelPan.delayBox.None")));
892                                 
893                         }
894                         
895                 }
896         }
897         
898         
899         
900         
901         //////////////////////
902         
903         
904         private class CurveSelectionRenderer implements ListCellRenderer {
905                 
906                 private final ListCellRenderer renderer;
907                 
908                 public CurveSelectionRenderer(ListCellRenderer renderer) {
909                         this.renderer = renderer;
910                 }
911                 
912                 @Override
913                 public Component getListCellRendererComponent(JList list, Object value, int index,
914                                 boolean isSelected, boolean cellHasFocus) {
915                         
916                         Component c = renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
917                         if (value instanceof MotorHolder) {
918                                 MotorHolder m = (MotorHolder) value;
919                                 c.setForeground(getColor(m.getIndex()));
920                         }
921                         
922                         return c;
923                 }
924                 
925         }
926         
927         
928         ////////  Row filters
929         
930         /**
931          * Abstract adapter class.
932          */
933         private abstract class MotorRowFilter extends RowFilter<TableModel, Integer> {
934                 @Override
935                 public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
936                         int index = entry.getIdentifier();
937                         ThrustCurveMotorSet m = model.getMotorSet(index);
938                         return filterByDiameter(m) && filterByString(m);
939                 }
940                 
941                 public abstract boolean filterByDiameter(ThrustCurveMotorSet m);
942                 
943                 
944                 public boolean filterByString(ThrustCurveMotorSet m) {
945                         main: for (String s : searchTerms) {
946                                 for (ThrustCurveMotorColumns col : ThrustCurveMotorColumns.values()) {
947                                         String str = col.getValue(m).toString().toLowerCase();
948                                         if (str.indexOf(s) >= 0)
949                                                 continue main;
950                                 }
951                                 return false;
952                         }
953                         return true;
954                 }
955         }
956         
957         /**
958          * Show all motors.
959          */
960         private class MotorRowFilterAll extends MotorRowFilter {
961                 @Override
962                 public boolean filterByDiameter(ThrustCurveMotorSet m) {
963                         return true;
964                 }
965         }
966         
967         /**
968          * Show motors smaller than the mount.
969          */
970         private class MotorRowFilterSmaller extends MotorRowFilter {
971                 @Override
972                 public boolean filterByDiameter(ThrustCurveMotorSet m) {
973                         return (m.getDiameter() <= diameter + 0.0004);
974                 }
975         }
976         
977         /**
978          * Show motors that fit the mount.
979          */
980         private class MotorRowFilterExact extends MotorRowFilter {
981                 @Override
982                 public boolean filterByDiameter(ThrustCurveMotorSet m) {
983                         return ((m.getDiameter() <= diameter + 0.0004) && (m.getDiameter() >= diameter - 0.0015));
984                 }
985         }
986         
987         
988         /**
989          * Custom layered pane that sets the bounds of the components on every layout.
990          */
991         public class CustomLayeredPane extends JLayeredPane {
992                 @Override
993                 public void doLayout() {
994                         synchronized (getTreeLock()) {
995                                 int w = getWidth();
996                                 int h = getHeight();
997                                 chartPanel.setBounds(0, 0, w, h);
998                                 zoomIcon.setBounds(w - ZOOM_ICON_POSITION_NEGATIVE_X, ZOOM_ICON_POSITION_POSITIVE_Y, 50, 50);
999                         }
1000                 }
1001         }
1002 }