From 97d6043d036f6dc739c779f54a8830abe4452ff8 Mon Sep 17 00:00:00 2001 From: John Cortell Date: Mon, 14 May 2007 15:53:36 +0000 Subject: [PATCH] Applied patch for 186405. Fixed some problems with breakpoint actions. --- .../BreakpointActionManager.java | 51 ++- .../breakpointactions/IBreakpointAction.java | 90 ++--- .../breakpointactions/ExternalToolAction.java | 28 +- .../debug/ui/breakpointactions/LogAction.java | 20 +- .../ui/breakpointactions/ResumeAction.java | 52 ++- .../ui/breakpointactions/SoundAction.java | 366 +++++++++--------- .../ui/breakpointactions/messages.properties | 96 ++--- 7 files changed, 416 insertions(+), 287 deletions(-) diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/BreakpointActionManager.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/BreakpointActionManager.java index 911a81d4d97..1df5de7c943 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/BreakpointActionManager.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/BreakpointActionManager.java @@ -14,7 +14,6 @@ import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.util.ArrayList; import java.util.Iterator; -import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -31,7 +30,11 @@ import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.jobs.Job; import org.eclipse.debug.core.model.IBreakpoint; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -96,22 +99,52 @@ public class BreakpointActionManager { return false; } - public void executeActions(IBreakpoint breakpoint, IAdaptable context) { - + public void executeActions(final IBreakpoint breakpoint, final IAdaptable context) { if (breakpoint != null) { IMarker marker = breakpoint.getMarker(); String actionNames = marker.getAttribute(BREAKPOINT_ACTION_ATTRIBUTE, ""); //$NON-NLS-1$ - StringTokenizer tok = new StringTokenizer(actionNames, ","); //$NON-NLS-1$ - while (tok.hasMoreTokens()) { - String actionName = tok.nextToken(); - IBreakpointAction action = findBreakpointAction(actionName); - if (action != null) { - action.execute(breakpoint, context); + final String[] actions = actionNames.split(","); + if (actions.length > 0){ + Job job = new Job("Execute breakpoint actions") { + public IStatus run(final IProgressMonitor monitor) { + return doExecuteActions(breakpoint, context, actions, monitor); + } + }; + job.schedule(); + try { + // wait for actions to execute + job.join(); + }catch (InterruptedException e) + { + e.printStackTrace(); } } } } + private IStatus doExecuteActions(final IBreakpoint breakpoint, final IAdaptable context, String[] actions, IProgressMonitor monitor) { + try { + for (int i = 0; i < actions.length && !monitor.isCanceled(); i++) { + String actionName = actions[i]; + IBreakpointAction action = findBreakpointAction(actionName); + if (action != null) { + monitor.setTaskName(action.getSummary()); + IStatus status = action.execute(breakpoint, context, monitor); + if (status.getCode() != IStatus.OK) { + // do not log status if user canceled. + if (status.getCode() != IStatus.CANCEL) + CDebugCorePlugin.log(status); + return status; + } + } + monitor.worked(1); + } + } catch (Exception e) { + return new Status( IStatus.ERROR, CDebugCorePlugin.getUniqueIdentifier(), CDebugCorePlugin.INTERNAL_ERROR, "Internal Error", e ); + } + return monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS; + } + public IBreakpointAction findBreakpointAction(String name) { for (Iterator iter = getBreakpointActions().iterator(); iter.hasNext();) { IBreakpointAction action = (IBreakpointAction) iter.next(); diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/IBreakpointAction.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/IBreakpointAction.java index d7f35c45338..5e5ae73f633 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/IBreakpointAction.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/breakpointactions/IBreakpointAction.java @@ -1,44 +1,46 @@ -/******************************************************************************* - * Copyright (c) 2007 Nokia 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: - * Nokia - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.debug.core.breakpointactions; - -import org.eclipse.core.runtime.IAdaptable; -import org.eclipse.debug.core.model.IBreakpoint; - -/** - * Interface implemented by plug-ins that wish to contribute breakpoint actions. - * - * THIS INTERFACE IS PROVISIONAL AND WILL CHANGE IN THE FUTURE BREAKPOINT ACTION - * CONTRIBUTIONS USING THIS INTERFACE WILL NEED TO BE REVISED TO WORK WITH - * FUTURE VERSIONS OF CDT. - * - */ -public interface IBreakpointAction { - - public void execute(IBreakpoint breakpoint, IAdaptable context); - - public String getMemento(); - - public void initializeFromMemento(String data); - - public String getDefaultName(); - - public String getSummary(); - - public String getTypeName(); - - public String getIdentifier(); - - public String getName(); - - public void setName(String name); - -} +/******************************************************************************* + * Copyright (c) 2007 Nokia 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: + * Nokia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.debug.core.breakpointactions; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.debug.core.model.IBreakpoint; + +/** + * Interface implemented by plug-ins that wish to contribute breakpoint actions. + * + * THIS INTERFACE IS PROVISIONAL AND WILL CHANGE IN THE FUTURE BREAKPOINT ACTION + * CONTRIBUTIONS USING THIS INTERFACE WILL NEED TO BE REVISED TO WORK WITH + * FUTURE VERSIONS OF CDT. + * + */ +public interface IBreakpointAction { + + public IStatus execute(IBreakpoint breakpoint, IAdaptable context, IProgressMonitor monitor); + + public String getMemento(); + + public void initializeFromMemento(String data); + + public String getDefaultName(); + + public String getSummary(); + + public String getTypeName(); + + public String getIdentifier(); + + public String getName(); + + public void setName(String name); + +} diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ExternalToolAction.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ExternalToolAction.java index 60c71e8de04..c0efb683a1d 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ExternalToolAction.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ExternalToolAction.java @@ -22,9 +22,15 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.eclipse.cdt.debug.core.CDIDebugModel; import org.eclipse.cdt.debug.core.breakpointactions.AbstractBreakpointAction; +import org.eclipse.cdt.debug.internal.core.ICDebugInternalConstants; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchManager; @@ -39,18 +45,38 @@ public class ExternalToolAction extends AbstractBreakpointAction { private String externalToolName = new String(""); //$NON-NLS-1$ - public void execute(IBreakpoint breakpoint, IAdaptable context) { + public IStatus execute(IBreakpoint breakpoint, IAdaptable context, IProgressMonitor monitor) { + IStatus errorStatus = null; ILaunchManager lcm = DebugPlugin.getDefault().getLaunchManager(); try { + boolean launched = false; ILaunchConfiguration[] launchConfigurations = lcm.getLaunchConfigurations(); for (int i = 0; i < launchConfigurations.length; i++) { if (launchConfigurations[i].getName().equals(externalToolName)) { DebugUITools.launch(launchConfigurations[i], ILaunchManager.RUN_MODE); + launched = true; + break; } } + if (!launched) { + String errorMsg = MessageFormat.format(Messages.getString("ExternalToolAction.error.0"), new Object[] { externalToolName }); //$NON-NLS-1$ + errorStatus = new Status( IStatus.ERROR, CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, errorMsg, null); + } + } catch (CoreException e) { + errorStatus = e.getStatus(); + } catch (Exception e) { + errorStatus = new Status( IStatus.ERROR, CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, e.getMessage(), e ); + } + + if (errorStatus != null) { + String errorMsg = MessageFormat.format(Messages.getString("ExternalToolAction.error.1"), new Object[] { externalToolName }); //$NON-NLS-1$ + MultiStatus ms = new MultiStatus( CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, errorMsg, null ); + ms.add(errorStatus); + return ms; } + return Status.OK_STATUS; } public String getDefaultName() { diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/LogAction.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/LogAction.java index b35aa49eee5..76c94e30a6f 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/LogAction.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/LogAction.java @@ -11,8 +11,8 @@ package org.eclipse.cdt.debug.ui.breakpointactions; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.StringReader; +import java.text.MessageFormat; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -22,9 +22,14 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.eclipse.cdt.debug.core.CDIDebugModel; import org.eclipse.cdt.debug.core.breakpointactions.AbstractBreakpointAction; import org.eclipse.cdt.debug.core.breakpointactions.ILogActionEnabler; +import org.eclipse.cdt.debug.internal.core.ICDebugInternalConstants; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.model.IBreakpoint; import org.eclipse.ui.console.ConsolePlugin; import org.eclipse.ui.console.IConsole; @@ -49,7 +54,12 @@ public class LogAction extends AbstractBreakpointAction { this.evaluateExpression = evaluateExpression; } - public void execute(IBreakpoint breakpoint, IAdaptable context) { + /* + * (non-Javadoc) + * @see org.eclipse.cdt.debug.core.breakpointactions.IBreakpointAction#execute(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.runtime.IAdaptable, org.eclipse.core.runtime.IProgressMonitor) + */ + public IStatus execute(IBreakpoint breakpoint, IAdaptable context, IProgressMonitor monitor) { + IStatus result = Status.OK_STATUS; try { openConsole(Messages.getString("LogAction.ConsoleTitle")); //$NON-NLS-1$ String logMessage = getMessage(); @@ -63,11 +73,11 @@ public class LogAction extends AbstractBreakpointAction { MessageConsoleStream stream = console.newMessageStream(); stream.println(logMessage); stream.close(); - } catch (IOException e) { - e.printStackTrace(); } catch (Exception e) { - e.printStackTrace(); + String errorMsg = MessageFormat.format(Messages.getString("LogAction.error.0"), new Object[] {getSummary()}); //$NON-NLS-1$ + result = new Status( IStatus.ERROR, CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, errorMsg, e ); } + return result; } private void openConsole(String consoleName) { diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ResumeAction.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ResumeAction.java index bb2f7c02316..77370235dc7 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ResumeAction.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/ResumeAction.java @@ -22,9 +22,17 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import org.eclipse.cdt.debug.core.CDIDebugModel; import org.eclipse.cdt.debug.core.breakpointactions.AbstractBreakpointAction; import org.eclipse.cdt.debug.core.breakpointactions.IResumeActionEnabler; +import org.eclipse.cdt.debug.internal.core.ICDebugInternalConstants; +import org.eclipse.cdt.debug.internal.ui.IInternalCDebugUIConstants; +import org.eclipse.cdt.debug.ui.CDebugUIPlugin; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.model.IBreakpoint; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -33,16 +41,50 @@ import org.xml.sax.helpers.DefaultHandler; public class ResumeAction extends AbstractBreakpointAction { + final int INCRIMENT_MSEC = 100; + int pauseTime = 0; - - public void execute(IBreakpoint breakpoint, IAdaptable context) { + + /* + * (non-Javadoc) + * @see org.eclipse.cdt.debug.core.breakpointactions.IBreakpointAction#execute(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.runtime.IAdaptable, org.eclipse.core.runtime.IProgressMonitor) + */ + public IStatus execute(IBreakpoint breakpoint, IAdaptable context, IProgressMonitor monitor) { + IStatus errorStatus = null; + long endTime = System.currentTimeMillis() + getPauseTime()*1000; IResumeActionEnabler enabler = (IResumeActionEnabler) context.getAdapter(IResumeActionEnabler.class); - if (enabler != null) + + if (enabler != null) { try { - enabler.resume(); + monitor.beginTask(getName(), getPauseTime()*1000/INCRIMENT_MSEC); + + long currentTime = System.currentTimeMillis(); + while (!monitor.isCanceled() && currentTime < endTime ){ + monitor.setTaskName(MessageFormat.format(Messages.getString("ResumeAction.SummaryResumeTime"), new Object[] { new Long((endTime - currentTime)/1000) })); //$NON-NLS-1$) + monitor.worked(1); + Thread.sleep(INCRIMENT_MSEC); + currentTime = System.currentTimeMillis(); + } + + if (!monitor.isCanceled()) { + monitor.setTaskName( Messages.getString("ResumeAction.SummaryImmediately")); //$NON-NLS-1$) + enabler.resume(); + } + monitor.worked(1); } catch (Exception e) { - e.printStackTrace(); + errorStatus = new Status( IStatus.ERROR, CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, e.getMessage(), e ); } + } else + errorStatus = new Status( IStatus.ERROR, CDebugUIPlugin.getUniqueIdentifier(), IInternalCDebugUIConstants.INTERNAL_ERROR, Messages.getString("ResumeAction.error.0"), null ); //$NON-NLS-1$ + + if (errorStatus != null) { + MultiStatus ms = new MultiStatus( CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, Messages.getString("ResumeAction.error.1"), null ); //$NON-NLS-1$ + ms.add( errorStatus); + errorStatus = ms; + } else { + errorStatus = monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS; + } + return errorStatus; } public String getDefaultName() { diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/SoundAction.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/SoundAction.java index c795d93dab7..ccf87b54550 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/SoundAction.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/SoundAction.java @@ -1,178 +1,188 @@ -/******************************************************************************* - * Copyright (c) 2007 Nokia 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: - * Nokia - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.debug.ui.breakpointactions; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.StringReader; - -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioInputStream; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.SourceDataLine; -import javax.sound.sampled.UnsupportedAudioFileException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.eclipse.cdt.debug.core.breakpointactions.AbstractBreakpointAction; -import org.eclipse.core.runtime.IAdaptable; -import org.eclipse.debug.core.model.IBreakpoint; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.xml.sax.InputSource; -import org.xml.sax.helpers.DefaultHandler; - -public class SoundAction extends AbstractBreakpointAction { - - static public void playSoundFile(final File soundFile) { - - class SoundPlayer extends Thread { - - public void run() { - AudioInputStream soundStream; - try { - soundStream = AudioSystem.getAudioInputStream(soundFile); - AudioFormat audioFormat = soundStream.getFormat(); - DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat); - SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); - byte[] soundBuffer = new byte[5000]; - sourceDataLine.open(audioFormat); - sourceDataLine.start(); - int dataCount = 0; - - while ((dataCount = soundStream.read(soundBuffer, 0, soundBuffer.length)) != -1) { - if (dataCount > 0) { - sourceDataLine.write(soundBuffer, 0, dataCount); - } - } - sourceDataLine.drain(); - sourceDataLine.close(); - - } catch (UnsupportedAudioFileException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (LineUnavailableException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - - } - ; - - if (soundFile.exists()) { - - new SoundPlayer().start(); - } - } - - private File soundFile; - - public SoundAction() { - } - - public void execute(IBreakpoint breakpoint, IAdaptable context) { - playSoundFile(soundFile); - } - - public String getDefaultName() { - // if (soundFile != null) { - // return MessageFormat.format("Play {0}", new Object[] - // {soundFile.getName()}); - // } - return "Untitled Sound Action"; //$NON-NLS-1$ - } - - public File getSoundFile() { - return soundFile; - } - - public String getSummary() { - if (soundFile == null) - return new String(""); //$NON-NLS-1$ - return soundFile.getAbsolutePath(); - } - - public String getTypeName() { - return Messages.getString("SoundAction.ActionTypeName"); //$NON-NLS-1$ - } - - public String getMemento() { - String soundData = new String(""); //$NON-NLS-1$ - if (soundFile != null) { - DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); - DocumentBuilder docBuilder = null; - try { - docBuilder = dfactory.newDocumentBuilder(); - Document doc = docBuilder.newDocument(); - - Element rootElement = doc.createElement("soundData"); //$NON-NLS-1$ - rootElement.setAttribute("file", soundFile.getAbsolutePath()); //$NON-NLS-1$ - - doc.appendChild(rootElement); - - ByteArrayOutputStream s = new ByteArrayOutputStream(); - - TransformerFactory factory = TransformerFactory.newInstance(); - Transformer transformer = factory.newTransformer(); - transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ - - DOMSource source = new DOMSource(doc); - StreamResult outputTarget = new StreamResult(s); - transformer.transform(source, outputTarget); - - soundData = s.toString("UTF8"); //$NON-NLS-1$ - - } catch (Exception e) { - e.printStackTrace(); - } - } - return soundData; - } - - public void initializeFromMemento(String data) { - Element root = null; - DocumentBuilder parser; - try { - parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - parser.setErrorHandler(new DefaultHandler()); - root = parser.parse(new InputSource(new StringReader(data))).getDocumentElement(); - String value = root.getAttribute("file"); //$NON-NLS-1$ - if (value == null) - throw new Exception(); - soundFile = new File(value); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public String getIdentifier() { - return "org.eclipse.cdt.debug.ui.breakpointactions.SoundAction"; //$NON-NLS-1$ - } - - public void setSoundFile(File soundFile) { - this.soundFile = soundFile; - } - -} +/******************************************************************************* + * Copyright (c) 2007 Nokia 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: + * Nokia - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.debug.ui.breakpointactions; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.text.MessageFormat; + +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.DataLine; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.SourceDataLine; +import javax.sound.sampled.UnsupportedAudioFileException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.eclipse.cdt.debug.core.CDIDebugModel; +import org.eclipse.cdt.debug.core.breakpointactions.AbstractBreakpointAction; +import org.eclipse.cdt.debug.internal.core.ICDebugInternalConstants; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.debug.core.model.IBreakpoint; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; +import org.xml.sax.helpers.DefaultHandler; + +public class SoundAction extends AbstractBreakpointAction { + + static public void playSoundFile(final File soundFile) { + + class SoundPlayer extends Thread { + + public void run() { + AudioInputStream soundStream; + try { + soundStream = AudioSystem.getAudioInputStream(soundFile); + AudioFormat audioFormat = soundStream.getFormat(); + DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat); + SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); + byte[] soundBuffer = new byte[5000]; + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + int dataCount = 0; + + while ((dataCount = soundStream.read(soundBuffer, 0, soundBuffer.length)) != -1) { + if (dataCount > 0) { + sourceDataLine.write(soundBuffer, 0, dataCount); + } + } + sourceDataLine.drain(); + sourceDataLine.close(); + + } catch (UnsupportedAudioFileException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + } + + if (soundFile.exists()) { + new SoundPlayer().start(); + } + } + + private File soundFile; + + public SoundAction() { + } + + public IStatus execute(IBreakpoint breakpoint, IAdaptable context, IProgressMonitor monitor) { + if (soundFile == null || !soundFile.exists()) { + String errorMsg = MessageFormat.format(Messages.getString("SoundAction.error.0"), new Object[] {getSummary()}); //$NON-NLS-1$ + return new Status( IStatus.ERROR, CDIDebugModel.getPluginIdentifier(), ICDebugInternalConstants.STATUS_CODE_ERROR, errorMsg, null); + } + + playSoundFile(soundFile); + return Status.OK_STATUS; + } + + public String getDefaultName() { + // if (soundFile != null) { + // return MessageFormat.format("Play {0}", new Object[] + // {soundFile.getName()}); + // } + return "Untitled Sound Action"; //$NON-NLS-1$ + } + + public File getSoundFile() { + return soundFile; + } + + public String getSummary() { + if (soundFile == null) + return new String(""); //$NON-NLS-1$ + return soundFile.getAbsolutePath(); + } + + public String getTypeName() { + return Messages.getString("SoundAction.ActionTypeName"); //$NON-NLS-1$ + } + + public String getMemento() { + String soundData = new String(""); //$NON-NLS-1$ + if (soundFile != null) { + DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder docBuilder = null; + try { + docBuilder = dfactory.newDocumentBuilder(); + Document doc = docBuilder.newDocument(); + + Element rootElement = doc.createElement("soundData"); //$NON-NLS-1$ + rootElement.setAttribute("file", soundFile.getAbsolutePath()); //$NON-NLS-1$ + + doc.appendChild(rootElement); + + ByteArrayOutputStream s = new ByteArrayOutputStream(); + + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ + + DOMSource source = new DOMSource(doc); + StreamResult outputTarget = new StreamResult(s); + transformer.transform(source, outputTarget); + + soundData = s.toString("UTF8"); //$NON-NLS-1$ + + } catch (Exception e) { + e.printStackTrace(); + } + } + return soundData; + } + + public void initializeFromMemento(String data) { + Element root = null; + DocumentBuilder parser; + try { + parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + parser.setErrorHandler(new DefaultHandler()); + root = parser.parse(new InputSource(new StringReader(data))).getDocumentElement(); + String value = root.getAttribute("file"); //$NON-NLS-1$ + if (value == null) + throw new Exception(); + soundFile = new File(value); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public String getIdentifier() { + return "org.eclipse.cdt.debug.ui.breakpointactions.SoundAction"; //$NON-NLS-1$ + } + + public void setSoundFile(File soundFile) { + this.soundFile = soundFile; + } + +} diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/messages.properties b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/messages.properties index bcb8fe4a622..501f0843f4b 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/messages.properties +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/ui/breakpointactions/messages.properties @@ -1,45 +1,51 @@ -ActionsPropertyPage.1=Actions for this breakpoint: -ActionsPropertyPage.2=Available actions: -ActionsPreferencePage.0=Actions available for any breakpoint in the workspace: -PreferenceInitializer.1=Default value -GlobalActionsList.0=Name -GlobalActionsList.1=Type -GlobalActionsList.2=Summary -GlobalActionsList.3=Attach -GlobalActionsList.4=New... -GlobalActionsList.5=Edit... -GlobalActionsList.6=Delete -ActionsList.0=Name -ActionsList.1=Type -ActionsList.2=Summary -ActionsList.3=Remove -ActionsList.4=Up -ActionsList.5=Down -ActionDialog.0=New Breakpoint Action -ActionDialog.1=Action name: -ActionDialog.2=Action type: -SoundActionComposite.4=Select a sound to play when the breakpoint is hit: -SoundActionComposite.5=Choose a sound file: -SoundActionComposite.6=Browse... -SoundActionComposite.7=Play Sound -SoundActionComposite.9=That sound file does not exist. -SoundAction.ActionTypeName=Sound Action -LogActionComposite.0=Message to log when the breakpoint is hit: -LogActionComposite.1=Evaluate as expression -LogAction.ConsoleTitle=Log Action Messages -LogAction.UntitledName=Untitled Log Action -LogAction.TypeName=Log Action - -ExternalToolActionComposite.ToolLabel=External Tool: -ExternalToolActionComposite.DialogTitle=External Tools -ExternalToolActionComposite.DialogMessage=Choose an External Tool to run -ExternalToolActionComposite.ChooseButtonTitle=Choose... -ExternalToolActionComposite.ExternalToolsButtonTitle=External Tools... -ExternalToolAction.Summary=Run {0} -ExternalToolAction.TypeName=External Tool Action -ResumeAction.UntitledName=Untitled Resume Action -ResumeAction.SummaryImmediately=Resume immediately -ResumeActionComposite.ResumeAfterLabel=Resume after -ResumeAction.SummaryResumeTime=Resume after {0} seconds -ResumeActionComposite.Seconds=seconds -ResumeAction.TypeName=Resume Action +ActionsPropertyPage.1=Actions for this breakpoint: +ActionsPropertyPage.2=Available actions: +ActionsPreferencePage.0=Actions available for any breakpoint in the workspace: +PreferenceInitializer.1=Default value +GlobalActionsList.0=Name +GlobalActionsList.1=Type +GlobalActionsList.2=Summary +GlobalActionsList.3=Attach +GlobalActionsList.4=New... +GlobalActionsList.5=Edit... +GlobalActionsList.6=Delete +ActionsList.0=Name +ActionsList.1=Type +ActionsList.2=Summary +ActionsList.3=Remove +ActionsList.4=Up +ActionsList.5=Down +ActionDialog.0=New Breakpoint Action +ActionDialog.1=Action name: +ActionDialog.2=Action type: +SoundActionComposite.4=Select a sound to play when the breakpoint is hit: +SoundActionComposite.5=Choose a sound file: +SoundActionComposite.6=Browse... +SoundActionComposite.7=Play Sound +SoundActionComposite.9=That sound file does not exist. +SoundAction.error.0=Missing sound file "{0}" +SoundAction.ActionTypeName=Sound Action +LogActionComposite.0=Message to log when the breakpoint is hit: +LogActionComposite.1=Evaluate as expression +LogAction.ConsoleTitle=Log Action Messages +LogAction.UntitledName=Untitled Log Action +LogAction.TypeName=Log Action +LogAction.error.0=Could not execute log action: "{0}". + +ExternalToolActionComposite.ToolLabel=External Tool: +ExternalToolActionComposite.DialogTitle=External Tools +ExternalToolActionComposite.DialogMessage=Choose an External Tool to run +ExternalToolActionComposite.ChooseButtonTitle=Choose... +ExternalToolActionComposite.ExternalToolsButtonTitle=External Tools... +ExternalToolAction.Summary=Run {0} +ExternalToolAction.error.0=There is no launch configuration named "{0}". +ExternalToolAction.error.1=Could not launch "{0}". +ExternalToolAction.TypeName=External Tool Action +ResumeAction.UntitledName=Untitled Resume Action +ResumeAction.SummaryImmediately=Resume immediately +ResumeActionComposite.ResumeAfterLabel=Resume after +ResumeAction.SummaryResumeTime=Resume after {0} seconds +ResumeActionComposite.Seconds=seconds +ResumeAction.TypeName=Resume Action +ResumeAction.error.0=IResumeActionEnabler not registered in context. +ResumeAction.error.1=Could not resume.