create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / components / CsvOptionPanel.java
1 package net.sf.openrocket.gui.components;
2
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;
8
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;
13
14 /**
15  * A panel that shows options for saving CSV files.
16  * 
17  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18  */
19 public class CsvOptionPanel extends JPanel {
20         
21         private static final Translator trans = Application.getTranslator();
22         
23         private static final String SPACE = trans.get("CsvOptionPanel.separator.space");
24         private static final String TAB = trans.get("CsvOptionPanel.separator.tab");
25         
26         private final String baseClassName;
27         
28         private final JComboBox fieldSeparator;
29         private final JCheckBox[] options;
30         private final JComboBox commentCharacter;
31         
32         /**
33          * Sole constructor.
34          * 
35          * @param includeComments       a list of comment inclusion options to provide;
36          *                                                      every second item is the option name and every second the tooltip
37          */
38         public CsvOptionPanel(Class<?> baseClass, String... includeComments) {
39                 super(new MigLayout("fill, insets 0"));
40                 
41                 this.baseClassName = baseClass.getSimpleName();
42                 
43                 JPanel panel;
44                 JLabel label;
45                 String tip;
46                 
47
48                 // TODO: HIGH: Rename the translation keys
49                 
50                 // Field separator panel
51                 panel = new JPanel(new MigLayout("fill"));
52                 panel.setBorder(BorderFactory.createTitledBorder(trans.get("SimExpPan.border.Fieldsep")));
53                 
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");
59                 
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");
65                 
66                 this.add(panel, "growx, wrap unrel");
67                 
68
69
70                 // Comments separator panel
71                 panel = new JPanel(new MigLayout("fill"));
72                 panel.setBorder(BorderFactory.createTitledBorder(trans.get("SimExpPan.border.Comments")));
73                 
74
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);
78                 }
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");
85                 }
86                 
87
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");
92                 
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");
98                 
99                 this.add(panel, "growx, wrap");
100         }
101         
102         
103         public String getFieldSeparator() {
104                 return fieldSeparator.getSelectedItem().toString();
105         }
106         
107         public String getCommentCharacter() {
108                 return commentCharacter.getSelectedItem().toString();
109         }
110         
111         public boolean getSelectionOption(int index) {
112                 return options[index].isSelected();
113         }
114         
115         /**
116          * Store the selected options to the user preferences.
117          */
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());
123                 }
124         }
125         
126 }