📄 indicatorplot.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.text.NumberFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Iterator;import java.util.List;import net.sourceforge.eclipsetrader.charts.events.PlotEvent;import net.sourceforge.eclipsetrader.charts.events.PlotListener;import net.sourceforge.eclipsetrader.charts.internal.PixelTools;import net.sourceforge.eclipsetrader.core.db.Bar;import net.sourceforge.eclipsetrader.core.db.BarData;import org.eclipse.swt.SWT;import org.eclipse.swt.events.ControlEvent;import org.eclipse.swt.events.ControlListener;import org.eclipse.swt.events.DisposeEvent;import org.eclipse.swt.events.DisposeListener;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.graphics.Color;import org.eclipse.swt.graphics.GC;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.graphics.Point;import org.eclipse.swt.graphics.Rectangle;import org.eclipse.swt.widgets.Canvas;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Event;/** */public class IndicatorPlot extends Canvas implements ControlListener, DisposeListener, PaintListener{ protected Image image; private BarData barData = new BarData(); private int gridWidth = 5; private int marginWidth = 2; private Point plotLocation = new Point(0, 0); private List plotListeners = new ArrayList(); private Scaler scaler; private boolean autoScale = false; private NumberFormat nf = NumberFormat.getInstance(); private Color gridColor = new Color(null, 224, 224, 224); private Color selectionMarks = new Color(null, 0, 0, 255); private List indicators = new ArrayList(); private List objects = new ArrayList(); private Indicator selection; private ObjectPlugin objectSelection; private boolean needRepaint = true; private double marketValue = -1; public IndicatorPlot(Composite parent, int style) { super(parent, style|SWT.DOUBLE_BUFFERED|SWT.NO_BACKGROUND); addControlListener(this); addDisposeListener(this); addPaintListener(this); } public void setGridColor(Color newGridColor) { if (gridColor != null) gridColor.dispose(); gridColor = new Color(newGridColor.getDevice(), newGridColor.getRGB()); } public BarData getBarData() { return barData; } public void setBarData(BarData barData) { if (barData == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); this.barData = barData; } public void addIndicator(Indicator indicator) { indicators.add(indicator); } public void removeIndicator(Indicator indicator) { indicators.remove(indicator); if (indicator == selection) selection = null; } public void clearIndicators() { indicators.clear(); selection = null; } public List getIndicators() { return Collections.unmodifiableList(indicators); } public void addObject(ObjectPlugin object) { objects.add(object); } public void removeObject(ObjectPlugin object) { objects.remove(object); if (object == objectSelection) objectSelection = null; } public List getObjects() { return objects; } public Point getPlotLocation() { return new Point(plotLocation.x, plotLocation.y); } public void setPlotLocation(Point location) { if (location == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); setPlotBounds(location.x, location.y, 0, 0, true, false); } public void setPlotLocation(int x, int y) { setPlotBounds(x, y, 0, 0, true, false); } public Point getPlotSize() { Rectangle rect = new Rectangle(0, 0, 0, getSize().y); if (image != null && !image.isDisposed()) rect = image.getBounds(); return new Point(rect.width, rect.height); } public void setPlotSize(int width, int height) { setPlotBounds(0, 0, width, height, false, true); } public Rectangle getPlotBounds() { Rectangle rect = new Rectangle(0, 0, 0, getSize().y); if (image != null && !image.isDisposed()) rect = image.getBounds(); rect.x = plotLocation.x; rect.y = plotLocation.y; return rect; } public void setPlotBounds(int x, int y, int width, int height) { setPlotBounds(x, y, width, height, true, true); } public void setPlotBounds(Rectangle rectangle) { setPlotBounds(rectangle.x, rectangle.y, rectangle.width, rectangle.height, true, true); } void setPlotBounds(int x, int y, int width, int height, boolean move, boolean resize) { Rectangle rect = new Rectangle(0, 0, 0, 0); if (image != null && !image.isDisposed()) rect = image.getBounds(); rect.x = plotLocation.x; rect.y = plotLocation.y; height = getSize().y;// if (width == 0 || height == 0)// resize = false; if (resize && (rect.width != width || rect.height != height)) { if (image != null && !image.isDisposed()) image.dispose(); if (width != 0 && height != 0) { image = new Image(getDisplay(), width, height); if (scaler != null) scaler.set(getSize().y); GC gc = new GC(image); draw(gc); gc.dispose(); } Event event = new Event(); event.type = SWT.Resize; event.display = getDisplay(); event.widget = this; for (Iterator iter = plotListeners.iterator(); iter.hasNext(); ) ((PlotListener)iter.next()).plotResized(new PlotEvent(event)); redraw(); } if (move && (rect.x != x || rect.y != y)) { this.plotLocation = new Point(x, y); redraw(); } } public void addPlotListener(PlotListener listener) { if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); plotListeners.add(listener); } public void removePlotListener(PlotListener listener) { if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); plotListeners.remove(listener); } public int getGridWidth() { return gridWidth; } public void setGridWidth(int columnWidth) { this.gridWidth = columnWidth; } public int getMarginWidth() { return marginWidth; } public void setMarginWidth(int marginWidth) { this.marginWidth = marginWidth; } public void setScaler(Scaler scaler) { this.scaler = scaler; } /* (non-Javadoc) * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent) */ public void controlMoved(ControlEvent e) { } /* (non-Javadoc) * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent) */ public void controlResized(ControlEvent e) { if (image != null && !image.isDisposed()) { Rectangle rect = image.getBounds(); if (getSize().y != rect.height && getSize().y != 0) { image.dispose(); image = new Image(getDisplay(), rect.width, getSize().y); if (scaler != null) scaler.set(getSize().y); GC gc = new GC(image); draw(gc); gc.dispose(); } } else if (image == null) { Rectangle rect = getBounds(); if (rect.height != 0 && rect.width != 0) { image = new Image(getDisplay(), rect.width, rect.height); if (scaler != null) scaler.set(rect.height); GC gc = new GC(image); draw(gc); gc.dispose(); } } } public void deselectAll() { this.selection = null; this.objectSelection = null; } public Indicator getSelection() { return selection; } public void setSelection(Indicator selection) { this.selection = selection; this.objectSelection = null; } /** * Returns a possibly empty array of all chart elements at the given position. * * @param point - position * @return an array of ObjectPlugin and Indicator objects */ public Object[] getElementsAt(Point point) { List selection = new ArrayList(); selection.addAll(Arrays.asList(getIndicatorAt(point))); selection.addAll(Arrays.asList(getObjectAt(point.x, point.y))); return selection.toArray(); } /** * Returns a possibly empty array of all chart indicators at the given position. * * @param point - position * @return an array of indicators */ public Indicator[] getIndicatorAt(Point point) { List selection = new ArrayList(); if (scaler != null && barData.size() != 0) { for (int ii = indicators.size() - 1; ii >= 0; ii--) { Indicator indicator = (Indicator)indicators.get(ii);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -