]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/SetIndexItem.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / SetIndexItem.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.charts.ui;
13
14 import java.awt.Color;
15 import java.util.List;
16
17 import org.eclipse.jface.action.ContributionItem;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.jface.resource.LocalResourceManager;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.MenuItem;
27 import org.simantics.Simantics;
28 import org.simantics.charts.Activator;
29 import org.simantics.charts.query.ChartItemIndexQuery;
30 import org.simantics.charts.query.SetChartItemIndexRequest;
31 import org.simantics.db.Resource;
32 import org.simantics.db.Session;
33 import org.simantics.db.exception.DatabaseException;
34
35 public class SetIndexItem extends ContributionItem implements SelectionListener {
36
37         int index;
38         Color color;
39         String label;
40         Image image;
41         LocalResourceManager rm;
42         List<Resource> chartItems;
43         boolean analogIcon; 
44         
45         public SetIndexItem(Color color, int index, String label, boolean analogIcon, List<Resource> chartItems)
46         {
47                 super("org.simantics.charts.ui.ColorItem."+index);
48                 this.index = index;
49                 this.label = label;
50                 this.color = color;
51                 this.chartItems = chartItems;
52                 this.analogIcon = analogIcon;
53         }
54         
55         @Override
56         public void fill(Menu menu, int index) {
57                 
58                 MenuItem item = new MenuItem(menu, SWT.CHECK);
59 //              item.setText( this.index+":"+label );
60                 item.setText( this.index+"" );
61                 item.addSelectionListener(this);
62
63                 boolean selected = false;
64                 if (chartItems.size()==1) {
65                         try {
66                                 Integer selIndex = Simantics.getSession().sync( new ChartItemIndexQuery(chartItems.get(0)) );
67                                 selected = (selIndex!=null) && (this.index == selIndex);
68                         } catch (DatabaseException e) {
69                         }
70                 }
71                         
72                 item.setSelection( selected );
73                 if (rm==null) rm = new LocalResourceManager(JFaceResources.getResources());
74                                 
75                 ImageDescriptor icon = Activator.getDefault().chartIcons.createIcon(analogIcon, color, selected);
76                 
77                 image = (Image) rm.get(icon);
78                 item.setImage( image );
79         }
80         
81         @Override
82         public void dispose() {
83                 if (rm != null) rm.dispose();
84                 super.dispose();
85         }
86
87         @Override
88         public void widgetSelected(SelectionEvent e) {
89                 for ( Resource chartItem : chartItems) {
90                         Session s = Simantics.getSession();
91                         s.markUndoPoint();
92                         s.async( new SetChartItemIndexRequest(chartItem, index) );
93                 }
94         }
95
96         @Override
97         public void widgetDefaultSelected(SelectionEvent e) {
98                 widgetSelected(e);
99         }
100         
101 }