Skip to content

Commit

Permalink
Adding import/export JSON to incubator in and out configure window
Browse files Browse the repository at this point in the history
Add buttons/functions to save and open JSON files for the in and out
configure window.

Change-Id: Iee1ffa2ee16fbec2728cadc86d93686eb3f2f0f2
Signed-off-by: Estelle Foisy <estelle.foisy@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass.incubator/org.eclipse.tracecompass.incubator/+/199565
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
  • Loading branch information
Estelle Foisy authored and MatthewKhouzam committed Feb 16, 2023
1 parent a4b9741 commit 7013173
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void stop(@Nullable BundleContext context) throws Exception {
*
* @return the shared instance
*/
public static @Nullable Activator getDefault() {
public static Activator getDefault() {
return plugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.Dialog;
Expand All @@ -43,10 +45,13 @@
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tracecompass.incubator.internal.inandout.core.analysis.InAndOutAnalysisModule;
import org.eclipse.tracecompass.incubator.internal.inandout.core.analysis.SegmentSpecifier;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;

/**
* Configuration dialog
Expand All @@ -61,6 +66,7 @@ public class InAndOutConfigDialog extends Dialog {
private File fPath = null;
private ITmfTrace fTrace;
private boolean fChanged = false;
private static final String PREF_SAVED_OPEN_CONFIG_LOCATION = "PREF_LAST_OPEN_CONFIG_LOCATION"; //$NON-NLS-1$

/**
* Constructor
Expand Down Expand Up @@ -94,7 +100,7 @@ protected Control createDialogArea(@Nullable Composite parent) {
Composite localParent = (Composite) super.createDialogArea(parent);
localParent.addControlListener(resizeLayouter(localParent));
localParent.setLayout(GridLayoutFactory.fillDefaults().numColumns(2).create());
localParent.setLayoutData(GridDataFactory.fillDefaults().hint(400, 240).grab(true, true).create());
localParent.setLayoutData(GridDataFactory.fillDefaults().hint(400, 340).grab(true, true).create());

fSpecifiers = new ListViewer(localParent);
fSpecifiers.setLabelProvider(new LabelProvider());
Expand Down Expand Up @@ -152,6 +158,26 @@ public void widgetSelected(@Nullable SelectionEvent e) {
}));
reset.setText("Reset"); //$NON-NLS-1$

Button importButton = new Button(controls, SWT.PUSH);
importButton.setEnabled(true);
importButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(@Nullable SelectionEvent e) {
importFile();
}
});
importButton.setText("Import..."); //$NON-NLS-1$

Button exportButton = new Button(controls, SWT.PUSH);
exportButton.setEnabled(true);
exportButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(@Nullable SelectionEvent e) {
exportFile();
}
});
exportButton.setText("Export..."); //$NON-NLS-1$

Button up = new Button(controls, SWT.PUSH);
up.setEnabled(false);
fSpecifiers.addSelectionChangedListener(event -> up.setEnabled(!event.getSelection().isEmpty() && fData.size() != 1));
Expand Down Expand Up @@ -181,6 +207,51 @@ public void widgetSelected(@Nullable SelectionEvent e) {
return localParent;
}

private void exportFile() {
FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.SAVE);
dialog.setFilterExtensions(new String[] { "*.json" }); //$NON-NLS-1$
loadFileDialogSettings(dialog);
String selectedFileName = dialog.open();
if (selectedFileName != null) {
File fileName = new File(selectedFileName);
InAndOutAnalysisModule.write(fileName, fData);
saveFileDialogSettings(fileName.getParent());
}
}

private void importFile() {
FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
dialog.setFilterExtensions(new String[] { "*.json"}); //$NON-NLS-1$
loadFileDialogSettings(dialog);
String selectedFileName = dialog.open();
if (selectedFileName != null) {
File fileName = new File (selectedFileName);
List<@NonNull SegmentSpecifier> read = InAndOutAnalysisModule.read(fileName);
fData.addAll(read);
fSpecifiers.refresh();
saveFileDialogSettings(fileName.getParent());
}
}

private static void loadFileDialogSettings(FileDialog fd) {
IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
String lastLocation = defaultPreferences.get(PREF_SAVED_OPEN_CONFIG_LOCATION, null);
if (lastLocation != null && !lastLocation.isEmpty()) {
File parentFile = new File(lastLocation).getParentFile();
if (parentFile != null && parentFile.exists()) {
fd.setFilterPath(parentFile.toString());
}
}
}

private static void saveFileDialogSettings(String filePath) {
if (filePath == null) {
return;
}

InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID).put(PREF_SAVED_OPEN_CONFIG_LOCATION, filePath);
}

private static ControlListener resizeLayouter(Composite composite) {
return new ControlListener() {

Expand Down

0 comments on commit 7013173

Please sign in to comment.