Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic/configurable SMA intervals #2503

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -374,6 +374,15 @@ public class Messages extends NLS
public static String DivvyDiaryMissingAPIKey;
public static String DivvyDiaryMsgUploading;
public static String DivvyDiaryUploadSuccessfulMsg;
public static String EditSmaIntervalDialog_AddIntervalLabel;
public static String EditSmaIntervalDialog_ColorLabel;
public static String EditSmaIntervalDialog_DeleteIntervalLabel;
public static String EditSmaIntervalDialog_InsertIntervalLabel;
public static String EditSmaIntervalDialog_IntervalLabel;
public static String EditSmaIntervalDialog_ManageLabel;
public static String EditSmaIntervalDialog_ValidateEnterNumberLabel;
public static String EditSmaIntervalDialog_ValidateEnterPositiveNumberLabel;
public static String EditSmaIntervalDialog_ValidateIntervalAlreadyExistsLabel;
public static String EditWizardAttributesTitle;
public static String EditWizardLatestQuoteFeedTitle;
public static String EditWizardMasterDataLinkNewCategory;
Expand Down Expand Up @@ -520,6 +529,7 @@ public class Messages extends NLS
public static String LabelChartDetailMovingAverage;
public static String LabelChartDetailMovingAverageSMA;
public static String LabelChartDetailMovingAverageEMA;
public static String LabelChartDetailMovingAverage_Xdays;
public static String LabelChartDetailMovingAverage_50days;
public static String LabelChartDetailMovingAverage_5days;
public static String LabelChartDetailMovingAverage_20days;
Expand Down
@@ -0,0 +1,266 @@
package name.abuchen.portfolio.ui.dialogs;

import java.util.Arrays;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColorCellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import name.abuchen.portfolio.model.IntervalSettings;
import name.abuchen.portfolio.model.IntervalSettings.IntervalSetting;
import name.abuchen.portfolio.ui.Images;
import name.abuchen.portfolio.ui.Messages;
import name.abuchen.portfolio.util.ColorConversion;

public class EditSmaIntervalsDialog extends Dialog
{
private final IntervalSettings intervals;

public EditSmaIntervalsDialog(Shell parentShell, IntervalSettings smaIntervals)
{
super(parentShell);

// create copy of settings to work on them
intervals = new IntervalSettings();
for(IntervalSettings.IntervalSetting i : smaIntervals.getAll())
intervals.add(i.getInterval(), i.getRGB(), i.getIsActive());
}

@Override
protected void configureShell(Shell shell)
{
super.configureShell(shell);
shell.setText(Messages.LabelClientFilter);
}

@Override
protected void createButtonsForButtonBar(Composite parent)
{
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}

@Override
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);

GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
container.setLayout(gridLayout);

final TableViewer table = new TableViewer(container, SWT.FULL_SELECTION);

createIntervalColumn(table);
createColorColumn(table);

table.getTable().setHeaderVisible(true);
table.getTable().setLinesVisible(true);
table.setContentProvider(new ArrayContentProvider());

updateTable(table);

final Composite buttonArea = new Composite(container, SWT.NONE);
buttonArea.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
final GridLayout gridLayoutButton = new GridLayout();
gridLayoutButton.numColumns = 1;
buttonArea.setLayout(gridLayoutButton);
createAddButton(buttonArea, table);
createRemoveButton(buttonArea, table);

return container;
}

private void createAddButton(Composite container, TableViewer table)
{
final Button addButton = new Button(container, SWT.FLAT | SWT.PUSH);
addButton.setImage(Images.ADD.image());
addButton.setToolTipText(Messages.EditSmaIntervalDialog_AddIntervalLabel);
addButton.addListener(SWT.MouseUp, e ->
{
if (e.type == SWT.MouseUp)
{
InputDialog dlg = new InputDialog(this.getShell(), Messages.EditSmaIntervalDialog_InsertIntervalLabel, Messages.EditSmaIntervalDialog_InsertIntervalLabel, "", new IntervalValidator(intervals.getAll())); //$NON-NLS-1$
if(dlg.open() == Window.OK)
{
intervals.add(Integer.parseInt(dlg.getValue()), new RGB(107, 179, 143), false);
updateTable(table);
}
}
});
}

