/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland 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: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.proconf.g3d.views; import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IPartListener; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.forms.events.ExpansionAdapter; import org.eclipse.ui.forms.events.ExpansionEvent; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.ScrolledForm; import org.eclipse.ui.forms.widgets.Section; import org.simantics.db.Graph; import org.simantics.db.GraphRequestAdapter; import org.simantics.db.GraphRequestStatus; import org.simantics.db.Session; import org.simantics.proconf.g3d.common.StructuredResourceSelection; import org.simantics.proconf.ui.workbench.ResourceEditorPart; import org.simantics.utils.ui.ErrorLogger; import org.simantics.utils.ui.ISelectionUtils; import org.simantics.utils.ui.jface.BaseSelectionProvider; public abstract class SinglePageResourceEditor extends ResourceEditorPart { private ScrolledForm form; private BaseSelectionProvider defaultInputSelectionProvider = new BaseSelectionProvider(); protected FormToolkit toolkit; @Override public void createPartControl(Composite parent) { this.getEditorSite().getPage().addPartListener(new IPartListener() { boolean opened = false; boolean activated = false; public void partOpened(IWorkbenchPart part) { if (part.equals(SinglePageResourceEditor.this.getEditorSite().getPart())) { opened = true; } } public void partActivated(IWorkbenchPart part) { if (part.equals(SinglePageResourceEditor.this.getEditorSite().getPart())) { if (opened & !activated) { activated = true; load(); } } } public void partBroughtToTop(IWorkbenchPart part) {} public void partClosed(IWorkbenchPart part) {} public void partDeactivated(IWorkbenchPart part) {} private void load() { Session ses = SinglePageResourceEditor.this.getSession(); GraphRequestAdapter r = new GraphRequestAdapter() { @Override public GraphRequestStatus perform(Graph g) throws Exception { reload(g); return GraphRequestStatus.transactionComplete(); } }; ses.asyncRead(r); } }); try { toolkit = new FormToolkit(parent.getDisplay()); form = getToolkit().createScrolledForm(parent); GridLayout layout = new GridLayout(2, false); form.getBody().setLayout(layout); form.getBody().setLayoutData( new GridData(GridData.FILL, GridData.FILL, true, true)); // By default make this ViewPart use a default ISelectionProvider // that will offer the viewparts input resource as its selection. // The Resource is wrapped into a ResourceSelection object. // Any widgets created in createWidgets may override the default // selection provider. getEditorSite().setSelectionProvider(defaultInputSelectionProvider); beforeCreateWidgets(); createWidgets(); //reload(); form.setText(getFormText()); // Finally Set the default selection which will have an effect only // if nothing in createWidgets has overridden the default selection // provider. ISelection s = ISelectionUtils .createSelection(new StructuredResourceSelection( getInputResource())); defaultInputSelectionProvider.setSelection(s); } catch (Exception e) { Display d = getSite().getShell().getDisplay(); d.asyncExec(new Runnable() { public void run() { getSite().getPage().closeEditor( SinglePageResourceEditor.this, false); } }); ErrorLogger.defaultLogError("Single-page type editor failed to open, see exception for details",e); } } public ScrolledForm getActiveForm() { return form; } protected Composite getBody() { return form.getBody(); } public Composite newGridSection(int formColumns, int childColumns, boolean equalWidth, boolean grabVertical, String text, String description) { return newGridSection(getBody(), formColumns, childColumns, equalWidth, grabVertical, text, description); } public Composite newGridSection(Composite parent, int formColumns, int childColumns, boolean equalWidth, boolean grabVertical, String text, String description) { FormToolkit toolkit = getToolkit(); Section section = toolkit.createSection(parent, Section.DESCRIPTION | Section.TWISTIE | Section.TITLE_BAR | Section.EXPANDED); section.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, grabVertical, formColumns, 1)); section.addExpansionListener(new ExpansionAdapter() { public void expansionStateChanged(ExpansionEvent e) { //System.out.println("SinglePageTypeEditor: expansionStateChanged " + e); //reflow(true); } }); section.setText(text); section.setDescription(description); Composite sectionClient = toolkit.createComposite(section); sectionClient.setLayout(new GridLayout(childColumns, equalWidth)); sectionClient.setLayoutData(new GridData()); section.setClient(sectionClient); return sectionClient; } //---------------------------------------------------------------------- // Getters public FormToolkit getToolkit() { return toolkit; } //---------------------------------------------------------------------- // Event utilities public void reflow(boolean flushCache) { //System.out.println("FormTypeEditorBase.reflow(" + flushCache + ")"); getActiveForm().reflow(flushCache); } @Override public void dispose() { if (toolkit != null) { toolkit.dispose(); } super.dispose(); } @Override public void setFocus() { //System.out.println("FormTypeEditorBase.setFocus(): Input = " + getInput()); ScrolledForm form = getActiveForm(); if (form != null) { form.setFocus(); } } protected abstract String getFormText(); /** * Returns null by default which makes {@link #updateTitle()} not set the * part name programmatically, i.e. the plugin-defined view name will stay. * * @return */ protected String getTitleText() { return null; } /** * Return null by default which makes {@link #updateTitle()} clear the * tooltip. * * @return */ protected String getTitleTooltip() { return null; } /** * A method for performing initializations just before UI initialization. */ protected void beforeCreateWidgets() { } /** * A method for initializing the UI of the view. */ protected void createWidgets() { } }