View Javadoc

1   /*
2    * $Id: OWLNewLibraryWizard.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.util.ArrayList;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.Map;
17  import java.util.Properties;
18  
19  import org.eclipse.core.resources.IResource;
20  import org.eclipse.jface.viewers.IStructuredSelection;
21  import org.eclipse.jface.viewers.StructuredSelection;
22  import org.eclipse.jface.wizard.IWizardPage;
23  import org.eclipse.jface.wizard.Wizard;
24  import org.eclipse.ui.INewWizard;
25  import org.eclipse.ui.IWorkbench;
26  
27  import com.bbn.swede.core.IOWLElement;
28  import com.bbn.swede.core.IOWLLibrary;
29  import com.bbn.swede.core.libraries.IConfigurationValidator;
30  import com.bbn.swede.core.libraries.ILibraryDescriptor;
31  import com.bbn.swede.core.libraries.ILibraryEntryDescriptor;
32  import com.bbn.swede.core.libraries.Libraries;
33  
34  /***
35   * A wizard for creating new SWeDE libraries.
36   * 
37   * @author aperezlo
38   */
39  public class OWLNewLibraryWizard extends Wizard implements INewWizard
40  {
41     private OWLLibraryBuilderPageZero _pageZero;
42     private OWLLibraryBuilderPageOne _pageOne;
43     private OWLLibraryBuilderPageTwo _pageTwo;
44     private OWLLibraryBuilderPageThree _pageThree;
45     
46     /***
47      * Default constructor.
48      */
49     public OWLNewLibraryWizard()
50     {
51        super();
52     }
53     
54     /*
55      *  (non-Javadoc)
56      * @see org.eclipse.ui.IWorkbenchWizard#init(
57      *    org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
58      */
59     public void init(IWorkbench workbench, IStructuredSelection selection)
60     {
61        setWindowTitle("SWeDE - Create a new library");
62        List newSelection = new ArrayList();
63        
64        Iterator i = selection.iterator();
65        Object o = null;
66        IOWLElement ioe = null;
67        while(i.hasNext())
68        {
69           o = i.next();
70           if(o instanceof IResource)
71           {
72              newSelection.add(o);
73           }
74           else
75           {
76              if(o instanceof IOWLElement)
77              {
78                 ioe = (IOWLElement) o;
79                 if(!(ioe instanceof IOWLLibrary) && (!(ioe.getParent() instanceof IOWLLibrary)))
80                 {
81                    newSelection.add(((IOWLElement) o).getCorrespondingResource());
82                 }
83              }
84           }
85        }
86        
87        _pageZero = new OWLLibraryBuilderPageZero();
88        _pageOne = new OWLLibraryBuilderPageOne(new StructuredSelection(newSelection));
89        _pageTwo = new OWLLibraryBuilderPageTwo(new ArrayList());
90        _pageThree = new OWLLibraryBuilderPageThree();
91        addPage(_pageZero);
92        addPage(_pageOne);
93        addPage(_pageTwo);
94        addPage(_pageThree);
95     }
96  
97     /*
98      *  (non-Javadoc)
99      * @see org.eclipse.jface.wizard.IWizard#getStartingPage()
100     */
101    public IWizardPage getStartingPage()
102    {
103       IWizardPage toReturn = _pageZero;
104       return toReturn;
105    }
106    
107    /*
108     *  (non-Javadoc)
109     * @see org.eclipse.jface.wizard.IWizard#canFinish()
110     */
111    public boolean canFinish()
112    {
113       // if I'm on page 1, i need the sources from page 1
114       // if I'm on page 2, then I need the sources from page 2
115       // how can I tell which page I'm on?
116       Collection col = null;
117       col = (Collection) _pageOne.getSources(); 
118       LibraryResourceTable.LibraryResourceDescriptor lrd = null;
119       IConfigurationValidator entryValidator = null;
120       Iterator i = null;
121       boolean sourcesComplete = true;
122       if(col != null)
123       {
124          i = col.iterator();
125          while(i.hasNext())
126          {
127             lrd = (LibraryResourceTable.LibraryResourceDescriptor) i.next();
128             entryValidator = lrd.getConfiguration().getPropertiesValidator();
129             if(!entryValidator.isValid(lrd.getConfiguration()))
130             {
131                sourcesComplete = false;
132                String errorMessage = "";
133                String[] errors = entryValidator.getErrorMessages();
134                for(int index = 0; index < errors.length; errorMessage += errors[index++] + "\n")
135                {
136                   //do nothing
137                }
138             }
139          }
140       }
141       else
142       {
143          sourcesComplete = false; // shouldn't be neccesary, but it doesn't hurt...
144       }
145       return super.canFinish() && sourcesComplete;
146    }
147       
148    /*
149     *  (non-Javadoc)
150     * @see org.eclipse.jface.wizard.IWizard#performFinish()
151     */
152    public boolean performFinish()
153    {
154       boolean toReturn = false;
155       
156       Iterator i = _pageOne.getSources().iterator();
157       Iterator propsIterator = null;
158       LibraryResourceTable.LibraryResourceDescriptor lrd = null;
159       String libName = null;
160       Map.Entry tempProperty = null;
161       ILibraryDescriptor ild = null;
162       ILibraryEntryDescriptor iled = null;
163       libName = _pageZero.getLibraryName();
164       
165       Properties props = _pageThree.getProperties();
166       propsIterator = props.entrySet().iterator();
167       if(Libraries.createLibrary(libName))
168       {
169          ild = Libraries.getLibrary(libName);
170          ild.getConfiguration().setUserProperties(props);
171          while(i.hasNext())
172          {
173             lrd = (LibraryResourceTable.LibraryResourceDescriptor) i.next();
174             if(lrd.getType() != LibraryResourceTable.LibraryResourceDescriptor.ECLIPSE)
175             {
176                iled = ild.createEntry(lrd.getFile(), new Properties(), null);   
177             }
178             else
179             {
180                iled = ild.createEntry(new File(lrd.getIFile().getRawLocation().toOSString()), new Properties(), null);
181             }
182             iled.getConfiguration().setProperties(lrd.getConfiguration().getProperties());
183             ild.addEntry(iled);
184          }
185          ild.writeLibrary();
186          toReturn = true;
187       }
188       
189       return toReturn;
190    }
191 
192    /*
193     *  (non-Javadoc)
194     * @see org.eclipse.jface.wizard.IWizard#getNextPage(org.eclipse.jface.wizard.IWizardPage)
195     */
196    public IWizardPage getNextPage(IWizardPage page)
197    {
198       IWizardPage toReturn = null;
199       
200       if(page == _pageZero)
201       {
202          toReturn = _pageOne;
203       }
204       else if(page == _pageOne)
205       {
206          _pageTwo.setSources(_pageOne.getSources());
207          toReturn = _pageTwo;
208       }
209       else if(page == _pageTwo)
210       {
211          toReturn = _pageThree;
212       }
213       
214       return toReturn;
215    }
216 }