1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.tools;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
19 import org.eclipse.jface.layout.GridLayoutFactory;
20 import org.eclipse.jface.resource.JFaceResources;
21 import org.eclipse.jface.resource.LocalResourceManager;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.FillLayout;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.ToolBar;
28 import org.eclipse.ui.ISelectionListener;
29 import org.eclipse.ui.IWorkbenchPart;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.forms.widgets.FormToolkit;
32 import org.eclipse.ui.forms.widgets.ScrolledForm;
33 import org.eclipse.ui.forms.widgets.Section;
34 import org.eclipse.ui.part.ViewPart;
35 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;
38 public class DiagramToolViewPart extends ViewPart {
40 private Composite parent;
42 private final List<DiagramTools> tools = new ArrayList<DiagramTools>();
43 private final Map<DiagramTools,Composite> toolComposites = new HashMap<DiagramTools, Composite>();
45 private ISelectionListener listener;
47 protected LocalResourceManager resourceManager;
49 protected FormToolkit formToolkit;
51 protected ScrolledForm mainForm;
53 public DiagramToolViewPart() {
58 public void createPartControl(Composite parent) {
60 this.parent.setLayout(new FillLayout());
61 //this.parent.setLayout(new GridLayout(1, true));
63 listener = new ISelectionListener() {
66 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
67 // FIXME: selection logic must be improved.
68 // We want to disable tools, if selection does not come from DiagramEditor, or its outline
69 if (!(part instanceof DiagramEditor)) {
72 for (DiagramTools t : tools) {
73 t.setSelection(selection);
78 getSite().getPage().addPostSelectionListener(listener);
80 resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), parent);
82 formToolkit = new FormToolkit(parent.getDisplay());
83 formToolkit.adapt(parent);
84 mainForm = formToolkit.createScrolledForm(parent);
85 mainForm.getBody().setLayout(GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(true).margins(0, 5).spacing(1,1).create());
86 formToolkit.decorateFormHeading(mainForm.getForm());
87 mainForm.setText("Tools");
89 // Default set of tools.
90 openTool(new AlignTools());
94 public void setFocus() {
99 public void openTool(final DiagramTools tool) {
100 // Allow opening tool only once.
101 for (DiagramTools t : tools) {
102 if (t.getClass().equals(tool.getClass()))
106 Section group = formToolkit.createSection(mainForm.getBody(), Section.TITLE_BAR | Section.TWISTIE);
107 group.setText(tool.getName());
108 GridData data = new GridData(SWT.FILL, SWT.TOP, true, false);
109 group.setLayoutData(data);
110 Composite composite = formToolkit.createComposite(group);
112 tool.createContols(composite, formToolkit, resourceManager);
113 group.setClient(composite);
115 ToolBar toolBar = new ToolBar(group, SWT.NO_FOCUS | SWT.FLAT);
116 // ToolItem closeButton = new ToolItem(toolBar, SWT.PUSH);
117 // closeButton.setToolTipText("Close " + tool.getName() + " Tools");
118 // closeButton.setImage(resourceManager.createImage(ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/silk/cross.png"))));
119 group.setTextClient(toolBar);
121 group.setExpanded(true);
123 // closeButton.addSelectionListener(new SelectionAdapter() {
125 // public void widgetSelected(SelectionEvent e) {
131 toolComposites.put(tool, group);
132 parent.layout(true, true);
134 tool.setSelection(getSite().getPage().getSelection());
138 private void closeTool(DiagramTools tool) {
141 toolComposites.remove(tool).dispose();
145 public void dispose() {
146 List<DiagramTools> tls = new ArrayList<DiagramTools>();
148 for (DiagramTools t : tls) {
151 getSite().getPage().removePostSelectionListener(listener);
153 formToolkit.dispose();
157 public static void open(DiagramTools tool) throws PartInitException {
158 DiagramToolViewPart wp = (DiagramToolViewPart)WorkbenchUtils.showView("org.simantics.modeling.ui.diagramtool");