moving to core/
[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.MotorDigest;
58 import net.sf.openrocket.motor.ThrustCurveMotor;
59 import net.sf.openrocket.startup.Application;
60 import net.sf.openrocket.unit.UnitGroup;
61 import net.sf.openrocket.util.BugException;
62 import net.sf.openrocket.utils.MotorCorrelation;
63
64 import org.jfree.chart.ChartColor;
65 import org.jfree.chart.ChartFactory;
66 import org.jfree.chart.ChartPanel;
67 import org.jfree.chart.JFreeChart;
68 import org.jfree.chart.axis.ValueAxis;
69 import org.jfree.chart.plot.PlotOrientation;
70 import org.jfree.chart.plot.XYPlot;
71 import org.jfree.chart.title.TextTitle;
72 import org.jfree.data.xy.XYSeries;
73 import org.jfree.data.xy.XYSeriesCollection;
74
75 public class ThrustCurveMotorSelectionPanel extends JPanel implements MotorSelector {
76         private static final LogHelper log = Application.getLogger();
77         private static final Translator trans = Application.getTranslator();
78
79         private static final double MOTOR_SIMILARITY_THRESHOLD = 0.95;
80         
81         private static final int SHOW_ALL = 0;
82         private static final int SHOW_SMALLER = 1;
83         private static final int SHOW_EXACT = 2;
84         private static final String[] SHOW_DESCRIPTIONS = {
85                 //// Show all motors
86                 trans.get("TCMotorSelPan.SHOW_DESCRIPTIONS.desc1"),
87                         //// Show motors with diameter less than that of the motor mount
88                 trans.get("TCMotorSelPan.SHOW_DESCRIPTIONS.desc2"),
89                         //// Show motors with diameter equal to that of the motor mount
90                 trans.get("TCMotorSelPan.SHOW_DESCRIPTIONS.desc3")
91         };
92         private static final int SHOW_MAX = 2;
93         
94         private static final int ZOOM_ICON_POSITION_NEGATIVE_X = 50;
95         private static final int ZOOM_ICON_POSITION_POSITIVE_Y = 12;
96         
97         private static final Paint[] CURVE_COLORS = ChartColor.createDefaultPaintArray();
98         
99         private static final Color NO_COMMENT_COLOR = Color.GRAY;
100         private static final Color WITH_COMMENT_COLOR = Color.BLACK;
101         
102         private static final ThrustCurveMotorComparator MOTOR_COMPARATOR = new ThrustCurveMotorComparator();
103         
104
105
106         private final List<ThrustCurveMotorSet> database;
107         
108         private final double diameter;
109         private CloseableDialog dialog = null;
110         
111
112         private final ThrustCurveMotorDatabaseModel model;
113         private final JTable table;
114         private final TableRowSorter<TableModel> sorter;
115         
116         private final JCheckBox hideSimilarBox;
117         
118         private final JTextField searchField;
119         private String[] searchTerms = new String[0];
120         
121
122         private final JLabel curveSelectionLabel;
123         private final JComboBox curveSelectionBox;
124         private final DefaultComboBoxModel curveSelectionModel;
125         
126         private final JLabel totalImpulseLabel;
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, "wrap");
417                 
418                 //// Avg. thrust:
419                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Avgthrust")));
420                 avgThrustLabel = new JLabel();
421                 panel.add(avgThrustLabel, "wrap");
422                 
423                 //// Max. thrust:
424                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Maxthrust")));
425                 maxThrustLabel = new JLabel();
426                 panel.add(maxThrustLabel, "wrap");
427                 
428                 //// Burn time:
429                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Burntime")));
430                 burnTimeLabel = new JLabel();
431                 panel.add(burnTimeLabel, "wrap");
432                 
433                 //// Launch mass:
434                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Launchmass")));
435                 launchMassLabel = new JLabel();
436                 panel.add(launchMassLabel, "wrap");
437                 
438                 //// Empty mass:
439                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Emptymass")));
440                 emptyMassLabel = new JLabel();
441                 panel.add(emptyMassLabel, "wrap");
442                 
443                 //// Data points:
444                 panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Datapoints")));
445                 dataPointsLabel = new JLabel();
446                 panel.add(dataPointsLabel, "wrap para");
447                 
448                 if (System.getProperty("openrocket.debug.motordigest") != null) {
449                         //// Digest:
450                         panel.add(new JLabel(trans.get("TCMotorSelPan.lbl.Digest")));
451                         digestLabel = new JLabel();
452                         panel.add(digestLabel, "w :300:, wrap para");
453                 } else {
454                         digestLabel = null;
455                 }
456                 
457
458                 comment = new JTextArea(5, 5);
459                 GUIUtil.changeFontSize(comment, -2);
460                 withCommentFont = comment.getFont();
461                 noCommentFont = withCommentFont.deriveFont(Font.ITALIC);
462                 comment.setLineWrap(true);
463                 comment.setWrapStyleWord(true);
464                 comment.setEditable(false);
465                 scrollpane = new JScrollPane(comment);
466                 panel.add(scrollpane, "spanx, growx, wrap para");
467                 
468
469
470
471                 // Thrust curve plot
472                 chart = ChartFactory.createXYLineChart(
473                                 null, // title
474                                 null, // xAxisLabel
475                                 null, // yAxisLabel
476                                 null, // dataset
477                                 PlotOrientation.VERTICAL,
478                                 false, // legend
479                                 false, // tooltips
480                                 false // urls
481                                 );
482                 
483
484                 // Add the data and formatting to the plot
485                 XYPlot plot = chart.getXYPlot();
486                 
487                 changeLabelFont(plot.getRangeAxis(), -2);
488                 changeLabelFont(plot.getDomainAxis(), -2);
489                 
490                 //// Thrust curve:
491                 chart.setTitle(new TextTitle(trans.get("TCMotorSelPan.title.Thrustcurve"), this.getFont()));
492                 chart.setBackgroundPaint(this.getBackground());
493                 plot.setBackgroundPaint(Color.WHITE);
494                 plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
495                 plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
496                 
497                 chartPanel = new ChartPanel(chart,
498                                 false, // properties
499                                 false, // save
500                                 false, // print
501                                 false, // zoom
502                                 false); // tooltips
503                 chartPanel.setMouseZoomable(false);
504                 chartPanel.setPopupMenu(null);
505                 chartPanel.setMouseWheelEnabled(false);
506                 chartPanel.setRangeZoomable(false);
507                 chartPanel.setDomainZoomable(false);
508                 
509                 chartPanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
510                 chartPanel.addMouseListener(new MouseAdapter() {
511                         @Override
512                         public void mouseClicked(MouseEvent e) {
513                                 if (selectedMotor == null || selectedMotorSet == null)
514                                         return;
515                                 if (e.getButton() == MouseEvent.BUTTON1) {
516                                         // Open plot dialog
517                                         List<ThrustCurveMotor> motors = getFilteredCurves();
518                                         ThrustCurveMotorPlotDialog plotDialog = new ThrustCurveMotorPlotDialog(motors,
519                                                         motors.indexOf(selectedMotor),
520                                                         SwingUtilities.getWindowAncestor(ThrustCurveMotorSelectionPanel.this));
521                                         plotDialog.setVisible(true);
522                                 }
523                         }
524                 });
525                 
526                 JLayeredPane layer = new CustomLayeredPane();
527                 
528                 layer.setBorder(BorderFactory.createLineBorder(Color.BLUE));
529                 
530                 layer.add(chartPanel, (Integer) 0);
531                 
532                 zoomIcon = new JLabel(Icons.ZOOM_IN);
533                 zoomIcon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
534                 layer.add(zoomIcon, (Integer) 1);
535                 
536
537                 panel.add(layer, "width 300:300:, height 180:180:, grow, spanx");
538                 
539
540
541                 this.add(panel, "grow");
542                 
543
544
545                 // Sets the filter:
546                 int showMode = Application.getPreferences().getChoice(net.sf.openrocket.startup.Preferences.MOTOR_DIAMETER_FILTER, SHOW_MAX, SHOW_EXACT);
547                 filterComboBox.setSelectedIndex(showMode);
548                 
549
550                 // Update the panel data
551                 updateData();
552                 selectedDelay = delay;
553                 setDelays(false);
554                 
555         }
556         
557         
558         @Override
559         public Motor getSelectedMotor() {
560                 return selectedMotor;
561         }
562         
563         
564         @Override
565         public double getSelectedDelay() {
566                 return selectedDelay;
567         }
568         
569         
570         @Override
571         public JComponent getDefaultFocus() {
572                 return searchField;
573         }
574         
575         @Override
576         public void selectedMotor(Motor motorSelection) {
577                 if (!(motorSelection instanceof ThrustCurveMotor)) {
578                         log.error("Received argument that was not ThrustCurveMotor: " + motorSelection);
579                         return;
580                 }
581                 
582                 ThrustCurveMotor motor = (ThrustCurveMotor) motorSelection;
583                 ThrustCurveMotorSet set = findMotorSet(motor);
584                 if (set == null) {
585                         log.error("Could not find set for motor:" + motorSelection);
586                         return;
587                 }
588                 
589                 // Store selected motor in preferences node, set all others to false
590                 Preferences prefs = ((SwingPreferences) Application.getPreferences()).getNode(net.sf.openrocket.startup.Preferences.PREFERRED_THRUST_CURVE_MOTOR_NODE);
591                 for (ThrustCurveMotor m : set.getMotors()) {
592                         String digest = MotorDigest.digestMotor(m);
593                         prefs.putBoolean(digest, m == motor);
594                 }
595         }
596         
597         public void setCloseableDialog(CloseableDialog dialog) {
598                 this.dialog = dialog;
599         }
600         
601         
602
603         private void changeLabelFont(ValueAxis axis, float size) {
604                 Font font = axis.getTickLabelFont();
605                 font = font.deriveFont(font.getSize2D() + size);
606                 axis.setTickLabelFont(font);
607         }
608         
609         
610         /**
611          * Called when a different motor is selected from within the panel.
612          */
613         private void select(ThrustCurveMotor motor) {
614                 if (selectedMotor == motor)
615                         return;
616                 
617                 ThrustCurveMotorSet set = findMotorSet(motor);
618                 if (set == null) {
619                         throw new BugException("Could not find motor from database, motor=" + motor);
620                 }
621                 
622                 boolean updateDelays = (selectedMotorSet != set);
623                 
624                 selectedMotor = motor;
625                 selectedMotorSet = set;
626                 updateData();
627                 if (updateDelays) {
628                         setDelays(true);
629                 }
630         }
631         
632         
633         private void updateData() {
634                 
635                 if (selectedMotorSet == null) {
636                         // No motor selected
637                         curveSelectionModel.removeAllElements();
638                         curveSelectionBox.setEnabled(false);
639                         curveSelectionLabel.setEnabled(false);
640                         totalImpulseLabel.setText("");
641                         avgThrustLabel.setText("");
642                         maxThrustLabel.setText("");
643                         burnTimeLabel.setText("");
644                         launchMassLabel.setText("");
645                         emptyMassLabel.setText("");
646                         dataPointsLabel.setText("");
647                         if (digestLabel != null) {
648                                 digestLabel.setText("");
649                         }
650                         setComment("");
651                         chart.getXYPlot().setDataset(new XYSeriesCollection());
652                         return;
653                 }
654                 
655
656                 // Check which thrust curves to display
657                 List<ThrustCurveMotor> motors = getFilteredCurves();
658                 final int index = motors.indexOf(selectedMotor);
659                 
660
661                 // Update the thrust curve selection box
662                 curveSelectionModel.removeAllElements();
663                 for (int i = 0; i < motors.size(); i++) {
664                         curveSelectionModel.addElement(new MotorHolder(motors.get(i), i));
665                 }
666                 curveSelectionBox.setSelectedIndex(index);
667                 
668                 if (motors.size() > 1) {
669                         curveSelectionBox.setEnabled(true);
670                         curveSelectionLabel.setEnabled(true);
671                 } else {
672                         curveSelectionBox.setEnabled(false);
673                         curveSelectionLabel.setEnabled(false);
674                 }
675                 
676
677                 // Update thrust curve data
678                 totalImpulseLabel.setText(UnitGroup.UNITS_IMPULSE.getDefaultUnit().toStringUnit(
679                                 selectedMotor.getTotalImpulseEstimate()));
680                 avgThrustLabel.setText(UnitGroup.UNITS_FORCE.getDefaultUnit().toStringUnit(
681                                 selectedMotor.getAverageThrustEstimate()));
682                 maxThrustLabel.setText(UnitGroup.UNITS_FORCE.getDefaultUnit().toStringUnit(
683                                 selectedMotor.getMaxThrustEstimate()));
684                 burnTimeLabel.setText(UnitGroup.UNITS_SHORT_TIME.getDefaultUnit().toStringUnit(
685                                 selectedMotor.getBurnTimeEstimate()));
686                 launchMassLabel.setText(UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(
687                                 selectedMotor.getLaunchCG().weight));
688                 emptyMassLabel.setText(UnitGroup.UNITS_MASS.getDefaultUnit().toStringUnit(
689                                 selectedMotor.getEmptyCG().weight));
690                 dataPointsLabel.setText("" + (selectedMotor.getTimePoints().length - 1));
691                 if (digestLabel != null) {
692                         digestLabel.setText(MotorDigest.digestMotor(selectedMotor));
693                 }
694                 
695                 setComment(selectedMotor.getDescription());
696                 
697
698                 // Update the plot
699                 XYPlot plot = chart.getXYPlot();
700                 
701                 XYSeriesCollection dataset = new XYSeriesCollection();
702                 for (int i = 0; i < motors.size(); i++) {
703                         ThrustCurveMotor m = motors.get(i);
704                         
705                         //// Thrust
706                         XYSeries series = new XYSeries(trans.get("TCMotorSelPan.title.Thrust"));
707                         double[] time = m.getTimePoints();
708                         double[] thrust = m.getThrustPoints();
709                         
710                         for (int j = 0; j < time.length; j++) {
711                                 series.add(time[j], thrust[j]);
712                         }
713                         
714                         dataset.addSeries(series);
715                         
716                         boolean selected = (i == index);
717                         plot.getRenderer().setSeriesStroke(i, new BasicStroke(selected ? 3 : 1));
718                         plot.getRenderer().setSeriesPaint(i, getColor(i));
719                 }
720                 
721                 plot.setDataset(dataset);
722         }
723         
724         
725         private List<ThrustCurveMotor> getFilteredCurves() {
726                 List<ThrustCurveMotor> motors = selectedMotorSet.getMotors();
727                 if (hideSimilarBox.isSelected()) {
728                         List<ThrustCurveMotor> filtered = new ArrayList<ThrustCurveMotor>(motors.size());
729                         for (int i = 0; i < motors.size(); i++) {
730                                 ThrustCurveMotor m = motors.get(i);
731                                 if (m.equals(selectedMotor)) {
732                                         filtered.add(m);
733                                         continue;
734                                 }
735                                 
736                                 double similarity = MotorCorrelation.similarity(selectedMotor, m);
737                                 log.debug("Motor similarity: " + similarity);
738                                 if (similarity < MOTOR_SIMILARITY_THRESHOLD) {
739                                         filtered.add(m);
740                                 }
741                         }
742                         motors = filtered;
743                 }
744                 
745                 Collections.sort(motors, MOTOR_COMPARATOR);
746                 
747                 return motors;
748         }
749         
750         
751         private void setComment(String s) {
752                 s = s.trim();
753                 if (s.length() == 0) {
754                         //// No description available.
755                         comment.setText("No description available.");
756                         comment.setFont(noCommentFont);
757                         comment.setForeground(NO_COMMENT_COLOR);
758                 } else {
759                         comment.setText(s);
760                         comment.setFont(withCommentFont);
761                         comment.setForeground(WITH_COMMENT_COLOR);
762                 }
763                 comment.setCaretPosition(0);
764         }
765         
766         private void scrollSelectionVisible() {
767                 if (selectedMotorSet != null) {
768                         int index = table.convertRowIndexToView(model.getIndex(selectedMotorSet));
769                         System.out.println("index=" + index);
770                         table.getSelectionModel().setSelectionInterval(index, index);
771                         Rectangle rect = table.getCellRect(index, 0, true);
772                         rect = new Rectangle(rect.x, rect.y - 100, rect.width, rect.height + 200);
773                         table.scrollRectToVisible(rect);
774                 }
775         }
776         
777         
778         public static Color getColor(int index) {
779                 return (Color) CURVE_COLORS[index % CURVE_COLORS.length];
780         }
781         
782         
783         /**
784          * Find the ThrustCurveMotorSet that contains a motor.
785          * 
786          * @param motor         the motor to look for.
787          * @return                      the ThrustCurveMotorSet, or null if not found.
788          */
789         private ThrustCurveMotorSet findMotorSet(ThrustCurveMotor motor) {
790                 for (ThrustCurveMotorSet set : database) {
791                         if (set.getMotors().contains(motor)) {
792                                 return set;
793                         }
794                 }
795                 
796                 return null;
797         }
798         
799         
800
801         /**
802          * Select the default motor from this ThrustCurveMotorSet.  This uses primarily motors
803          * that the user has previously used, and secondarily a heuristic method of selecting which
804          * thrust curve seems to be better or more reliable.
805          * 
806          * @param set   the motor set
807          * @return              the default motor in this set
808          */
809         private ThrustCurveMotor selectMotor(ThrustCurveMotorSet set) {
810                 if (set.getMotorCount() == 0) {
811                         throw new BugException("Attempting to select motor from empty ThrustCurveMotorSet: " + set);
812                 }
813                 if (set.getMotorCount() == 1) {
814                         return set.getMotors().get(0);
815                 }
816                 
817
818                 // Find which motor has been used the most recently
819                 List<ThrustCurveMotor> list = set.getMotors();
820                 Preferences prefs = ((SwingPreferences) Application.getPreferences()).getNode(net.sf.openrocket.startup.Preferences.PREFERRED_THRUST_CURVE_MOTOR_NODE);
821                 for (ThrustCurveMotor m : list) {
822                         String digest = MotorDigest.digestMotor(m);
823                         if (prefs.getBoolean(digest, false)) {
824                                 return m;
825                         }
826                 }
827                 
828                 // No motor has been used
829                 Collections.sort(list, MOTOR_COMPARATOR);
830                 return list.get(0);
831         }
832         
833         
834         /**
835          * Set the values in the delay combo box.  If <code>reset</code> is <code>true</code>
836          * then sets the selected value as the value closest to selectedDelay, otherwise
837          * leaves selection alone.
838          */
839         private void setDelays(boolean reset) {
840                 if (selectedMotor == null) {
841                         
842                         //// None
843                         delayBox.setModel(new DefaultComboBoxModel(new String[] { trans.get("TCMotorSelPan.delayBox.None") }));
844                         delayBox.setSelectedIndex(0);
845                         
846                 } else {
847                         
848                         List<Double> delays = selectedMotorSet.getDelays();
849                         String[] delayStrings = new String[delays.size()];
850                         double currentDelay = selectedDelay; // Store current setting locally
851                         
852                         for (int i = 0; i < delays.size(); i++) {
853                                 //// None
854                                 delayStrings[i] = ThrustCurveMotor.getDelayString(delays.get(i), trans.get("TCMotorSelPan.delayBox.None"));
855                         }
856                         delayBox.setModel(new DefaultComboBoxModel(delayStrings));
857                         
858                         if (reset) {
859                                 
860                                 // Find and set the closest value
861                                 double closest = Double.NaN;
862                                 for (int i = 0; i < delays.size(); i++) {
863                                         // if-condition to always become true for NaN
864                                         if (!(Math.abs(delays.get(i) - currentDelay) > Math.abs(closest - currentDelay))) {
865                                                 closest = delays.get(i);
866                                         }
867                                 }
868                                 if (!Double.isNaN(closest)) {
869                                         selectedDelay = closest;
870                                 //// None
871                                         delayBox.setSelectedItem(ThrustCurveMotor.getDelayString(closest, trans.get("TCMotorSelPan.delayBox.None")));
872                                 } else {
873                                         delayBox.setSelectedItem("None");
874                                 }
875                                 
876                         } else {
877                                 
878                                 selectedDelay = currentDelay;
879                         //// None
880                                 delayBox.setSelectedItem(ThrustCurveMotor.getDelayString(currentDelay, trans.get("TCMotorSelPan.delayBox.None")));
881                                 
882                         }
883                         
884                 }
885         }
886         
887         
888
889
890         //////////////////////
891         
892
893         private class CurveSelectionRenderer implements ListCellRenderer {
894                 
895                 private final ListCellRenderer renderer;
896                 
897                 public CurveSelectionRenderer(ListCellRenderer renderer) {
898                         this.renderer = renderer;
899                 }
900                 
901                 @Override
902                 public Component getListCellRendererComponent(JList list, Object value, int index,
903                                 boolean isSelected, boolean cellHasFocus) {
904                         
905                         Component c = renderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
906                         if (value instanceof MotorHolder) {
907                                 MotorHolder m = (MotorHolder) value;
908                                 c.setForeground(getColor(m.getIndex()));
909                         }
910                         
911                         return c;
912                 }
913                 
914         }
915         
916         
917         ////////  Row filters
918         
919         /**
920          * Abstract adapter class.
921          */
922         private abstract class MotorRowFilter extends RowFilter<TableModel, Integer> {
923                 @Override
924                 public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
925                         int index = entry.getIdentifier();
926                         ThrustCurveMotorSet m = model.getMotorSet(index);
927                         return filterByDiameter(m) && filterByString(m);
928                 }
929                 
930                 public abstract boolean filterByDiameter(ThrustCurveMotorSet m);
931                 
932                 
933                 public boolean filterByString(ThrustCurveMotorSet m) {
934                         main: for (String s : searchTerms) {
935                                 for (ThrustCurveMotorColumns col : ThrustCurveMotorColumns.values()) {
936                                         String str = col.getValue(m).toString().toLowerCase();
937                                         if (str.indexOf(s) >= 0)
938                                                 continue main;
939                                 }
940                                 return false;
941                         }
942                         return true;
943                 }
944         }
945         
946         /**
947          * Show all motors.
948          */
949         private class MotorRowFilterAll extends MotorRowFilter {
950                 @Override
951                 public boolean filterByDiameter(ThrustCurveMotorSet m) {
952                         return true;
953                 }
954         }
955         
956         /**
957          * Show motors smaller than the mount.
958          */
959         private class MotorRowFilterSmaller extends MotorRowFilter {
960                 @Override
961                 public boolean filterByDiameter(ThrustCurveMotorSet m) {
962                         return (m.getDiameter() <= diameter + 0.0004);
963                 }
964         }
965         
966         /**
967          * Show motors that fit the mount.
968          */
969         private class MotorRowFilterExact extends MotorRowFilter {
970                 @Override
971                 public boolean filterByDiameter(ThrustCurveMotorSet m) {
972                         return ((m.getDiameter() <= diameter + 0.0004) && (m.getDiameter() >= diameter - 0.0015));
973                 }
974         }
975         
976         
977         /**
978          * Custom layered pane that sets the bounds of the components on every layout.
979          */
980         public class CustomLayeredPane extends JLayeredPane {
981                 @Override
982                 public void doLayout() {
983                         synchronized (getTreeLock()) {
984                                 int w = getWidth();
985                                 int h = getHeight();
986                                 chartPanel.setBounds(0, 0, w, h);
987                                 zoomIcon.setBounds(w - ZOOM_ICON_POSITION_NEGATIVE_X, ZOOM_ICON_POSITION_POSITIVE_Y, 50, 50);
988                         }
989                 }
990         }
991 }