| /** |
| ******************************************************************************** |
| * Copyright (c) 2017 - 2018 Robert Bosch GmbH. |
| * |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Robert Bosch GmbH - initial API and implementation |
| ******************************************************************************** |
| */ |
| package org.eclipse.app4mc.emfutils.content.extractor.views; |
| |
| import java.io.IOException; |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Method; |
| import java.util.Collections; |
| import java.util.Stack; |
| |
| import org.eclipse.app4mc.emfutils.content.extractor.Activator; |
| import org.eclipse.app4mc.emfutils.content.extractor.builders.EClassAllReferencesBufferBuilder; |
| import org.eclipse.app4mc.emfutils.content.extractor.builders.EClassContentsAndHierarchyBufferBuilder; |
| import org.eclipse.app4mc.emfutils.content.extractor.builders.EClassContentsFromReferenceBufferBuilder; |
| import org.eclipse.app4mc.emfutils.content.extractor.builders.EClassHierarchyBufferBuilder; |
| import org.eclipse.app4mc.emfutils.content.extractor.builders.ObjectContentBufferBuilder; |
| import org.eclipse.app4mc.emfutils.content.extractor.handlers.AbstractHandlerUtils; |
| import org.eclipse.app4mc.emfutils.content.extractor.preferences.PreferenceConstants; |
| import org.eclipse.app4mc.emfutils.content.extractor.utils.ExecutionCategory; |
| import org.eclipse.app4mc.emfutils.content.extractor.utils.ExecutionUtil; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.ResourcesPlugin; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.emf.common.ui.URIEditorInput; |
| import org.eclipse.emf.common.util.BasicEMap; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.emf.common.util.EMap; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.jface.action.Action; |
| import org.eclipse.jface.action.IAction; |
| import org.eclipse.jface.action.IToolBarManager; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.viewers.StructuredSelection; |
| import org.eclipse.jface.viewers.TreeViewer; |
| import org.eclipse.jface.viewers.Viewer; |
| import org.eclipse.sphinx.emf.editors.forms.BasicTransactionalFormEditor; |
| import org.eclipse.sphinx.emf.ui.util.EcoreUIUtil; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.browser.Browser; |
| import org.eclipse.swt.browser.LocationEvent; |
| import org.eclipse.swt.browser.LocationListener; |
| import org.eclipse.swt.browser.ProgressEvent; |
| import org.eclipse.swt.browser.ProgressListener; |
| import org.eclipse.swt.events.KeyAdapter; |
| import org.eclipse.swt.events.KeyEvent; |
| import org.eclipse.swt.events.KeyListener; |
| import org.eclipse.swt.events.TraverseEvent; |
| import org.eclipse.swt.events.TraverseListener; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.ui.IActionBars; |
| import org.eclipse.ui.IEditorPart; |
| import org.eclipse.ui.IEditorReference; |
| import org.eclipse.ui.IViewSite; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.PartInitException; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.ide.IDE; |
| import org.eclipse.ui.part.FileEditorInput; |
| import org.eclipse.ui.part.MultiPageEditorPart; |
| import org.eclipse.ui.part.ViewPart; |
| |
| |
| public class DiagramView extends ViewPart { |
| |
| /** |
| * The ID of the view as specified by the extension. |
| */ |
| public static final String ID = "org.eclipse.app4mc.emfutils.content.extractor.views.DiagramView"; |
| |
| /** counter for the generated files (which are associated to the collections ) **/ |
| private int i = 0; |
| |
| private Browser browser; |
| |
| |
| final EMap<String, Object> idObjectsMap; |
| |
| |
| /** |
| * The constructor. |
| */ |
| public DiagramView() { |
| |
| this.idObjectsMap = new BasicEMap<String, Object>(); |
| } |
| |
| @Override |
| public void init(final IViewSite site) throws PartInitException { |
| super.init(site); |
| addActions(); |
| } |
| |
| |
| |
| /** |
| * This is a callback that will allow us to create the viewer and initialize it. |
| */ |
| @Override |
| public void createPartControl(final Composite parent) { |
| this.browser = new Browser(parent, SWT.None); |
| |
| this.browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| |
| CustomBrowserListener listener = new CustomBrowserListener(); |
| this.browser.addKeyListener(listener ); |
| |
| this.browser.addLocationListener(listener); |
| |
| |
| |
| } |
| |
| class CustomBrowserListener implements LocationListener, KeyListener { |
| |
| String lastSource = ""; |
| |
| public Stack<String> urls=new Stack<>();; |
| |
| boolean isBackButtonPressed=false; |
| |
| @Override |
| public void changing(final LocationEvent event) { |
| |
| if(isBackButtonPressed) { |
| isBackButtonPressed=false; |
| return; |
| } |
| |
| final String location = event.location; |
| //TODO: found this |
| if (!location.endsWith(".svg")) { |
| final int lastIndexOf = location.lastIndexOf("/"); |
| |
| String id = ""; |
| if (lastIndexOf != -1) { |
| id = location.substring(lastIndexOf + 1, location.length()); |
| } |
| final Object source = event.getSource(); |
| |
| if (source instanceof Browser) { |
| |
| try { |
| // ((Browser) source).setUrl(this.lastSource); |
| } |
| catch (final Exception e) { |
| // skipping exception logging, as here goal is to still retain the same image |
| } |
| } |
| |
| final Object eObject = DiagramView.this.idObjectsMap.get(id); |
| |
| if (eObject != null) { |
| |
| if (eObject instanceof EObject) { |
| if (Activator.getDefault().getPreferenceStore() |
| .getBoolean(PreferenceConstants.P_DRILL_DOWN)) { |
| |
| /*- ======================================== Regeneration of SVG Buffer for populating content specific to the selection =====================*/ |
| |
| StringBuffer buildBuffer = new StringBuffer(); |
| |
| |
| if (ExecutionUtil.isExecuting(ExecutionCategory.eClassHierarchy) |
| && (eObject instanceof EClass)) { |
| |
| final EClassHierarchyBufferBuilder bufferBuilder = new EClassHierarchyBufferBuilder(); |
| |
| buildBuffer = bufferBuilder.buildBuffer((EClass) eObject); |
| |
| getIdObjectsMap().addAll(bufferBuilder.getId2ObjectsMap()); |
| } |
| else if (ExecutionUtil.isExecuting(ExecutionCategory.eClassContentsAndHierarchy) |
| && (eObject instanceof EClass)) { |
| |
| final EClassContentsAndHierarchyBufferBuilder bufferBuilder = new EClassContentsAndHierarchyBufferBuilder(); |
| |
| buildBuffer = bufferBuilder.buildBuffer((EClass) eObject); |
| |
| getIdObjectsMap().addAll(bufferBuilder.getId2ObjectsMap()); |
| } |
| else if (ExecutionUtil.isExecuting(ExecutionCategory.eClassAllReferences) |
| && (eObject instanceof EClass)) { |
| |
| |
| final EClassAllReferencesBufferBuilder bufferBuilder = new EClassAllReferencesBufferBuilder(); |
| |
| buildBuffer = bufferBuilder.buildBuffer((EClass) eObject); |
| |
| getIdObjectsMap().addAll(bufferBuilder.getId2ObjectsMap()); |
| |
| |
| } |
| else if (ExecutionUtil.isExecuting(ExecutionCategory.eClassContentsFromReference) |
| && (eObject instanceof EClass)) { |
| |
| |
| final EClassContentsFromReferenceBufferBuilder bufferBuilder = new EClassContentsFromReferenceBufferBuilder(); |
| |
| buildBuffer = bufferBuilder.buildBuffer((EClass) eObject); |
| |
| getIdObjectsMap().addAll(bufferBuilder.getId2ObjectsMap()); |
| |
| |
| } |
| |
| else { |
| |
| /*- EClassContents and EObjectRefs behaviour is included in ObjectContentBufferBuilder java class */ |
| final ObjectContentBufferBuilder bufferBuilder = new ObjectContentBufferBuilder(); |
| |
| buildBuffer = bufferBuilder.buildBuffer((EObject) eObject); |
| |
| getIdObjectsMap().addAll(bufferBuilder.getId2ObjectsMap()); |
| } |
| |
| |
| System.out.println(buildBuffer); |
| |
| try { |
| |
| |
| final String svgFile = AbstractHandlerUtils.generateSVGFile(buildBuffer, |
| ((EObject) eObject).eClass().getName() + DiagramView.this.i++, |
| new BasicEMap<String, Object>()); |
| ((Browser) source).setUrl(svgFile); |
| } |
| catch (final IOException e) { |
| e.printStackTrace(); |
| } |
| |
| |
| } |
| else { |
| |
| |
| if (ExecutionUtil.isExecuting(ExecutionCategory.eClassHierarchy) |
| && (eObject instanceof EClass)) { |
| // do nothing |
| } |
| |
| |
| else if (ExecutionUtil.isExecuting(ExecutionCategory.eClassContentsAndHierarchy) |
| && (eObject instanceof EClass)) { |
| // do nothing |
| } |
| else if (ExecutionUtil.isExecuting(ExecutionCategory.eClassAllReferences) |
| && (eObject instanceof EClass)) { |
| // do nothing |
| } |
| else { |
| selectElement((EObject) eObject); |
| |
| } |
| |
| // ((Browser)source).stop(); |
| |
| event.doit=false; |
| } |
| |
| } |
| else if (eObject instanceof EList<?>) { |
| |
| if (Activator.getDefault().getPreferenceStore() |
| .getBoolean(PreferenceConstants.P_DRILL_DOWN)) { |
| |
| /*- ======================================== Regeneration of SVG Buffer for populating content specific to the selection =====================*/ |
| |
| final ObjectContentBufferBuilder bufferBuilder = new ObjectContentBufferBuilder(); |
| |
| final StringBuffer buildBuffer = bufferBuilder |
| .buildBufferForCollection((EList<?>) eObject); |
| System.out.println(buildBuffer); |
| |
| try { |
| |
| getIdObjectsMap().addAll(bufferBuilder.getId2ObjectsMap()); |
| |
| final String svgFile = AbstractHandlerUtils.generateSVGFile(buildBuffer, |
| "Collection" + DiagramView.this.i++, bufferBuilder.getId2ObjectsMap()); |
| ((Browser) source).setUrl(svgFile); |
| } |
| catch (final IOException e) { |
| e.printStackTrace(); |
| } |
| |
| |
| } |
| else { |
| MessageDialog.openError(Display.getDefault().getActiveShell(), "Element Selection", |
| "Selection of list is not possible for AMALTHEA model"); |
| |
| try { |
| ((Browser) source).setUrl(this.lastSource); |
| |
| } |
| catch (final Exception e) { |
| throw e; |
| } |
| } |
| |
| } |
| |
| |
| } |
| |
| |
| } |
| |
| |
| } |
| |
| @Override |
| public void changed(final LocationEvent event) { |
| this.lastSource = event.location; |
| urls.push(this.lastSource); |
| } |
| |
| /** |
| * This method is used to select specified AMALTHEA element in the Sphinx editor |
| * |
| * @param element |
| */ |
| private void selectElement(final EObject element) { |
| |
| final URIEditorInput input = EcoreUIUtil.createURIEditorInput(element.eResource()); |
| if (null != input) { |
| |
| final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null |
| ? PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() |
| : PlatformUI.getWorkbench().getWorkbenchWindows()[0].getActivePage(); |
| |
| Display.getDefault().asyncExec(new java.lang.Runnable() { |
| |
| @Override |
| public void run() { |
| final IEditorReference[] editorReferences = page.getEditorReferences(); |
| |
| for (final IEditorReference iEditorReference : editorReferences) { |
| |
| |
| if (iEditorReference.getId().equals("org.eclipse.app4mc.amalthea.sphinx.ui.editor") |
| || iEditorReference.getId().equals("org.itea2.amalthea.sphinx.ui.editor") |
| || iEditorReference.getId().equals("com.inchron.realtime.root.presentation.RootEditorID")) { |
| try { |
| final FileEditorInput editorInput = (FileEditorInput) iEditorReference |
| .getEditorInput(); |
| |
| final IFile fileFromEditor = editorInput.getFile(); |
| |
| final Resource eResource = element.eResource(); |
| |
| final URI uri = eResource.getURI(); |
| |
| final IFile fileFromResource = ResourcesPlugin.getWorkspace().getRoot() |
| .getFile(new Path(uri.toPlatformString(true))); |
| |
| |
| if ((fileFromEditor != null && fileFromResource != null) |
| && fileFromEditor.toString().equals(fileFromResource.toString())) { |
| |
| final IEditorPart activeEditor = iEditorReference.getEditor(false); |
| |
| if (activeEditor instanceof MultiPageEditorPart) { |
| |
| IDE.openEditor(page, editorInput, iEditorReference.getId()); |
| |
| if (canCollapseTreeElements()) { |
| |
| Method method = activeEditor.getClass().getMethod("getViewer"); |
| |
| |
| final Viewer viewer = (Viewer) method.invoke(activeEditor); |
| |
| if (viewer instanceof TreeViewer) { |
| ((TreeViewer) viewer).collapseAll(); |
| |
| } |
| } |
| |
| if(activeEditor instanceof BasicTransactionalFormEditor) { |
| ((BasicTransactionalFormEditor) activeEditor) |
| .setSelectionToViewer(Collections.singleton(element)); |
| |
| }else { |
| |
| Method method = activeEditor.getClass().getMethod("getViewer"); |
| |
| |
| final Viewer viewer = (Viewer) method.invoke(activeEditor); |
| |
| if (viewer instanceof TreeViewer) { |
| ((TreeViewer) viewer).setSelection(new StructuredSelection(element), true); |
| |
| } |
| } |
| } |
| |
| } |
| } |
| catch (final PartInitException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
| |
| } |
| |
| private boolean canCollapseTreeElements() { |
| return Activator.getDefault().getPreferenceStore() |
| .getBoolean(PreferenceConstants.P_REDRAW_TREE_ON_SELECTION); |
| } |
| }); |
| |
| } |
| |
| } |
| |
| @Override |
| public void keyPressed(KeyEvent e) { |
| |
| if(e.character=='\b') { |
| isBackButtonPressed=true; |
| Object source = e.getSource(); |
| if (source instanceof Browser) { |
| |
| try { |
| if(urls.isEmpty()==false) { |
| String pop = urls.pop(); |
| |
| if(urls.isEmpty()==false) { |
| pop = urls.pop(); |
| ((Browser) source).setUrl(pop); |
| } |
| } |
| } |
| catch (final Exception exception) { |
| // skipping exception logging, as here goal is to still retain the same image |
| } |
| } |
| |
| } |
| } |
| |
| @Override |
| public void keyReleased(KeyEvent e) { |
| |
| |
| } |
| }; |
| |
| private void addActions() { |
| final String P_DRILL_DOWN = Activator.getDefault().getPreferenceStore() |
| .getString(PreferenceConstants.P_DRILL_DOWN); |
| |
| boolean state = false; |
| if (P_DRILL_DOWN == null) { |
| state = true; |
| } |
| else if (P_DRILL_DOWN.equals("false")) { |
| state = true; |
| } |
| else if (P_DRILL_DOWN.equals("true")) { |
| state = false; |
| } |
| |
| |
| final IActionBars actionBars = getViewSite().getActionBars(); |
| |
| final IToolBarManager toolBar = actionBars.getToolBarManager(); |
| final Action action = new Action("Select in Editor", IAction.AS_CHECK_BOX) { |
| @Override |
| public void setText(final String text) { |
| super.setText(text); |
| } |
| |
| @Override |
| public void run() { |
| |
| if (isChecked() == false) { |
| |
| try { |
| |
| Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_DRILL_DOWN, true); |
| } |
| catch (final Exception e) { |
| |
| throw e; |
| } |
| } |
| else if (isChecked() == true) { |
| |
| try { |
| Activator.getDefault().getPreferenceStore().setValue(PreferenceConstants.P_DRILL_DOWN, false); |
| |
| } |
| catch (final Exception e) { |
| throw e; |
| } |
| } |
| |
| } |
| |
| |
| }; |
| |
| action.setChecked(state); |
| |
| toolBar.add(action); |
| |
| } |
| |
| /** |
| * Passing the focus request to the viewer's control. |
| */ |
| @Override |
| public void setFocus() { |
| getViewSite().getActionBars().updateActionBars(); |
| |
| |
| } |
| |
| public Browser getBrowser() { |
| return this.browser; |
| } |
| |
| public EMap<String, Object> getIdObjectsMap() { |
| return this.idObjectsMap; |
| } |
| |
| } |