private class IntervalValidator implements IInputValidator
{
final IntervalSetting[] existingIntervals;

public IntervalValidator(IntervalSetting[] intervalSettings)
{
this.existingIntervals = intervalSettings;
}

@Override
public String isValid(String newText)
{
int tempInterval = -1;
try
{
tempInterval = Integer.parseInt(newText);
}
catch(NumberFormatException ex)
{
return Messages.EditSmaIntervalDialog_ValidateEnterNumberLabel;
}

final int parsedInterval = tempInterval;
if(parsedInterval <= 0)
{
return Messages.EditSmaIntervalDialog_ValidateEnterPositiveNumberLabel;
}

if(Arrays.stream(existingIntervals).anyMatch(i -> i.getInterval() == parsedInterval))
{
return Messages.EditSmaIntervalDialog_ValidateIntervalAlreadyExistsLabel;
}
return null;
}
}

private void createRemoveButton(Composite container, TableViewer table)
{
final Button removeButton = new Button(container, SWT.FLAT | SWT.PUSH);
removeButton.setEnabled(false); // disable by default. will be enabled if row is selected
removeButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
removeButton.setImage(Images.REMOVE.image());
removeButton.setToolTipText(Messages.EditSmaIntervalDialog_DeleteIntervalLabel);
removeButton.addListener(SWT.MouseUp, e ->
{
if (e.type == SWT.MouseUp)
{
IntervalSettings.IntervalSetting interval = (IntervalSettings.IntervalSetting)table.getStructuredSelection().getFirstElement();
table.remove(interval);
intervals.remove(interval.getInterval());
}
});

table.addSelectionChangedListener(event -> removeButton.setEnabled(event.getStructuredSelection().size() == 1));
}

private void updateTable(TableViewer table)
{
table.getTable().removeAll();

IntervalSettings.IntervalSetting[] temp = intervals.getAll();
Arrays.sort(temp, new IntervalSettings.SortIntervalSetting());
table.setInput(temp);
}

private void createIntervalColumn(TableViewer table)
{
TableViewerColumn colInterval = new TableViewerColumn(table, SWT.NONE);
colInterval.getColumn().setWidth(80);
colInterval.getColumn().setText(Messages.EditSmaIntervalDialog_IntervalLabel);
colInterval.setLabelProvider(new ColumnLabelProvider()
{
@Override
public String getText(Object element)
{
return String.valueOf(((IntervalSettings.IntervalSetting)element).getInterval());
}
});
}

private void createColorColumn(TableViewer table)
{
TableViewerColumn colColor = new TableViewerColumn(table, SWT.NONE);
colColor.getColumn().setWidth(160);
colColor.getColumn().setText(Messages.EditSmaIntervalDialog_ColorLabel);
colColor.setEditingSupport(new ColorEditingSupport(table));
colColor.setLabelProvider(new ColumnLabelProvider()
{
@Override
public String getText(Object element)
{
return null;
}

@Override
public String getToolTipText(Object element)
{
IntervalSettings.IntervalSetting i = (IntervalSettings.IntervalSetting) element;
return ColorConversion.toHex(i.getRGB());
}

@Override
public Color getBackground(Object element)
{
IntervalSettings.IntervalSetting i = (IntervalSettings.IntervalSetting) element;
return new Color(i.getRGB());
}
});
}

public IntervalSettings.IntervalSetting[] getIntervals()
{
return intervals.getAll();
}


private class ColorEditingSupport extends EditingSupport
{
private final TableViewer viewer;
private final CellEditor editor;

public ColorEditingSupport(TableViewer viewer)
{
super(viewer);
this.viewer = viewer;
this.editor = new ColorCellEditor(viewer.getTable());
}

@Override
protected CellEditor getCellEditor(Object element)
{
return editor;
}

@Override
protected boolean canEdit(Object element)
{
return true;
}

@Override
protected Object getValue(Object element)
{
return ((IntervalSettings.IntervalSetting) element).getRGB();
}

@Override
protected void setValue(Object element, Object userInputValue)
{
((IntervalSettings.IntervalSetting) element).setRGB((RGB)userInputValue);
viewer.update(element, null);
}

}

}
Expand Up @@ -726,6 +726,24 @@ DivvyDiaryMsgUploading = Uploading holdings to DivvyDiary

DivvyDiaryUploadSuccessfulMsg = Completed upload to DivvyDiary successfully

EditSmaIntervalDialog_AddIntervalLabel = Add Interval

EditSmaIntervalDialog_ColorLabel = Color

EditSmaIntervalDialog_DeleteIntervalLabel = Remove Interval

