View Javadoc

1   /*
2    * $Id: OWLEditLibraryWizard.java,v 1.4 2005/05/31 21:57:12 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.ui.wizards;
10  
11  import java.io.File;
12  import java.net.URL;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.Iterator;
16  import java.util.List;
17  import java.util.Properties;
18  
19  import org.eclipse.jface.dialogs.MessageDialog;
20  import org.eclipse.jface.viewers.IStructuredSelection;
21  import org.eclipse.jface.wizard.IWizardPage;
22  import org.eclipse.jface.wizard.Wizard;
23  import org.eclipse.ui.IWorkbench;
24  import org.eclipse.ui.IWorkbenchWizard;
25  
26  import com.bbn.swede.core.libraries.EditLibraryJob;
27  import com.bbn.swede.core.libraries.IConfigurationValidator;
28  import com.bbn.swede.core.libraries.ILibraryDescriptor;
29  import com.bbn.swede.core.libraries.ILibraryEntryDescriptor;
30  import com.bbn.swede.core.libraries.Libraries;
31  import com.bbn.swede.core.libraries.LibraryConfiguration;
32  import com.bbn.swede.core.libraries.LibraryEdit;
33  import com.bbn.swede.core.libraries.LibraryStructuralCompositeEdit;
34  import com.bbn.swede.core.libraries.LibraryStructuralEdit;
35  
36  /***
37   * A wizard that allows for editing of SWeDE Libraries.
38   * 
39   * @author aperezlo
40   */
41  public class OWLEditLibraryWizard extends Wizard implements IWorkbenchWizard
42  {
43     private OWLLibraryBuilderPageZero _pageZero;
44     private OWLLibraryEditorPageOne _pageOne;
45     private OWLLibraryBuilderPageTwo _pageTwo;
46     private OWLLibraryBuilderPageThree _pageThree;
47     
48     private ILibraryDescriptor _library;
49     
50     /*
51      *  (non-Javadoc)
52      * @see org.eclipse.ui.IWorkbenchWizard#init(
53      *    org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
54      */
55     public void init(IWorkbench workbench, IStructuredSelection selection)
56     {
57        _library = (ILibraryDescriptor) selection.getFirstElement();
58        setWindowTitle("SWeDE - Edit an existing library");
59        _pageZero = new OWLLibraryEditorPageZero();
60  
61        _pageZero.setLibraryName(_library.getName());
62        
63        _pageOne = new OWLLibraryEditorPageOne(_library);
64        _pageTwo = new OWLLibraryBuilderPageTwo(new ArrayList());
65        _pageThree = new OWLLibraryEditorPageThree(_library);
66        addPage(_pageZero);
67        addPage(_pageOne);
68        addPage(_pageTwo);
69        addPage(_pageThree);
70     }
71     
72     /*
73      *  (non-Javadoc)
74      * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
75      */
76     public IWizardPage getNextPage(IWizardPage page)
77     {
78        IWizardPage toReturn = null;
79        if(page == _pageZero)
80        {
81           toReturn = _pageOne;
82        } 
83        else if(page == _pageOne)
84        {
85           _pageTwo.setSources(_pageOne.getSources());
86           toReturn = _pageTwo;
87        }
88        else if(page == _pageTwo)
89        {
90           toReturn = _pageThree;
91        }
92        
93        return toReturn;
94     }
95     
96     /*
97      *  (non-Javadoc)
98      * @see org.eclipse.jface.wizard.IWizard#getPreviousPage(org.eclipse.jface.wizard.IWizardPage)
99      */
100    public IWizardPage getPreviousPage(IWizardPage page)
101    {
102       IWizardPage toReturn = super.getPreviousPage(page);
103       return toReturn;
104    }
105    
106    /*
107     *  (non-Javadoc)
108     * @see org.eclipse.jface.wizard.IWizard#getStartingPage()
109     */
110    public IWizardPage getStartingPage()
111    {
112       return _pageOne;
113    }
114    
115    /*
116     *  (non-Javadoc)
117     * @see org.eclipse.jface.wizard.IWizard#canFinish()
118     */
119    public boolean canFinish()
120    {
121       // if I'm on page 1, i need the sources from page 1
122       // if I'm on page 2, then I need the sources from page 2
123       // how can I tell which page I'm on?
124       Collection col = null;
125       col = (Collection) _pageOne.getSources(); 
126       LibraryResourceTable.LibraryResourceDescriptor lrd = null;
127       IConfigurationValidator entryValidator = null;
128       Iterator i = null;
129       boolean sourcesComplete = true;
130       if(col != null)
131       {
132          i = col.iterator();
133          while(i.hasNext())
134          {
135             lrd = (LibraryResourceTable.LibraryResourceDescriptor) i.next();
136             entryValidator = lrd.getConfiguration().getPropertiesValidator();
137             if(!entryValidator.isValid(lrd.getConfiguration()))
138             {
139                sourcesComplete = false;
140                String errorMessage = "";
141                String[] errors = entryValidator.getErrorMessages();
142                for(int index = 0; index < errors.length; errorMessage += errors[index++] + "\n")
143                {
144                   //do nothing
145                }
146             }
147          }
148       }
149       else
150       {
151          sourcesComplete = false; // shouldn't be neccesary, but it doesn't hurt...
152       }
153       return super.canFinish() && sourcesComplete;
154    }
155 
156    private List createAdditionsAndConfigurations(List resources, List entries)
157    {         
158       // additions are defined as those resources which exist in the resources that should be in the 
159       // library, but are not found in the collection of current entries
160       List toReturn = new ArrayList();
161       Iterator resourcesIterator = null;
162       Iterator entryIterator = null; 
163       ILibraryEntryDescriptor iled = null;
164       LibraryResourceTable.LibraryResourceDescriptor lrd = null;
165       LibraryStructuralEdit singleEdit = null;
166       boolean found = false;
167       
168       resourcesIterator = resources.iterator();
169       while(resourcesIterator.hasNext())
170       {  
171          lrd = (LibraryResourceTable.LibraryResourceDescriptor) resourcesIterator.next();
172          // if this file is not already in the library (we do this because 
173          // if it IS already in the library, then a copy of this unzipped 
174          // file already is in the edit directory, and we don't need to 
175          // re-copy it there from wherever it lives.
176          entryIterator = entries.iterator();
177          found = false;
178          while(entryIterator.hasNext())
179          {
180             iled = (ILibraryEntryDescriptor) entryIterator.next();
181             if(iled.getConfiguration().getName().equals(lrd.getConfiguration().getName()))
182             {
183                found = true;
184                break;
185             }
186          }
187          if(!found)
188          {
189             if(lrd.getType() != LibraryResourceTable.LibraryResourceDescriptor.ECLIPSE)
190             {
191                singleEdit = new LibraryStructuralEdit(lrd.getFile(), lrd
192                   .getConfiguration().getName(), LibraryEdit.ADDITION);
193             }
194             else
195             {
196                singleEdit = new LibraryStructuralEdit(new File(lrd.getIFile()
197                   .getRawLocation().toOSString()), lrd.getConfiguration()
198                      .getName(), LibraryEdit.ADDITION);
199             }
200             toReturn.add(singleEdit);               
201          }
202          toReturn.add(new LibraryStructuralEdit(lrd.getConfiguration(), LibraryEdit.CONFIGURATION));
203       }
204       return toReturn;
205    }
206    
207    private List createDeletions(List resources, List entries)
208    {         
209       // now go through and figure out deletions.  Deletions are those resources that are in the 
210       // library's list of entries, but not in the LRT's list of resources...
211       List toReturn = new ArrayList();
212       Iterator resourcesIterator = null;
213       Iterator entryIterator = null; 
214       ILibraryEntryDescriptor iled = null;
215       LibraryResourceTable.LibraryResourceDescriptor lrd = null;
216       LibraryStructuralEdit singleEdit = null;
217       boolean found = false;
218       
219       entryIterator = entries.iterator();
220       while(entryIterator.hasNext())
221       {  
222          iled = (ILibraryEntryDescriptor) entryIterator.next();
223          
224          // if this file is not already in the library (we do this because 
225          // if it IS already in the library, then a copy of this unzipped 
226          // file already is in the edit directory, and we don't need to 
227          // re-copy it there from wherever it lives.
228          resourcesIterator = resources.iterator();
229          found = false;
230          while(resourcesIterator.hasNext())
231          {
232             lrd = (LibraryResourceTable.LibraryResourceDescriptor) resourcesIterator.next();   
233             if(iled.getConfiguration().getName().equals(lrd.getConfiguration().getName()))
234             {
235                found = true;
236                break;
237             }
238          }
239          if(!found)
240          {
241             singleEdit = new LibraryStructuralEdit((URL)null, iled.getConfiguration().getName(), LibraryEdit.DELETION);
242             toReturn.add(singleEdit);               
243          }
244       }
245       return toReturn;
246    }
247    
248    /*
249     *  (non-Javadoc)
250     * @see org.eclipse.jface.wizard.IWizard#performFinish()
251     */
252    public boolean performFinish()
253    {
254       boolean toReturn = false;
255       
256       Iterator i = null;
257       String libName = _pageZero.getLibraryName();
258       Properties props = _pageThree.getProperties();
259       File oldLibraryFile = null;
260       LibraryStructuralCompositeEdit groupEdit = null;
261       LibraryStructuralEdit singleEdit = null;
262       boolean renamed = (!libName.equals(_library.getConfiguration().getName()));      
263       if(!_library.isAvailable())
264       {
265          MessageDialog
266             .openError(
267                getShell(),
268                "Library unavailable...",
269                "This edit could not be completed because the library in "
270                + "question is presently unavailable (most likely because of "
271                + "a refresh operation or other edit.  Please try this edit "
272                + "again later.");
273          toReturn = false;
274       }
275       else
276       {
277          // set initialize this object to what the current configuration of the 
278          // library is...
279          LibraryConfiguration newConf = new LibraryConfiguration();
280          newConf.setProperties(_library.getConfiguration().getProperties());
281          // check to see if we need to change the system property associated 
282          // with the library name...
283          if(renamed)
284          {
285             newConf.setSystemProperty(LibraryConfiguration.LIBRARY_NAME_PROPERTY, libName);
286          }
287 
288          newConf.setUserProperties(props);
289          groupEdit = new LibraryStructuralCompositeEdit(_library, Libraries.getLibraryEditPath(_library.getName()));
290          groupEdit.addEdit(new LibraryStructuralEdit(newConf, LibraryEdit.LIBRARY_LEVEL | LibraryEdit.CONFIGURATION));
291 
292          List additions = createAdditionsAndConfigurations(_pageOne.getSources(), _library.getEntryDescriptors());
293          List deletions = createDeletions(_pageOne.getSources(), _library.getEntryDescriptors());
294          i = additions.iterator();
295          while(i.hasNext())
296          {
297             singleEdit = (LibraryStructuralEdit) i.next();
298             groupEdit.addEdit(singleEdit);
299          }
300          i = deletions.iterator();
301          while(i.hasNext())
302          {
303             singleEdit = (LibraryStructuralEdit) i.next();
304             groupEdit.addEdit(singleEdit);
305          }
306 
307          LibraryEdit edit = new LibraryEdit(_library);
308          edit.add(groupEdit);
309          EditLibraryJob elj = new EditLibraryJob(_library, edit);
310          elj.schedule();
311          toReturn = true;
312       }
313       
314       return toReturn;
315    }
316 }