|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 |
|
18 package com.symbian.smt.gui; |
|
19 |
|
20 import org.eclipse.core.resources.IFile; |
|
21 import org.eclipse.core.resources.IMarker; |
|
22 import org.eclipse.core.resources.IResource; |
|
23 import org.eclipse.core.runtime.CoreException; |
|
24 import org.eclipse.jface.action.Action; |
|
25 import org.eclipse.jface.viewers.ISelection; |
|
26 import org.eclipse.jface.viewers.ISelectionProvider; |
|
27 import org.eclipse.jface.viewers.IStructuredSelection; |
|
28 import org.eclipse.ui.IWorkbenchPage; |
|
29 import org.eclipse.ui.PartInitException; |
|
30 import org.eclipse.ui.part.FileEditorInput; |
|
31 |
|
32 /** |
|
33 * @author barbararosi-schwartz |
|
34 * |
|
35 */ |
|
36 public class OpenFileAction extends Action { |
|
37 |
|
38 IWorkbenchPage page; |
|
39 ISelectionProvider selectionProvider; |
|
40 |
|
41 public OpenFileAction(IWorkbenchPage page, |
|
42 ISelectionProvider selectionProvider) { |
|
43 this.page = page; |
|
44 this.selectionProvider = selectionProvider; |
|
45 |
|
46 setText("Open"); |
|
47 } |
|
48 |
|
49 /* |
|
50 * (non-Javadoc) |
|
51 * |
|
52 * @see org.eclipse.jface.action.Action#isEnabled() |
|
53 */ |
|
54 @Override |
|
55 public boolean isEnabled() { |
|
56 ISelection selection = selectionProvider.getSelection(); |
|
57 |
|
58 if (!selection.isEmpty()) { |
|
59 IStructuredSelection ssel = (IStructuredSelection) selection; |
|
60 |
|
61 if (ssel.size() > 1) { |
|
62 return false; |
|
63 } |
|
64 |
|
65 IResource resource = (IResource) ssel.getFirstElement(); |
|
66 |
|
67 if (resource instanceof IFile) { |
|
68 try { |
|
69 return !isResourceMarkedAsUrl((IFile) resource); |
|
70 } catch (CoreException e) { |
|
71 return false; |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 return false; |
|
77 } |
|
78 |
|
79 private boolean isResourceMarkedAsUrl(IFile file) throws CoreException { |
|
80 IMarker[] messageMarkers = file.findMarkers(IMarker.TASK, false, |
|
81 IResource.DEPTH_ZERO); |
|
82 |
|
83 for (int i = 0; i < messageMarkers.length; i++) { |
|
84 IMarker marker = messageMarkers[i]; |
|
85 |
|
86 if (marker.getAttribute(ManageResources.IS_URL, false)) { |
|
87 return true; |
|
88 } |
|
89 } |
|
90 |
|
91 return false; |
|
92 } |
|
93 |
|
94 /* |
|
95 * (non-Javadoc) |
|
96 * |
|
97 * @see org.eclipse.jface.action.Action#run() |
|
98 */ |
|
99 @Override |
|
100 public void run() { |
|
101 IStructuredSelection ssel = (IStructuredSelection) selectionProvider |
|
102 .getSelection(); |
|
103 IFile file = (IFile) ssel.getFirstElement(); |
|
104 |
|
105 try { |
|
106 page.openEditor(new FileEditorInput(file), |
|
107 "com.symbian.smt.gui.editors.xmleditor.XMLEditor", true); |
|
108 } catch (PartInitException e) { |
|
109 Logger.log(e.getMessage(), e); |
|
110 } |
|
111 } |
|
112 |
|
113 } |