1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.menu;
\r
14 import java.awt.Color;
\r
15 import java.awt.Font;
\r
17 import org.eclipse.jface.action.ContributionItem;
\r
18 import org.eclipse.jface.action.IContributionItem;
\r
19 import org.eclipse.swt.SWT;
\r
20 import org.eclipse.swt.events.SelectionEvent;
\r
21 import org.eclipse.swt.events.SelectionListener;
\r
22 import org.eclipse.swt.widgets.Menu;
\r
23 import org.eclipse.swt.widgets.MenuItem;
\r
24 import org.simantics.db.ReadGraph;
\r
25 import org.simantics.db.Resource;
\r
26 import org.simantics.db.WriteGraph;
\r
27 import org.simantics.db.common.request.WriteRequest;
\r
28 import org.simantics.db.exception.DatabaseException;
\r
29 import org.simantics.diagram.G2DUtils;
\r
30 import org.simantics.diagram.stubs.G2DResource;
\r
31 import org.simantics.sysdyn.ui.properties.widgets.CustomFontDialog;
\r
32 import org.simantics.ui.SimanticsUI;
\r
33 import org.simantics.ui.contribution.DynamicMenuContribution;
\r
34 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
37 * Context menu contribution for modifying fonts and font colors in diagram elements
\r
39 * @author Teemu Lempinen
\r
42 public class FontContextMenuContribution extends DynamicMenuContribution {
\r
45 protected IContributionItem[] getContributionItems(final ReadGraph graph,
\r
46 Object[] selection) throws DatabaseException {
\r
47 if (selection.length == 0)
\r
48 return new IContributionItem[0];
\r
50 return new IContributionItem[] { new ContributionItem() {
\r
53 public void fill(Menu menu, int index) {
\r
55 G2DResource G2D = G2DResource.getInstance(graph);
\r
57 Object[] selections = getSelectedObjects();
\r
63 * Find a common font and color for the selected elements.
\r
65 * If a common font or color is not found, the initial value in dialog is empty.
\r
68 for(Object o : selections) {
\r
69 Resource element = ResourceAdaptionUtils
\r
70 .adaptToResource(o);
\r
71 if(element != null) {
\r
72 Resource fontResource = graph.getPossibleObject(element, G2D.HasFont);
\r
73 if(fontResource != null) {
\r
74 Font newFont = G2DUtils.getFont(graph, fontResource);
\r
79 if(font != null && !font.equals(newFont)) {
\r
86 } catch (DatabaseException e) {
\r
87 e.printStackTrace();
\r
91 for(Object o : selections) {
\r
92 Resource element = ResourceAdaptionUtils
\r
93 .adaptToResource(o);
\r
94 if(element != null) {
\r
95 Resource colorResource = graph.getPossibleObject(element, G2D.HasColor);
\r
96 if(colorResource != null ) {
\r
97 Color newColor = G2DUtils.getColor(graph, colorResource);
\r
101 if(color != null && !color.equals(newColor)) {
\r
108 } catch (DatabaseException e) {
\r
111 // Create the menu item with a selection listener
\r
113 item = new MenuItem(menu, SWT.PUSH);
\r
115 item.setText("Font...");
\r
117 item.addSelectionListener(new FontSelectionListener(selections, font, color));
\r
125 * Selection listener for font context menu action
\r
126 * @author Teemu Lempinen
\r
129 class FontSelectionListener implements SelectionListener {
\r
132 private Color color;
\r
133 private Object[] selections;
\r
136 * Font selection listener for context menu action in diagram
\r
138 * @param selections Selected elements
\r
139 * @param font Possible common font for the selected elements
\r
140 * @param color Possible common color for the selected elements
\r
142 public FontSelectionListener(Object[] selections, Font font, Color color) {
\r
143 this.selections = selections;
\r
145 this.color = color;
\r
149 public void widgetSelected(SelectionEvent e) {
\r
150 // Create the dialog
\r
151 CustomFontDialog dialog = new CustomFontDialog(e.widget.getDisplay().getActiveShell(), "Sample");
\r
153 // Set possible font and color defaults
\r
155 dialog.setAWTFont(font);
\r
158 dialog.setColor(color);
\r
164 final Font resultFont = dialog.getAWTFont();
\r
165 final Color resultColor = dialog.getAWTColor();
\r
167 // Apply results to all selected elements
\r
168 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
171 public void perform(WriteGraph graph) throws DatabaseException {
\r
172 G2DResource G2D = G2DResource.getInstance(graph);
\r
174 for(Object o : selections) {
\r
175 Resource element = ResourceAdaptionUtils
\r
176 .adaptToResource(o);
\r
177 if(resultFont != null) {
\r
178 graph.deny(element, G2D.HasFont);
\r
179 graph.claim(element, G2D.HasFont, G2DUtils.createFont(graph, resultFont));
\r
182 if(resultColor != null) {
\r
183 graph.deny(element, G2D.HasColor);
\r
184 graph.claim(element, G2D.HasColor, G2DUtils.createColor(graph, resultColor));
\r
192 public void widgetDefaultSelected(SelectionEvent e) {
\r