| /******************************************************************************* |
| * Copyright (c) 2004-2008 Istvan Rath and Daniel Varro |
| * 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: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.frameworkgui.views.code;
|
|
|
| import java.io.IOException; |
| import java.io.StringWriter; |
| import java.net.URISyntaxException; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.core.runtime.Assert; |
| import org.eclipse.jface.action.Action; |
| 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.Font; |
| import org.eclipse.swt.graphics.FontData; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Item; |
| import org.eclipse.swt.widgets.Text; |
| import org.eclipse.ui.ISharedImages; |
| import org.eclipse.ui.PlatformUI; |
| import org.eclipse.ui.part.ViewPart; |
| import org.eclipse.viatra2.buffers.BufferStore; |
| import org.eclipse.viatra2.buffers.IBufferStoreListener; |
| import org.eclipse.viatra2.codegen.CodeOutputPlugin; |
| import org.eclipse.viatra2.core.ICoreNotificationListener; |
| import org.eclipse.viatra2.core.notification.ICoreNotificationObject; |
| import org.eclipse.viatra2.core.notification.NotificationType; |
| import org.eclipse.viatra2.framework.FrameworkManager; |
| import org.eclipse.viatra2.framework.IFramework; |
| import org.eclipse.viatra2.framework.IFrameworkListChangedListener; |
|
|
| /**
|
| * New generation code buffer view.
|
| * @author Istvan Rath
|
| *
|
| */
|
| public class CodeBufferView extends ViewPart implements IFrameworkListChangedListener
|
| {
|
|
|
| public static final String ID = "org.eclipse.viatra2.gui.codebufferview";
|
|
|
| /**
|
| * True, if the output is buffered (i.e. should only be refreshed on
|
| * transaction end).
|
| */
|
| public boolean isBuffering = true;
|
|
|
|
|
| protected class DummyPage
|
| implements ICoreNotificationListener,
|
| IBufferStoreListener,
|
| CodeOutputPlugin
|
| {
|
|
|
| protected class DummyBufferPage
|
| {
|
| CTabItem item;
|
| Text text;
|
| String key;
|
|
|
| public DummyBufferPage(String aKey)
|
| {
|
| key = aKey;
|
| item = new CTabItem(internalFolder, SWT.NONE);
|
| text = new Text(internalFolder, SWT.MULTI//|SWT.H_SCROLL |
| |SWT.V_SCROLL|SWT.READ_ONLY|SWT.WRAP);
|
|
|
| if (System.getProperty("file.separator").equals("/"))
|
| {
|
| FontData fd = new FontData("Monospace", 9, SWT.NONE);
|
| text.setFont(new Font(Display.getDefault(), fd));
|
| }
|
| else
|
| {
|
| FontData fd = new FontData("Courier New", 9, SWT.NONE);
|
| text.setFont(new Font(Display.getDefault(), fd));
|
| }
|
|
|
| text.setBackground(ColorFactory.getColor(255,255,255));
|
| text.setForeground(ColorFactory.getColor(0, 0, 0));
|
|
|
| item.setText(key);
|
| item.setControl(text);
|
| item.setData(this);
|
| internalFolder.setSelection(item);
|
|
|
| //refresh();
|
| }
|
|
|
| public void refresh()
|
| {
|
| Display.getDefault().syncExec(new Runnable()
|
| {
|
| public void run()
|
| {
|
| try { |
| String s = ((StringWriter) |
| BufferStore.getBuffer( |
| FrameworkManager.getInstance().getFramework(frameworkId).getTopmodel(), key)) |
| .getBuffer().toString();
|
| text.setText(s); |
| // autoscroll to end |
| text.setSelection(s.length()-1);
|
| } catch (URISyntaxException e)
|
| { // impossible
|
|
|
| } catch (IOException e)
|
| {
|
| // impossible
|
| }
|
| }
|
| });
|
| }
|
| }
|
|
|
|
|
| protected Text defaultDisplay;
|
|
|
| protected StringBuffer defaultBuffer;
|
|
|
| protected CTabItem defaultItem;
|
|
|
| protected CTabFolder internalFolder;
|
|
|
| protected Map<String, DummyBufferPage> internalPages;
|
|
|
| protected String frameworkId;
|
|
|
| protected Item externalTabItem;
|
|
|
| public DummyPage(String id)
|
| {
|
| frameworkId = id;
|
| defaultBuffer = new StringBuffer();
|
| internalPages = new HashMap<String, DummyBufferPage>();
|
|
|
| // register as code output plugin
|
| IFramework fw = FrameworkManager.getInstance().getFramework(frameworkId);
|
| fw.addCodeOutListener(this);
|
|
|
| // register as transaction listener
|
| fw.getTopmodel().getNotificationManager().addAllListener(this);
|
| }
|
|
|
| public void disposePage()
|
| {
|
| // deregister as code output plugin
|
| IFramework fw = FrameworkManager.getInstance().getFramework(frameworkId);
|
| if (fw!=null) { |
| fw.removeCodeOutListener(this);
|
|
|
| //deregister as transaction listener |
| fw.getTopmodel().getNotificationManager().removeAllListener(this);
|
| }
|
| // deregister as buffer store listener
|
| BufferStore.removeListener(this);
|
|
|
| // dispose item
|
| externalTabItem.dispose();
|
| }
|
|
|
| public Control createPageControl(Composite parent)
|
| {
|
| // create internal tabfolder
|
| internalFolder = new CTabFolder(parent, SWT.BOTTOM | SWT.FLAT);
|
| internalFolder.setSimple(false);
|
|
|
| // create default page
|
| defaultItem = new CTabItem(internalFolder,SWT.NONE);
|
| defaultItem.setText("default");
|
|
|
| defaultDisplay = new Text(internalFolder, SWT.MULTI | SWT.V_SCROLL //| SWT.H_SCROLL |
| | SWT.READ_ONLY | SWT.WRAP);
|
| //defaultDisplay = new Text(internalFolder, SWT.MULTI | SWT.READ_ONLY);
|
| if (System.getProperty("file.separator").equals("/"))
|
| {
|
| FontData fd = new FontData("Monospace", 9, SWT.NONE);
|
| defaultDisplay.setFont(new Font(Display.getDefault(), fd));
|
| }
|
| else
|
| {
|
| FontData fd = new FontData("Courier New", 9, SWT.NONE);
|
| defaultDisplay.setFont(new Font(Display.getDefault(), fd));
|
| }
|
|
|
| defaultItem.setControl(defaultDisplay);
|
| defaultItem.setData(defaultDisplay);
|
|
|
| //defaultDisplay.setText("hello\n asd \n asdasd \n asdasdasdasd \n asdasdasdasdasd \n werwerwerwerwe \n aqweqweqweq \n qwwdsdafasd \n aqasdasdasd \n 23234234 \n asdasdasdasdas \n asdasdasdasads \n adasdasdasd \n asdasdasdasd2234234 \n");
|
| defaultDisplay.setBackground(ColorFactory.getColor(255,255,255));
|
| defaultDisplay.setForeground(ColorFactory.getColor(0, 0, 0));
|
|
|
| internalFolder.setSelection(defaultItem);
|
|
|
| // register buffer store listener
|
| BufferStore.addListener(this);
|
|
|
| return internalFolder;
|
| }
|
|
|
| public void refreshContent()
|
| {
|
| Display.getDefault().syncExec(new Runnable()
|
| {
|
| public void run()
|
| {
|
| defaultDisplay.setText(defaultBuffer.toString()); |
| // autoscroll to end |
| defaultDisplay.setSelection(defaultBuffer.length()-1);
|
|
|
| for (DummyBufferPage p : internalPages.values())
|
| {
|
| p.refresh();
|
| }
|
| }
|
| });
|
| }
|
|
|
| public void clear()
|
| {
|
| defaultBuffer = new StringBuffer();
|
| refreshContent();
|
| }
|
|
|
|
|
| public void beginWork()
|
| {
|
| //buffer.append("----------------\n");
|
| }
|
|
|
| public void codeOut(final String s)
|
| {
|
| if (!isBuffering)
|
| {
|
| //refreshContent();
|
| Display.getDefault().asyncExec(new Runnable(){
|
|
|
| public void run() {
|
| defaultDisplay.append(s);
|
|
|
| // refresh buffers
|
| for (DummyBufferPage p : internalPages.values())
|
| {
|
| p.refresh();
|
| }
|
| }
|
|
|
| });
|
| //display.append(s);
|
| }
|
| else
|
| {
|
| defaultBuffer.append(s);
|
| }
|
| }
|
|
|
| public void endWork() { }
|
|
|
| public void init(IFramework fw)
|
| {
|
| fw.getLogger().info("Textual Output View buffer for framework "+frameworkId+" initialized.");
|
| }
|
|
|
| public void actionPerformed(ICoreNotificationObject notification)
|
| {
|
| if (!isBuffering) return;
|
| NotificationType type = notification.getActionTypeEnum();
|
| if (type.equals(NotificationType.TA_TRANSACTION_END))
|
| {
|
| refreshContent();
|
| }
|
| }
|
|
|
| public int getListenerCategory() { return 0; }
|
|
|
| public void bufferAdded(final String newKey)
|
| {
|
| Display.getDefault().syncExec(new Runnable(){
|
| public void run()
|
| {
|
| internalPages.put(newKey, new DummyBufferPage(newKey));
|
| }
|
| });
|
|
|
| }
|
|
|
| public void bufferRemoved(String removedKey) {
|
| // TODO implement remove buffer
|
|
|
| }
|
|
|
| }
|
|
|
|
|
| protected CTabFolder tabFolder;
|
|
|
| /**
|
| * frameworkId ==> DummyPage map
|
| */
|
| private Map<String, DummyPage> pages = new HashMap<String, DummyPage>();
|
|
|
|
|
| private CTabFolder getTabFolder()
|
| {
|
| return tabFolder;
|
| }
|
|
|
| protected int getActivePage() {
|
| CTabFolder tabFolder = getTabFolder();
|
| if (tabFolder != null && !tabFolder.isDisposed()) {
|
| return tabFolder.getSelectionIndex();
|
| }
|
| return -1;
|
| }
|
|
|
| protected int getPageCount() {
|
| CTabFolder folder = getTabFolder();
|
| // May not have been created yet, or may have been disposed.
|
| if (folder != null && !folder.isDisposed()) {
|
| return folder.getItemCount();
|
| }
|
| return 0;
|
| }
|
|
|
| protected void setActivePage(int pageIndex) {
|
| Assert.isTrue(pageIndex >= 0 && pageIndex < getPageCount());
|
| getTabFolder().setSelection(pageIndex);
|
| pageChange(pageIndex);
|
| }
|
|
|
| private CTabItem getItem(int pageIndex) {
|
| return getTabFolder().getItem(pageIndex);
|
| }
|
|
|
| protected void setPageText(int pageIndex, String text) {
|
| getItem(pageIndex).setText(text);
|
| }
|
|
|
| protected String getPageText(int pageIndex) {
|
| return getItem(pageIndex).getText();
|
| }
|
|
|
| @Override
|
| public void createPartControl(Composite parent)
|
| {
|
| parent.setLayout(new FillLayout());
|
| tabFolder = new CTabFolder(parent, SWT.BOTTOM | SWT.FLAT);
|
| tabFolder.setSimple(false);
|
| tabFolder.addSelectionListener(new SelectionAdapter() {
|
| public void widgetSelected(SelectionEvent e) {
|
| int newPageIndex = tabFolder.indexOf((CTabItem) e.item);
|
| pageChange(newPageIndex);
|
| }
|
| });
|
|
|
| createPages();
|
| if (getPageCount() > 0 && getActivePage() == -1)
|
| setActivePage(0);
|
|
|
| // register frameworklistchanged listener
|
| FrameworkManager.getInstance().addFrameworkListChangedListener(this);
|
|
|
| // add toolbar actions
|
| addToolbarActions();
|
| }
|
|
|
| private void addToolbarActions()
|
| {
|
| // add save action
|
| getViewSite().getActionBars().getToolBarManager().add(new SaveAllBuffersAction(this));
|
| getViewSite().getActionBars().getToolBarManager().add(new SaveOutputAction(this));
|
|
|
| Action action = new Action("Clear buffer")
|
| {
|
| public void run()
|
| {
|
| int outerindex = getActivePage();
|
| int innerindex = ((DummyPage)getItem( outerindex ).getData()).internalFolder.getSelectionIndex();
|
| DummyPage p = ((DummyPage)getItem( outerindex ).getData());
|
| if (innerindex == 0)
|
| {
|
| // clear the default buffer
|
| p.clear();
|
| }
|
| else
|
| {
|
| // we want to clear a core buffer
|
| try
|
| {
|
| DummyPage.DummyBufferPage pp = (DummyPage.DummyBufferPage) ((DummyPage)getItem( outerindex ).getData()).internalFolder.getItem(innerindex).getData();
|
| IFramework fw = FrameworkManager.getInstance().getFramework(p.frameworkId);
|
| ((StringWriter)BufferStore.getBuffer(fw.getTopmodel(), pp.key)).getBuffer().setLength(0);
|
| ((StringWriter)BufferStore.getBuffer(fw.getTopmodel(), pp.key)).getBuffer().trimToSize();
|
| p.refreshContent();
|
| }
|
| catch (IOException e) { } // impossible
|
| catch (URISyntaxException e) { } // impossible
|
| }
|
| }
|
| };
|
| action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
|
| action.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
|
| //action.setHoverImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_HOVER));
|
| action.setToolTipText("Clear output");
|
| getViewSite().getActionBars().getToolBarManager().add(action);
|
|
|
| // add buffer toggle action
|
| getViewSite().getActionBars().getToolBarManager().add(new ToggleAction(this));
|
| }
|
|
|
| public DummyPage getCurrentOuterPage()
|
| {
|
| int outerindex = getActivePage();
|
| DummyPage p = ((DummyPage)getItem( outerindex ).getData());
|
| return p;
|
| }
|
|
|
| public DummyPage.DummyBufferPage getCurrentInnerPage()
|
| {
|
| // XXX kludgy hack alert
|
| try {
|
| int outerindex = getActivePage();
|
| int innerindex = ((DummyPage)getItem( outerindex ).getData()).internalFolder.getSelectionIndex();
|
| return (DummyPage.DummyBufferPage) ((DummyPage)getItem( outerindex ).getData()).internalFolder.getItem(innerindex).getData();
|
| }
|
| catch (Exception e)
|
| {
|
| return null;
|
| }
|
| }
|
|
|
| protected void createPages()
|
| {
|
| for (String id : FrameworkManager.getInstance().getAllFrameWorks())
|
| {
|
| createNewPage(id);
|
| }
|
| }
|
|
|
| private void createNewPage(String _id)
|
| {
|
| final String id = _id;
|
|
|
| // Display.getDefault().syncExec(new Runnable(){
|
|
|
| // public void run() {
|
| DummyPage p = new DummyPage(id);
|
| pages.put(id, p);
|
| int index = getPageCount();
|
| Control control = p.createPageControl(getTabFolder());
|
| CTabItem item = new CTabItem(getTabFolder(), SWT.NONE, index);
|
| item.setControl(control);
|
| item.setData(p);
|
| p.externalTabItem = item;
|
| setPageText(index, id);
|
| // }
|
|
|
| // });
|
| }
|
|
|
| private void removePage(String _id)
|
| {
|
| final String id = _id;
|
| Display.getDefault().syncExec(new Runnable(){
|
|
|
| public void run()
|
| {
|
| DummyPage p = pages.get(id);
|
| if (p!=null)
|
| {
|
| p.disposePage();
|
| pages.remove(id);
|
| }
|
| }
|
|
|
| });
|
| }
|
|
|
| protected void pageChange(int newPageIndex)
|
| {
|
| // currently empty
|
| }
|
|
|
|
|
| @Override
|
| public void setFocus() {
|
| setFocus(getActivePage());
|
| }
|
|
|
| private void setFocus(int pageIndex)
|
| {
|
| // currently empty
|
| }
|
|
|
| public void frameworkAdded(final IFramework f)
|
| {
|
| Display.getDefault().syncExec(new Runnable(){
|
|
|
| public void run() {
|
| // add a new page
|
| createNewPage(f.getId());
|
| // set it as active
|
| setActivePage(getPageCount()-1);
|
| }
|
| });
|
| }
|
|
|
| public void frameworkRemoved(final IFramework f)
|
| {
|
| Display.getDefault().syncExec(new Runnable(){
|
|
|
| public void run() {
|
| // remove page
|
| removePage(f.getId());
|
|
|
| }
|
| });
|
| }
|
| |
| |
| @Override |
| public void dispose() { |
| FrameworkManager.getInstance().removeFrameworkListChangedListener(this); |
| for (String id : pages.keySet()) { |
| removePage(id); |
| } |
| super.dispose(); |
| }
|
|
|
| }
|