source: trunk/org.modelica.mdt/src/org/modelica/mdt/ui/ProjectsViewDoubleClickAction.java @ 99

Last change on this file since 99 was 99, checked in by boris, 19 years ago
  • elements in modelica projects view are now double-click able
File size: 2.4 KB
Line 
1package org.modelica.mdt.ui;
2
3import org.eclipse.core.resources.IFile;
4import org.eclipse.core.resources.IFolder;
5import org.eclipse.core.resources.IProject;
6import org.eclipse.jface.action.Action;
7import org.eclipse.jface.viewers.ISelection;
8import org.eclipse.jface.viewers.IStructuredSelection;
9import org.eclipse.jface.viewers.TreeViewer;
10import org.eclipse.ui.IWorkbenchPage;
11import org.eclipse.ui.PartInitException;
12import org.eclipse.ui.PlatformUI;
13import org.eclipse.ui.ide.IDE;
14import org.modelica.mdt.core.IModelicaFile;
15import org.modelica.mdt.core.IModelicaFolder;
16import org.modelica.mdt.core.IModelicaPackage;
17import org.modelica.mdt.core.IModelicaProject;
18import org.modelica.mdt.core.ISystemLibrary;
19
20public 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(((IModelicaFile)obj).getContainer());
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}
Note: See TracBrowser for help on using the repository browser.