From: luukkainen Date: Wed, 16 Jan 2013 10:57:37 +0000 (+0000) Subject: chart component moved to jfreechart plug-in. X-Git-Tag: simantics-1.10.1~73 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=0c37e404b060e53f54243f4f699e8aa975623e86;p=simantics%2Fsysdyn.git chart component moved to jfreechart plug-in. refs #3988 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26621 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java deleted file mode 100644 index a1b8cd46..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java +++ /dev/null @@ -1,77 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.ui.trend; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.dnd.DND; -import org.eclipse.swt.dnd.DropTargetAdapter; -import org.eclipse.swt.dnd.DropTargetEvent; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Display; -import org.simantics.db.Resource; -import org.simantics.ui.utils.AdaptionUtils; - -/** - * Drop target for dropping charts in chart panel - * @author Teemu Lempinen - * - */ -public class ChartDropTarget extends DropTargetAdapter { - - private Composite separator; - private ChartPanelElement element; - private Display display; - private ChartPanel panel; - - public ChartDropTarget(Composite separator, ChartPanelElement element, ChartPanel panel) { - this.separator = separator; - this.display = separator.getDisplay(); - this.element = element; - this.panel = panel; - } - - - - /** - * Display effect on the composite when drag entered - */ - @Override - public void dragEnter(DropTargetEvent event) { - if ((event.operations & DND.DROP_COPY) != 0) { - event.detail = DND.DROP_COPY; - } else if ((event.operations & DND.DROP_MOVE) != 0) { - event.detail = DND.DROP_MOVE; - } else { - event.detail = DND.DROP_NONE; - } - separator.setBackground(display.getSystemColor(SWT.COLOR_DARK_GRAY)); - } - - /** - * Revert effect when drag leaves - */ - @Override - public void dragLeave(DropTargetEvent event) { - separator.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); - } - - /** - * Drop the data to chart panel - */ - @Override - public void drop(DropTargetEvent event) { - Resource chartResource = AdaptionUtils.adaptToSingle(event.data, Resource.class); - if(chartResource != null) - panel.addChart(chartResource, element); - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java deleted file mode 100644 index 18021278..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java +++ /dev/null @@ -1,471 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.ui.trend; - -import java.util.ArrayList; -import java.util.LinkedHashMap; - -import org.eclipse.jface.dialogs.IDialogSettings; -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.ScrolledComposite; -import org.eclipse.swt.dnd.DND; -import org.eclipse.swt.dnd.DropTarget; -import org.eclipse.swt.dnd.DropTargetAdapter; -import org.eclipse.swt.dnd.DropTargetEvent; -import org.eclipse.swt.dnd.Transfer; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.graphics.Rectangle; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.ui.IMemento; -import org.eclipse.ui.IViewSite; -import org.eclipse.ui.PartInitException; -import org.eclipse.ui.part.ViewPart; -import org.simantics.db.AsyncReadGraph; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.procedure.AsyncListener; -import org.simantics.db.request.Read; -import org.simantics.sysdyn.JFreeChartResource; -import org.simantics.sysdyn.ui.Activator; -import org.simantics.ui.SimanticsUI; -import org.simantics.ui.dnd.LocalObjectTransfer; -import org.simantics.utils.RunnableWithObject; - -/** - * Chart panel displays multiple charts in a single view. The view can be oriented - * vertically or horizontally, the default is vertical. Charts can be added, removed - * minimized or expanded. The order of the charts can be changed by dragging the charts. - * - * @author Teemu Lempinen - * - */ -public class ChartPanel extends ViewPart { - - private Composite body; - private ScrolledComposite sc; - - private IDialogSettings settings; - private LinkedHashMap charts; - private ArrayList minimizedResources; - - private ArrayList chartElements; - - public static final String CHART_PANEL_SETTINGS = "CHART_PANEL_SETTINGS"; - public static final String CHARTS = "CHART_PANEL_CHARTS"; - public static final String MINIMIZED_CHARTS = "CHART_PANEL_MINIMIZED_CHARTS"; - public static final String CHART_PANEL_ORIENTATION = "CHART_PANEL_ORIENTATION"; - - public static final String CHART_PANEL_VERTICAL = "CHART_PANEL_ORIENTATION_VERTICAL"; - public static final String CHART_PANEL_HORIZONTAL = "CHART_PANEL_ORIENTATION_HORIZONTAL"; - - private boolean vertical = true; - - /** - * Initialize the view. Load charts that have previously been open (if there are any). - */ - @Override - public void init(IViewSite site, IMemento memento) throws PartInitException { - super.init(site, memento); - - minimizedResources = new ArrayList(); - - settings = Activator.getDefault().getDialogSettings().getSection(CHART_PANEL_SETTINGS); - - // Initialize settings if there are no settings - if (settings == null) { - settings = Activator.getDefault().getDialogSettings().addNewSection(CHART_PANEL_SETTINGS); - } - - if(settings.getArray(CHARTS) == null) { - String[] chartUris = new String[] {}; - settings.put(CHARTS, chartUris); - } - - if(settings.getArray(MINIMIZED_CHARTS) == null) { - String[] minimizedChartUris = new String[] {}; - settings.put(MINIMIZED_CHARTS, minimizedChartUris); - } - - // initialize chart lists - charts = new LinkedHashMap(); - - // add chart resources to chart lists from settings - try { - SimanticsUI.getSession().syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); - Resource chart = null; - String[] chartURIs = settings.getArray(CHARTS); - for(String uri : chartURIs) { - chart = graph.getPossibleResource(uri); - if(chart != null && graph.isInstanceOf(chart, jfree.Chart)) { - charts.put(chart, null); - setChartExistingListener(chart); - } - } - - String[] minimizedUris = settings.getArray(MINIMIZED_CHARTS); - for(String uri : minimizedUris) { - chart = graph.getPossibleResource(uri); - if(chart != null && graph.isInstanceOf(chart, jfree.Chart)) { - minimizedResources.add(chart); - } - } - } - }); - } catch (DatabaseException e1) { - e1.printStackTrace(); - } - - // set the orientation of the panel - String orientation = settings.get(CHART_PANEL_ORIENTATION); - if(CHART_PANEL_VERTICAL.equals(orientation)) - this.vertical = true; - else if(CHART_PANEL_HORIZONTAL.equals(orientation)) - this.vertical = false; - - } - - /** - * Create a scrolled composite that will contain all the charts, then call the actual - * content creator. - */ - @Override - public void createPartControl(Composite parent) { - sc = new ScrolledComposite(parent, SWT.NONE | SWT.H_SCROLL | SWT.V_SCROLL); - GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(sc); - GridDataFactory.fillDefaults().grab(true, true).applyTo(sc); - sc.setExpandHorizontal(true); - sc.setExpandVertical(true); - sc.getVerticalBar().setIncrement(sc.getVerticalBar().getIncrement()*3); - sc.getHorizontalBar().setIncrement(sc.getHorizontalBar().getIncrement()*3); - - body = new Composite(sc, SWT.NONE); - GridLayoutFactory.fillDefaults().margins(3, 0).spacing(0, 0).applyTo(body); - GridDataFactory.fillDefaults().grab(true, true).applyTo(body); - - sc.setContent(body); - createContents(); - - setupDropTarget(); - - } - - /** - * Creates the contents of this chart panel. - * Removes all old contents before creating new content - */ - private void createContents() { - chartElements = new ArrayList(); - - for(Control child : body.getChildren()) { - child.dispose(); - } - - // Set the initial layout - ElementContainer elementHolder; - for(Resource e : charts.keySet()) { - elementHolder = new ElementContainer(body, SWT.NONE); - elementHolder.setBackground(new Color(elementHolder.getDisplay(), 255, 0, 0)); - ChartPanelElement element = new ChartPanelElement(elementHolder, this, e, SWT.NONE); - elementHolder.setLayout(GridLayoutFactory.copyLayout((GridLayout)element.getLayout())); - chartElements.add(element); - charts.put(e, element); - if(minimizedResources.contains(e)) { - element.toggleMinimize(); - } - } - - elementHolder = new ElementContainer(body, SWT.NONE); - elementHolder.setBackground(new Color(elementHolder.getDisplay(), 0, 255, 0)); - ChartPanelElement element = new ChartPanelElement(elementHolder, this, null, SWT.NONE); // Last element is empty -> only the separator - elementHolder.setLayout(GridLayoutFactory.copyLayout((GridLayout)element.getLayout())); - chartElements.add(element); - - layout(); - saveState(); - - } - - /** - * Lays out this panel (the body composite) - */ - public void layout() { - if(vertical) { - GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(body); - GridDataFactory.fillDefaults().grab(true, true).applyTo(body); - } else { - // Need to calculate horizontal elements for gridLayout - int chartPanels = chartElements.size(); - GridLayoutFactory.fillDefaults().spacing(0, 0).numColumns(chartPanels).applyTo(body); - GridDataFactory.fillDefaults().grab(true, true).applyTo(body); - } - body.layout(); - sc.setMinSize(body.computeSize(SWT.DEFAULT, SWT.DEFAULT)); - } - - @Override - public void setFocus() { - if(!sc.isDisposed()) - sc.setFocus(); - } - - @Override - public void saveState(IMemento memento) { - super.saveState(memento); - saveState(); - } - - /** - * Save the current state of the view to IDialogSettings - */ - public void saveState() { - try { - SimanticsUI.getSession().syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - if (settings != null) { - String[] uris = new String[chartElements.size() - 1]; - ArrayList minimized = new ArrayList(); - minimizedResources.clear(); - for(int i = 0; i < uris.length; i++) { - ChartPanelElement e = chartElements.get(i); - Resource r = e.getResource(); - if(r != null) { - uris[i] = graph.getURI(r); - if(e.isMinimized()) { - minimized.add(uris[i]); - minimizedResources.add(r); - } - } else { - uris[i] = ""; - } - } - settings.put(CHARTS, uris); - if(!minimized.isEmpty()) - settings.put(MINIMIZED_CHARTS, minimized.toArray(new String[minimized.size()])); - else - settings.put(MINIMIZED_CHARTS, new String[0]); - - if(vertical) - settings.put(CHART_PANEL_ORIENTATION, CHART_PANEL_VERTICAL); - else - settings.put(CHART_PANEL_ORIENTATION, CHART_PANEL_HORIZONTAL); - } - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - } - - /** - * Set the orientation for this chart panel. - * - * @param orientation Orientation (ChartPanel.CHART_PANEL_VERTICAL or ChartPanel.CHART_PANEL_HORIZONTAL) - */ - public void setOrientation(String orientation) { - if(CHART_PANEL_VERTICAL.equals(orientation)) - this.vertical = true; - else { - this.vertical = false; - } - createContents(); - } - - /** - * Removes a chart from this panel - * - * @param chart The chart to be removed - */ - public void removeChart(Resource chart) { - ChartPanelElement element = charts.get(chart); - chartElements.remove(element); - element.getParent().dispose(); - charts.remove(chart); - minimizedResources.remove(chart); - saveState(); - layout(); - } - - /** - * Sets up drag-scrolling for a scrolled composite - * - * @param control - */ - protected void setupDropTarget() { - DropTarget target = new DropTarget(sc, DND.DROP_MOVE); - target.setTransfer(new Transfer[] { LocalObjectTransfer.getTransfer() }); - - target.addDropListener(new DropTargetAdapter() { - - private int activeMargin = 20; - private int moveAmount = 1; - - @Override - public void dragOver(DropTargetEvent event) { - Point original = sc.getOrigin(); - Point origin = sc.getOrigin(); - Point pointer = sc.toControl(event.x, event.y); - Rectangle bounds = sc.getBounds(); - - if(pointer.y < activeMargin) - origin.y = origin.y - moveAmount; - else if(bounds.height - pointer.y < activeMargin) - origin.y = origin.y + moveAmount; - if(pointer.x < activeMargin) - origin.x = origin.x - moveAmount; - else if(bounds.width - pointer.x < activeMargin) - origin.x = origin.x + moveAmount; - - if(origin != original) { - sc.setOrigin (origin.x, origin.y); - sc.redraw(); - } - } - - }); - - DropTarget target2 = new DropTarget(body, DND.DROP_COPY | DND.DROP_MOVE); - target2.setTransfer(new Transfer[] { LocalObjectTransfer.getTransfer() }); - target2.addDropListener(new ChartDropTarget(body, null, this)); - - } - - /** - * Is the panel vertically oriented - * @return Is the panel vertically oriented - */ - public boolean isVertical() { - return vertical; - } - - /** - * Adds chart after given element. If element == null, adds chart to the top. - * - * @param element To which place the chart will be placed. (null allowed) - */ - public void addChart(Resource chartResource, ChartPanelElement element) { - addChart(chartResource, element, true); - } - - /** - * Adds chart after given element. If element == null, adds chart to the top. - * - * @param element To which place the chart will be placed. (null allowed) - * @param layout refresh layout. use with vertical layout. - */ - public void addChart(Resource chartResource, ChartPanelElement element, boolean layout) { - if(element == null) - element = chartElements.get(chartElements.size() - 1); - int index = chartElements.indexOf(element); - if(index >= 0) { - ChartPanelElement e = chartElements.get(index); - ChartPanelElement newElement; - - if(charts.containsKey(chartResource)) { - // Old element being moved to a new place - newElement = charts.get(chartResource); - int oldIndex = chartElements.indexOf(newElement); - if(newElement.equals(element) || oldIndex == index - 1) - return; // Not moving anywhere, do nothing - Composite oldParent = newElement.getParent(); - newElement.setParent(e.getParent()); - oldParent.dispose(); - if(oldIndex < index) - index--; - chartElements.remove(newElement); - } else { - newElement = new ChartPanelElement(e.getParent(), this, chartResource, SWT.NONE); - } - - // Add a new chart element to the location of the old element - chartElements.add(index, newElement); - charts.put(chartResource, newElement); - - ElementContainer elementHolder; - // Move elements back after index - for(int i = index + 1 /*indexes after the new element*/; i < chartElements.size(); i++) { - e = chartElements.get(i); - if(i == chartElements.size() - 1) { - // last element (the empty element) element to a new container - elementHolder = new ElementContainer(body, SWT.NONE); - elementHolder.setBackground(new Color(elementHolder.getDisplay(), 0, 0, 255)); - elementHolder.setLayout(GridLayoutFactory.copyLayout((GridLayout)e.getLayout())); - e.setParent(elementHolder); - } else { - // element to the next elements container - elementHolder = (ElementContainer)chartElements.get(i + 1).getParent(); - e.setParent(elementHolder); - } - } - - layout(); - saveState(); - } - } - - /** - * Set a listener to listen if the chart resource has been removed. - * If the resource has been removed, close also the chart element in this panel. - * - * @param chart Listened chart resource - */ - private void setChartExistingListener(final Resource chart) { - SimanticsUI.getSession().asyncRequest(new Read() { - - @Override - public Boolean perform(ReadGraph graph) throws DatabaseException { - return graph.hasStatement(chart); - } - }, new AsyncListener() { - - boolean disposed = false; - - @Override - public void execute(AsyncReadGraph graph, Boolean result) { - if(result != null && result == false && body != null && !body.isDisposed()) { - body.getDisplay().asyncExec(new RunnableWithObject(chart){ - public void run() { - removeChart((Resource)getObject()); - } - }) ; - disposed = true; - } - } - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - throwable.printStackTrace(); - } - - @Override - public boolean isDisposed() { - return !disposed && !charts.containsKey(chart); - } - }); - } - - public ArrayList getElements() { - return chartElements; - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java deleted file mode 100644 index 405294a9..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java +++ /dev/null @@ -1,193 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.ui.trend; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.swt.SWT; -import org.eclipse.swt.dnd.DND; -import org.eclipse.swt.dnd.DropTarget; -import org.eclipse.swt.dnd.Transfer; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.widgets.Composite; -import org.simantics.db.Resource; -import org.simantics.sysdyn.ui.trend.chart.ChartComposite; -import org.simantics.ui.dnd.LocalObjectTransfer; - -/** - * This class represents an expanded chart element in {@link ChartPanel}. It contains - * a header {@link ChartPanelHeader} and the actual chart. - * - * @author Teemu Lempinen - * - */ -public class ChartPanelElement extends Composite { - - public static int CHART_MINIMUM_WIDTH = 300; - public static int CHART_MINIMUM_HEIGHT = 200; - - private ChartPanel panel; - private ChartComposite chartComposite; - private Resource chartResource; - private boolean minimized = false; - - public Resource getResource() { - return chartResource; - } - - @Override - public Object getLayoutData () { - checkWidget(); - Object oldData = super.getLayoutData(); - if(oldData == null || !(oldData instanceof GridData)) { - oldData = GridDataFactory.fillDefaults().create(); - } - - int size = panel.getElements().size(); - GridData data = (GridData) oldData; - // Horizontal data - data.widthHint = CHART_MINIMUM_WIDTH; - if(getResource() == null && size == 1) { - data.grabExcessHorizontalSpace = true; - } else if(getResource() == null && !panel.isVertical()){ - data.grabExcessHorizontalSpace = false; - data.widthHint = SWT.DEFAULT; - } else if(minimized && !panel.isVertical()) { - data.grabExcessHorizontalSpace = false; - data.widthHint = SWT.DEFAULT; - } else { - data.grabExcessHorizontalSpace = true; - } - - // Vertical data - if(getResource() == null && size == 1) { - data.grabExcessVerticalSpace = true; - } else if(!minimized && getResource() != null) { - data.grabExcessVerticalSpace = true; - data.heightHint = CHART_MINIMUM_HEIGHT; - } else if(!panel.isVertical()){ - data.grabExcessVerticalSpace = true; - } else { - data.grabExcessVerticalSpace = false; - data.heightHint = SWT.DEFAULT; - } - return data; - } - - /** - * Creates an expanded chart panel element into parent composite. - * - * @param parent The parent composite where the chart element is created - * @param panel The {@link ChartPanel} containing the chart element - * @param name The name of the chart - * @param style The Style of the created chart element - */ - public ChartPanelElement(Composite parent, ChartPanel panel, Resource chartResource, int style) { - this(parent, panel, chartResource, false, style); - } - - /** - * Creates a chart panel element into parent composite. - * @param parent The parent composite where the chart element is created - * @param panel The {@link ChartPanel} containing the chart element - * @param name The name of the chart - * @param minimized Is the chart-section minimized - * @param style The Style of the created chart element - */ - public ChartPanelElement(Composite parent, ChartPanel panel, Resource chartResource, boolean minimized, int style) { - super(parent, style | SWT.NONE ); - - this.panel = panel; - this.chartResource = chartResource; - this.minimized = minimized; - - if(panel.isVertical() || getResource() == null) - GridLayoutFactory.fillDefaults().spacing(0,0).applyTo(this); - else - GridLayoutFactory.fillDefaults().numColumns(2).spacing(0,0).applyTo(this); - - GridDataFactory.fillDefaults().applyTo(this); - - // Separator for dropping other elements - ChartPanelSeparator separator = new ChartPanelSeparator(this, panel, this, SWT.NONE); - - if (chartResource != null) { - Composite c = new Composite(this, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, true).applyTo(c); - GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(c); - // Header - new ChartPanelHeader(c, this, chartResource, SWT.BORDER); - - // Chart - chartComposite = new ChartComposite(c, chartResource, SWT.BORDER); - } - - DropTarget target = new DropTarget(this, DND.DROP_COPY | DND.DROP_MOVE); - target.setTransfer(new Transfer[] { LocalObjectTransfer.getTransfer() }); - target.addDropListener(new ChartDropTarget(separator, this, panel)); - - } - - /** - * Returns the chart resource associated with this element - * @return chart resource - */ - public Resource getChartResource() { - return this.chartResource; - } - - /** - * Returns the minimized state of this element - * @return is the element minimized - */ - public boolean isMinimized() { - return minimized; - } - - /** - * Change the minimized state of this element - */ - public void toggleMinimize() { - toggleMinimize(false); - } - /** - * Change the minimized state of this element - */ - public void toggleMinimize(boolean callSave) { - minimized = Boolean.FALSE.equals(minimized); - GridData data = (GridData) chartComposite.getLayoutData(); - if(panel.isVertical()) - data.exclude = minimized; - else - data.exclude = false; - chartComposite.setVisible(!minimized); - - Composite parent = getParent(); - data = (GridData) getLayoutData(); - GridData parentData = (GridData)parent.getLayoutData(); - parentData.grabExcessHorizontalSpace = data.grabExcessHorizontalSpace; - parentData.grabExcessVerticalSpace = data.grabExcessVerticalSpace; - parentData.heightHint = data.heightHint; - - if(callSave) { - panel.saveState(); - } - panel.layout(); - } - - /** - * Remove this chart panel element from its panel - */ - public void remove() { - panel.removeChart(chartResource); - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java deleted file mode 100644 index 03955086..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java +++ /dev/null @@ -1,397 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.ui.trend; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.jface.viewers.StructuredSelection; -import org.eclipse.swt.SWT; -import org.eclipse.swt.dnd.DND; -import org.eclipse.swt.dnd.DragSource; -import org.eclipse.swt.dnd.DragSourceEvent; -import org.eclipse.swt.dnd.DragSourceListener; -import org.eclipse.swt.dnd.Transfer; -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.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; -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.widgets.Canvas; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Event; -import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.ToolBar; -import org.eclipse.swt.widgets.ToolItem; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.PossibleObjectWithType; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.procedure.Listener; -import org.simantics.db.request.Read; -import org.simantics.layer0.Layer0; -import org.simantics.sysdyn.JFreeChartResource; -import org.simantics.sysdyn.ui.Activator; -import org.simantics.ui.SimanticsUI; -import org.simantics.ui.dnd.LocalObjectTransfer; -import org.simantics.utils.datastructures.Pair; - -/** - * Header of a chart element in {@link ChartPanel}. Only this header is - * shown if a chart is minimized. If a chart is expanded, this header is added - * to the charts {@link ChartPanelElement}. - * - * @author Teemu Lempinen - * - */ -public class ChartPanelHeader extends Composite { - - public static int HEADER_MINIMUM_WIDTH = 250; - private ChartPanelElement element; - private Resource resource; - private Label name; - private Canvas iconCanvas; - private Image icon; - private ToolItem minimize, remove; - private Color defaultColor, darker, evenDarker; - private Image gradientBackgroundImage, borderImage; - - private static ImageDescriptor closeDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/close.gif")); - private static Image closeImage = closeDescriptor.createImage(); - - private static ImageDescriptor minimizeDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/minimize.gif")); - private static Image minimizeImage = minimizeDescriptor.createImage(); - - private static ImageDescriptor maximizeDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/maximize.gif")); - private static Image maximizeImage = maximizeDescriptor.createImage(); - - private static ImageDescriptor lineChartDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_line_light.png")); - private static Image lineChartImage = lineChartDescriptor.createImage(); - - private static ImageDescriptor barChartDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_bar_light.png")); - private static Image barChartImage = barChartDescriptor.createImage(); - - private static ImageDescriptor pieChartDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/chart_pie_light.png")); - private static Image pieChartImage = pieChartDescriptor.createImage(); - - - /** - * Chart panel header with minimize and close buttons. - * - * @param parent The composite where the header is added - * @param panel The {@link ChartPanel} containing the header - * @param name The name of the chart - * @param style he Style of the created chart element - */ - public ChartPanelHeader(Composite c, ChartPanelElement element, Resource chartResource, int style) { - super(c, style); - this.resource = chartResource; - this.element = element; - - GridLayoutFactory.fillDefaults().margins(3, 0).numColumns(3).applyTo(this); - GridDataFactory.fillDefaults().grab(true, false).applyTo(this); - - // Colors - - // Chart icon - iconCanvas = new Canvas (this, SWT.NONE); - GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).hint(16, 16).applyTo(iconCanvas); - iconCanvas.addPaintListener (new PaintListener() { - - @Override - public void paintControl(PaintEvent e) { - if(icon != null) - e.gc.drawImage (icon, 0, 0); - } - }); - - // Label for the chart name (also minimize/expand) - name = new Label(this, SWT.NONE); - - try { - // name updater - Pair result = SimanticsUI.getSession().syncRequest(new Read>() { - - @Override - public Pair perform(ReadGraph graph) throws DatabaseException { - JFreeChartResource jfree = JFreeChartResource.getInstance(graph); - Layer0 l0 = Layer0.getInstance(graph); - String label = graph.getPossibleRelatedValue(resource, l0.HasLabel); - Image image = null; - Resource plot = graph.syncRequest(new PossibleObjectWithType(resource, l0.ConsistsOf, jfree.Plot)); - if(plot != null) { - if(graph.isInstanceOf(plot, jfree.CategoryPlot)) - image = barChartImage; - else if(graph.isInstanceOf(plot, jfree.PiePlot)) - image = pieChartImage; - else - image = lineChartImage; - } - return new Pair(label, image); - } - - }, new Listener>() { - - @Override - public void execute(final Pair result) { - if(result == null) - return; - - name.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - if(!name.isDisposed() && result.first != null) - name.setText(result.first); - - if(!iconCanvas.isDisposed() && result.second != null) { - icon = result.second; - iconCanvas.redraw(); - ChartPanelHeader.this.layout(); - } - } - }); - } - - @Override - public void exception(Throwable t) { - t.printStackTrace(); - } - - @Override - public boolean isDisposed() { - return name.isDisposed(); - } - - }); - name.setText(result.first); - } catch (DatabaseException e) { - e.printStackTrace(); - name.setText("No label"); - } - GridDataFactory.fillDefaults().grab(true, false).applyTo(name); - - ToolBar toolbar = new ToolBar(this, SWT.FLAT); - // item for minimizing/expanding chart - minimize = new ToolItem(toolbar, SWT.PUSH); - minimize.addSelectionListener(new MinimizeListener()); - if(isMinimized()) { - minimize.setToolTipText("Expand"); - minimize.setImage(maximizeImage); - } else { - minimize.setToolTipText("Minimize"); - minimize.setImage(minimizeImage); - } - - // item for closing/removing the chart - remove = new ToolItem(toolbar, SWT.PUSH); - remove.setImage(closeImage); - remove.addSelectionListener(new RemoveChartListener()); - remove.setToolTipText("Remove"); - - - /* ******************************** - * DnD - * ********************************/ - - // Allow data to be copied or moved from the drag source - int operations = DND.DROP_MOVE; - source = new DragSource(name, operations); - - // Provide data in Text format - Transfer[] types = new Transfer[] { LocalObjectTransfer.getTransfer() }; - source.setTransfer(types); - dragSourceListener = new DragSourceListener() { - - @Override - public void dragStart(DragSourceEvent event) { - if(name.isDisposed()) - event.doit = false; - event.detail = DND.DROP_LINK; - - } - - @Override - public void dragSetData(DragSourceEvent event) { - event.data = new StructuredSelection(resource); - } - - @Override - public void dragFinished(DragSourceEvent event) { - } - }; - source.addDragListener(dragSourceListener); - - name.addDisposeListener(new DisposeListener() { - - @Override - public void widgetDisposed(DisposeEvent e) { - if(dragSourceListener != null && source != null && !source.isDisposed()) { - source.removeDragListener(dragSourceListener); - } - } - }); - this.setBackgroundImage(getGradientBackgroundImage()); - this.setBackgroundMode(SWT.INHERIT_FORCE); - - this.addListener(SWT.MouseEnter, new EnterListener()); - this.addListener(SWT.MouseExit, new ExitListener()); - - for(Control child : this.getChildren()) { - child.addListener(SWT.MouseEnter, new EnterListener()); - child.addListener(SWT.MouseExit, new ExitListener()); - - } - } - - private class EnterListener implements org.eclipse.swt.widgets.Listener { - public void handleEvent(Event event) { - ChartPanelHeader.this.setBackgroundImage(getHighlightedGradientBackgroundImage()); - } - } - - private class ExitListener implements org.eclipse.swt.widgets.Listener { - public void handleEvent(Event event) { - ChartPanelHeader.this.setBackgroundImage(getGradientBackgroundImage()); - } - } - - private void createColors() { - if(defaultColor == null) { - defaultColor = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); - try { - defaultColor = new Color(getDisplay(), defaultColor.getRed() + 500, defaultColor.getGreen() + 10, defaultColor.getBlue() + 10); - } catch (IllegalArgumentException e) { - // Do nothing, use the default color - } - } - - if(darker == null) { - try { - darker = new Color(getDisplay(), defaultColor.getRed() - 30, defaultColor.getGreen() - 30, defaultColor.getBlue() - 30); - } catch (IllegalArgumentException e) { - // Do nothing, use the default color - darker = defaultColor; - } - } - - if(evenDarker == null) { - try { - evenDarker = new Color(getDisplay(), defaultColor.getRed() - 50, defaultColor.getGreen() - 50, defaultColor.getBlue() - 50); - } catch (IllegalArgumentException e) { - // Do nothing, use the default color - evenDarker = defaultColor; - } - } - - } - - private Image getHighlightedGradientBackgroundImage() { - createColors(); - this.layout(); - Point size = this.getSize(); - - borderImage = new Image(this.getDisplay(), 1, Math.max(1, size.y)); - GC gc = new GC(borderImage); - gc.setForeground(defaultColor); - gc.setBackground(evenDarker); - gc.fillGradientRectangle(0, 0, 1, size.y, true); - gc.dispose(); - - return borderImage; - } - - private Image getGradientBackgroundImage() { - createColors(); - this.layout(); - Point size = this.computeSize(SWT.DEFAULT, SWT.DEFAULT); - if(gradientBackgroundImage == null) { - gradientBackgroundImage = new Image(this.getDisplay(), 1, Math.max(1, size.y)); - GC gc = new GC(gradientBackgroundImage); - gc.setForeground(defaultColor); - gc.setBackground(darker); - gc.fillGradientRectangle(0, 0, 1, size.y, true); - gc.dispose(); - } - - return gradientBackgroundImage; - } - - private DragSourceListener dragSourceListener; - private DragSource source; - - /** - * Return true if this element is minimized, false if expanded - * @return true if this element is minimized, false if expanded - */ - private boolean isMinimized() { - return element.isMinimized(); - } - - /** - * Listener to minimize chart button. Expands and minimizes - * the chart of this header. - * - * @author Teemu Lempinen - * - */ - private class MinimizeListener implements SelectionListener { - @Override - public void widgetSelected(SelectionEvent e) { - if(ChartPanelHeader.this.isDisposed()) - return; - - element.toggleMinimize(true); - - if(!name.isDisposed() && !minimize.isDisposed()) { - if(isMinimized()) { - minimize.setToolTipText("Expand"); - } else { - minimize.setToolTipText("Minimize"); - } - } - } - - @Override - public void widgetDefaultSelected(SelectionEvent e) { - widgetSelected(e); - } - - } - - /** - * Listener for removing this chart from the chart panel. - * - * @author Teemu Lempinen - * - */ - private class RemoveChartListener implements SelectionListener { - @Override - public void widgetSelected(SelectionEvent e) { - if(!ChartPanelHeader.this.isDisposed()) { - element.remove(); - } - } - - @Override - public void widgetDefaultSelected(SelectionEvent e) { - widgetSelected(e); - } - - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java deleted file mode 100644 index e0bde1e2..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java +++ /dev/null @@ -1,87 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.ui.trend; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.swt.events.MouseAdapter; -import org.eclipse.swt.events.MouseEvent; -import org.eclipse.swt.layout.GridData; -import org.eclipse.swt.widgets.Composite; - -/** - * Class for separating charts in {@link ChartPanel}. Acts as a drop participant for adding - * and moving charts in {@link ChartPanel}. - * - * @author Teemu Lempinen - * - */ -public class ChartPanelSeparator extends Composite { - - private ChartPanel panel; - - @Override - public Object getLayoutData() { - checkWidget(); - Object oldData = super.getLayoutData(); - if(oldData == null || !(oldData instanceof GridData)) { - oldData = GridDataFactory.fillDefaults().create(); - } - GridData data = (GridData) oldData; - // Empty panel -> drop area the size of the whole panel - if(panel.getElements().size() == 1 && panel.getElements().get(0).getResource() == null) { - data.grabExcessHorizontalSpace = true; - data.grabExcessVerticalSpace = true; - } - else { - if(panel.isVertical()) { - data.grabExcessHorizontalSpace = true; - data.grabExcessVerticalSpace = false; - } else { - data.grabExcessHorizontalSpace = false; - data.grabExcessVerticalSpace = true; - } - } - return data; - } - - /** - * Set up a small horizontal or vertical separator depending on SWT style - * - * @param parent - * @param style - */ - public ChartPanelSeparator(Composite parent, ChartPanel panel, ChartPanelElement element, int style) { - super(parent, style); - GridLayoutFactory.fillDefaults().margins(2, 2).applyTo(this); - GridDataFactory.fillDefaults().applyTo(this); - this.panel = panel; - addMouseListener(new SCFocusListener(panel)); - } - - - /** - * Listener for directing focus to scrollableComposite - * @author Teemu Lempinen - * - */ - private class SCFocusListener extends MouseAdapter { - ChartPanel panel; - public SCFocusListener(ChartPanel panel) { - this.panel = panel; - } - public void mouseDown(MouseEvent e) { - panel.setFocus(); - } - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java deleted file mode 100644 index 56d9fa8c..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java +++ /dev/null @@ -1,41 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * 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: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.sysdyn.ui.trend; - -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; - -/** - * Container for a chart panel element. Needed for - * moving chart panel elements around using setParent() - * @author Teemu Lempinen - * - */ -public class ElementContainer extends Composite { - - @Override - public Object getLayoutData () { - Control[] children = getChildren(); - if(children.length == 1) { - if(children[0] instanceof ChartPanelElement) { - ChartPanelElement element = (ChartPanelElement)children[0]; - return element.getLayoutData(); - } - } - return super.getLayoutData(); - } - - public ElementContainer(Composite parent, int style) { - super(parent, style); - } - -}