View Javadoc

1   /*
2    * $Id: OWLLibraryExportWizard.java,v 1.7 2005/06/01 17:47:43 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.ui.wizards;
10  
11  import java.lang.reflect.InvocationTargetException;
12  import java.util.ArrayList;
13  import java.util.Arrays;
14  import java.util.Collection;
15  import java.util.Iterator;
16  import java.util.List;
17  
18  import org.eclipse.core.resources.ResourcesPlugin;
19  import org.eclipse.core.runtime.IPath;
20  import org.eclipse.core.runtime.IProgressMonitor;
21  import org.eclipse.core.runtime.IStatus;
22  import org.eclipse.core.runtime.MultiStatus;
23  import org.eclipse.core.runtime.Path;
24  import org.eclipse.jface.dialogs.ProgressMonitorDialog;
25  import org.eclipse.jface.operation.IRunnableWithProgress;
26  import org.eclipse.jface.viewers.CheckStateChangedEvent;
27  import org.eclipse.jface.viewers.CheckboxTableViewer;
28  import org.eclipse.jface.viewers.ICheckStateListener;
29  import org.eclipse.jface.viewers.IStructuredSelection;
30  import org.eclipse.jface.viewers.LabelProvider;
31  import org.eclipse.jface.wizard.Wizard;
32  import org.eclipse.jface.wizard.WizardPage;
33  import org.eclipse.swt.SWT;
34  import org.eclipse.swt.events.SelectionAdapter;
35  import org.eclipse.swt.events.SelectionEvent;
36  import org.eclipse.swt.graphics.Image;
37  import org.eclipse.swt.layout.GridData;
38  import org.eclipse.swt.layout.GridLayout;
39  import org.eclipse.swt.widgets.Button;
40  import org.eclipse.swt.widgets.Composite;
41  import org.eclipse.swt.widgets.DirectoryDialog;
42  import org.eclipse.swt.widgets.Label;
43  import org.eclipse.swt.widgets.Text;
44  import org.eclipse.ui.IExportWizard;
45  import org.eclipse.ui.IWorkbench;
46  import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
47  import org.eclipse.ui.dialogs.ContainerSelectionDialog;
48  import org.eclipse.ui.model.BaseWorkbenchContentProvider;
49  
50  import com.bbn.swede.core.IOWLLibrary;
51  import com.bbn.swede.core.libraries.ILibraryDescriptor;
52  import com.bbn.swede.core.libraries.LibraryOWLDocument;
53  import com.bbn.swede.core.libraries.OWLLibrary;
54  import com.bbn.swede.ui.OWLImages;
55  import com.bbn.swede.ui.UIPlugin;
56  
57  /***
58   * Wizard for exporting Ontology Libraries.
59   * @author aperezlo
60   */
61  public class OWLLibraryExportWizard extends Wizard implements IExportWizard
62  {
63     private IWorkbench _workbench;
64     private List _input = new ArrayList(); 
65     private LibraryExportWizardPage _pageOne;
66     
67     /*
68      *  (non-Javadoc)
69      * @see org.eclipse.jface.wizard.IWizard#performFinish()
70      */
71     public boolean performFinish()
72     {
73        boolean toReturn = true;
74        IRunnableWithProgress exportJob = new IRunnableWithProgress()
75        {
76           public void run(IProgressMonitor monitor)
77           {
78              MultiStatus toReturn = new MultiStatus(UIPlugin.getID(), IStatus.OK, "Export succeeded.", null);
79              List sources = _pageOne.getDocuments();
80              IPath destination = _pageOne.getPath();
81              LibraryOWLDocument lod = null;
82              ILibraryDescriptor library = null;
83              Iterator i = sources.iterator(); 
84              
85              while(i.hasNext() && !monitor.isCanceled())
86              {
87                 lod = (LibraryOWLDocument) i.next();
88                 library = ((IOWLLibrary) lod.getParent()).getLibrary();
89                 toReturn.add(library.export(destination, library.getDescriptorFor(lod.getElementName()), monitor));
90              }
91              if(monitor.isCanceled())
92              {
93                 toReturn.add(MultiStatus.CANCEL_STATUS);
94              }
95           }   
96        };
97        
98        ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(getShell());
99        progressDialog.setBlockOnOpen(false);
100       progressDialog.setCancelable(true);
101       progressDialog.open();
102       try
103       {
104          (new WorkspaceModifyDelegatingOperation(exportJob)).run(progressDialog.getProgressMonitor());
105       }
106       catch (InvocationTargetException e)
107       {
108          e.printStackTrace();
109       }
110       catch (InterruptedException e)
111       {
112          e.printStackTrace();
113       }
114      
115       return toReturn;
116    }
117    
118    /*
119     *  (non-Javadoc)
120     * @see org.eclipse.ui.IWorkbenchWizard#init(
121     *    org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
122     */
123    public void init(IWorkbench workbench, IStructuredSelection selection)
124    {
125       Iterator i = selection.iterator();
126       Object obj = null;
127       _input = new ArrayList();
128       
129       while(i.hasNext())
130       {
131          obj = i.next();
132          if(obj instanceof LibraryOWLDocument)
133          {
134             _input.add(obj);
135          }
136          else if(obj instanceof IOWLLibrary)
137          {
138             _input.addAll(Arrays.asList(((IOWLLibrary) obj).getElementInfo().getChildren()));
139          }
140       }
141       _pageOne = new LibraryExportWizardPage(_input); 
142       addPage(_pageOne);
143       setWindowTitle("SWeDE Library Export");
144    }
145 
146 
147    private final class LibraryExportWizardPage extends WizardPage
148    {
149       private CheckboxTableViewer _tableViewer;
150       private List _sources;
151       
152       private Text _destinationFSText;
153       private Text _destinationSWeDEText;
154       
155       private IPath _destinationPath;
156       
157       /***
158        * Constructor.
159        * 
160        * @param sources the list of resources to export
161        */
162       public LibraryExportWizardPage(List sources)
163       {
164          super("owl-library-export-wizard-page-one");
165          _sources = new ArrayList();
166          _sources.addAll(sources);
167          setTitle("SWeDE Library Export");
168          setMessage("Select the resources to export from the SWeDE library, and the destination for them.");
169          setPageComplete(false);
170       }
171       
172       /***
173        * Retrieves a list of documents to export from the library.
174        * @return a List of LibraryOWLDocuments to export
175        */
176       public List getDocuments()
177       {
178          List selectedOnes = new ArrayList();
179          if(_tableViewer != null)
180          {
181             selectedOnes.addAll(Arrays.asList(_tableViewer.getCheckedElements()));
182          }
183          return selectedOnes;
184       }
185 
186       /***
187        * Accessor.
188        * 
189        * @return the path to which the files will be exported
190        */
191       public IPath getPath()
192       {
193          return _destinationPath;
194       }
195       
196       private boolean validatePage()
197       {
198          boolean toReturn = false;
199          if(getDocuments().size() > 0)
200          {
201             if(_destinationPath != null)
202             {
203                toReturn = true;
204             }
205          }
206          return toReturn;
207       }
208       
209       /*
210        *  (non-Javadoc)
211        * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
212        */
213       public void createControl(Composite parent)
214       {
215          Composite mainPanel = new Composite(parent, parent.getStyle());
216          mainPanel.setFont(parent.getFont());
217          mainPanel.setLayout(new GridLayout(2, false));
218          mainPanel.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
219          
220          _tableViewer = CheckboxTableViewer.newCheckList(mainPanel, mainPanel.getStyle() | SWT.BORDER);
221          _tableViewer.setContentProvider(new InternalContentProvider());
222          _tableViewer.setLabelProvider(new InternalLabelProvider());
223          _tableViewer.setInput(_sources);
224          _tableViewer.getTable().setLayoutData(
225             new GridData(GridData.VERTICAL_ALIGN_BEGINNING
226                | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL
227                | GridData.FILL_BOTH));
228          _tableViewer.getTable().setFont(mainPanel.getFont());
229          _tableViewer.addCheckStateListener(new ICheckStateListener()
230          {
231             public void checkStateChanged(CheckStateChangedEvent event)
232             {
233                setPageComplete(validatePage());
234             }
235          });
236 
237          Composite pathPanel = new Composite(mainPanel, mainPanel.getStyle());
238          pathPanel.setFont(mainPanel.getFont());
239          pathPanel.setLayout(new GridLayout());
240          pathPanel.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
241          createDestinationPanel(pathPanel);
242          
243          setControl(mainPanel);
244       }
245       private void createDestinationPanel(Composite parent)
246       {
247          Composite mainPanel = new Composite(parent, parent.getStyle());
248          mainPanel.setFont(parent.getFont());
249          mainPanel.setLayout(new GridLayout(3, false));
250          mainPanel.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
251          
252          Label fsLabel = new Label(mainPanel, mainPanel.getStyle());
253          fsLabel.setFont(mainPanel.getFont());
254          fsLabel.setText("Export to file system:");
255          
256          _destinationFSText = new Text(mainPanel, mainPanel.getStyle() | SWT.BORDER | SWT.READ_ONLY);
257          _destinationFSText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
258          _destinationFSText.setFont(mainPanel.getFont());
259          _destinationFSText.setEnabled(false);
260          
261          final Button browseFSButton = new Button(mainPanel, mainPanel.getStyle() | SWT.PUSH);
262          browseFSButton.setFont(mainPanel.getFont());
263          browseFSButton.setText("Browse");
264          browseFSButton.addSelectionListener(new SelectionAdapter()
265          {
266             public void widgetSelected(SelectionEvent e)
267             {
268                DirectoryDialog dd = new DirectoryDialog(getShell());
269                dd.setMessage("Select an export directory...");
270                String string = dd.open();
271                if(string != null)
272                {
273                   _destinationFSText.setEnabled(true);
274                   _destinationSWeDEText.setEnabled(false);
275                   _destinationSWeDEText.setText("");
276                   _destinationPath = new Path(string);
277                   if(_destinationPath.lastSegment() == null)
278                   {
279                      _destinationFSText.setText(_destinationPath.makeAbsolute().toOSString());
280                   }
281                   else
282                   {
283                      _destinationFSText.setText(_destinationPath.lastSegment());
284                   }
285                }
286                setPageComplete(validatePage());
287             }
288          });
289 
290          Label swedeLabel = new Label(mainPanel, mainPanel.getStyle());
291          swedeLabel.setFont(mainPanel.getFont());
292          swedeLabel.setText("Export to a SWeDE project:");
293          
294          _destinationSWeDEText = new Text(mainPanel, mainPanel.getStyle() | SWT.BORDER | SWT.READ_ONLY);
295          _destinationSWeDEText.setFont(mainPanel.getFont());
296          _destinationSWeDEText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
297          _destinationSWeDEText.setEnabled(false);
298          
299          final Button browseSWeDEButton = new Button(mainPanel, mainPanel.getStyle() | SWT.PUSH);
300          browseSWeDEButton.setFont(mainPanel.getFont());
301          browseSWeDEButton.setText("Browse");
302          browseSWeDEButton.addSelectionListener(new SelectionAdapter()
303          {
304             public void widgetSelected(SelectionEvent e)
305             {
306                ContainerSelectionDialog dd = new ContainerSelectionDialog(
307                   getShell(), ResourcesPlugin.getWorkspace().getRoot(), true,
308                   "Select export location...");
309                dd.setMessage("Select a project...");
310                int returnCode = dd.open();
311                String string = null;
312                Object[] results = null;
313                if(returnCode == ContainerSelectionDialog.OK)
314                {
315                   results = dd.getResult();
316                   if(results.length > 0)
317                   {
318                      if(results[0] != null)
319                      {
320                         _destinationSWeDEText.setEnabled(true);
321                         _destinationFSText.setEnabled(false);
322                         
323                         _destinationPath = (IPath) results[0];
324                         _destinationSWeDEText.setText(_destinationPath.lastSegment());
325                         _destinationFSText.setText("");
326                      }
327                   }
328                   setPageComplete(validatePage());
329                }
330             }
331          });
332       }
333       
334       private final class InternalLabelProvider extends LabelProvider
335       {
336          /*
337           *  (non-Javadoc)
338           * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
339           */
340          public String getText(Object element)
341          {
342             String toReturn = super.getText(element);
343             if(element instanceof OWLLibrary)
344             {
345                toReturn = ((OWLLibrary) element).getElementName();
346             }
347             else if(element instanceof LibraryOWLDocument)
348             {
349                toReturn = ((LibraryOWLDocument) element).getElementName();
350             }
351             
352             return toReturn;
353          }
354          /*
355           *  (non-Javadoc)
356           * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
357           */
358          public Image getImage(Object element)
359          {
360             return OWLImages.getSharedImage(OWLImages.IMG_OWL_OBJ);
361          }
362       }
363       private final class InternalContentProvider extends BaseWorkbenchContentProvider
364       {
365          /*
366           *  (non-Javadoc)
367           * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
368           */
369          public Object[] getElements(Object element)
370          {
371             Object[] toReturn = new Object[0];
372             if(element instanceof Collection)
373             {
374                toReturn = ((Collection) element).toArray();
375             }
376             return toReturn;
377          }   
378       }
379    }
380    
381 }