KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > action > CompileTemplateAction


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: CompileTemplateAction.java,v 1.2 2005/06/08 06:23:43 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.action;
18
19
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.IActionDelegate;
32
33 import org.eclipse.emf.codegen.jet.JETCompileTemplateOperation;
34 import org.eclipse.emf.codegen.jet.JETNature;
35 import org.eclipse.emf.codegen.presentation.CodeGenUIPlugin;
36
37
38 /**
39  * Compile the JET template.
40  */

41 public class CompileTemplateAction implements IActionDelegate
42 {
43   protected ISelection selection;
44
45   public void run(IAction action)
46   {
47     if (action.isEnabled())
48     {
49       IRunnableWithProgress op =
50         new IRunnableWithProgress()
51         {
52          public void run(IProgressMonitor monitor)
53          {
54            try
55            {
56              for (Iterator JavaDoc i = getSelectedObjects().iterator(); i.hasNext(); )
57              {
58                IFile file = (IFile)i.next();
59                JETNature jetNature = JETNature.getRuntime(file.getProject());
60                if (jetNature != null)
61                {
62                  JETCompileTemplateOperation compileTemplate =
63                    new JETCompileTemplateOperation(file.getProject(), jetNature.getTemplateContainers(), Collections.singleton(file));
64                  compileTemplate.run(monitor);
65                }
66              }
67            }
68            catch (Exception JavaDoc e)
69            {
70              CodeGenUIPlugin.write(e);
71            }
72          }
73        };
74
75       try
76       {
77         ProgressMonitorDialog dialog =
78           new ProgressMonitorDialog(CodeGenUIPlugin.getPlugin().getWorkbench().getActiveWorkbenchWindow().getShell());
79         dialog.run(false, true, op);
80       }
81       catch (Exception JavaDoc e)
82       {
83         CodeGenUIPlugin.write(e);
84       }
85     }
86   }
87
88   public void selectionChanged(IAction action, ISelection selection)
89   {
90     this.selection = selection;
91     setActionState(action);
92   }
93
94   protected void setActionState(IAction action)
95   {
96     action.setEnabled(true);
97   }
98
99   protected List JavaDoc getSelectedObjects()
100   {
101     return
102       selection instanceof IStructuredSelection ?
103         ((IStructuredSelection)selection).toList() :
104         Collections.EMPTY_LIST;
105   }
106
107   protected boolean isSupportedAction(Object JavaDoc object)
108   {
109     return object instanceof IFile && ((IFile)object).isAccessible();
110   }
111 }
112
Popular Tags