EditSmaIntervalDialog_InsertIntervalLabel = Enter interval

EditSmaIntervalDialog_IntervalLabel = Interval

EditSmaIntervalDialog_ManageLabel = Manage...

EditSmaIntervalDialog_ValidateEnterNumberLabel = Please enter an integer.

EditSmaIntervalDialog_ValidateEnterPositiveNumberLabel = Please enter positive number.

EditSmaIntervalDialog_ValidateIntervalAlreadyExistsLabel = This interval already exists.

EditWizardAttributesTitle = Additional Attributes

EditWizardLatestQuoteFeedTitle = Latest Quote
Expand Down Expand Up @@ -1054,6 +1072,8 @@ LabelChartDetailMovingAverageEMA = Exponential Moving Average (EMA)

LabelChartDetailMovingAverageSMA = Simple Moving Average (SMA)

LabelChartDetailMovingAverage_Xdays = {0} days

LabelChartDetailMovingAverage_100days = 100 days

LabelChartDetailMovingAverage_200days = 200 days
Expand Down
Expand Up @@ -719,6 +719,24 @@ DivvyDiaryMsgUploading = Best\u00E4nden auf DivvyDiary hochladen

DivvyDiaryUploadSuccessfulMsg = Der Upload zu DivvyDiary wurde erfolgreich abgeschlossen

EditSmaIntervalDialog_AddIntervalLabel = Interval hinzuf�gen

EditSmaIntervalDialog_ColorLabel = Farbe

EditSmaIntervalDialog_DeleteIntervalLabel = Interval entfernen

EditSmaIntervalDialog_InsertIntervalLabel = Interval eingeben

EditSmaIntervalDialog_IntervalLabel = Interval

EditSmaIntervalDialog_ManageLabel = Verwalten...

EditSmaIntervalDialog_ValidateEnterNumberLabel = Bitte eine Ganzzahl eingeben.

EditSmaIntervalDialog_ValidateEnterPositiveNumberLabel = Bitte positive Zahl eingeben.

EditSmaIntervalDialog_ValidateIntervalAlreadyExistsLabel = Dieses Interval gibt es bereits.

EditWizardAttributesTitle = Weitere Attribute

EditWizardLatestQuoteFeedTitle = Aktueller Kurs
Expand Down Expand Up @@ -1047,6 +1065,8 @@ LabelChartDetailMovingAverageEMA = Exponentieller gleitender Durchschnitt (EMA)

LabelChartDetailMovingAverageSMA = Einfacher gleitender Durchschnitt (SMA)

LabelChartDetailMovingAverage_Xdays = {0} Tage

LabelChartDetailMovingAverage_100days = 100 Tage

LabelChartDetailMovingAverage_200days = 200 Tage
Expand Down
Expand Up @@ -719,6 +719,24 @@ DivvyDiaryMsgUploading = Subir participaciones a DivvyDiary

DivvyDiaryUploadSuccessfulMsg = Carga completada a DivvyDiary con \u00E9xito

EditSmaIntervalDialog_AddIntervalLabel = Agregar intervalo

EditSmaIntervalDialog_ColorLabel = Color

EditSmaIntervalDialog_DeleteIntervalLabel = Eliminar el intervalo

EditSmaIntervalDialog_InsertIntervalLabel = Introduzca el intervalo

EditSmaIntervalDialog_IntervalLabel = Intervalo

EditSmaIntervalDialog_ManageLabel = Gestionar...

EditSmaIntervalDialog_ValidateEnterNumberLabel = Por favor, introduzca un n\u00FAmero entero.

EditSmaIntervalDialog_ValidateEnterPositiveNumberLabel = Por favor, introduzca un n\u00FAmero positivo.

EditSmaIntervalDialog_ValidateIntervalAlreadyExistsLabel = Este intervalo ya existe.

EditWizardAttributesTitle = Atributos adicionales

EditWizardLatestQuoteFeedTitle = \u00DAltima cotizaci\u00F3n
Expand Down Expand Up @@ -1047,6 +1065,8 @@ LabelChartDetailMovingAverageEMA = Media m\u00F3vil exponencial (EMA)

LabelChartDetailMovingAverageSMA = Media m\u00F3vil simple (SMA)

LabelChartDetailMovingAverage_Xdays = {0} d\u00EDas

LabelChartDetailMovingAverage_100days = 100 d\u00EDas

LabelChartDetailMovingAverage_200days = 200 d\u00EDas
Expand Down