View Javadoc

1   /*
2    * $Id: SemanticWebTranslationWizard.java,v 1.4 2005/06/01 17:47:51 aperezlo 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.Iterator;
12  import java.util.List;
13  
14  import org.eclipse.core.resources.IFile;
15  import org.eclipse.core.runtime.IPath;
16  import org.eclipse.core.runtime.IProgressMonitor;
17  import org.eclipse.core.runtime.IStatus;
18  import org.eclipse.core.runtime.Status;
19  import org.eclipse.jface.dialogs.ErrorDialog;
20  import org.eclipse.jface.dialogs.MessageDialog;
21  import org.eclipse.jface.viewers.IStructuredSelection;
22  import org.eclipse.jface.wizard.IWizardPage;
23  import org.eclipse.jface.wizard.Wizard;
24  import org.eclipse.ui.IExportWizard;
25  import org.eclipse.ui.IWorkbench;
26  import org.eclipse.ui.actions.WorkspaceModifyOperation;
27  
28  import com.bbn.swede.core.OWLCore;
29  
30  /***
31   * A wizard that allows users to translate documents in a given format within
32   * Eclipse. For instance, this wizard could allow a user to generate an OWL
33   * N-Triples representation in one project of the documents in a different
34   * project that are in OWL RDF/XML. This wizard is invoked via a popup menu
35   * available to all views. <br>
36   * <br>
37   * This class is intended to be instantiated and used, but not to be subclassed.
38   * 
39   * @author aperezlo
40   */
41  public class SemanticWebTranslationWizard extends Wizard implements
42     IExportWizard
43  {
44  
45     private SemanticWebTranslatorWizardHelper _helper;
46  
47     /***
48      * Performs the translation using a WorkspaceModifyOperation.
49      * 
50      * @see org.eclipse.jface.wizard.IWizard#performFinish()
51      */
52     public boolean performFinish()
53     {
54        boolean toReturn = true;
55        if(_helper.getInputFiles().size() < 1)
56        {
57           toReturn = false;
58        }
59        else
60        {
61           try
62           {
63              (new WorkspaceModifyOperation()
64              {
65                 protected void execute(IProgressMonitor progMonitor)
66                 {
67                    List inputFiles = _helper.getInputFiles();
68                    IPath outputPath = _helper.getOutputPath();
69                    List relevantTranslators = _helper.getTranslatorDescriptors();
70                    TranslatorDescriptor translator = null;
71                    Iterator i = inputFiles.iterator();
72                    IFile inFile = null;
73                    IStatus status = null;
74                    progMonitor.beginTask("Translating " + inputFiles.size()
75                       + " resource" + (inputFiles.size() > 1 ? "s" : ""),
76                       inputFiles.size());
77                    TranslationRunner result = null;
78                    Iterator translatorIterator = null;
79  
80                    while (i.hasNext())
81                    {
82                       inFile = (IFile)i.next();
83                       translator = null;
84                       translatorIterator = relevantTranslators.iterator();
85                       while (translatorIterator.hasNext())
86                       {
87                          translator = (TranslatorDescriptor)translatorIterator
88                             .next();
89                          if(translator.getInputExtension().equals(
90                             inFile.getFileExtension()))
91                          {
92                             break;
93                          }
94                       }
95                       try
96                       {
97                          result = _helper.performTranslation(inFile, outputPath,
98                             translator);
99                       }
100                      catch (Exception e)
101                      {
102                         status = new Status(IStatus.ERROR, TranslatorPlugin
103                            .getPlugin().getID(), IStatus.OK, e.getMessage(), e);
104                         ErrorDialog.openError(getShell(),
105                            "SWeDE Translation Error",
106                            "An error was encountered while trying to translate '"
107                               + inFile.getName() + "'.", status);
108                         OWLCore.logWarning(
109                            TranslatorPlugin.getPlugin().getID(),
110                            "Translation operation failed.", e);
111                      }
112                      if(result != null)
113                      {
114                         if(result.getErrorString() != null)
115                         {
116                            MessageDialog.openError(getShell(),
117                               "SWeDE Translation Error",
118                               "An error was encountered while trying to translate '"
119                                  + inFile.getName()
120                                  + "'.\n  The translator reported:\n"
121                                  + result.getErrorString());
122                         }
123                         if(result.getException() != null)
124                         {
125                            status = new Status(IStatus.ERROR, TranslatorPlugin
126                               .getPlugin().getID(), IStatus.OK,
127                               "Unexpected Translation Error", result
128                                  .getException());
129                            ErrorDialog.openError(getShell(),
130                               "SWeDE Translation Error",
131                               "An unexpected error was encountered while trying to translate '"
132                                  + inFile.getName() + "'.", status);
133                         }
134                      }
135                      progMonitor.worked(1);
136                   }
137                   progMonitor.done();
138                }
139             }).run(_helper.getProgressMonitor());
140          }
141          catch (Exception e)
142          {
143             OWLCore.logError(TranslatorPlugin.getPlugin().getID(), e
144                .getMessage(), e);
145          }
146       }
147 
148       return toReturn;
149    }
150 
151    /*
152     * 
153     * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
154     *      org.eclipse.jface.viewers.IStructuredSelection)
155     */
156    public void init(IWorkbench workbench, IStructuredSelection selection)
157    {
158       Iterator i = null;
159 
160       _helper = new SemanticWebTranslatorWizardHelper(
161          SemanticWebTranslatorWizardHelper.TRANSLATE, workbench, selection);
162       i = _helper.getPages().iterator();
163 
164       while (i.hasNext())
165       {
166          addPage((IWizardPage)i.next());
167       }
168       setHelpAvailable(false);
169       //setDialogSettings
170       setNeedsProgressMonitor(true);
171       //setTitleBarColor()
172       setWindowTitle("Translate a Semantic Web Document");
173 
174    }
175 
176 }