SCViewer/com.minres.scviewer.ui/src/com/minres/scviewer/ui/views/TxOutlinePage.java

223 lines
8.1 KiB
Java
Raw Normal View History

2012-06-17 20:34:50 +02:00
/*******************************************************************************
* Copyright (c) 2014, 2015 MINRES Technologies GmbH and others.
2012-06-17 20:34:50 +02:00
* 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:
* MINRES Technologies GmbH - initial API and implementation
2012-06-17 20:34:50 +02:00
*******************************************************************************/
package com.minres.scviewer.ui.views;
2012-06-17 19:53:05 +02:00
2015-01-20 18:50:15 +01:00
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
2015-01-01 23:17:32 +01:00
import java.util.LinkedList;
import org.eclipse.jface.action.Action;
2012-06-17 19:53:05 +02:00
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.ISelectionListener;
2015-01-01 23:17:32 +01:00
import org.eclipse.ui.ISharedImages;
2012-06-17 19:53:05 +02:00
import org.eclipse.ui.IWorkbenchPart;
2015-01-01 23:17:32 +01:00
import org.eclipse.ui.PlatformUI;
2012-06-17 19:53:05 +02:00
import org.eclipse.ui.part.IPageSite;
import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
2015-01-06 17:14:16 +01:00
import com.minres.scviewer.database.IHierNode;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.IWaveformEvent;
import com.minres.scviewer.ui.TxEditorPart;
import com.minres.scviewer.ui.views.provider.TxDbTreeContentProvider;
import com.minres.scviewer.ui.views.provider.TxDbTreeLabelProvider;
2012-06-17 19:53:05 +02:00
/**
* Creates an outline pagebook for this editor.
*/
2015-01-20 18:50:15 +01:00
public class TxOutlinePage extends ContentOutlinePage implements ISelectionListener, ISelectionProvider, PropertyChangeListener {
2012-06-17 19:53:05 +02:00
2015-01-01 23:17:32 +01:00
public static final int ADD_TO_WAVE = 0;
public static final int ADD_ALL_TO_WAVE = 1;
public static final int REMOVE_FROM_WAVE = 2;
public static final int REMOVE_ALL_FROM_WAVE = 3;
2012-06-17 19:53:05 +02:00
private TxEditorPart editor;
2015-01-20 18:50:15 +01:00
TreeViewer contentOutlineViewer ;
2012-06-17 19:53:05 +02:00
public TxOutlinePage(TxEditorPart editor) {
this.editor = editor;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ui.part.IPage#createControl(org.eclipse.swt.widgets.Composite
* )
*/
public void createControl(Composite parent) {
super.createControl(parent);
2015-01-20 18:50:15 +01:00
contentOutlineViewer = getTreeViewer();
2012-06-17 19:53:05 +02:00
contentOutlineViewer.addSelectionChangedListener(this);
// Set up the tree viewer
contentOutlineViewer.setContentProvider(new TxDbTreeContentProvider());
contentOutlineViewer.setLabelProvider(new TxDbTreeLabelProvider());
contentOutlineViewer.setInput(editor.getDatabase());
// initialize context menu depending on the the selectec item
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(contentOutlineViewer.getControl());
contentOutlineViewer.getTree().setMenu(menu);
getSite().registerContextMenu("com.minres.scviewer.ui.outline.contextmenu", menuMgr, contentOutlineViewer);
2012-06-17 19:53:05 +02:00
// add me as selection listener
getSite().getPage().addSelectionListener((ISelectionListener) this);
//getSite().getPage().addSelectionListener("SampleViewId",(ISelectionListener)this);
getSite().setSelectionProvider(this);
2015-01-20 18:50:15 +01:00
editor.getDatabase().addPropertyChangeListener(this);
2012-06-17 19:53:05 +02:00
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.part.IPage#dispose()
*/
public void dispose() {
// dispose
super.dispose();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.part.IPage#getControl()
*/
public Control getControl() {
return getTreeViewer().getControl();
}
/**
* @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
*/
public void init(IPageSite pageSite) {
super.init(pageSite);
// IActionBars bars = pageSite.getActionBars();
2012-06-17 19:53:05 +02:00
}
private void fillContextMenu(IMenuManager menuMgr) {
// initalize the context menu
getTreeViewer().getSelection();
ISelection selection = getTreeViewer().getSelection();
2015-01-01 23:17:32 +01:00
if(selection instanceof IStructuredSelection){
IStructuredSelection sel = (IStructuredSelection) selection;
Object obj = sel.getFirstElement();
2015-01-06 17:14:16 +01:00
menuMgr.add(makeStreamAction("Add to Wave", ISharedImages.IMG_OBJ_ADD, sel, obj instanceof IWaveform, false));
2015-01-01 23:17:32 +01:00
menuMgr.add(makeStreamAction("Add all to Wave", ISharedImages.IMG_OBJ_ADD, sel, true, false));
2015-01-06 17:14:16 +01:00
// menuMgr.add(makeStreamAction("Remove from Wave", ISharedImages.IMG_TOOL_DELETE, sel, obj instanceof IWaveform, true));
// menuMgr.add(makeStreamAction("Remove all from Wave", ISharedImages.IMG_TOOL_DELETE, sel, true, true));
2015-01-01 23:17:32 +01:00
}
2012-06-17 19:53:05 +02:00
}
//ISelectionListener methods
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
2015-01-01 23:17:32 +01:00
// if(!(part instanceof ContentOutline) && selection instanceof IStructuredSelection){
// if(((IStructuredSelection)selection).getFirstElement() instanceof ITransaction){
// System.out.println("Transaction with id "+((ITransaction)((IStructuredSelection)selection).getFirstElement()).getId() +" selected");
// } else if(((IStructuredSelection)selection).getFirstElement() != null)
// System.out.println("Something else selected");
// }
2012-06-17 19:53:05 +02:00
}
/**
* Returns the current selection for this provider.
*
* @return the current selection
*/
public ISelection getSelection() {
if (getTreeViewer() == null) {
return StructuredSelection.EMPTY;
}
return getTreeViewer().getSelection();
}
public void setSelection(ISelection selection){
if (getTreeViewer() != null) {
getTreeViewer().setSelection(selection);
}
}
/**
* @see org.eclipse.jface.viewers.ISelectionProvider#selectionChanged(SelectionChangedEvent)
*/
public void selectionChanged(SelectionChangedEvent anEvent) {
// translate the tree selection
ISelection selection = anEvent.getSelection();
if (!selection.isEmpty()) {
Object tmp = ((IStructuredSelection) selection).getFirstElement();
2015-01-06 17:14:16 +01:00
if (tmp instanceof IHierNode) {
fireSelectionChanged(new StructuredSelection((IHierNode) tmp));
2012-06-17 19:53:05 +02:00
}
}
}
2015-01-01 23:17:32 +01:00
private Action makeStreamAction(String text, String imgDescriptor, final IStructuredSelection selection, boolean enabled, final boolean remove) {
Action action = new Action() {
@SuppressWarnings("unchecked")
2015-01-01 23:17:32 +01:00
public void run() {
if(selection!=null)
for(Object obj :selection.toArray()){
2015-01-06 17:14:16 +01:00
if(obj instanceof IWaveform){
2015-01-01 23:17:32 +01:00
if(remove)
editor.removeStreamFromList((IWaveform<? extends IWaveformEvent>) obj);
2015-01-01 23:17:32 +01:00
else
editor.addStreamToList((IWaveform<? extends IWaveformEvent>) obj);
2015-01-06 17:14:16 +01:00
} else if(obj instanceof IHierNode){
LinkedList<IHierNode> queue = new LinkedList<IHierNode>();
LinkedList<IWaveform<? extends IWaveformEvent>> streams = new LinkedList<IWaveform<? extends IWaveformEvent>>();
2015-01-06 17:14:16 +01:00
queue.add((IHierNode)obj);
2015-01-01 23:17:32 +01:00
while(queue.size()>0){
2015-01-06 17:14:16 +01:00
IHierNode n = queue.poll();
if(n instanceof IWaveform) streams.add((IWaveform<? extends IWaveformEvent>) n);
2015-01-01 23:17:32 +01:00
queue.addAll(n.getChildNodes());
}
if(remove)
2015-01-06 17:14:16 +01:00
editor.removeStreamsFromList(streams.toArray(new IWaveform[]{}));
2015-01-01 23:17:32 +01:00
else
2015-01-06 17:14:16 +01:00
editor.addStreamsToList(streams.toArray(new IWaveform[]{}));
2015-01-01 23:17:32 +01:00
}
}
}
};
action.setText(text);
action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(imgDescriptor));
2015-01-06 17:14:16 +01:00
if(selection.getFirstElement() instanceof IWaveform && editor.getStreamList().contains(selection.getFirstElement()))
2015-01-01 23:17:32 +01:00
action.setEnabled(false);
else
action.setEnabled(true);
return action;
}
2015-01-20 18:50:15 +01:00
@Override
public void propertyChange(PropertyChangeEvent evt) {
if("CHILDS".equals(evt.getPropertyName())) {
contentOutlineViewer.refresh();
}
}
2012-06-17 19:53:05 +02:00
}