📄 plot.java
字号:
/* * Copyright (c) 2004-2006 Marco Maccaferri and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Marco Maccaferri - initial API and implementation */package net.sourceforge.eclipsetrader.charts;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import net.sourceforge.eclipsetrader.charts.events.PlotMouseEvent;import net.sourceforge.eclipsetrader.charts.events.PlotMouseListener;import net.sourceforge.eclipsetrader.charts.events.PlotSelectionEvent;import net.sourceforge.eclipsetrader.charts.events.PlotSelectionListener;import net.sourceforge.eclipsetrader.charts.views.ChartView;import net.sourceforge.eclipsetrader.core.db.Bar;import net.sourceforge.eclipsetrader.core.db.BarData;import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.swt.SWT;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.MouseListener;import org.eclipse.swt.events.MouseMoveListener;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.Cursor;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Event;import org.eclipse.ui.PlatformUI;import org.eclipse.ui.themes.ITheme;import org.eclipse.ui.themes.IThemeManager;/** */public class Plot extends Composite implements MouseListener, MouseMoveListener{ private Summary summary; private DateSummaryItem dateSummary; private IndicatorPlot indicatorPlot; private ScalePlot scalePlot; private Color selectionColor = new Color(null, 224, 0, 0); private int scaleWidth = 75; private Scaler scaler = new Scaler(); private DatePlot datePlot; private boolean buttonDown = false; private List plotMouseListeners = new ArrayList(); private List selectionListeners = new ArrayList(); private int drawCount = 0; private Cursor crossCursor = new Cursor(null, SWT.CURSOR_CROSS); private Cursor handCursor = new Cursor(null, SWT.CURSOR_HAND); private Cursor currentCursor; private boolean selection = false; ITheme theme; IPropertyChangeListener themeChangeListener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { String property = event.getProperty(); if (property.equals(IThemeManager.CHANGE_CURRENT_THEME)) { IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); if (theme != null) theme.removePropertyChangeListener(themeChangeListener); theme = themeManager.getCurrentTheme(); theme.addPropertyChangeListener(themeChangeListener); summary.setBackground(theme.getColorRegistry().get(property)); summary.setForeground(theme.getColorRegistry().get(property)); indicatorPlot.setBackground(theme.getColorRegistry().get(ChartView.THEME_CHART_BACKGROUND)); scalePlot.setForeground(theme.getColorRegistry().get(ChartView.THEME_SCALE_FOREGROUND)); scalePlot.setBackground(theme.getColorRegistry().get(ChartView.THEME_SCALE_BACKGROUND)); scalePlot.setSeparatorColor(theme.getColorRegistry().get(ChartView.THEME_SCALE_FOREGROUND)); summary.layout(); indicatorPlot.redrawAll(); scalePlot.redrawAll(); } else if (property.equals(ChartView.THEME_CHART_BACKGROUND)) { indicatorPlot.setBackground(theme.getColorRegistry().get(property)); indicatorPlot.redrawAll(); } else if (property.equals(ChartView.THEME_CHART_GRID_COLOR)) { indicatorPlot.setGridColor(theme.getColorRegistry().get(property)); indicatorPlot.redrawAll(); } else if (property.equals(ChartView.THEME_SCALE_FOREGROUND)) { scalePlot.setForeground(theme.getColorRegistry().get(property)); scalePlot.setSeparatorColor(theme.getColorRegistry().get(property)); scalePlot.redrawAll(); } else if (property.equals(ChartView.THEME_SCALE_BACKGROUND)) { scalePlot.setBackground(theme.getColorRegistry().get(property)); scalePlot.redrawAll(); } else if (property.equals(ChartView.THEME_SUMMARY_BACKGROUND)) { summary.setBackground(theme.getColorRegistry().get(property)); summary.layout(); } else if (property.equals(ChartView.THEME_SUMMARY_FOREGROUND)) { summary.setForeground(theme.getColorRegistry().get(property)); summary.layout(); } } }; DisposeListener disposeListener = new DisposeListener() { public void widgetDisposed(DisposeEvent e) { IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); themeManager.removePropertyChangeListener(themeChangeListener); if (theme != null) theme.removePropertyChangeListener(themeChangeListener); if (crossCursor != null) crossCursor.dispose(); if (handCursor != null) handCursor.dispose(); } }; public Plot(Composite parent, int style) { super(parent, style); addDisposeListener(disposeListener); IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager(); themeManager.addPropertyChangeListener(themeChangeListener); theme = themeManager.getCurrentTheme(); theme.addPropertyChangeListener(themeChangeListener); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; gridLayout.marginWidth = gridLayout.marginHeight = 0; gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0; setLayout(gridLayout); summary = new Summary(this, SWT.NONE); summary.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1)); summary.setForeground(theme.getColorRegistry().get(ChartView.THEME_SUMMARY_FOREGROUND)); summary.setBackground(theme.getColorRegistry().get(ChartView.THEME_SUMMARY_BACKGROUND)); dateSummary = new DateSummaryItem(summary, SWT.NONE); indicatorPlot = new IndicatorPlot(this, SWT.NONE); indicatorPlot.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 1, 1)); indicatorPlot.setBackground(theme.getColorRegistry().get(ChartView.THEME_CHART_BACKGROUND)); indicatorPlot.setGridColor(theme.getColorRegistry().get(ChartView.THEME_CHART_GRID_COLOR)); indicatorPlot.setScaler(scaler); indicatorPlot.addMouseListener(this); indicatorPlot.addMouseMoveListener(this); scalePlot = new ScalePlot(this, SWT.NONE); GridData gridData = new GridData(); gridData.widthHint = scaleWidth; gridData.grabExcessVerticalSpace = true; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; scalePlot.setLayoutData(gridData); scalePlot.setForeground(theme.getColorRegistry().get(ChartView.THEME_SCALE_FOREGROUND)); scalePlot.setBackground(theme.getColorRegistry().get(ChartView.THEME_SCALE_BACKGROUND)); scalePlot.setSeparatorColor(theme.getColorRegistry().get(ChartView.THEME_SCALE_FOREGROUND)); scalePlot.setScaler(scaler); pack(); } public boolean getSelection() { return selection; } public void setSelection(boolean selection) { this.selection = selection; if (selection) scalePlot.setSeparatorColor(selectionColor); else scalePlot.setSeparatorColor(theme.getColorRegistry().get(ChartView.THEME_SCALE_FOREGROUND)); } public void addIndicator(Indicator indicator) { double high = scaler.getHigh(); double low = scaler.getLow(); for (Iterator iter = indicator.getLines().iterator(); iter.hasNext(); ) { PlotLine line = (PlotLine)iter.next(); if ((line.getLabel() != null || line.getType() == PlotLine.BAR || line.getType() == PlotLine.CANDLE) && line.getSize() != 0) { if (line.getType() == PlotLine.BAR || line.getType() == PlotLine.CANDLE) { BarSummaryItem item = new BarSummaryItem(summary, SWT.NONE); item.setForeground(line.getColor()); line.setData(item); } else { NumberSummaryItem item = new NumberSummaryItem(summary, SWT.NONE); item.setText(line.getLabel().toUpperCase()); item.setForeground(line.getColor()); line.setData(item); } } if (!line.getScaleFlag()) { if (line.getHigh() > high) high = line.getHigh(); if (line.getLow() < low) low = line.getLow(); } } scaler.set(high, low); indicatorPlot.addIndicator(indicator); indicatorPlot.setBarData(datePlot.getBarData()); if (drawCount <= 0) { indicatorPlot.redrawAll(); scalePlot.redrawAll(); updateSummary(); } } public void removeIndicator(Indicator indicator) { Control[] childs = summary.getChildren(); for (int i = 1; i < childs.length; i++) childs[i].dispose(); indicatorPlot.removeIndicator(indicator); double high = -99999999; double low = 99999999; List list = new ArrayList(); for (Iterator iter = indicatorPlot.getIndicators().iterator(); iter.hasNext(); ) list.addAll(((Indicator)iter.next()).getLines()); for (Iterator iter = list.iterator(); iter.hasNext(); ) { PlotLine line = (PlotLine)iter.next(); if (line.getType() == PlotLine.BAR || line.getType() == PlotLine.CANDLE) { BarSummaryItem item = new BarSummaryItem(summary, SWT.NONE); item.setForeground(line.getColor()); line.setData(item); } else if (line.getLabel() != null && line.getSize() != 0) { NumberSummaryItem item = new NumberSummaryItem(summary, SWT.NONE); item.setText(line.getLabel().toUpperCase()); item.setForeground(line.getColor()); line.setData(item); } else line.setData(null); if (line.getHigh() > high) high = line.getHigh(); if (line.getLow() < low) low = line.getLow(); } scaler.set(high, low); if (drawCount <= 0) { indicatorPlot.redrawAll(); scalePlot.redrawAll(); updateSummary(); } } public void clearIndicators() { for (Iterator iter2 = indicatorPlot.getIndicators().iterator(); iter2.hasNext(); ) { Indicator indicator = (Indicator)iter2.next(); for (Iterator iter = indicator.getLines().iterator(); iter.hasNext(); ) { PlotLine line = (PlotLine)iter.next(); if (line.getData() != null) { ((SummaryItem)line.getData()).dispose(); line.setData(null); } } } indicatorPlot.clearIndicators(); scaler.set(-99999999, 99999999); if (drawCount <= 0) { indicatorPlot.redrawAll(); scalePlot.redrawAll(); updateSummary(); } } public void clearObjects() { indicatorPlot.getObjects().clear(); indicatorPlot.setObjectSelection(null); } /* (non-Javadoc) * @see org.eclipse.swt.widgets.Control#setRedraw(boolean) */ public void setRedraw(boolean redraw) { super.setRedraw(redraw); if (redraw) { if (--drawCount == 0) { indicatorPlot.redrawAll(); scalePlot.redrawAll(); updateSummary(); } } else drawCount++; } public void updateSummary(int x) { int index = (x - getIndicatorPlot().getPlotLocation().x - indicatorPlot.getMarginWidth()) / indicatorPlot.getGridWidth(); if (index >= 0 && index < datePlot.getBarData().size()) dateSummary.setData(datePlot.getBarData().getDate(index), datePlot.getInterval() < BarData.INTERVAL_DAILY); else dateSummary.setData(null, datePlot.getInterval() < BarData.INTERVAL_DAILY); List list = indicatorPlot.getIndicators(); for (int i = 0; i < list.size(); i++) { Indicator indicator = (Indicator)list.get(i); for (Iterator iter = indicator.getLines().iterator(); iter.hasNext(); ) { PlotLine line = (PlotLine)iter.next(); int ofs = index - (datePlot.getBarData().size() - line.getSize()); if (line.getData() != null) { if (line.getType() == PlotLine.BAR || line.getType() == PlotLine.CANDLE) { BarSummaryItem item = (BarSummaryItem)line.getData(); if (ofs >= 0 && ofs < line.getSize()) { if (ofs > 0) item.setData(line.getBar(ofs), line.getBar(ofs - 1)); else item.setData(line.getBar(ofs)); } else item.setData(null); } else { NumberSummaryItem item = (NumberSummaryItem)line.getData(); if (ofs >= 0 && ofs < line.getSize()) item.setData(line.getDouble(ofs)); else item.setData(null); } } } } summary.layout(); } public void updateSummary() { int index = datePlot.getBarData().size() - 1; if (index >= 0) dateSummary.setData(datePlot.getBarData().getDate(index), datePlot.getInterval() < BarData.INTERVAL_DAILY); else dateSummary.setData(null, datePlot.getInterval() < BarData.INTERVAL_DAILY); List list = indicatorPlot.getIndicators(); for (int i = 0; i < list.size(); i++) { Indicator indicator = (Indicator)list.get(i); for (Iterator iter = indicator.getLines().iterator(); iter.hasNext(); ) { PlotLine line = (PlotLine)iter.next(); if (line.getData() != null) { if (line.getType() == PlotLine.BAR || line.getType() == PlotLine.CANDLE) { BarSummaryItem item = (BarSummaryItem)line.getData(); if (line.getSize() > 1) item.setData(line.getBar(line.getSize() - 1), line.getBar(line.getSize() - 2)); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -