X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FSetIndexItem.java;fp=bundles%2Forg.simantics.charts%2Fsrc%2Forg%2Fsimantics%2Fcharts%2Fui%2FSetIndexItem.java;h=bcd4ede0d83ad362ddacbcca2ea900c8bd220c0f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/SetIndexItem.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/SetIndexItem.java new file mode 100644 index 000000000..bcd4ede0d --- /dev/null +++ b/bundles/org.simantics.charts/src/org/simantics/charts/ui/SetIndexItem.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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.charts.ui; + +import java.awt.Color; +import java.util.List; + +import org.eclipse.jface.action.ContributionItem; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.jface.resource.LocalResourceManager; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Menu; +import org.eclipse.swt.widgets.MenuItem; +import org.simantics.Simantics; +import org.simantics.charts.Activator; +import org.simantics.charts.query.ChartItemIndexQuery; +import org.simantics.charts.query.SetChartItemIndexRequest; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; + +public class SetIndexItem extends ContributionItem implements SelectionListener { + + int index; + Color color; + String label; + Image image; + LocalResourceManager rm; + List chartItems; + boolean analogIcon; + + public SetIndexItem(Color color, int index, String label, boolean analogIcon, List chartItems) + { + super("org.simantics.charts.ui.ColorItem."+index); + this.index = index; + this.label = label; + this.color = color; + this.chartItems = chartItems; + this.analogIcon = analogIcon; + } + + @Override + public void fill(Menu menu, int index) { + + MenuItem item = new MenuItem(menu, SWT.CHECK); +// item.setText( this.index+":"+label ); + item.setText( this.index+"" ); + item.addSelectionListener(this); + + boolean selected = false; + if (chartItems.size()==1) { + try { + Integer selIndex = Simantics.getSession().sync( new ChartItemIndexQuery(chartItems.get(0)) ); + selected = (selIndex!=null) && (this.index == selIndex); + } catch (DatabaseException e) { + } + } + + item.setSelection( selected ); + if (rm==null) rm = new LocalResourceManager(JFaceResources.getResources()); + + ImageDescriptor icon = Activator.getDefault().chartIcons.createIcon(analogIcon, color, selected); + + image = (Image) rm.get(icon); + item.setImage( image ); + } + + @Override + public void dispose() { + if (rm != null) rm.dispose(); + super.dispose(); + } + + @Override + public void widgetSelected(SelectionEvent e) { + for ( Resource chartItem : chartItems) { + Session s = Simantics.getSession(); + s.markUndoPoint(); + s.async( new SetChartItemIndexRequest(chartItem, index) ); + } + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + widgetSelected(e); + } + +} \ No newline at end of file