2012-06-17 20:34:50 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2012 IT Just working.
|
|
|
|
* 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:
|
|
|
|
* IT Just working - initial API and implementation
|
|
|
|
*******************************************************************************/
|
2015-01-03 16:34:32 +01:00
|
|
|
package com.minres.scviewer.ui;
|
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-03 16:34:32 +01:00
|
|
|
import java.io.File;
|
2015-01-20 18:50:15 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.util.ArrayList;
|
2015-01-01 23:17:32 +01:00
|
|
|
import java.util.List;
|
2015-01-10 00:23:46 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2012-06-17 19:53:05 +02:00
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
import org.eclipse.core.runtime.IPath;
|
2012-06-17 19:53:05 +02:00
|
|
|
import org.eclipse.core.runtime.IProgressMonitor;
|
2015-01-01 23:17:32 +01:00
|
|
|
import org.eclipse.jface.dialogs.MessageDialog;
|
2015-01-20 18:50:15 +01:00
|
|
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
2012-06-17 19:53:05 +02:00
|
|
|
import org.eclipse.jface.viewers.ISelection;
|
2015-01-20 18:50:15 +01:00
|
|
|
import org.eclipse.swt.graphics.Color;
|
2012-06-17 19:53:05 +02:00
|
|
|
import org.eclipse.swt.widgets.Composite;
|
|
|
|
import org.eclipse.ui.IEditorInput;
|
|
|
|
import org.eclipse.ui.IEditorSite;
|
|
|
|
import org.eclipse.ui.IFileEditorInput;
|
2015-01-20 18:50:15 +01:00
|
|
|
import org.eclipse.ui.IWorkbench;
|
2012-06-17 19:53:05 +02:00
|
|
|
import org.eclipse.ui.PartInitException;
|
2015-01-03 16:34:32 +01:00
|
|
|
import org.eclipse.ui.PlatformUI;
|
2015-01-01 23:17:32 +01:00
|
|
|
import org.eclipse.ui.ide.FileStoreEditorInput;
|
2012-06-17 19:53:05 +02:00
|
|
|
import org.eclipse.ui.part.EditorPart;
|
2015-01-20 18:50:15 +01:00
|
|
|
import org.eclipse.ui.progress.IProgressService;
|
2012-06-17 19:53:05 +02:00
|
|
|
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
|
|
|
|
import org.eclipse.ui.views.properties.IPropertySheetPage;
|
|
|
|
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
|
|
|
|
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
|
2015-01-01 23:17:32 +01:00
|
|
|
import org.osgi.framework.BundleContext;
|
|
|
|
import org.osgi.framework.FrameworkUtil;
|
|
|
|
import org.osgi.framework.ServiceReference;
|
2012-06-17 19:53:05 +02:00
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
import com.minres.scviewer.database.IWaveformDb;
|
|
|
|
import com.minres.scviewer.database.ITxStream;
|
|
|
|
import com.minres.scviewer.database.IWaveform;
|
2015-01-10 00:23:46 +01:00
|
|
|
import com.minres.scviewer.database.WaveformDb;
|
|
|
|
import com.minres.scviewer.ui.handler.GotoDirection;
|
2015-01-03 16:34:32 +01:00
|
|
|
import com.minres.scviewer.ui.swt.TxDisplay;
|
|
|
|
import com.minres.scviewer.ui.views.TxOutlinePage;
|
2012-06-17 19:53:05 +02:00
|
|
|
|
|
|
|
public class TxEditorPart extends EditorPart implements ITabbedPropertySheetPageContributor {
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
public static final String ID = "com.minres.scviewer.ui.TxEditorPart"; //$NON-NLS-1$
|
2012-06-17 19:53:05 +02:00
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
public static final String WAVE_ACTION_ID = "com.minres.scviewer.ui.action.AddToWave";
|
2012-06-17 19:53:05 +02:00
|
|
|
|
2015-01-01 23:17:32 +01:00
|
|
|
private TxDisplay txDisplay;
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2012-06-17 19:53:05 +02:00
|
|
|
/** This is the root of the editor's model. */
|
2015-01-06 17:14:16 +01:00
|
|
|
private IWaveformDb database;
|
2012-06-17 19:53:05 +02:00
|
|
|
|
2015-01-01 23:17:32 +01:00
|
|
|
private Composite myParent;
|
2012-06-17 19:53:05 +02:00
|
|
|
|
|
|
|
public TxEditorPart() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create contents of the editor part.
|
|
|
|
* @param parent
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void createPartControl(Composite parent) {
|
2015-01-10 00:23:46 +01:00
|
|
|
myParent=parent;
|
2015-01-20 18:50:15 +01:00
|
|
|
database=new WaveformDb();
|
2015-01-01 23:17:32 +01:00
|
|
|
txDisplay = new TxDisplay(parent);
|
2015-01-20 18:50:15 +01:00
|
|
|
txDisplay.setMaxTime(0);
|
2012-06-17 19:53:05 +02:00
|
|
|
getSite().setSelectionProvider(txDisplay);
|
2015-01-20 18:50:15 +01:00
|
|
|
new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
loadDatabases();
|
|
|
|
} catch (InvocationTargetException | IOException | InterruptedException e) {
|
|
|
|
handleLoadException(e);
|
2015-01-01 23:17:32 +01:00
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
}
|
|
|
|
}).run();
|
|
|
|
database.addPropertyChangeListener(new PropertyChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void propertyChange(PropertyChangeEvent evt) {
|
|
|
|
if("WAVEFORMS".equals(evt.getPropertyName())) {
|
|
|
|
myParent.getDisplay().syncExec(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
txDisplay.setMaxTime(database.getMaxTime());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
|
|
|
|
*/
|
|
|
|
protected void setInput(IEditorInput input) {
|
|
|
|
super.setInput(input);
|
2015-01-20 18:50:15 +01:00
|
|
|
if(input instanceof IFileEditorInput && !(input instanceof TxEditorInput))
|
|
|
|
super.setInput(new TxEditorInput(((IFileEditorInput)input).getFile()));
|
|
|
|
setPartName(input.getName());
|
2015-01-10 00:23:46 +01:00
|
|
|
}
|
|
|
|
|
2015-01-20 18:50:15 +01:00
|
|
|
protected void loadDatabases() throws IOException, InvocationTargetException, InterruptedException {
|
|
|
|
IWorkbench wb = PlatformUI.getWorkbench();
|
|
|
|
IProgressService ps = wb.getProgressService();
|
|
|
|
IEditorInput input = getEditorInput();
|
|
|
|
File file=null;
|
|
|
|
ArrayList<File> filesToLoad=new ArrayList<File>();
|
|
|
|
if(input instanceof IFileEditorInput){
|
|
|
|
file = ((IFileEditorInput) input).getFile().getLocation().toFile();
|
|
|
|
} else if(input instanceof FileStoreEditorInput){
|
|
|
|
file=new File(((FileStoreEditorInput) input).getURI().getPath());
|
|
|
|
}
|
|
|
|
if(file.exists()){
|
|
|
|
filesToLoad.add(file);
|
2015-01-10 00:23:46 +01:00
|
|
|
String ext = getFileExtension(file.getName());
|
|
|
|
if("vcd".equals(ext.toLowerCase())){
|
2015-01-20 18:50:15 +01:00
|
|
|
if(askIfToLoad(new File(renameFileExtension(file.getCanonicalPath(), "txdb"))))
|
|
|
|
filesToLoad.add(new File(renameFileExtension(file.getCanonicalPath(), "txdb")));
|
|
|
|
else if(askIfToLoad(new File(renameFileExtension(file.getCanonicalPath(), "txlog"))))
|
|
|
|
filesToLoad.add(new File(renameFileExtension(file.getCanonicalPath(), "txlog")));
|
2015-01-10 00:23:46 +01:00
|
|
|
} else if("txdb".equals(ext.toLowerCase()) || "txlog".equals(ext.toLowerCase())){
|
2015-01-20 18:50:15 +01:00
|
|
|
if(askIfToLoad(new File(renameFileExtension(file.getCanonicalPath(), "vcd"))))
|
|
|
|
filesToLoad.add(new File(renameFileExtension(file.getCanonicalPath(), "vcd")));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
final File[] files=filesToLoad.toArray(new File[filesToLoad.size()]);
|
|
|
|
ps.busyCursorWhile(new IRunnableWithProgress() {
|
|
|
|
public void run(IProgressMonitor pm) throws InvocationTargetException {
|
|
|
|
pm.beginTask("Loading database "+files[0].getName(), files.length);
|
|
|
|
try {
|
|
|
|
database.load(files[0]);
|
|
|
|
database.addPropertyChangeListener(txDisplay);
|
|
|
|
pm.worked(1);
|
|
|
|
if(pm.isCanceled()) return;
|
|
|
|
if(files.length==2){
|
|
|
|
database.load(files[1]);
|
|
|
|
pm.worked(1);
|
|
|
|
}
|
|
|
|
myParent.getDisplay().syncExec(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
updateTxDisplay();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
database=null;
|
|
|
|
throw new InvocationTargetException(e);
|
|
|
|
}
|
|
|
|
pm.done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean askIfToLoad(File txFile) {
|
|
|
|
if(txFile.exists() &&
|
|
|
|
MessageDialog.openQuestion(myParent.getDisplay().getActiveShell(), "Database open",
|
|
|
|
"Would you like to open the adjacent database "+txFile.getName()+" as well?")){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateTxDisplay() {
|
|
|
|
txDisplay.setMaxTime(database.getMaxTime());
|
|
|
|
if(TxEditorPart.this.getEditorInput() instanceof TxEditorInput &&
|
|
|
|
((TxEditorInput) TxEditorPart.this.getEditorInput()).getStreamNames().size()>0){
|
|
|
|
for(String streamName:((TxEditorInput) TxEditorPart.this.getEditorInput()).getStreamNames()){
|
|
|
|
IWaveform stream = database.getStreamByName(streamName);
|
|
|
|
if(stream!=null)
|
|
|
|
txDisplay.getStreamList().add(stream);
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-01 23:17:32 +01:00
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
protected static String renameFileExtension(String source, String newExt) {
|
2015-01-20 18:50:15 +01:00
|
|
|
String target;
|
|
|
|
String currentExt = getFileExtension(source);
|
|
|
|
if (currentExt.equals("")){
|
|
|
|
target=source+"."+newExt;
|
|
|
|
} else {
|
|
|
|
target=source.replaceFirst(Pattern.quote("."+currentExt)+"$", Matcher.quoteReplacement("."+newExt));
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
2015-01-10 00:23:46 +01:00
|
|
|
|
|
|
|
protected static String getFileExtension(String f) {
|
2015-01-20 18:50:15 +01:00
|
|
|
String ext = "";
|
|
|
|
int i = f.lastIndexOf('.');
|
|
|
|
if (i > 0 && i < f.length() - 1) {
|
|
|
|
ext = f.substring(i + 1);
|
|
|
|
}
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
2012-06-17 19:53:05 +02:00
|
|
|
private void handleLoadException(Exception e) {
|
2015-01-20 18:50:15 +01:00
|
|
|
e.printStackTrace();
|
2015-01-03 16:34:32 +01:00
|
|
|
MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
|
|
|
|
"Error loading database", e.getMessage());
|
2012-06-17 19:53:05 +02:00
|
|
|
database = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFocus() {
|
2015-01-01 23:17:32 +01:00
|
|
|
myParent.setFocus();
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doSave(IProgressMonitor monitor) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doSaveAs() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
@Override
|
|
|
|
public Object getAdapter(Class type) {
|
|
|
|
if (type == IContentOutlinePage.class) // outline page
|
|
|
|
return new TxOutlinePage(this);
|
|
|
|
else if (type == IPropertySheetPage.class) // use tabbed property sheet instead of standard one
|
2015-01-20 18:50:15 +01:00
|
|
|
return new TabbedPropertySheetPage(this);
|
2012-06-17 19:53:05 +02:00
|
|
|
return super.getAdapter(type);
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public IWaveformDb getModel() {
|
2012-06-17 19:53:05 +02:00
|
|
|
return database;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init(IEditorSite site, IEditorInput input)
|
|
|
|
throws PartInitException {
|
|
|
|
// Initialize the editor part
|
|
|
|
setSite(site);
|
|
|
|
setInput(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isDirty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSaveAsAllowed() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public IWaveformDb getDatabase() {
|
2012-06-17 19:53:05 +02:00
|
|
|
return database;
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public void addStreamToList(IWaveform obj){
|
|
|
|
if(getEditorInput() instanceof TxEditorInput && !((TxEditorInput) getEditorInput()).getStreamNames().contains(obj.getFullName())){
|
|
|
|
((TxEditorInput) getEditorInput()).getStreamNames().add(obj.getFullName());
|
2015-01-20 18:50:15 +01:00
|
|
|
txDisplay.getStreamList().add(obj);
|
2015-01-01 23:17:32 +01:00
|
|
|
} else
|
2015-01-20 18:50:15 +01:00
|
|
|
txDisplay.getStreamList().add(obj);
|
|
|
|
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public void addStreamsToList(IWaveform[] iWaveforms){
|
|
|
|
for(IWaveform stream:iWaveforms)
|
2015-01-01 23:17:32 +01:00
|
|
|
addStreamToList(stream);
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public void removeStreamFromList(IWaveform obj){
|
|
|
|
if(getEditorInput() instanceof TxEditorInput && ((TxEditorInput) getEditorInput()).getStreamNames().contains(obj.getFullName())){
|
|
|
|
((TxEditorInput) getEditorInput()).getStreamNames().remove(obj.getFullName());
|
2015-01-20 18:50:15 +01:00
|
|
|
txDisplay.getStreamList().remove(obj);
|
2015-01-01 23:17:32 +01:00
|
|
|
} else
|
2015-01-20 18:50:15 +01:00
|
|
|
txDisplay.getStreamList().remove(obj);
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public void removeStreamsFromList(IWaveform[] iWaveforms){
|
|
|
|
for(IWaveform stream:iWaveforms)
|
2015-01-01 23:17:32 +01:00
|
|
|
removeStreamFromList(stream);
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public List<IWaveform> getStreamList(){
|
2015-01-01 23:17:32 +01:00
|
|
|
return txDisplay.getStreamList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSelection(final ISelection selection){
|
|
|
|
myParent.getDisplay().asyncExec(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
txDisplay.setSelection(selection);
|
|
|
|
}
|
|
|
|
});
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2012-06-17 19:53:05 +02:00
|
|
|
@Override
|
|
|
|
public String getContributorId() {
|
|
|
|
return getSite().getId();
|
|
|
|
}
|
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
public void moveSelection(GotoDirection next) {
|
|
|
|
txDisplay.moveSelection( next);
|
|
|
|
}
|
|
|
|
|
2012-06-17 19:53:05 +02:00
|
|
|
}
|