import javax.swing.BorderFactory;
import javax.swing.JButton;
-import javax.swing.JCheckBox;
-import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
-import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
-import javax.swing.filechooser.FileFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.document.Simulation;
+import net.sf.openrocket.gui.util.FileHelper;
+import net.sf.openrocket.gui.util.GUIUtil;
+import net.sf.openrocket.gui.util.SwingPreferences;
+import net.sf.openrocket.gui.util.SaveCSVWorker;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.simulation.FlightData;
import net.sf.openrocket.simulation.FlightDataBranch;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.unit.Unit;
import net.sf.openrocket.unit.UnitGroup;
-import net.sf.openrocket.util.GUIUtil;
-import net.sf.openrocket.util.Prefs;
-import net.sf.openrocket.util.SaveCSVWorker;
public class SimulationExportPanel extends JPanel {
-
+
private static final String SPACE = "SPACE";
private static final String TAB = "TAB";
private static final Translator trans = Application.getTranslator();
- private static final FileFilter CSV_FILE_FILTER = new FileFilter() {
- @Override
- public String getDescription() {
- //// Comma Separated Files (*.csv)
- return trans.get("SimExpPan.desc");
- }
- @Override
- public boolean accept(File f) {
- if (f.isDirectory())
- return true;
- String name = f.getName().toLowerCase();
- return name.endsWith(".csv");
- }
- };
-
-
+ private static final int OPTION_SIMULATION_COMMENTS = 0;
+ private static final int OPTION_FIELD_DESCRIPTIONS = 1;
+ private static final int OPTION_FLIGHT_EVENTS = 2;
private final JTable table;
private final SelectionTableModel tableModel;
private final FlightDataType[] types;
private final Unit[] units;
- private final JComboBox fieldSeparator;
- private final JCheckBox simulationComments;
- private final JCheckBox fieldNameComments;
- private final JCheckBox eventComments;
- private final JComboBox commentCharacter;
+ private final CsvOptionPanel csvOptions;
public SimulationExportPanel(Simulation sim) {
super(new MigLayout("fill, flowy"));
-
- JLabel label;
+
JPanel panel;
JButton button;
- String tip;
-
+
this.simulation = sim;
// TODO: MEDIUM: Only exports primary branch
final FlightData data = simulation.getSimulatedData();
-
+
// Check that data exists
- if (data == null || data.getBranchCount() == 0 ||
+ if (data == null || data.getBranchCount() == 0 ||
data.getBranch(0).getTypes().length == 0) {
throw new IllegalArgumentException("No data for panel");
}
-
+
// Create the data model
branch = data.getBranch(0);
-
+
types = branch.getTypes();
Arrays.sort(types);
selected = new boolean[types.length];
units = new Unit[types.length];
for (int i = 0; i < types.length; i++) {
- selected[i] = Prefs.isExportSelected(types[i]);
+ selected[i] = ((SwingPreferences) Application.getPreferences()).isExportSelected(types[i]);
units[i] = types[i].getUnitGroup().getDefaultUnit();
}
-
+
//// Create the panel
-
+
// Set up the variable selection table
tableModel = new SelectionTableModel();
table = new JTable(tableModel);
- table.setDefaultRenderer(Object.class,
+ table.setDefaultRenderer(Object.class,
new SelectionBackgroundCellRenderer(table.getDefaultRenderer(Object.class)));
- table.setDefaultRenderer(Boolean.class,
+ table.setDefaultRenderer(Boolean.class,
new SelectionBackgroundCellRenderer(table.getDefaultRenderer(Boolean.class)));
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
col = columnModel.getColumn(2);
col.setPreferredWidth(100);
-
+
table.addMouseListener(new GUIUtil.BooleanTableClickListener(table));
// Add table
});
panel.add(button, "growx 1, sizegroup selectbutton, wrap");
-
+
selectedCountLabel = new JLabel();
updateSelectedCount();
panel.add(selectedCountLabel);
this.add(panel, "grow 100, wrap");
+
+ // These need to be in the order of the OPTIONS_XXX indices
+ csvOptions = new CsvOptionPanel(SimulationExportPanel.class,
+ trans.get("SimExpPan.checkbox.Includesimudesc"),
+ trans.get("SimExpPan.checkbox.ttip.Includesimudesc"),
+ trans.get("SimExpPan.checkbox.Includefielddesc"),
+ trans.get("SimExpPan.checkbox.ttip.Includefielddesc"),
+ trans.get("SimExpPan.checkbox.Incflightevents"),
+ trans.get("SimExpPan.checkbox.ttip.Incflightevents"));
+ this.add(csvOptions, "spany, split, growx 1");
- // Field separator panel
- panel = new JPanel(new MigLayout("fill"));
- panel.setBorder(BorderFactory.createTitledBorder(trans.get("SimExpPan.border.Fieldsep")));
-
- label = new JLabel(trans.get("SimExpPan.lbl.Fieldsepstr"));
- //// <html>The string used to separate the fields in the exported file.<br>
- //// Use ',' for a Comma Separated Values (CSV) file.
- tip = trans.get("SimExpPan.lbl.longA1") +
- trans.get("SimExpPan.lbl.longA2");
- label.setToolTipText(tip);
- panel.add(label);
-
- fieldSeparator = new JComboBox(new String[] { ",", ";", SPACE, TAB });
- fieldSeparator.setEditable(true);
- fieldSeparator.setSelectedItem(Prefs.getString(Prefs.EXPORT_FIELD_SEPARATOR,
- ","));
- fieldSeparator.setToolTipText(tip);
- panel.add(fieldSeparator);
-
- this.add(panel, "spany, split, growx 1");
-
-
-
-
- // Comments separator panel
- panel = new JPanel(new MigLayout("fill"));
- //// Comments
- panel.setBorder(BorderFactory.createTitledBorder(trans.get("SimExpPan.border.Comments")));
-
- //// Include simulation description
- simulationComments = new JCheckBox(trans.get("SimExpPan.checkbox.Includesimudesc"));
- //// Include a comment at the beginning of the file describing the simulation.
- simulationComments.setToolTipText(trans.get("SimExpPan.checkbox.ttip.Includesimudesc"));
- simulationComments.setSelected(Prefs.getBoolean(Prefs.EXPORT_SIMULATION_COMMENT,
- true));
- panel.add(simulationComments, "wrap");
-
- //// Include field descriptions
- fieldNameComments = new JCheckBox(trans.get("SimExpPan.checkbox.Includefielddesc"));
- //// Include a comment line with the descriptions of the exported variables.
- fieldNameComments.setToolTipText(trans.get("SimExpPan.checkbox.ttip.Includefielddesc"));
- fieldNameComments.setSelected(Prefs.getBoolean(Prefs.EXPORT_FIELD_NAME_COMMENT, true));
- panel.add(fieldNameComments, "wrap");
-
-
- eventComments = new JCheckBox(trans.get("SimExpPan.checkbox.Incflightevents"));
- eventComments.setToolTipText(trans.get("SimExpPan.checkbox.ttip.Incflightevents"));
- eventComments.setSelected(Prefs.getBoolean(Prefs.EXPORT_EVENT_COMMENTS, true));
- panel.add(eventComments, "wrap");
-
-
- label = new JLabel(trans.get("SimExpPan.lbl.Commentchar"));
- tip = trans.get("SimExpPan.lbl.ttip.Commentchar");
- label.setToolTipText(tip);
- panel.add(label, "split 2");
-
- commentCharacter = new JComboBox(new String[] { "#", "%", ";" });
- commentCharacter.setEditable(true);
- commentCharacter.setSelectedItem(Prefs.getString(Prefs.EXPORT_COMMENT_CHARACTER, "#"));
- commentCharacter.setToolTipText(tip);
- panel.add(commentCharacter);
-
- this.add(panel, "growx 1");
-
// Space-filling panel
panel = new JPanel();
this.add(panel, "width 1, height 1, grow 1");
-
+
// Export button
button = new JButton(trans.get("SimExpPan.but.Exporttofile"));
button.addActionListener(new ActionListener() {
private void doExport() {
JFileChooser chooser = new JFileChooser();
- chooser.setFileFilter(CSV_FILE_FILTER);
- chooser.setCurrentDirectory(Prefs.getDefaultDirectory());
+ chooser.setFileFilter(FileHelper.CSV_FILE_FILTER);
+ chooser.setCurrentDirectory(((SwingPreferences) Application.getPreferences()).getDefaultDirectory());
if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
return;
if (file == null)
return;
- if (file.getName().indexOf('.') < 0) {
- String name = file.getAbsolutePath();
- name = name + ".csv";
- file = new File(name);
- }
-
- if (file.exists()) {
- int ret = JOptionPane.showConfirmDialog(this,
- "File \"" + file.getName() + "\" exists. Overwrite?",
- "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
- if (ret != JOptionPane.YES_OPTION)
- return;
+ file = FileHelper.ensureExtension(file, "csv");
+ if (!FileHelper.confirmWrite(file, this)) {
+ return;
}
+
- String commentChar = commentCharacter.getSelectedItem().toString();
- String fieldSep = fieldSeparator.getSelectedItem().toString();
- boolean simulationComment = simulationComments.isSelected();
- boolean fieldComment = fieldNameComments.isSelected();
- boolean eventComment = eventComments.isSelected();
+ String commentChar = csvOptions.getCommentCharacter();
+ String fieldSep = csvOptions.getFieldSeparator();
+ boolean simulationComment = csvOptions.getSelectionOption(OPTION_SIMULATION_COMMENTS);
+ boolean fieldComment = csvOptions.getSelectionOption(OPTION_FIELD_DESCRIPTIONS);
+ boolean eventComment = csvOptions.getSelectionOption(OPTION_FLIGHT_EVENTS);
+ csvOptions.storePreferences();
// Store preferences and export
int n = 0;
- Prefs.setDefaultDirectory(chooser.getCurrentDirectory());
- for (int i=0; i < selected.length; i++) {
- Prefs.setExportSelected(types[i], selected[i]);
+ ((SwingPreferences) Application.getPreferences()).setDefaultDirectory(chooser.getCurrentDirectory());
+ for (int i = 0; i < selected.length; i++) {
+ ((SwingPreferences) Application.getPreferences()).setExportSelected(types[i], selected[i]);
if (selected[i])
n++;
}
- Prefs.putString(Prefs.EXPORT_FIELD_SEPARATOR, fieldSep);
- Prefs.putString(Prefs.EXPORT_COMMENT_CHARACTER, commentChar);
- Prefs.putBoolean(Prefs.EXPORT_EVENT_COMMENTS, eventComment);
- Prefs.putBoolean(Prefs.EXPORT_FIELD_NAME_COMMENT, fieldComment);
- Prefs.putBoolean(Prefs.EXPORT_SIMULATION_COMMENT, simulationComment);
-
+
FlightDataType[] fieldTypes = new FlightDataType[n];
Unit[] fieldUnits = new Unit[n];
int pos = 0;
- for (int i=0; i < selected.length; i++) {
+ for (int i = 0; i < selected.length; i++) {
if (selected[i]) {
fieldTypes[pos] = types[i];
fieldUnits[pos] = units[i];
fieldSep = "\t";
}
-
- SaveCSVWorker.export(file, simulation, branch, fieldTypes, fieldUnits, fieldSep,
- commentChar, simulationComment, fieldComment, eventComment,
+
+ SaveCSVWorker.export(file, simulation, branch, fieldTypes, fieldUnits, fieldSep,
+ commentChar, simulationComment, fieldComment, eventComment,
SwingUtilities.getWindowAncestor(this));
}
int n = 0;
String str;
- for (int i=0; i < selected.length; i++) {
+ for (int i = 0; i < selected.length; i++) {
if (selected[i])
n++;
}
if (n == 1) {
- str = "Exporting 1 variable out of " + total + ".";
+ //// Exporting 1 variable out of
+ str = trans.get("SimExpPan.ExportingVar.desc1") + " " + total + ".";
} else {
- str = "Exporting "+n+" variables out of " + total + ".";
+ //// Exporting
+ //// variables out of
+ str = trans.get("SimExpPan.ExportingVar.desc2") + " " + n + " " +
+ trans.get("SimExpPan.ExportingVar.desc3") + " " + total + ".";
}
-
+
selectedCountLabel.setText(str);
}
-
+
/**
* A table cell renderer that uses another renderer and sets the background and
* foreground of the returned component based on the selection of the variable.
*/
private class SelectionBackgroundCellRenderer implements TableCellRenderer {
-
+
private final TableCellRenderer renderer;
public SelectionBackgroundCellRenderer(TableCellRenderer renderer) {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
- Component component = renderer.getTableCellRendererComponent(table,
+ Component component = renderer.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
if (selected[row]) {
private static final int SELECTED = 0;
private static final int NAME = 1;
private static final int UNIT = 2;
-
+
@Override
public int getColumnCount() {
return 3;
}
-
+
@Override
public int getRowCount() {
return types.length;
case SELECTED:
return "";
case NAME:
- return "Variable";
+ //// Variable
+ return trans.get("SimExpPan.Col.Variable");
case UNIT:
- return "Unit";
+ //// Unit
+ return trans.get("SimExpPan.Col.Unit");
default:
throw new IndexOutOfBoundsException("column=" + column);
}
throw new IndexOutOfBoundsException("column=" + column);
}
}
-
+
@Override
public Object getValueAt(int row, int column) {
-
+
switch (column) {
case SELECTED:
return selected[row];
return units[row];
default:
- throw new IndexOutOfBoundsException("column="+column);
+ throw new IndexOutOfBoundsException("column=" + column);
}
}
-
+
@Override
public void setValueAt(Object value, int row, int column) {
switch (column) {
case SELECTED:
- selected[row] = (Boolean)value;
+ selected[row] = (Boolean) value;
this.fireTableRowsUpdated(row, row);
updateSelectedCount();
break;
-
+
case NAME:
break;
-
+
case UNIT:
- units[row] = (Unit)value;
+ units[row] = (Unit) value;
break;
-
+
default:
- throw new IndexOutOfBoundsException("column="+column);
+ throw new IndexOutOfBoundsException("column=" + column);
}
}
-
+
@Override
public boolean isCellEditable(int row, int column) {
switch (column) {
return types[row].getUnitGroup().getUnitCount() > 1;
default:
- throw new IndexOutOfBoundsException("column="+column);
+ throw new IndexOutOfBoundsException("column=" + column);
}
}