KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > ui > GenModelReloadActionDelegate


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 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: GenModelReloadActionDelegate.java,v 1.3 2005/06/12 13:35:21 emerks Exp $
16  */

17 package org.eclipse.emf.importer.ui;
18
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.StructuredSelection;
24 import org.eclipse.jface.wizard.WizardDialog;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.IEditorActionDelegate;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.IFileEditorInput;
30 import org.eclipse.ui.IWorkbench;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.actions.ActionDelegate;
33
34
35 /**
36  * Reloads a GenModel file and the Ecore files behind it.
37  * This just invokes the {@link EMFWizard} with an existing file.
38  * If this action is being contributed to an editor, we'll use its input as the file. Otherwise, we'll
39  * try to obtain it from the selection.
40  */

41 public class GenModelReloadActionDelegate extends ActionDelegate implements IEditorActionDelegate
42 {
43   protected boolean fixedFile = false;
44   protected IFile file;
45
46   public GenModelReloadActionDelegate()
47   {
48     super();
49   }
50
51   public GenModelReloadActionDelegate(IEditorPart editorPart)
52   {
53     this();
54   }
55
56   public void dispose()
57   {
58     file = null;
59     super.dispose();
60   }
61
62   public void selectionChanged(IAction action, ISelection selection)
63   {
64     if (fixedFile) return;
65
66     if (selection instanceof IStructuredSelection)
67     {
68       Object JavaDoc object = ((IStructuredSelection)selection).getFirstElement();
69       if (object instanceof IFile)
70       {
71         file = (IFile)object;
72         action.setEnabled(true);
73         return;
74       }
75     }
76     file = null;
77     action.setEnabled(false);
78   }
79
80   /**
81    * This is only called when being used as an editor action delegate.
82    */

83   public void setActiveEditor(IAction action, IEditorPart targetEditor)
84   {
85     if (targetEditor != null)
86     {
87       IEditorInput input = targetEditor.getEditorInput();
88       if (input instanceof IFileEditorInput)
89       {
90         file = ((IFileEditorInput)input).getFile();
91         fixedFile = true;
92         action.setEnabled(true);
93         return;
94       }
95     }
96     file = null;
97     action.setEnabled(false);
98   }
99
100   public void run(IAction action)
101   {
102     run(PlatformUI.getWorkbench(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), file);
103   }
104
105   protected void run(IWorkbench workbench, Shell shell, IFile file)
106   {
107     EMFModelWizard wizard = new EMFModelWizard(file);
108     wizard.init(workbench, new StructuredSelection(file));
109
110     WizardDialog wizardDialog = new WizardDialog(shell, wizard);
111     wizardDialog.create();
112     wizardDialog.getShell().setSize(540, 580);
113     wizardDialog.open();
114   }
115 }
116
Popular Tags