Skip to content

Commit

Permalink
Fixed #235 - Modify/Delete Mappings
Browse files Browse the repository at this point in the history
The import/export icon has been taken from org.eclipse.ui.
  • Loading branch information
eselmeister committed May 22, 2021
1 parent 59013a1 commit 749e4e1
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
Expand All @@ -36,6 +37,7 @@
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.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
Expand Down Expand Up @@ -67,6 +69,7 @@ public class ExtendedLegendUI extends Composite {
private boolean capturePosition = false;
//
private List<Control> controls = new ArrayList<>();
private Object input;
//
private IPreferenceStore preferenceStore = ResourceSupport.getPreferenceStore();

Expand All @@ -88,6 +91,7 @@ public void setInput(Object input) {
/*
* First adjust the settings.
*/
this.input = input;
if(input instanceof ISeries<?>[] && scrollableChart != null) {
ISeries<?>[] seriesArray = (ISeries<?>[])input;
BaseChart baseChart = scrollableChart.getBaseChart();
Expand All @@ -97,7 +101,7 @@ public void setInput(Object input) {
/*
* Then fill the series list.
*/
seriesListUI.setInput(input);
updateSeriesList();
}

private void createControl() {
Expand Down Expand Up @@ -322,8 +326,19 @@ private Button createButtonMappings(Composite parent) {
@Override
public void widgetSelected(SelectionEvent e) {

MappingsDialog mappingsDialog = new MappingsDialog(e.display.getActiveShell());
mappingsDialog.open();
MappingsDialog mappingsDialog = new MappingsDialog(e.display.getActiveShell(), scrollableChart);
int returnCode = mappingsDialog.open();
if(returnCode == IDialogConstants.OK_ID) {
updateSeriesList();
Display.getDefault().asyncExec(new Runnable() {

@Override
public void run() {

scrollableChart.adjustRange(true);
}
});
}
}
});
//
Expand Down Expand Up @@ -460,6 +475,11 @@ private void updateControls() {
}
}

private void updateSeriesList() {

seriesListUI.setInput(input);
}

private boolean validate(IValidator validator, ControlDecoration controlDecoration, Text text) {

IStatus status = validator.validate(text.getText().trim());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Lablicate GmbH.
* Copyright (c) 2020, 2021 Lablicate GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -12,10 +12,12 @@
*******************************************************************************/
package org.eclipse.swtchart.extensions.core;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
Expand All @@ -25,22 +27,37 @@
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtchart.extensions.internal.support.SeriesMapper;
import org.eclipse.swtchart.extensions.preferences.PreferenceConstants;

public class MappingsDialog extends Dialog {

private static final String DESCRIPTION = "Mappings";
private static final String IMPORT = "Import " + DESCRIPTION;
private static final String EXPORT = "Export " + DESCRIPTION;
private static final String FILTER_EXTENSION = "*.txt";
private static final String FILTER_NAME = "SWTChart Mappings (*.txt)";
private static final String FILE_NAME = "SWTChartMappings.txt";
//
private MappingsListUI mappingsListUI;
private ScrollableChart scrollableChart;
//
private IPreferenceStore preferenceStore = ResourceSupport.getPreferenceStore();

public MappingsDialog(Shell shell, ScrollableChart scrollableChart) {

public MappingsDialog(Shell shell) {
super(shell);
this.scrollableChart = scrollableChart;
}

@Override
protected void configureShell(Shell shell) {

super.configureShell(shell);
shell.setText("Setting Mappings");
shell.setText(DESCRIPTION);
}

@Override
Expand All @@ -59,8 +76,10 @@ protected Point getInitialSize() {
protected Control createDialogArea(Composite parent) {

Composite composite = (Composite)super.createDialogArea(parent);
//
createToolbarMain(composite);
mappingsListUI = createMappingsList(composite);
// createToolbarMain(composite);
//
updateInput();
//
return composite;
Expand All @@ -73,40 +92,47 @@ private MappingsListUI createMappingsList(Composite parent) {
return mappingsListUI;
}

@SuppressWarnings("unused")
private void createToolbarMain(Composite parent) {

Composite composite = new Composite(parent, SWT.NONE);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalAlignment = SWT.END;
composite.setLayoutData(gridData);
composite.setLayout(new GridLayout(2, false));
composite.setLayout(new GridLayout(4, false));
//
createButtonDelete(composite);
createButtonDeleteAll(composite);
createButtonImport(composite);
createButtonExport(composite);
}

private Button createButtonDelete(Composite parent) {

Button button = new Button(parent, SWT.PUSH);
button.setText("");
button.setToolTipText("Delete selected mapping(s).");
button.setToolTipText("Delete the selected mappings.");
button.setImage(ResourceSupport.getImage(ResourceSupport.ICON_DELETE));
button.addSelectionListener(new SelectionAdapter() {

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void widgetSelected(SelectionEvent e) {

Iterator iterator = mappingsListUI.getStructuredSelection().iterator();
while(iterator.hasNext()) {
Object object = iterator.next();
if(object instanceof Map.Entry) {
Map.Entry<String, ISeriesSettings> entry = (Map.Entry<String, ISeriesSettings>)object;
SeriesMapper.remove(entry.getKey());
MessageBox messageBox = new MessageBox(e.display.getActiveShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
messageBox.setText(DESCRIPTION);
messageBox.setMessage("Would you like to delete the selected mappings?");
int decision = messageBox.open();
if(SWT.YES == decision) {
Iterator iterator = mappingsListUI.getStructuredSelection().iterator();
while(iterator.hasNext()) {
Object object = iterator.next();
if(object instanceof Map.Entry) {
Map.Entry<String, ISeriesSettings> entry = (Map.Entry<String, ISeriesSettings>)object;
SeriesMapper.remove(entry.getKey());
}
}
updateInput();
}
updateInput();
}
});
//
Expand All @@ -124,8 +150,94 @@ private Button createButtonDeleteAll(Composite parent) {
@Override
public void widgetSelected(SelectionEvent e) {

SeriesMapper.clear();
updateInput();
MessageBox messageBox = new MessageBox(e.display.getActiveShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
messageBox.setText(DESCRIPTION);
messageBox.setMessage("Would you like to delete all mappings?");
int decision = messageBox.open();
if(SWT.YES == decision) {
SeriesMapper.clear();
updateInput();
}
}
});
//
return button;
}

private Button createButtonImport(Composite parent) {

Button button = new Button(parent, SWT.PUSH);
button.setText("");
button.setToolTipText(IMPORT);
button.setImage(ResourceSupport.getImage(ResourceSupport.ICON_IMPORT));
button.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {

FileDialog fileDialog = new FileDialog(e.display.getActiveShell(), SWT.READ_ONLY);
fileDialog.setText(IMPORT);
fileDialog.setFilterExtensions(new String[]{FILTER_EXTENSION});
fileDialog.setFilterNames(new String[]{FILTER_NAME});
fileDialog.setFilterPath(preferenceStore.getString(PreferenceConstants.P_PATH_MAPPINGS_IMPORT));
String path = fileDialog.open();
if(path != null) {
preferenceStore.putValue(PreferenceConstants.P_PATH_MAPPINGS_IMPORT, fileDialog.getFilterPath());
File file = new File(path);
Map<String, ISeriesSettings> mappings = MappingsIO.importSettings(file);
for(Map.Entry<String, ISeriesSettings> mapping : mappings.entrySet()) {
/*
* Map
*/
String id = mapping.getKey();
ISeriesSettings seriesSettings = null;
/*
* Adjust the series
*/
if(scrollableChart != null) {
BaseChart baseChart = scrollableChart.getBaseChart();
seriesSettings = baseChart.getSeriesSettings(id);
if(seriesSettings != null) {
MappingsIO.transferSettings(mapping.getValue(), seriesSettings);
MappingsIO.transferSettings(mapping.getValue().getSeriesSettingsHighlight(), seriesSettings.getSeriesSettingsHighlight());
}
}
//
SeriesMapper.put(id, seriesSettings == null ? mapping.getValue() : seriesSettings);
}
//
updateInput();
}
}
});
//
return button;
}

private Button createButtonExport(Composite parent) {

Button button = new Button(parent, SWT.PUSH);
button.setText("");
button.setToolTipText(EXPORT);
button.setImage(ResourceSupport.getImage(ResourceSupport.ICON_EXPORT));
button.addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {

FileDialog fileDialog = new FileDialog(e.display.getActiveShell(), SWT.SAVE);
fileDialog.setOverwrite(true);
fileDialog.setText(EXPORT);
fileDialog.setFilterExtensions(new String[]{FILTER_EXTENSION});
fileDialog.setFilterNames(new String[]{FILTER_NAME});
fileDialog.setFileName(FILE_NAME);
fileDialog.setFilterPath(preferenceStore.getString(PreferenceConstants.P_PATH_MAPPINGS_EXPORT));
String path = fileDialog.open();
if(path != null) {
preferenceStore.putValue(PreferenceConstants.P_PATH_MAPPINGS_EXPORT, fileDialog.getFilterPath());
File file = new File(path);
MappingsIO.exportSettings(file, SeriesMapper.getMappings());
}
}
});
//
Expand Down
Loading

0 comments on commit 749e4e1

Please sign in to comment.