Skip to content

Commit f0558bc

Browse files
committed
midi play command file selection
The play command checks the currently activated view/editor and its selection and sets the audio view content accordingly.
1 parent c8553c2 commit f0558bc

2 files changed

Lines changed: 91 additions & 14 deletions

File tree

org.eclipse.ui.views.midi/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Bundle-Vendor: Open Source Community
77
Require-Bundle: org.eclipse.ui,
88
org.eclipse.core.runtime,
99
org.eclipse.ui.views.file;bundle-version="0.1.0",
10-
javax.util;bundle-version="0.0.0"
10+
javax.util;bundle-version="0.0.0",
11+
org.eclipse.util
1112
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
1213
Export-Package: org.eclipse.ui.views.midi
1314
Bundle-Activator: org.eclipse.ui.views.midi.Activator

org.eclipse.ui.views.midi/src/org/eclipse/ui/views/midi/PlayPauseHandler.java

Lines changed: 89 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,113 @@
33
import org.eclipse.core.commands.AbstractHandler;
44
import org.eclipse.core.commands.ExecutionEvent;
55
import org.eclipse.core.commands.ExecutionException;
6+
import org.eclipse.core.resources.IFile;
7+
import org.eclipse.jface.viewers.ISelection;
8+
import org.eclipse.jface.viewers.IStructuredSelection;
9+
import org.eclipse.ui.IEditorInput;
610
import org.eclipse.ui.IViewPart;
11+
import org.eclipse.ui.IWorkbenchPage;
12+
import org.eclipse.ui.IWorkbenchPart;
13+
import org.eclipse.ui.PartInitException;
14+
import org.eclipse.ui.PlatformUI;
715
import org.eclipse.ui.handlers.HandlerUtil;
16+
import org.eclipse.ui.part.EditorPart;
817
import org.eclipse.ui.views.file.FileView;
918
import org.eclipse.ui.views.file.IFileViewType;
19+
import org.eclipse.util.ResourceUtils;
1020

1121
public class PlayPauseHandler extends AbstractHandler {
1222

1323
private static final String PARAMETER_VIEW = "org.eclipse.ui.views.midi.commands.PlayPause.view"; //$NON-NLS-1$ //XXX see plugin.xml
1424

1525
@Override
1626
public Object execute(ExecutionEvent event) throws ExecutionException {
27+
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
28+
FileView midiView = getMidiFileView(activePart);
1729
String viewId = event.getParameter(PARAMETER_VIEW);
18-
if (viewId != null) {
30+
IFile file = null;
31+
if (midiView == null && viewId != null) {
32+
file = getMidiFileForSelection(event);
33+
//if midi view not already active find it
1934
IViewPart view = HandlerUtil.getActivePart(event).getSite().getPage().findView(viewId);
20-
if(view == null) {
21-
//TODO open view
35+
//if it does not exist, but there is a matching midi file - create the view
36+
if (file != null && view == null) {
37+
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
38+
try {
39+
view = page.showView(viewId);
40+
} catch (PartInitException e) {
41+
Activator.logError("error creating midi view", e);
42+
}
2243
}
23-
if (view instanceof FileView) {
24-
//TODO set the correct file if not open
25-
FileView fileView = (FileView)view;
26-
IFileViewType<?> fileViewType = fileView.getType();
27-
if (fileViewType instanceof MidiViewType) {
28-
MidiViewType midiViewType = (MidiViewType)fileViewType;
29-
MidiViewPage midiViewPage = midiViewType.getPage();
30-
if (midiViewPage != null) {
31-
midiViewPage.togglePlayback();
44+
midiView = getMidiFileView(view);
45+
}
46+
togglePlayForGivenFile(midiView, file);
47+
return null;
48+
}
49+
50+
private void togglePlayForGivenFile(FileView midiView, IFile fileToPlay) {
51+
if (midiView != null) {
52+
boolean waitForViewToOpen = false;
53+
if (fileToPlay != null) {
54+
midiView.show(fileToPlay);
55+
waitForViewToOpen = true;
56+
}
57+
IFileViewType<?> type = midiView.getType();
58+
for (int i = 0; i < 3; i++) {
59+
MidiViewPage page = ((MidiViewType) type).getPage();
60+
if (page != null) {
61+
page.togglePlayback();
62+
return;
63+
} else if (waitForViewToOpen) {
64+
//page may be null if view was closed and link toggle is disabled
65+
try {
66+
Thread.sleep(1000);
67+
} catch (InterruptedException e) {
3268
}
3369
}
3470
}
3571
}
72+
}
73+
74+
private FileView getMidiFileView(IWorkbenchPart part) {
75+
if (part instanceof FileView) {
76+
FileView fileView = (FileView) part;
77+
IFileViewType<?> fileViewType = fileView.getType();
78+
if (fileViewType instanceof MidiViewType) {
79+
return fileView;
80+
}
81+
}
3682
return null;
3783
}
3884

39-
}
85+
private IFile getMidiFileForSelection(ExecutionEvent event) {
86+
IFile file = null;
87+
IWorkbenchPart part = HandlerUtil.getActivePart(event);
88+
if (part instanceof EditorPart) {
89+
IEditorInput input = ((EditorPart) part).getEditorInput();
90+
file = input.getAdapter(IFile.class);
91+
} else if (part instanceof FileView) {
92+
file = ((FileView) part).getFile();
93+
} else {
94+
ISelection selection = part.getSite().getSelectionProvider().getSelection();
95+
if (selection instanceof IStructuredSelection) {
96+
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
97+
if (structuredSelection.size() == 1) {
98+
Object firstElement = structuredSelection.getFirstElement();
99+
if (firstElement instanceof IFile) {
100+
file = (IFile) firstElement;
101+
}
102+
}
103+
}
104+
}
105+
if (file != null) {
106+
if (!MidiViewType.EXTENSION.equals(file.getFileExtension())) {
107+
file = ResourceUtils.replaceExtension(file, MidiViewType.EXTENSION);
108+
}
109+
if (!file.exists()) {
110+
file = null;
111+
}
112+
}
113+
return file;
114+
}
115+
}

0 commit comments

Comments
 (0)