KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > action > OpenEditorAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: OpenEditorAction.java,v 1.2 2005/06/08 06:17:55 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.action;
18
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.ui.IActionDelegate;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.ActionDelegate;
31 import org.eclipse.ui.part.FileEditorInput;
32
33 import org.eclipse.emf.ecore.resource.Resource;
34
35 /**
36  * Generate Java beans from the XML Schema
37  */

38 public class OpenEditorAction extends ActionDelegate
39        implements IActionDelegate
40 {
41   protected IFile file;
42
43   public OpenEditorAction()
44   {
45   }
46
47   public void run(IAction action)
48   {
49     IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
50     IWorkbenchPage page = workbenchWindow.getActivePage();
51
52     // Open an editor on the new file.
53
//
54
try
55     {
56       page.openEditor
57         (new FileEditorInput(file),
58           action.getId().indexOf("XML") == -1 ?
59             "org.eclipse.emf.ecore.presentation.ReflectiveEditorID" :
60             "org.eclipse.emf.ecore.presentation.XMLReflectiveEditorID");
61     }
62     catch (PartInitException exception)
63     {
64       MessageDialog.openError(workbenchWindow.getShell(), "Open Editor", exception.getMessage());
65     }
66   }
67
68   public void selectionChanged(IAction action, ISelection selection)
69   {
70     if (selection instanceof IStructuredSelection)
71     {
72       Object JavaDoc object = ((IStructuredSelection)selection).getFirstElement();
73       if (object instanceof IFile)
74       {
75         file = (IFile)object;
76
77         action.setEnabled
78           (action.getId().indexOf("XML") != -1 ||
79              Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey
80                (file.getFullPath().getFileExtension()));
81         return;
82       }
83     }
84     file = null;
85     action.setEnabled(false);
86   }
87 }
88
Popular Tags