Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Stieber2012-08-09 05:15:06 -0400
committerUwe Stieber2012-08-09 05:15:06 -0400
commitec531d22c43aa3ecae948aa20b4defdfe4877ace (patch)
tree62b34c1a929c62d3553ac9ce27520b4b686fe0d9 /target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views
parent2c9db2f4f297f92647e3c36d8d46d5d9f7b26ae9 (diff)
downloadorg.eclipse.tcf-ec531d22c43aa3ecae948aa20b4defdfe4877ace.tar.gz
org.eclipse.tcf-ec531d22c43aa3ecae948aa20b4defdfe4877ace.tar.xz
org.eclipse.tcf-ec531d22c43aa3ecae948aa20b4defdfe4877ace.zip
Target Explorer: Unify properties editor page save handling and provide StatusHandlerUtil
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/help/IContextHelpIds.java5
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.java1
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.properties6
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/ScriptPad.java1226
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/actions/PlayAction.java361
5 files changed, 784 insertions, 815 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/help/IContextHelpIds.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/help/IContextHelpIds.java
index 0dd002483..56c3082ec 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/help/IContextHelpIds.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/help/IContextHelpIds.java
@@ -32,6 +32,11 @@ public interface IContextHelpIds {
public final static String SCRIPT_PAD_ERROR_OPEN_FILE = PREFIX + "ScriptPadErrorOpenFile"; //$NON-NLS-1$
/**
+ * Script Pad error: save failed
+ */
+ public final static String SCRIPT_PAD_ERROR_SAVE_FILE = PREFIX + "ScriptPadErrorSaveFile"; //$NON-NLS-1$
+
+ /**
* Script Pad error: script play failed
*/
public final static String SCRIPT_PAD_ERROR_PLAY_FAILED = PREFIX + "ScriptPadErrorPlayFailed"; //$NON-NLS-1$
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.java
index d5ad2f1ff..8302b62b3 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.java
@@ -35,6 +35,7 @@ public class Messages extends NLS {
public static String Monitor_Console_name_with_peer;
public static String ScriptPad_error_title;
+ public static String ScriptPad_error_possibleCause;
public static String ScriptPad_error_openFile;
public static String ScriptPad_error_saveFile;
}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.properties b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.properties
index d8069d3f0..012778f30 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.properties
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/nls/Messages.properties
@@ -14,6 +14,6 @@ Monitor_Console_name=Monitor Console
Monitor_Console_name_with_peer=Monitor Console: {0} [{1}]
ScriptPad_error_title=Error
-ScriptPad_error_openFile=Failed to open file ''{0}''.\n\nPossible cause:\n{1}
-ScriptPad_error_saveFile=Failed to save file ''{0}''.\n\nPossible cause:\n{1}
-
+ScriptPad_error_possibleCause=Possible Cause:\n{0}
+ScriptPad_error_openFile=Failed to open file ''{0}''.\n\n{1}
+ScriptPad_error_saveFile=Failed to save file ''{0}''.\n\n{1}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/ScriptPad.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/ScriptPad.java
index 4608430b2..b0f3fee33 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/ScriptPad.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/ScriptPad.java
@@ -1,627 +1,599 @@
-/*******************************************************************************
- * Copyright (c) 2011, 2012 Wind River Systems, Inc. and others. 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:
- * Wind River Systems - initial API and implementation
- *******************************************************************************/
-package org.eclipse.tcf.te.tcf.ui.views.scriptpad;
-
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.eclipse.core.runtime.Assert;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Path;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.jface.action.GroupMarker;
-import org.eclipse.jface.action.IMenuListener;
-import org.eclipse.jface.action.IMenuManager;
-import org.eclipse.jface.action.IToolBarManager;
-import org.eclipse.jface.action.MenuManager;
-import org.eclipse.jface.action.Separator;
-import org.eclipse.jface.resource.JFaceResources;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
-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.osgi.util.NLS;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.CaretEvent;
-import org.eclipse.swt.custom.CaretListener;
-import org.eclipse.swt.custom.StyledText;
-import org.eclipse.swt.dnd.DND;
-import org.eclipse.swt.dnd.DropTarget;
-import org.eclipse.swt.dnd.DropTargetAdapter;
-import org.eclipse.swt.dnd.DropTargetEvent;
-import org.eclipse.swt.dnd.FileTransfer;
-import org.eclipse.swt.dnd.TextTransfer;
-import org.eclipse.swt.dnd.Transfer;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.FileDialog;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Menu;
-import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
-import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
-import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerManager;
-import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandler;
-import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandlerConstants;
-import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
-import org.eclipse.tcf.te.tcf.ui.views.activator.UIPlugin;
-import org.eclipse.tcf.te.tcf.ui.views.help.IContextHelpIds;
-import org.eclipse.tcf.te.tcf.ui.views.nls.Messages;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.CopyAction;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.CutAction;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.DeleteAction;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.PasteAction;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.PeerAction;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.SelectAllAction;
-import org.eclipse.tcf.te.ui.swt.DisplayUtil;
-import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
-import org.eclipse.ui.IActionBars;
-import org.eclipse.ui.ISaveablePart;
-import org.eclipse.ui.IWorkbenchActionConstants;
-import org.eclipse.ui.IWorkbenchPartConstants;
-import org.eclipse.ui.actions.ActionFactory;
-import org.eclipse.ui.part.ViewPart;
-
-/**
- * Script Pad view implementation.
- */
-public class ScriptPad extends ViewPart implements ISelectionProvider, SelectionListener, ISaveablePart {
- // Reference to the header line
- private Label head;
- // Reference to the Text widget
- /* default */ StyledText text;
- // Reference to the line style listener
- private ScriptPadLineStyleListener lineStyleListener;
-
- // The list of registered selection changed listeners
- private final List<ISelectionChangedListener> listeners = new ArrayList<ISelectionChangedListener>();
-
- // Reference to the selected peer model
- /* default */ IPeerModel peerModel;
-
- // References to the global action handlers
- private CutAction cutHandler;
- private CopyAction copyHandler;
- private PasteAction pasteHandler;
- /* default */ DeleteAction deleteHandler;
- private SelectAllAction selectAllHandler;
-
- // If the user loaded a script either via the "Open" action or DnD, remember the file name
- // so the user can save it back.
- private String fileLoaded = null;
-
- // The dirty state
- private boolean dirty = false;
-
- /**
- * Constructor.
- */
- public ScriptPad() {
- super();
- listeners.clear();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.part.WorkbenchPart#dispose()
- */
- @Override
- public void dispose() {
- if (text != null && !text.isDisposed()) {
- text.removeSelectionListener(this);
- if (lineStyleListener != null) {
- text.removeLineStyleListener(lineStyleListener);
- lineStyleListener.dispose();
- lineStyleListener = null;
- }
- }
- listeners.clear();
- fileLoaded = null;
- super.dispose();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
- */
- @Override
- public void createPartControl(Composite parent) {
- Composite panel = new Composite(parent, SWT.NONE);
- GridLayout layout = new GridLayout();
- layout.marginHeight = 0; layout.marginWidth = 0;
- panel.setLayout(layout);
- panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-
- // Create the head label
- head = new Label(panel, SWT.HORIZONTAL);
- head.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-
- // Create the StyledText widget
- text = new StyledText(panel, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
- text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- text.setFont(JFaceResources.getTextFont());
- text.addSelectionListener(this);
- text.addCaretListener(new CaretListener() {
- @Override
- public void caretMoved(CaretEvent event) {
- if (deleteHandler != null) deleteHandler.updateEnabledState();
- }
- });
- text.addModifyListener(new ModifyListener() {
- @Override
- public void modifyText(ModifyEvent e) {
- if (!isDirty()) markDirty(true);
- }
- });
-
- lineStyleListener = new ScriptPadLineStyleListener();
- text.addLineStyleListener(lineStyleListener);
-
- // Register ourselves as selection provider
- getViewSite().setSelectionProvider(this);
-
- // Create the context menu
- createContextMenu();
- // Create the toolbar
- createToolbar();
-
- // Hook the global actions
- hookGlobalActions();
-
- // Setup Drag n Drop
- setupDnD();
-
- // Update the action bars
- getViewSite().getActionBars().updateActionBars();
- }
-
- /**
- * Creates the views context menu and register it to enable contributions.
- */
- private void createContextMenu() {
- // Create menu manager.
- MenuManager menuMgr = new MenuManager();
- menuMgr.setRemoveAllWhenShown(true);
- menuMgr.addMenuListener(new IMenuListener() {
- @Override
- public void menuAboutToShow(IMenuManager mgr) {
- fillContextMenu(mgr);
- }
- });
-
- // Create menu.
- Menu menu = menuMgr.createContextMenu(text);
- text.setMenu(menu);
-
- // Register menu for extension.
- getSite().registerContextMenu(menuMgr, this);
- }
-
- /**
- * Fill the context menu.
- *
- * @param manager The menu manager. Must not be <code>null</code>.
- */
- protected void fillContextMenu(IMenuManager manager) {
- Assert.isNotNull(manager);
- }
-
- /**
- * Hook the global actions.
- */
- protected void hookGlobalActions() {
- IActionBars actionBars = getViewSite().getActionBars();
-
- cutHandler = new CutAction(this);
- actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutHandler);
-
- copyHandler = new CopyAction(this);
- actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyHandler);
-
- pasteHandler = new PasteAction(this);
- actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteHandler);
-
- deleteHandler = new DeleteAction(this);
- actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteHandler);
-
- selectAllHandler = new SelectAllAction(this);
- actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), selectAllHandler);
-
- updateActionEnablements();
- }
-
- /**
- * Creates the views toolbar.
- */
- private void createToolbar() {
- IToolBarManager manager = getViewSite().getActionBars().getToolBarManager();
- manager.add(new Separator());
- manager.add(new GroupMarker("open")); //$NON-NLS-1$
- manager.add(new GroupMarker("save")); //$NON-NLS-1$
- manager.add(new Separator());
- manager.add(new GroupMarker("clear")); //$NON-NLS-1$
- manager.add(new Separator());
- manager.add(new GroupMarker("play")); //$NON-NLS-1$
- manager.add(new Separator());
- manager.add(new GroupMarker("peers")); //$NON-NLS-1$
- manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
- }
-
- /**
- * Setup Drag n Drop support.
- */
- private void setupDnD() {
- DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_COPY);
- target.setTransfer(new Transfer[] { TextTransfer.getInstance(), FileTransfer.getInstance() });
- target.addDropListener(new DropTargetAdapter() {
-
- @Override
- public void drop(DropTargetEvent event) {
- if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
- String[] names = (String[]) FileTransfer.getInstance().nativeToJava(event.currentDataType);
- if (names != null && names.length > 0) {
- openFile(names[0]);
- }
- }
- else if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
- String content = (String) TextTransfer.getInstance().nativeToJava(event.currentDataType);
- if (content != null && !"".equals(content)) { //$NON-NLS-1$
- if (text != null && !text.isDisposed()) text.setText(content);
- }
- }
- }
-
- @Override
- public void dragOperationChanged(DropTargetEvent event) {
- if (event.detail == DND.DROP_DEFAULT) event.detail = DND.DROP_COPY;
- }
-
- @Override
- public void dragEnter(DropTargetEvent event) {
- if (event.detail == DND.DROP_DEFAULT) event.detail = DND.DROP_COPY;
- }
- });
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
- */
- @Override
- public void setFocus() {
- if (text != null) text.setFocus();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
- */
- @Override
- public void addSelectionChangedListener(ISelectionChangedListener listener) {
- Assert.isNotNull(listener);
- if (!listeners.contains(listener)) listeners.add(listener);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
- */
- @Override
- public void removeSelectionChangedListener(ISelectionChangedListener listener) {
- Assert.isNotNull(listener);
- listeners.remove(listener);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
- */
- @Override
- public ISelection getSelection() {
- String selected = text.getSelectionText();
- return selected != null ? new StructuredSelection(selected) : null;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
- */
- @Override
- public void setSelection(ISelection selection) {
- if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
- Object element = ((IStructuredSelection)selection).getFirstElement();
- if (element instanceof String) {
- int start = text.getText().indexOf((String)element);
- if (start != -1) {
- text.setSelection(start, start + ((String)element).length());
- }
- }
- } else {
- text.setSelection(text.getCaretOffset(), text.getCaretOffset());
- }
-
- // Fire the selection changed event
- fireSelectionChanged();
- updateActionEnablements();
- }
-
- /**
- * Notify the registered selection changed listener about a changed selection.
- */
- private void fireSelectionChanged() {
- if (getSelection() == null) return;
-
- SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection());
- Iterator<ISelectionChangedListener> iterator = listeners.iterator();
- while (iterator.hasNext()) {
- ISelectionChangedListener listener = iterator.next();
- listener.selectionChanged(event);
- }
- }
-
- /**
- * Update the action enablements
- */
- public void updateActionEnablements() {
- if (cutHandler != null) cutHandler.updateEnabledState();
- if (copyHandler != null) copyHandler.updateEnabledState();
- if (pasteHandler != null) pasteHandler.updateEnabledState();
- if (deleteHandler != null) deleteHandler.updateEnabledState();
- if (selectAllHandler != null) selectAllHandler.updateEnabledState();
- }
-
- /**
- * Update the head label
- */
- protected void updateHeadLabel() {
- StringBuilder label = new StringBuilder();
-
- if (fileLoaded == null) {
- label.append("<no file>"); //$NON-NLS-1$
- } else {
- IPath path = new Path(fileLoaded);
- label.append(path.lastSegment());
- }
-
- label.append(" - "); //$NON-NLS-1$
-
- if (peerModel == null) {
- label.append("<no peer>"); //$NON-NLS-1$
- } else {
- PeerAction action = new PeerAction(this, peerModel);
- label.append(action.getText());
- }
-
- SWTControlUtil.setText(head, label.toString());
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
- */
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
- */
- @Override
- public void widgetSelected(SelectionEvent e) {
- fireSelectionChanged();
- updateActionEnablements();
- }
-
- /**
- * Sets the peer model.
- *
- * @param peerModel The peer model or <code>null</code>.
- */
- public void setPeerModel(IPeerModel peerModel) {
- this.peerModel = peerModel;
- // Update the head label
- updateHeadLabel();
- // Update the action bars
- getViewSite().getActionBars().updateActionBars();
- // Fire a property change (in the UI Thread)
- DisplayUtil.safeAsyncExec(new Runnable() {
- @SuppressWarnings("synthetic-access")
- @Override
- public void run() {
- firePropertyChange(IWorkbenchPartConstants.PROP_INPUT);
- }
- });
- }
-
- /**
- * Returns the associated peer model.
- *
- * @return The associated peer model.
- */
- public IPeerModel getPeerModel() {
- return peerModel;
- }
-
- /**
- * Returns the styled text widget.
- *
- * @return The styled text widget or <code>null</code>.
- */
- public StyledText getStyledText() {
- return text;
- }
-
- /**
- * Sets the dirty state.
- *
- * @param dirty <code>True</code> to mark the view dirty, <code>false</code> otherwise.
- */
- public void markDirty(boolean dirty) {
- if (this.dirty != dirty) {
- this.dirty = dirty;
- firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.ISaveablePart#isDirty()
- */
- @Override
- public boolean isDirty() {
- return dirty;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
- */
- @Override
- public void doSave(IProgressMonitor monitor) {
- if (fileLoaded != null) saveFile(fileLoaded);
- else doSaveAs();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.ISaveablePart#doSaveAs()
- */
- @Override
- public void doSaveAs() {
- FileDialog dialog = new FileDialog(getViewSite().getShell(), SWT.SAVE);
- dialog.setFilterExtensions(new String[] { "*.tcf", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
- dialog.setFilterPath(fileLoaded != null ? fileLoaded : System.getProperty("user.home")); //$NON-NLS-1$
- String file = dialog.open();
- if (file != null) {
- saveFile(file);
- // Save succeeded ?
- if (!isDirty()) {
- fileLoaded = file;
- updateHeadLabel();
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
- */
- @Override
- public boolean isSaveAsAllowed() {
- return true;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
- */
- @Override
- public boolean isSaveOnCloseNeeded() {
- return isDirty();
- }
-
- /**
- * Clears all content and reset the loaded file path.
- */
- public void clear() {
- this.fileLoaded = null;
- updateHeadLabel();
- if (text != null && !text.isDisposed()) text.setText(""); //$NON-NLS-1$
- }
-
- /**
- * Opens the file.
- *
- * @param file The absolute file name of the script to load. Must not be <code>null</code>.
- */
- public void openFile(String file) {
- Assert.isNotNull(file);
-
- // The file name must be absolute and denote a readable file
- File f = new File(file);
- if (f.isAbsolute() && f.isFile() && f.canRead()) {
- // Remember the file name
- this.fileLoaded = file;
- updateHeadLabel();
- // Clear out the old text
- text.setText(""); //$NON-NLS-1$
-
- FileReader reader = null;
- try {
- reader = new FileReader(f);
- StringBuilder buffer = new StringBuilder();
- int c;
- while ((c = reader.read()) != -1) {
- buffer.append((char)c);
- }
- text.setText(buffer.toString());
- markDirty(false);
- } catch (IOException e) {
- IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
- NLS.bind(Messages.ScriptPad_error_openFile, file, e.getLocalizedMessage()), e);
-
- IStatusHandler[] handlers = StatusHandlerManager.getInstance().getHandler(this);
- if (handlers.length > 0) {
- IPropertiesContainer data = new PropertiesContainer();
- data.setProperty(IStatusHandlerConstants.PROPERTY_TITLE, Messages.ScriptPad_error_title);
- data.setProperty(IStatusHandlerConstants.PROPERTY_CONTEXT_HELP_ID, IContextHelpIds.SCRIPT_PAD_ERROR_OPEN_FILE);
- data.setProperty(IStatusHandlerConstants.PROPERTY_CALLER, this);
-
- handlers[0].handleStatus(status, data, null);
- } else {
- UIPlugin.getDefault().getLog().log(status);
- }
- } finally {
- if (reader != null) try { reader.close(); } catch (IOException e) { /* ignored on purpose */ }
- }
- }
- updateActionEnablements();
- }
-
- /**
- * Saves the file.
- *
- * @param file The absolute file name to save the script to. Must not be <code>null</code>.
- */
- public void saveFile(String file) {
- Assert.isNotNull(file);
-
- // The file name must be absolute and denote a writable file
- File f = new File(file);
- if (f.isAbsolute() && ((f.exists() && f.isFile() && f.canWrite() || !f.exists()))) {
- String content = text.getText();
-
- FileWriter writer = null;
- try {
- writer = new FileWriter(f);
- writer.write(content);
- markDirty(false);
- } catch (Exception e) {
- IStatus status = new Status(IStatus.ERROR, UIPlugin.getUniqueIdentifier(),
- NLS.bind(Messages.ScriptPad_error_saveFile, file, e.getLocalizedMessage()), e);
-
- IStatusHandler[] handlers = StatusHandlerManager.getInstance().getHandler(this);
- if (handlers.length > 0) {
- IPropertiesContainer data = new PropertiesContainer();
- data.setProperty(IStatusHandlerConstants.PROPERTY_TITLE, Messages.ScriptPad_error_title);
- data.setProperty(IStatusHandlerConstants.PROPERTY_CONTEXT_HELP_ID, IContextHelpIds.SCRIPT_PAD_ERROR_OPEN_FILE);
- data.setProperty(IStatusHandlerConstants.PROPERTY_CALLER, this);
-
- handlers[0].handleStatus(status, data, null);
- } else {
- UIPlugin.getDefault().getLog().log(status);
- }
- } finally {
- if (writer != null) try { writer.close(); } catch (IOException e) { /* ignored on purpose */ }
- }
- }
- updateActionEnablements();
- }
-}
+/*******************************************************************************
+ * Copyright (c) 2011, 2012 Wind River Systems, Inc. and others. 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:
+ * Wind River Systems - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tcf.te.tcf.ui.views.scriptpad;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.action.GroupMarker;
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+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.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CaretEvent;
+import org.eclipse.swt.custom.CaretListener;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DropTarget;
+import org.eclipse.swt.dnd.DropTargetAdapter;
+import org.eclipse.swt.dnd.DropTargetEvent;
+import org.eclipse.swt.dnd.FileTransfer;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerUtil;
+import org.eclipse.tcf.te.runtime.utils.StatusHelper;
+import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
+import org.eclipse.tcf.te.tcf.ui.views.help.IContextHelpIds;
+import org.eclipse.tcf.te.tcf.ui.views.nls.Messages;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.CopyAction;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.CutAction;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.DeleteAction;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.PasteAction;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.PeerAction;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions.SelectAllAction;
+import org.eclipse.tcf.te.ui.swt.DisplayUtil;
+import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
+import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.ISaveablePart;
+import org.eclipse.ui.IWorkbenchActionConstants;
+import org.eclipse.ui.IWorkbenchPartConstants;
+import org.eclipse.ui.actions.ActionFactory;
+import org.eclipse.ui.part.ViewPart;
+
+/**
+ * Script Pad view implementation.
+ */
+public class ScriptPad extends ViewPart implements ISelectionProvider, SelectionListener, ISaveablePart {
+ // Reference to the header line
+ private Label head;
+ // Reference to the Text widget
+ /* default */ StyledText text;
+ // Reference to the line style listener
+ private ScriptPadLineStyleListener lineStyleListener;
+
+ // The list of registered selection changed listeners
+ private final List<ISelectionChangedListener> listeners = new ArrayList<ISelectionChangedListener>();
+
+ // Reference to the selected peer model
+ /* default */ IPeerModel peerModel;
+
+ // References to the global action handlers
+ private CutAction cutHandler;
+ private CopyAction copyHandler;
+ private PasteAction pasteHandler;
+ /* default */ DeleteAction deleteHandler;
+ private SelectAllAction selectAllHandler;
+
+ // If the user loaded a script either via the "Open" action or DnD, remember the file name
+ // so the user can save it back.
+ private String fileLoaded = null;
+
+ // The dirty state
+ private boolean dirty = false;
+
+ /**
+ * Constructor.
+ */
+ public ScriptPad() {
+ super();
+ listeners.clear();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.part.WorkbenchPart#dispose()
+ */
+ @Override
+ public void dispose() {
+ if (text != null && !text.isDisposed()) {
+ text.removeSelectionListener(this);
+ if (lineStyleListener != null) {
+ text.removeLineStyleListener(lineStyleListener);
+ lineStyleListener.dispose();
+ lineStyleListener = null;
+ }
+ }
+ listeners.clear();
+ fileLoaded = null;
+ super.dispose();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
+ */
+ @Override
+ public void createPartControl(Composite parent) {
+ Composite panel = new Composite(parent, SWT.NONE);
+ GridLayout layout = new GridLayout();
+ layout.marginHeight = 0; layout.marginWidth = 0;
+ panel.setLayout(layout);
+ panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+
+ // Create the head label
+ head = new Label(panel, SWT.HORIZONTAL);
+ head.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+
+ // Create the StyledText widget
+ text = new StyledText(panel, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
+ text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ text.setFont(JFaceResources.getTextFont());
+ text.addSelectionListener(this);
+ text.addCaretListener(new CaretListener() {
+ @Override
+ public void caretMoved(CaretEvent event) {
+ if (deleteHandler != null) deleteHandler.updateEnabledState();
+ }
+ });
+ text.addModifyListener(new ModifyListener() {
+ @Override
+ public void modifyText(ModifyEvent e) {
+ if (!isDirty()) markDirty(true);
+ }
+ });
+
+ lineStyleListener = new ScriptPadLineStyleListener();
+ text.addLineStyleListener(lineStyleListener);
+
+ // Register ourselves as selection provider
+ getViewSite().setSelectionProvider(this);
+
+ // Create the context menu
+ createContextMenu();
+ // Create the toolbar
+ createToolbar();
+
+ // Hook the global actions
+ hookGlobalActions();
+
+ // Setup Drag n Drop
+ setupDnD();
+
+ // Update the action bars
+ getViewSite().getActionBars().updateActionBars();
+ }
+
+ /**
+ * Creates the views context menu and register it to enable contributions.
+ */
+ private void createContextMenu() {
+ // Create menu manager.
+ MenuManager menuMgr = new MenuManager();
+ menuMgr.setRemoveAllWhenShown(true);
+ menuMgr.addMenuListener(new IMenuListener() {
+ @Override
+ public void menuAboutToShow(IMenuManager mgr) {
+ fillContextMenu(mgr);
+ }
+ });
+
+ // Create menu.
+ Menu menu = menuMgr.createContextMenu(text);
+ text.setMenu(menu);
+
+ // Register menu for extension.
+ getSite().registerContextMenu(menuMgr, this);
+ }
+
+ /**
+ * Fill the context menu.
+ *
+ * @param manager The menu manager. Must not be <code>null</code>.
+ */
+ protected void fillContextMenu(IMenuManager manager) {
+ Assert.isNotNull(manager);
+ }
+
+ /**
+ * Hook the global actions.
+ */
+ protected void hookGlobalActions() {
+ IActionBars actionBars = getViewSite().getActionBars();
+
+ cutHandler = new CutAction(this);
+ actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), cutHandler);
+
+ copyHandler = new CopyAction(this);
+ actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyHandler);
+
+ pasteHandler = new PasteAction(this);
+ actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), pasteHandler);
+
+ deleteHandler = new DeleteAction(this);
+ actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteHandler);
+
+ selectAllHandler = new SelectAllAction(this);
+ actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), selectAllHandler);
+
+ updateActionEnablements();
+ }
+
+ /**
+ * Creates the views toolbar.
+ */
+ private void createToolbar() {
+ IToolBarManager manager = getViewSite().getActionBars().getToolBarManager();
+ manager.add(new Separator());
+ manager.add(new GroupMarker("open")); //$NON-NLS-1$
+ manager.add(new GroupMarker("save")); //$NON-NLS-1$
+ manager.add(new Separator());
+ manager.add(new GroupMarker("clear")); //$NON-NLS-1$
+ manager.add(new Separator());
+ manager.add(new GroupMarker("play")); //$NON-NLS-1$
+ manager.add(new Separator());
+ manager.add(new GroupMarker("peers")); //$NON-NLS-1$
+ manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
+ }
+
+ /**
+ * Setup Drag n Drop support.
+ */
+ private void setupDnD() {
+ DropTarget target = new DropTarget(text, DND.DROP_DEFAULT | DND.DROP_COPY);
+ target.setTransfer(new Transfer[] { TextTransfer.getInstance(), FileTransfer.getInstance() });
+ target.addDropListener(new DropTargetAdapter() {
+
+ @Override
+ public void drop(DropTargetEvent event) {
+ if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
+ String[] names = (String[]) FileTransfer.getInstance().nativeToJava(event.currentDataType);
+ if (names != null && names.length > 0) {
+ openFile(names[0]);
+ }
+ }
+ else if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) {
+ String content = (String) TextTransfer.getInstance().nativeToJava(event.currentDataType);
+ if (content != null && !"".equals(content)) { //$NON-NLS-1$
+ if (text != null && !text.isDisposed()) text.setText(content);
+ }
+ }
+ }
+
+ @Override
+ public void dragOperationChanged(DropTargetEvent event) {
+ if (event.detail == DND.DROP_DEFAULT) event.detail = DND.DROP_COPY;
+ }
+
+ @Override
+ public void dragEnter(DropTargetEvent event) {
+ if (event.detail == DND.DROP_DEFAULT) event.detail = DND.DROP_COPY;
+ }
+ });
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
+ */
+ @Override
+ public void setFocus() {
+ if (text != null) text.setFocus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
+ */
+ @Override
+ public void addSelectionChangedListener(ISelectionChangedListener listener) {
+ Assert.isNotNull(listener);
+ if (!listeners.contains(listener)) listeners.add(listener);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
+ */
+ @Override
+ public void removeSelectionChangedListener(ISelectionChangedListener listener) {
+ Assert.isNotNull(listener);
+ listeners.remove(listener);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
+ */
+ @Override
+ public ISelection getSelection() {
+ String selected = text.getSelectionText();
+ return selected != null ? new StructuredSelection(selected) : null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
+ */
+ @Override
+ public void setSelection(ISelection selection) {
+ if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
+ Object element = ((IStructuredSelection)selection).getFirstElement();
+ if (element instanceof String) {
+ int start = text.getText().indexOf((String)element);
+ if (start != -1) {
+ text.setSelection(start, start + ((String)element).length());
+ }
+ }
+ } else {
+ text.setSelection(text.getCaretOffset(), text.getCaretOffset());
+ }
+
+ // Fire the selection changed event
+ fireSelectionChanged();
+ updateActionEnablements();
+ }
+
+ /**
+ * Notify the registered selection changed listener about a changed selection.
+ */
+ private void fireSelectionChanged() {
+ if (getSelection() == null) return;
+
+ SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection());
+ Iterator<ISelectionChangedListener> iterator = listeners.iterator();
+ while (iterator.hasNext()) {
+ ISelectionChangedListener listener = iterator.next();
+ listener.selectionChanged(event);
+ }
+ }
+
+ /**
+ * Update the action enablements
+ */
+ public void updateActionEnablements() {
+ if (cutHandler != null) cutHandler.updateEnabledState();
+ if (copyHandler != null) copyHandler.updateEnabledState();
+ if (pasteHandler != null) pasteHandler.updateEnabledState();
+ if (deleteHandler != null) deleteHandler.updateEnabledState();
+ if (selectAllHandler != null) selectAllHandler.updateEnabledState();
+ }
+
+ /**
+ * Update the head label
+ */
+ protected void updateHeadLabel() {
+ StringBuilder label = new StringBuilder();
+
+ if (fileLoaded == null) {
+ label.append("<no file>"); //$NON-NLS-1$
+ } else {
+ IPath path = new Path(fileLoaded);
+ label.append(path.lastSegment());
+ }
+
+ label.append(" - "); //$NON-NLS-1$
+
+ if (peerModel == null) {
+ label.append("<no peer>"); //$NON-NLS-1$
+ } else {
+ PeerAction action = new PeerAction(this, peerModel);
+ label.append(action.getText());
+ }
+
+ SWTControlUtil.setText(head, label.toString());
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ fireSelectionChanged();
+ updateActionEnablements();
+ }
+
+ /**
+ * Sets the peer model.
+ *
+ * @param peerModel The peer model or <code>null</code>.
+ */
+ public void setPeerModel(IPeerModel peerModel) {
+ this.peerModel = peerModel;
+ // Update the head label
+ updateHeadLabel();
+ // Update the action bars
+ getViewSite().getActionBars().updateActionBars();
+ // Fire a property change (in the UI Thread)
+ DisplayUtil.safeAsyncExec(new Runnable() {
+ @SuppressWarnings("synthetic-access")
+ @Override
+ public void run() {
+ firePropertyChange(IWorkbenchPartConstants.PROP_INPUT);
+ }
+ });
+ }
+
+ /**
+ * Returns the associated peer model.
+ *
+ * @return The associated peer model.
+ */
+ public IPeerModel getPeerModel() {
+ return peerModel;
+ }
+
+ /**
+ * Returns the styled text widget.
+ *
+ * @return The styled text widget or <code>null</code>.
+ */
+ public StyledText getStyledText() {
+ return text;
+ }
+
+ /**
+ * Sets the dirty state.
+ *
+ * @param dirty <code>True</code> to mark the view dirty, <code>false</code> otherwise.
+ */
+ public void markDirty(boolean dirty) {
+ if (this.dirty != dirty) {
+ this.dirty = dirty;
+ firePropertyChange(IWorkbenchPartConstants.PROP_DIRTY);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.ISaveablePart#isDirty()
+ */
+ @Override
+ public boolean isDirty() {
+ return dirty;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public void doSave(IProgressMonitor monitor) {
+ if (fileLoaded != null) saveFile(fileLoaded);
+ else doSaveAs();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.ISaveablePart#doSaveAs()
+ */
+ @Override
+ public void doSaveAs() {
+ FileDialog dialog = new FileDialog(getViewSite().getShell(), SWT.SAVE);
+ dialog.setFilterExtensions(new String[] { "*.tcf", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
+ dialog.setFilterPath(fileLoaded != null ? fileLoaded : System.getProperty("user.home")); //$NON-NLS-1$
+ String file = dialog.open();
+ if (file != null) {
+ saveFile(file);
+ // Save succeeded ?
+ if (!isDirty()) {
+ fileLoaded = file;
+ updateHeadLabel();
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
+ */
+ @Override
+ public boolean isSaveAsAllowed() {
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.ISaveablePart#isSaveOnCloseNeeded()
+ */
+ @Override
+ public boolean isSaveOnCloseNeeded() {
+ return isDirty();
+ }
+
+ /**
+ * Clears all content and reset the loaded file path.
+ */
+ public void clear() {
+ this.fileLoaded = null;
+ updateHeadLabel();
+ if (text != null && !text.isDisposed()) text.setText(""); //$NON-NLS-1$
+ }
+
+ /**
+ * Opens the file.
+ *
+ * @param file The absolute file name of the script to load. Must not be <code>null</code>.
+ */
+ public void openFile(String file) {
+ Assert.isNotNull(file);
+
+ // The file name must be absolute and denote a readable file
+ File f = new File(file);
+ if (f.isAbsolute() && f.isFile() && f.canRead()) {
+ // Remember the file name
+ this.fileLoaded = file;
+ updateHeadLabel();
+ // Clear out the old text
+ text.setText(""); //$NON-NLS-1$
+
+ FileReader reader = null;
+ try {
+ reader = new FileReader(f);
+ StringBuilder buffer = new StringBuilder();
+ int c;
+ while ((c = reader.read()) != -1) {
+ buffer.append((char)c);
+ }
+ text.setText(buffer.toString());
+ markDirty(false);
+ } catch (IOException e) {
+ String template = NLS.bind(Messages.ScriptPad_error_openFile, file, Messages.ScriptPad_error_possibleCause);
+ StatusHandlerUtil.handleStatus(StatusHelper.getStatus(e), this, template,
+ Messages.ScriptPad_error_title, IContextHelpIds.SCRIPT_PAD_ERROR_OPEN_FILE, this, null);
+ } finally {
+ if (reader != null) try { reader.close(); } catch (IOException e) { /* ignored on purpose */ }
+ }
+ }
+ updateActionEnablements();
+ }
+
+ /**
+ * Saves the file.
+ *
+ * @param file The absolute file name to save the script to. Must not be <code>null</code>.
+ */
+ public void saveFile(String file) {
+ Assert.isNotNull(file);
+
+ // The file name must be absolute and denote a writable file
+ File f = new File(file);
+ if (f.isAbsolute() && ((f.exists() && f.isFile() && f.canWrite() || !f.exists()))) {
+ String content = text.getText();
+
+ FileWriter writer = null;
+ try {
+ writer = new FileWriter(f);
+ writer.write(content);
+ markDirty(false);
+ } catch (Exception e) {
+ String template = NLS.bind(Messages.ScriptPad_error_saveFile, file, Messages.ScriptPad_error_possibleCause);
+ StatusHandlerUtil.handleStatus(StatusHelper.getStatus(e), this, template,
+ Messages.ScriptPad_error_title, IContextHelpIds.SCRIPT_PAD_ERROR_SAVE_FILE, this, null);
+ } finally {
+ if (writer != null) try { writer.close(); } catch (IOException e) { /* ignored on purpose */ }
+ }
+ }
+ updateActionEnablements();
+ }
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/actions/PlayAction.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/actions/PlayAction.java
index a2d5fc9af..19762debb 100644
--- a/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/actions/PlayAction.java
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.ui.views/src/org/eclipse/tcf/te/tcf/ui/views/scriptpad/actions/PlayAction.java
@@ -1,185 +1,176 @@
-/*******************************************************************************
- * Copyright (c) 2011 Wind River Systems, Inc. and others. 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:
- * Wind River Systems - initial API and implementation
- *******************************************************************************/
-package org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions;
-
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.tcf.protocol.IPeer;
-import org.eclipse.tcf.protocol.Protocol;
-import org.eclipse.tcf.te.runtime.callback.Callback;
-import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
-import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
-import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerManager;
-import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandler;
-import org.eclipse.tcf.te.runtime.statushandler.interfaces.IStatusHandlerConstants;
-import org.eclipse.tcf.te.tcf.core.scripting.interfaces.IScriptLauncherProperties;
-import org.eclipse.tcf.te.tcf.core.scripting.launcher.ScriptLauncher;
-import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
-import org.eclipse.tcf.te.tcf.ui.views.help.IContextHelpIds;
-import org.eclipse.tcf.te.tcf.ui.views.nls.Messages;
-import org.eclipse.tcf.te.tcf.ui.views.scriptpad.ScriptPad;
-import org.eclipse.ui.IActionDelegate2;
-import org.eclipse.ui.IPropertyListener;
-import org.eclipse.ui.IViewActionDelegate;
-import org.eclipse.ui.IViewPart;
-import org.eclipse.ui.IWorkbenchPartConstants;
-
-/**
- * Play script action implementation.
- */
-public class PlayAction extends Action implements IViewActionDelegate, IActionDelegate2 {
- // Reference to the action proxy
- /* default */ IAction actionProxy;
- // Parent view part
- /* default */ IViewPart view;
-
- // Reference to the view property listener
- private IPropertyListener listener;
-
- // Flag to remember if a script is currently running
- /* default */ boolean running;
-
- /**
- * Constructor.
- */
- public PlayAction() {
- super();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
- */
- @Override
- public void init(IViewPart view) {
- this.view = view;
-
- if (listener == null) {
- listener = new IPropertyListener() {
- @Override
- public void propertyChanged(Object source, int propId) {
- if (IWorkbenchPartConstants.PROP_INPUT == propId && actionProxy != null) {
- // Update the action enablement
- boolean enabled = false;
- if (PlayAction.this.view instanceof ScriptPad) enabled = ((ScriptPad)PlayAction.this.view).getPeerModel() != null;
- actionProxy.setEnabled(enabled && !running);
- }
- }
- };
- view.addPropertyListener(listener);
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
- */
- @Override
- public void init(IAction action) {
- this.actionProxy = action;
- if (action != null) {
- boolean enabled = false;
- if (view instanceof ScriptPad) enabled = ((ScriptPad)view).getPeerModel() != null;
- action.setEnabled(enabled && !running);
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jface.action.Action#run()
- */
- @Override
- public void run() {
- String script = null;
- IPeerModel peerModel = null;
-
- if (view instanceof ScriptPad && ((ScriptPad)view).getStyledText() != null) {
- script = ((ScriptPad)view).getStyledText().getText();
- peerModel = ((ScriptPad)view).getPeerModel();
- }
-
- if (script != null && !"".equals(script) && peerModel != null) { //$NON-NLS-1$
- final ScriptLauncher launcher = new ScriptLauncher();
-
- IPropertiesContainer properties = new PropertiesContainer();
- properties.setProperty(IScriptLauncherProperties.PROP_SCRIPT, script);
-
- final AtomicReference<IPeer> peer = new AtomicReference<IPeer>();
- final IPeerModel finPeerModel = peerModel;
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- peer.set(finPeerModel.getPeer());
- }
- };
- if (Protocol.isDispatchThread()) runnable.run();
- else Protocol.invokeAndWait(runnable);
-
- running = true;
-
- launcher.launch(peer.get(), properties, new Callback() {
- @Override
- protected void internalDone(Object caller, IStatus status) {
- running = false;
- launcher.dispose();
-
- boolean enabled = false;
- if (PlayAction.this.view instanceof ScriptPad) enabled = ((ScriptPad)PlayAction.this.view).getPeerModel() != null;
- actionProxy.setEnabled(enabled && !running);
-
- if (status != null && (status.getSeverity() == IStatus.ERROR || status.getSeverity() == IStatus.WARNING)) {
- IStatusHandler[] handler = StatusHandlerManager.getInstance().getHandler(view);
- if (handler != null && handler.length > 0) {
- IPropertiesContainer data = new PropertiesContainer();
- data.setProperty(IStatusHandlerConstants.PROPERTY_TITLE, Messages.ScriptPad_error_title);
- data.setProperty(IStatusHandlerConstants.PROPERTY_CONTEXT_HELP_ID, IContextHelpIds.SCRIPT_PAD_ERROR_PLAY_FAILED);
- data.setProperty(IStatusHandlerConstants.PROPERTY_CALLER, view);
-
- handler[0].handleStatus(status, data, null);
- }
- }
- }
- });
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
- */
- @Override
- public void run(IAction action) {
- run();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
- */
- @Override
- public void runWithEvent(IAction action, Event event) {
- runWithEvent(event);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
- */
- @Override
- public void selectionChanged(IAction action, ISelection selection) {
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate2#dispose()
- */
- @Override
- public void dispose() {
- if (listener != null) view.removePropertyListener(listener);
- }
-}
+/*******************************************************************************
+ * Copyright (c) 2011 Wind River Systems, Inc. and others. 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:
+ * Wind River Systems - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.tcf.te.tcf.ui.views.scriptpad.actions;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.tcf.protocol.IPeer;
+import org.eclipse.tcf.protocol.Protocol;
+import org.eclipse.tcf.te.runtime.callback.Callback;
+import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
+import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
+import org.eclipse.tcf.te.runtime.statushandler.StatusHandlerUtil;
+import org.eclipse.tcf.te.tcf.core.scripting.interfaces.IScriptLauncherProperties;
+import org.eclipse.tcf.te.tcf.core.scripting.launcher.ScriptLauncher;
+import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
+import org.eclipse.tcf.te.tcf.ui.views.help.IContextHelpIds;
+import org.eclipse.tcf.te.tcf.ui.views.nls.Messages;
+import org.eclipse.tcf.te.tcf.ui.views.scriptpad.ScriptPad;
+import org.eclipse.ui.IActionDelegate2;
+import org.eclipse.ui.IPropertyListener;
+import org.eclipse.ui.IViewActionDelegate;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPartConstants;
+
+/**
+ * Play script action implementation.
+ */
+public class PlayAction extends Action implements IViewActionDelegate, IActionDelegate2 {
+ // Reference to the action proxy
+ /* default */ IAction actionProxy;
+ // Parent view part
+ /* default */ IViewPart view;
+
+ // Reference to the view property listener
+ private IPropertyListener listener;
+
+ // Flag to remember if a script is currently running
+ /* default */ boolean running;
+
+ /**
+ * Constructor.
+ */
+ public PlayAction() {
+ super();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
+ */
+ @Override
+ public void init(IViewPart view) {
+ this.view = view;
+
+ if (listener == null) {
+ listener = new IPropertyListener() {
+ @Override
+ public void propertyChanged(Object source, int propId) {
+ if (IWorkbenchPartConstants.PROP_INPUT == propId && actionProxy != null) {
+ // Update the action enablement
+ boolean enabled = false;
+ if (PlayAction.this.view instanceof ScriptPad) enabled = ((ScriptPad)PlayAction.this.view).getPeerModel() != null;
+ actionProxy.setEnabled(enabled && !running);
+ }
+ }
+ };
+ view.addPropertyListener(listener);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
+ */
+ @Override
+ public void init(IAction action) {
+ this.actionProxy = action;
+ if (action != null) {
+ boolean enabled = false;
+ if (view instanceof ScriptPad) enabled = ((ScriptPad)view).getPeerModel() != null;
+ action.setEnabled(enabled && !running);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.action.Action#run()
+ */
+ @Override
+ public void run() {
+ String script = null;
+ IPeerModel peerModel = null;
+
+ if (view instanceof ScriptPad && ((ScriptPad)view).getStyledText() != null) {
+ script = ((ScriptPad)view).getStyledText().getText();
+ peerModel = ((ScriptPad)view).getPeerModel();
+ }
+
+ if (script != null && !"".equals(script) && peerModel != null) { //$NON-NLS-1$
+ final ScriptLauncher launcher = new ScriptLauncher();
+
+ IPropertiesContainer properties = new PropertiesContainer();
+ properties.setProperty(IScriptLauncherProperties.PROP_SCRIPT, script);
+
+ final AtomicReference<IPeer> peer = new AtomicReference<IPeer>();
+ final IPeerModel finPeerModel = peerModel;
+ Runnable runnable = new Runnable() {
+ @Override
+ public void run() {
+ peer.set(finPeerModel.getPeer());
+ }
+ };
+ if (Protocol.isDispatchThread()) runnable.run();
+ else Protocol.invokeAndWait(runnable);
+
+ running = true;
+
+ launcher.launch(peer.get(), properties, new Callback() {
+ @Override
+ protected void internalDone(Object caller, IStatus status) {
+ running = false;
+ launcher.dispose();
+
+ boolean enabled = false;
+ if (PlayAction.this.view instanceof ScriptPad) enabled = ((ScriptPad)PlayAction.this.view).getPeerModel() != null;
+ actionProxy.setEnabled(enabled && !running);
+
+ if (status != null && (status.getSeverity() == IStatus.ERROR || status.getSeverity() == IStatus.WARNING)) {
+ StatusHandlerUtil.handleStatus(status, view, null,
+ Messages.ScriptPad_error_title, IContextHelpIds.SCRIPT_PAD_ERROR_PLAY_FAILED, this, null);
+ }
+ }
+ });
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
+ */
+ @Override
+ public void run(IAction action) {
+ run();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
+ */
+ @Override
+ public void runWithEvent(IAction action, Event event) {
+ runWithEvent(event);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
+ */
+ @Override
+ public void selectionChanged(IAction action, ISelection selection) {
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate2#dispose()
+ */
+ @Override
+ public void dispose() {
+ if (listener != null) view.removePropertyListener(listener);
+ }
+}

Back to the top