1 package net.sf.openrocket.gui.components;
3 import javax.swing.BorderFactory;
4 import javax.swing.JCheckBox;
5 import javax.swing.JComboBox;
6 import javax.swing.JLabel;
7 import javax.swing.JPanel;
9 import net.miginfocom.swing.MigLayout;
10 import net.sf.openrocket.l10n.Translator;
11 import net.sf.openrocket.startup.Application;
12 import net.sf.openrocket.startup.Preferences;
15 * A panel that shows options for saving CSV files.
17 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19 public class CsvOptionPanel extends JPanel {
21 private static final Translator trans = Application.getTranslator();
23 private static final String SPACE = trans.get("CsvOptionPanel.separator.space");
24 private static final String TAB = trans.get("CsvOptionPanel.separator.tab");
26 private final String baseClassName;
28 private final JComboBox fieldSeparator;
29 private final JCheckBox[] options;
30 private final JComboBox commentCharacter;
35 * @param includeComments a list of comment inclusion options to provide;
36 * every second item is the option name and every second the tooltip
38 public CsvOptionPanel(Class<?> baseClass, String... includeComments) {
39 super(new MigLayout("fill, insets 0"));
41 this.baseClassName = baseClass.getSimpleName();
48 // TODO: HIGH: Rename the translation keys
50 // Field separator panel
51 panel = new JPanel(new MigLayout("fill"));
52 panel.setBorder(BorderFactory.createTitledBorder(trans.get("SimExpPan.border.Fieldsep")));
54 label = new JLabel(trans.get("SimExpPan.lbl.Fieldsepstr"));
55 tip = trans.get("SimExpPan.lbl.longA1") +
56 trans.get("SimExpPan.lbl.longA2");
57 label.setToolTipText(tip);
58 panel.add(label, "gapright unrel");
60 fieldSeparator = new JComboBox(new String[] { ",", ";", SPACE, TAB });
61 fieldSeparator.setEditable(true);
62 fieldSeparator.setSelectedItem(Application.getPreferences().getString(Preferences.EXPORT_FIELD_SEPARATOR, ","));
63 fieldSeparator.setToolTipText(tip);
64 panel.add(fieldSeparator, "growx");
66 this.add(panel, "growx, wrap unrel");
70 // Comments separator panel
71 panel = new JPanel(new MigLayout("fill"));
72 panel.setBorder(BorderFactory.createTitledBorder(trans.get("SimExpPan.border.Comments")));
75 // List of include comments options
76 if (includeComments.length % 2 == 1) {
77 throw new IllegalArgumentException("Invalid argument length, must be even, length=" + includeComments.length);
79 options = new JCheckBox[includeComments.length / 2];
80 for (int i = 0; i < includeComments.length / 2; i++) {
81 options[i] = new JCheckBox(includeComments[i * 2]);
82 options[i].setToolTipText(includeComments[i * 2 + 1]);
83 options[i].setSelected(Application.getPreferences().getBoolean("csvOptions." + baseClassName + "." + i, true));
84 panel.add(options[i], "wrap");
88 label = new JLabel(trans.get("SimExpPan.lbl.Commentchar"));
89 tip = trans.get("SimExpPan.lbl.ttip.Commentchar");
90 label.setToolTipText(tip);
91 panel.add(label, "split 2, gapright unrel");
93 commentCharacter = new JComboBox(new String[] { "#", "%", ";" });
94 commentCharacter.setEditable(true);
95 commentCharacter.setSelectedItem(Application.getPreferences().getString(Preferences.EXPORT_COMMENT_CHARACTER, "#"));
96 commentCharacter.setToolTipText(tip);
97 panel.add(commentCharacter, "growx");
99 this.add(panel, "growx, wrap");
103 public String getFieldSeparator() {
104 return fieldSeparator.getSelectedItem().toString();
107 public String getCommentCharacter() {
108 return commentCharacter.getSelectedItem().toString();
111 public boolean getSelectionOption(int index) {
112 return options[index].isSelected();
116 * Store the selected options to the user preferences.
118 public void storePreferences() {
119 Application.getPreferences().putString(Preferences.EXPORT_FIELD_SEPARATOR, getFieldSeparator());
120 Application.getPreferences().putString(Preferences.EXPORT_COMMENT_CHARACTER, getCommentCharacter());
121 for (int i = 0; i < options.length; i++) {
122 Application.getPreferences().putBoolean("csvOptions." + baseClassName + "." + i, options[i].isSelected());