| /******************************************************************************* |
| * Copyright (c) 2010 BestSolution.at and others. |
| * 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: |
| * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.e4.tools.emf.ui.internal.common.component.virtual; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| import javax.annotation.PostConstruct; |
| import javax.inject.Inject; |
| import org.eclipse.core.databinding.observable.list.IObservableList; |
| import org.eclipse.core.databinding.observable.value.WritableValue; |
| import org.eclipse.e4.tools.emf.ui.common.Util; |
| import org.eclipse.e4.tools.emf.ui.common.component.AbstractComponentEditor; |
| import org.eclipse.e4.tools.emf.ui.internal.ResourceProvider; |
| import org.eclipse.e4.tools.emf.ui.internal.common.ComponentLabelProvider; |
| import org.eclipse.e4.tools.emf.ui.internal.common.VirtualEntry; |
| import org.eclipse.e4.ui.model.application.ui.MElementContainer; |
| import org.eclipse.e4.ui.model.application.ui.MUIElement; |
| import org.eclipse.e4.ui.model.application.ui.advanced.impl.AdvancedPackageImpl; |
| import org.eclipse.e4.ui.model.application.ui.basic.impl.BasicPackageImpl; |
| import org.eclipse.e4.ui.model.application.ui.impl.UiPackageImpl; |
| import org.eclipse.emf.common.command.Command; |
| import org.eclipse.emf.databinding.EMFDataBindingContext; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.edit.command.AddCommand; |
| import org.eclipse.emf.edit.command.RemoveCommand; |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.databinding.viewers.ObservableListContentProvider; |
| import org.eclipse.jface.viewers.ArrayContentProvider; |
| import org.eclipse.jface.viewers.ComboViewer; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.jface.viewers.LabelProvider; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.CTabFolder; |
| import org.eclipse.swt.custom.CTabItem; |
| import org.eclipse.swt.events.SelectionAdapter; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Label; |
| |
| public class VWindowControlEditor extends AbstractComponentEditor { |
| private Composite composite; |
| private EMFDataBindingContext context; |
| private TableViewer viewer; |
| private List<Action> actions = new ArrayList<Action>(); |
| |
| @Inject |
| public VWindowControlEditor() { |
| super(); |
| } |
| |
| @PostConstruct |
| void init() { |
| actions.add(new Action(Messages.VWindowControlEditor_AddPerspectiveStack, createImageDescriptor(ResourceProvider.IMG_PerspectiveStack)) { |
| @Override |
| public void run() { |
| handleAdd(AdvancedPackageImpl.Literals.PERSPECTIVE_STACK); |
| } |
| }); |
| |
| actions.add(new Action(Messages.VWindowControlEditor_AddPartSashContainer, createImageDescriptor(ResourceProvider.IMG_PartSashContainer_vertical)) { |
| @Override |
| public void run() { |
| handleAdd(BasicPackageImpl.Literals.PART_SASH_CONTAINER); |
| } |
| }); |
| |
| actions.add(new Action(Messages.VWindowControlEditor_AddPartStack, createImageDescriptor(ResourceProvider.IMG_PartStack)) { |
| @Override |
| public void run() { |
| handleAdd(BasicPackageImpl.Literals.PART_STACK); |
| } |
| }); |
| |
| actions.add(new Action(Messages.VWindowControlEditor_AddPart, createImageDescriptor(ResourceProvider.IMG_Part)) { |
| @Override |
| public void run() { |
| handleAdd(BasicPackageImpl.Literals.PART); |
| } |
| }); |
| |
| actions.add(new Action(Messages.VWindowControlEditor_AddInputPart, createImageDescriptor(ResourceProvider.IMG_Part)) { |
| @Override |
| public void run() { |
| handleAdd(BasicPackageImpl.Literals.INPUT_PART); |
| } |
| }); |
| |
| actions.add(new Action(Messages.VWindowControlEditor_AddArea, createImageDescriptor(ResourceProvider.IMG_Area_vertical)) { |
| @Override |
| public void run() { |
| handleAdd(AdvancedPackageImpl.Literals.AREA); |
| } |
| }); |
| } |
| |
| @Override |
| public Image getImage(Object element, Display display) { |
| return null; |
| } |
| |
| @Override |
| public String getLabel(Object element) { |
| return Messages.VWindowControlEditor_TreeLabel; |
| } |
| |
| @Override |
| public String getDetailLabel(Object element) { |
| return null; |
| } |
| |
| @Override |
| public String getDescription(Object element) { |
| return Messages.VWindowControlEditor_TreeLabelDescription; |
| } |
| |
| @Override |
| public Composite doGetEditor(Composite parent, Object object) { |
| if (composite == null) { |
| context = new EMFDataBindingContext(); |
| composite = createForm(parent, context, getMaster()); |
| } |
| VirtualEntry<?> o = (VirtualEntry<?>) object; |
| viewer.setInput(o.getList()); |
| getMaster().setValue(o.getOriginalParent()); |
| return composite; |
| } |
| |
| private Composite createForm(Composite parent, EMFDataBindingContext context, WritableValue master) { |
| CTabFolder folder = new CTabFolder(parent, SWT.BOTTOM); |
| |
| CTabItem item = new CTabItem(folder, SWT.NONE); |
| item.setText(Messages.ModelTooling_Common_TabDefault); |
| |
| parent = createScrollableContainer(folder); |
| item.setControl(parent.getParent()); |
| |
| { |
| Label l = new Label(parent, SWT.NONE); |
| l.setText(Messages.VWindowControlEditor_Controls); |
| l.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); |
| |
| viewer = new TableViewer(parent); |
| ObservableListContentProvider cp = new ObservableListContentProvider(); |
| viewer.setContentProvider(cp); |
| viewer.setLabelProvider(new ComponentLabelProvider(getEditor(), Messages)); |
| GridData gd = new GridData(GridData.FILL_BOTH); |
| viewer.getControl().setLayoutData(gd); |
| |
| Composite buttonComp = new Composite(parent, SWT.NONE); |
| buttonComp.setLayoutData(new GridData(GridData.FILL, GridData.END, false, false)); |
| GridLayout gl = new GridLayout(2, false); |
| gl.marginLeft = 0; |
| gl.marginRight = 0; |
| gl.marginWidth = 0; |
| gl.marginHeight = 0; |
| buttonComp.setLayout(gl); |
| |
| Button b = new Button(buttonComp, SWT.PUSH | SWT.FLAT); |
| b.setText(Messages.ModelTooling_Common_Up); |
| b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_up)); |
| b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1)); |
| b.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| if (!viewer.getSelection().isEmpty()) { |
| IStructuredSelection s = (IStructuredSelection) viewer.getSelection(); |
| if (s.size() == 1) { |
| Object obj = s.getFirstElement(); |
| MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue(); |
| int idx = container.getChildren().indexOf(obj) - 1; |
| if (idx >= 0) { |
| if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj, getEditor().isLiveModel(), idx)) { |
| viewer.setSelection(new StructuredSelection(obj)); |
| } |
| } |
| |
| } |
| } |
| } |
| }); |
| |
| b = new Button(buttonComp, SWT.PUSH | SWT.FLAT); |
| b.setText(Messages.ModelTooling_Common_Down); |
| b.setImage(createImage(ResourceProvider.IMG_Obj16_arrow_down)); |
| b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1)); |
| b.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| if (!viewer.getSelection().isEmpty()) { |
| IStructuredSelection s = (IStructuredSelection) viewer.getSelection(); |
| if (s.size() == 1) { |
| Object obj = s.getFirstElement(); |
| MElementContainer<?> container = (MElementContainer<?>) getMaster().getValue(); |
| int idx = container.getChildren().indexOf(obj) + 1; |
| if (idx < container.getChildren().size()) { |
| if (Util.moveElementByIndex(getEditingDomain(), (MUIElement) obj, getEditor().isLiveModel(), idx)) { |
| viewer.setSelection(new StructuredSelection(obj)); |
| } |
| } |
| } |
| } |
| } |
| }); |
| |
| final ComboViewer childrenDropDown = new ComboViewer(buttonComp); |
| childrenDropDown.getControl().setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false)); |
| childrenDropDown.setContentProvider(new ArrayContentProvider()); |
| childrenDropDown.setLabelProvider(new LabelProvider() { |
| @Override |
| public String getText(Object element) { |
| EClass eclass = (EClass) element; |
| return eclass.getName(); |
| } |
| }); |
| childrenDropDown.setInput(new EClass[] { AdvancedPackageImpl.Literals.PERSPECTIVE_STACK, BasicPackageImpl.Literals.PART_SASH_CONTAINER, BasicPackageImpl.Literals.PART_STACK, BasicPackageImpl.Literals.PART, BasicPackageImpl.Literals.INPUT_PART, AdvancedPackageImpl.Literals.AREA }); |
| childrenDropDown.setSelection(new StructuredSelection(AdvancedPackageImpl.Literals.PERSPECTIVE_STACK)); |
| |
| b = new Button(buttonComp, SWT.PUSH | SWT.FLAT); |
| b.setImage(createImage(ResourceProvider.IMG_Obj16_table_add)); |
| b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, false, false)); |
| b.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| if (!childrenDropDown.getSelection().isEmpty()) { |
| EClass eClass = (EClass) ((IStructuredSelection) childrenDropDown.getSelection()).getFirstElement(); |
| handleAdd(eClass); |
| } |
| } |
| }); |
| |
| b = new Button(buttonComp, SWT.PUSH | SWT.FLAT); |
| b.setText(Messages.ModelTooling_Common_Remove); |
| b.setImage(createImage(ResourceProvider.IMG_Obj16_table_delete)); |
| b.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1)); |
| b.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| if (!viewer.getSelection().isEmpty()) { |
| List<?> elements = ((IStructuredSelection) viewer.getSelection()).toList(); |
| |
| Command cmd = RemoveCommand.create(getEditingDomain(), getMaster().getValue(), UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, elements); |
| if (cmd.canExecute()) { |
| getEditingDomain().getCommandStack().execute(cmd); |
| } |
| } |
| } |
| }); |
| } |
| |
| folder.setSelection(0); |
| |
| return folder; |
| } |
| |
| @Override |
| public IObservableList getChildList(Object element) { |
| return null; |
| } |
| |
| private void handleAdd(EClass eClass) { |
| EObject eObject = EcoreUtil.create(eClass); |
| |
| setElementId(eObject); |
| |
| Command cmd = AddCommand.create(getEditingDomain(), getMaster().getValue(), UiPackageImpl.Literals.ELEMENT_CONTAINER__CHILDREN, eObject); |
| |
| if (cmd.canExecute()) { |
| getEditingDomain().getCommandStack().execute(cmd); |
| getEditor().setSelection(eObject); |
| } |
| } |
| |
| @Override |
| public List<Action> getActions(Object element) { |
| ArrayList<Action> l = new ArrayList<Action>(super.getActions(element)); |
| l.addAll(actions); |
| return l; |
| } |
| } |