1 | package org.modelica.mdt.ui; |
---|
2 | |
---|
3 | import org.eclipse.core.resources.IFile; |
---|
4 | import org.eclipse.core.resources.IFolder; |
---|
5 | import org.eclipse.core.resources.IProject; |
---|
6 | import org.eclipse.jface.action.Action; |
---|
7 | import org.eclipse.jface.viewers.ISelection; |
---|
8 | import org.eclipse.jface.viewers.IStructuredSelection; |
---|
9 | import org.eclipse.jface.viewers.TreeViewer; |
---|
10 | import org.eclipse.ui.IWorkbenchPage; |
---|
11 | import org.eclipse.ui.PartInitException; |
---|
12 | import org.eclipse.ui.PlatformUI; |
---|
13 | import org.eclipse.ui.ide.IDE; |
---|
14 | import org.modelica.mdt.core.IModelicaFile; |
---|
15 | import org.modelica.mdt.core.IModelicaFolder; |
---|
16 | import org.modelica.mdt.core.IModelicaPackage; |
---|
17 | import org.modelica.mdt.core.IModelicaProject; |
---|
18 | import org.modelica.mdt.core.ISystemLibrary; |
---|
19 | |
---|
20 | public class ProjectsViewDoubleClickAction extends Action |
---|
21 | { |
---|
22 | private TreeViewer viewer; |
---|
23 | |
---|
24 | ProjectsViewDoubleClickAction(TreeViewer viewer) |
---|
25 | { |
---|
26 | this.viewer = viewer; |
---|
27 | } |
---|
28 | |
---|
29 | @Override |
---|
30 | public void run() |
---|
31 | { |
---|
32 | /* get the object that was double clicked */ |
---|
33 | ISelection selection = viewer.getSelection(); |
---|
34 | Object obj = ((IStructuredSelection)selection).getFirstElement(); |
---|
35 | |
---|
36 | /* open modelica files in an editor on double click */ |
---|
37 | if (obj instanceof IModelicaFile) |
---|
38 | { |
---|
39 | openFile((IFile)((IModelicaFile)obj).getResource()); |
---|
40 | } |
---|
41 | /* open files in an editor on double click */ |
---|
42 | else if (obj instanceof IFile) |
---|
43 | { |
---|
44 | openFile((IFile)obj); |
---|
45 | } |
---|
46 | /* expands a bunch of objects on double click */ |
---|
47 | else if (obj instanceof IModelicaProject || |
---|
48 | obj instanceof IModelicaFolder || |
---|
49 | obj instanceof IModelicaPackage || |
---|
50 | obj instanceof ISystemLibrary || |
---|
51 | obj instanceof IProject || |
---|
52 | obj instanceof IFolder ) |
---|
53 | |
---|
54 | { |
---|
55 | expandElement(obj); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * expands element in the treeviewer, so that it's immediate children are |
---|
61 | * visible |
---|
62 | * @param element element to expand |
---|
63 | */ |
---|
64 | private void expandElement(Object element) |
---|
65 | { |
---|
66 | viewer.expandToLevel(element, 1); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Opens file in the default editor |
---|
71 | * param file file to open |
---|
72 | */ |
---|
73 | private void openFile(IFile file) |
---|
74 | { |
---|
75 | |
---|
76 | IWorkbenchPage page = PlatformUI.getWorkbench(). |
---|
77 | getActiveWorkbenchWindow().getActivePage(); |
---|
78 | |
---|
79 | if (page != null) |
---|
80 | { |
---|
81 | |
---|
82 | try |
---|
83 | { |
---|
84 | |
---|
85 | IDE.openEditor(page, file); |
---|
86 | } |
---|
87 | catch (PartInitException e) |
---|
88 | { |
---|
89 | // TODO proper error handling |
---|
90 | e.printStackTrace(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | } |
---|