SCViewer/com.itjw.txviewer.ui/src/com/itjw/txviewer/ui/TxEditorPart.java

230 lines
6.8 KiB
Java
Raw Normal View History

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-01 23:17:32 +01:00
package com.itjw.txviewer.ui;
2012-06-17 19:53:05 +02:00
2015-01-01 23:17:32 +01:00
import java.util.List;
2012-06-17 19:53:05 +02:00
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
2015-01-01 23:17:32 +01:00
import org.eclipse.jface.dialogs.MessageDialog;
2012-06-17 19:53:05 +02:00
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
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;
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
import com.itjw.txviewer.database.ITrDb;
import com.itjw.txviewer.database.ITrStream;
2015-01-01 23:17:32 +01:00
import com.itjw.txviewer.database.ITransactionDbFactory;
import com.itjw.txviewer.ui.swt.TxDisplay;
import com.itjw.txviewer.ui.views.TxOutlinePage;
2012-06-17 19:53:05 +02:00
public class TxEditorPart extends EditorPart implements ITabbedPropertySheetPageContributor {
2015-01-01 23:17:32 +01:00
public static final String ID = "com.itjw.txviewer.ui.TxEditorPart"; //$NON-NLS-1$
2012-06-17 19:53:05 +02:00
2015-01-01 23:17:32 +01:00
public static final String WAVE_ACTION_ID = "com.itjw.txviewer.ui.action.AddToWave";
2012-06-17 19:53:05 +02:00
2015-01-01 23:17:32 +01:00
private TxDisplay txDisplay;
2012-06-17 19:53:05 +02:00
/** This is the root of the editor's model. */
private ITrDb database;
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-01 23:17:32 +01:00
myParent=parent;
2012-06-17 19:53:05 +02:00
/** Add handlers for global actions (delete, etc) */
IActionBars actionBars = getEditorSite().getActionBars();
2015-01-01 23:17:32 +01:00
actionBars.setGlobalActionHandler(WAVE_ACTION_ID, new Action() {
2012-06-17 19:53:05 +02:00
@Override
public void runWithEvent(Event event) {
System.out.println("AddToWave with event");
}
@Override
public void run() {
System.out.println("AddToWave");
}
});
2015-01-01 23:17:32 +01:00
txDisplay = new TxDisplay(parent);
2012-06-17 19:53:05 +02:00
getSite().setSelectionProvider(txDisplay);
2015-01-01 23:17:32 +01:00
if(getEditorInput()!=null && ((TxEditorInput) getEditorInput()).getStreamNames().size()>0){
if(MessageDialog.openConfirm(parent.getShell(), "Confirm", "Do you want the restore last state of the wave form?"))
for(String streamName:((TxEditorInput) getEditorInput()).getStreamNames()){
ITrStream stream = database.getStreamByName(streamName);
if(stream!=null)
txDisplay.addStream(stream);
}
}
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-01 23:17:32 +01:00
if(input instanceof IFileEditorInput){
if(!(input instanceof TxEditorInput))
super.setInput(new TxEditorInput(((IFileEditorInput)input).getFile()));
try {
IFile file = ((IFileEditorInput) input).getFile();
getTrDatabase();
database.load(file.getContents());
setPartName(((IFileEditorInput) input).getFile().getName());
} catch (Exception e) {
handleLoadException(e);
}
} else if(input instanceof FileStoreEditorInput){
try {
getTrDatabase();
database.load(((FileStoreEditorInput) input).getURI().toURL().openStream());
setPartName(((FileStoreEditorInput) input).getName());
} catch (Exception e) {
handleLoadException(e);
}
2012-06-17 19:53:05 +02:00
}
}
2015-01-01 23:17:32 +01:00
protected void getTrDatabase() {
BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
ServiceReference<?> serviceReference = context.getServiceReference(ITransactionDbFactory.class.getName());
database = ((ITransactionDbFactory) context.getService(serviceReference)).createDatabase();
if(txDisplay !=null) database.addPropertyChangeListener(txDisplay);
}
2012-06-17 19:53:05 +02:00
private void handleLoadException(Exception e) {
System.err.println("** Load failed. Using default model. **");
e.printStackTrace();
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
return new TabbedPropertySheetPage(this);
return super.getAdapter(type);
}
public ITrDb getModel() {
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;
}
public ITrDb getDatabase() {
return database;
}
public void addStreamToList(ITrStream stream){
2015-01-01 23:17:32 +01:00
if(getEditorInput() instanceof TxEditorInput && !((TxEditorInput) getEditorInput()).getStreamNames().contains(stream.getFullName())){
((TxEditorInput) getEditorInput()).getStreamNames().add(stream.getFullName());
txDisplay.addStream(stream);
} else
txDisplay.addStream(stream);
2012-06-17 19:53:05 +02:00
}
public void addStreamsToList(ITrStream[] streams){
2015-01-01 23:17:32 +01:00
for(ITrStream stream:streams)
addStreamToList(stream);
2012-06-17 19:53:05 +02:00
}
public void removeStreamFromList(ITrStream stream){
2015-01-01 23:17:32 +01:00
if(getEditorInput() instanceof TxEditorInput && ((TxEditorInput) getEditorInput()).getStreamNames().contains(stream.getFullName())){
((TxEditorInput) getEditorInput()).getStreamNames().remove(stream.getFullName());
txDisplay.removeStream(stream);
} else
txDisplay.removeStream(stream);
2012-06-17 19:53:05 +02:00
}
public void removeStreamsFromList(ITrStream[] streams){
2015-01-01 23:17:32 +01:00
for(ITrStream stream:streams)
removeStreamFromList(stream);
2012-06-17 19:53:05 +02:00
}
2015-01-01 23:17:32 +01:00
public List<ITrStream> getStreamList(){
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
}
@Override
public String getContributorId() {
return getSite().getId();
}
}