Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9cfc3a746934343d1fa531ac4cbc61afb8c7f906 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 *  Copyright (c) 2011  Oracle. 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: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.jaxb.ui.internal.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jpt.jaxb.core.context.JaxbContextNode;
import org.eclipse.jpt.jaxb.core.context.JaxbContextRoot;
import org.eclipse.jpt.jaxb.ui.JptJaxbUiMessages;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.BaseSelectionListenerAction;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;


public class OpenJaxbResourceAction
		extends BaseSelectionListenerAction {
	
	private JaxbContextNode selectedNode;
	
	
	public OpenJaxbResourceAction() {
		super("Open");   //$NON-NLS-1$
	}
	
	
	@Override
	public boolean updateSelection(IStructuredSelection s) {
		this.selectedNode = null;
		
		if (! super.updateSelection(s)) {
			return false;
		}
		
		if (s.size() != 1) {
			return false;
		}
		
		if (s.getFirstElement() instanceof JaxbContextRoot) {
			return false;
		}
		
		selectedNode = (JaxbContextNode) s.getFirstElement();
		
		return true;
	}
	
	@Override
	public void run() {
		if (! isEnabled()) {
			return;
		}
		
		IResource resource = this.selectedNode.getResource();
		
		if (resource != null && resource.exists() && resource.getType() == IResource.FILE) {
			openEditor((IFile) resource);
		}
	}
	
	protected void openEditor(IFile file) {
		IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry();
		IContentType contentType = IDE.getContentType(file);
		IEditorDescriptor editorDescriptor = registry.getDefaultEditor(file.getName(), contentType);
		if (editorDescriptor == null) {
			return;  // no editor associated...
		}
		
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		
		try {
			page.openEditor(new FileEditorInput(file), editorDescriptor.getId());
		} 
		catch (Exception e) {
			MessageDialog.openError(page.getWorkbenchWindow().getShell(), JptJaxbUiMessages.ERROR_OPENING_EDITOR, e.getMessage());
		}
	}
}

Back to the top