View Javadoc

1   /*
2    * $Id: TranslateAction.java,v 1.3 2005/05/31 20:25:08 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8   
9   package com.bbn.swede.tools.translator;
10  
11  import java.util.HashSet;
12  import java.util.Iterator;
13  import java.util.Set;
14  
15  import org.eclipse.core.resources.IResource;
16  import org.eclipse.core.runtime.IPath;
17  import org.eclipse.jface.action.IAction;
18  import org.eclipse.jface.viewers.ISelection;
19  import org.eclipse.jface.viewers.IStructuredSelection;
20  import org.eclipse.jface.wizard.WizardDialog;
21  import org.eclipse.swt.widgets.Shell;
22  import org.eclipse.ui.IObjectActionDelegate;
23  import org.eclipse.ui.IWorkbenchPart;
24  
25  import com.bbn.swede.core.IOWLElement;
26  import com.bbn.swede.core.IOWLLibrary;
27  import com.bbn.swede.ui.util.PixelConverter;
28  
29  /***
30   * An action delegate which launches the {@link SemanticWebTranslationWizard}
31   * when appropriate. This action appears in popup menus in views.
32   * 
33   * @author aperezlo
34   */
35  public class TranslateAction implements IObjectActionDelegate
36  {
37     private static final String ACTION_ID = "com.bbn.swede.tools.translator.TranslateAction";
38  
39     private IWorkbenchPart _parent;
40  
41     private IStructuredSelection _selection;
42  
43     /***
44      * Default constructor.
45      */
46     public TranslateAction()
47     {
48        super();
49     }
50  
51     /*
52      * (non-Javadoc)
53      * 
54      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
55      */
56     public void run(IAction action)
57     {
58        Shell shell = null;
59        WizardDialog wd = null;
60        SemanticWebTranslationWizard wiz = null;
61        PixelConverter converter = null;
62  
63        if(ACTION_ID.equals(action.getId()))
64        {
65           shell = _parent.getSite().getShell();
66           converter = new PixelConverter(shell);
67  
68           wiz = new SemanticWebTranslationWizard();
69           wiz.init(_parent.getSite().getWorkbenchWindow().getWorkbench(),
70              _selection);
71           wd = new WizardDialog(shell, wiz);
72           wd.setMinimumPageSize(converter.convertWidthInCharsToPixels(70),
73              converter.convertHeightInCharsToPixels(20));
74           wd.create();
75           //         wd.setBlockOnOpen(false);
76           wd.open();
77        }
78     }
79  
80     /*
81      * (non-Javadoc)
82      * 
83      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
84      *      org.eclipse.jface.viewers.ISelection)
85      */
86     public void selectionChanged(IAction action, ISelection selection)
87     {
88        Iterator i = null;
89        Set inputExtensions = new HashSet();
90        boolean allGood = true;
91        IStructuredSelection iss = null;
92        Iterator iter = null;
93        Object thing = null;
94        IPath path = null;
95        String extension = null;
96        Set selectionExtensions = new HashSet();
97        _selection = null;
98  
99        if(action.getId().equals(ACTION_ID))
100       {
101          if(selection instanceof IStructuredSelection)
102          {
103             iss = (IStructuredSelection)selection;
104             _selection = iss;
105             i = TranslatorPlugin.getPlugin().getTranslators().iterator();
106             while (i.hasNext())
107             {
108                inputExtensions.add(((TranslatorDescriptor)i.next())
109                   .getInputExtension());
110             }
111 
112             iter = iss.iterator();
113             while (iter.hasNext())
114             {
115                path = null;
116                thing = iter.next();
117                if((thing instanceof IResource)
118                   || (thing instanceof IOWLElement))
119                {
120                   if(thing instanceof IResource)
121                   {
122                      path = ((IResource)thing).getFullPath();
123                   }
124                   if(thing instanceof IOWLElement)
125                   {
126                      if(((IOWLElement)thing).getCorrespondingResource() != null)
127                      {
128                         path = ((IOWLElement)thing).getCorrespondingResource()
129                            .getFullPath();
130                      }
131                      if(((IOWLElement)thing).getParent() instanceof IOWLLibrary)
132                      {
133                         allGood = false;
134                         break;
135                      }
136                   }
137                   if(path == null)
138                   {
139                      allGood = false;
140                      break;
141                   }
142                   else
143                   {
144                      selectionExtensions.add(path.getFileExtension());
145                      if(selectionExtensions.size() != 1) // all extensions must
146                                                          // be the same...
147                      {
148                         allGood = false;
149                         break;
150                      }
151                      if(!inputExtensions.contains(path.getFileExtension()))
152                      {
153                         allGood = false;
154                         break;
155                      }
156                   }
157                }
158                else
159                {
160                   allGood = false;
161                   break;
162                }
163             }
164          }
165          action.setEnabled(allGood);
166 
167       }
168    }
169 
170    /*
171     * (non-Javadoc)
172     * 
173     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
174     *      org.eclipse.ui.IWorkbenchPart)
175     */
176    public void setActivePart(IAction action, IWorkbenchPart targetPart)
177    {
178       if(ACTION_ID.equals(action.getId()))
179       {
180          _parent = targetPart;
181       }
182    }
183 }