View Javadoc

1   /*
2    * $Id: LibraryStructuralCompositeEdit.java,v 1.4 2005/06/01 17:38:38 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.core.libraries;
10  
11  import java.io.File;
12  import java.util.ArrayList;
13  import java.util.Iterator;
14  import java.util.List;
15  
16  import org.eclipse.core.runtime.IProgressMonitor;
17  import org.eclipse.core.runtime.IStatus;
18  import org.eclipse.core.runtime.MultiStatus;
19  import org.eclipse.core.runtime.Status;
20  import org.eclipse.core.runtime.SubProgressMonitor;
21  
22  import com.bbn.swede.core.OWLCore;
23  
24  /***
25   * An object to aggregate related LibraryStructuralEdit operations into a
26   * single unit.
27   * 
28   * @author aperezlo
29   */
30  public class LibraryStructuralCompositeEdit
31  {
32     private ILibraryDescriptor _library;
33     private List _operations;
34     private List _configurationOperations;
35     
36     /***
37      * Creates a container for aggregating multiple edits to a library.
38      * As explained in 
39      * {@link LibraryDescriptor#edit(LibraryEdit, IProgressMonitor)},
40      * all of the changes to the resources destined to be in a library take 
41      * place in a particular directory.  The <code>directory</code> parameter
42      * specifies this location.  
43      * 
44      * @param ild the library to edit
45      * @param directory the directory where file additions/deletions/refreshes 
46      *        should occur
47      */
48     public LibraryStructuralCompositeEdit(ILibraryDescriptor ild, File directory)
49     {
50        _library = ild;
51        _operations = new ArrayList();
52        _configurationOperations = new ArrayList();
53     }
54     /***
55      * <p>Creates a container for aggregating multiple edits to a library.
56      * As explained in 
57      * {@link LibraryDescriptor#edit(LibraryEdit, IProgressMonitor)},
58      * all of the changes to the resources destined to be in a library take 
59      * place in a particular directory.  The <code>directory</code> parameter
60      * specifies this location.</p>
61      * 
62      * <p>This is a convenience method, fully equivalent to
63      * <blockquote>LibraryStructuralCompositeEdit(ild, new File(directory))</blockquote></p>
64      * @param ild The library to edit
65      * @param directory The path of the directory where 
66      *                  additions/deletions/refreshes should occur.
67      * @see #LibraryStructuralCompositeEdit(ILibraryDescriptor, File)
68      */
69     public LibraryStructuralCompositeEdit(ILibraryDescriptor ild, String directory)
70     {
71        this(ild, new File(directory));
72     }
73  
74     /***
75      * Adds an edit to the composite edit.
76      * @param lse the edit to add
77      */
78     public void addEdit(LibraryStructuralEdit lse)
79     {
80        if((lse.getType() & LibraryEdit.CONFIGURATION) == LibraryEdit.CONFIGURATION)
81        {
82           _configurationOperations.add(lse);
83        }
84        else
85        {
86           _operations.add(lse);
87        }
88     }
89     
90     /***
91      * <p>Perform all of the configuration edits that have been added to this
92      * composite.</p>
93      * 
94      * <p>This operation supports cancelation.</p>
95      * 
96      * @param ild the library upon which these configuration edits should occur
97      * @param progress an object to be notified of the progress of the operation
98      * @return <code>IStatus.OK</code>, <code>IStatus.CANCEL</code>, or
99      *         <code>IStatus.ERROR</code> depending on whether the operation
100     *         was a success, canceled, or if something went wrong, respectively.
101     */
102    public IStatus configure(ILibraryDescriptor ild, IProgressMonitor progress)
103    {
104       MultiStatus toReturn = new MultiStatus(OWLCore.getID(), IStatus.OK, "Configuration succeeded", null);
105       Iterator i = _configurationOperations.iterator();
106       LibraryStructuralEdit lse = null;
107       progress.beginTask("Configuring library '" + _library.getName() + "...", 100 * _configurationOperations.size());
108       while(i.hasNext() && !progress.isCanceled())
109       {
110          lse = (LibraryStructuralEdit) i.next();
111          toReturn.add(lse.configure(_library, new SubProgressMonitor(progress, 100)));
112       }
113       if(progress.isCanceled())
114       {
115          toReturn.add(Status.CANCEL_STATUS);
116       }
117       progress.done();
118       return toReturn;
119    }
120 
121    
122    /***
123     * <p>Perform all of the edits that have been added to this 
124     * composite.</p>
125     * 
126     * <p>This operation supports cancelation.</p>
127     * @param progress an object to be notified of the progress of the operation
128     * @return IStatus.OK if the operation was a success, IStatus.CANCEL if it was canceled, or IStatus.ERROR otherwise 
129     */
130    public IStatus perform(IProgressMonitor progress)
131    {
132       MultiStatus toReturn = new MultiStatus(OWLCore.getID(), IStatus.OK, "Edit succeeded", null);
133       Iterator i = _operations.iterator();
134       LibraryStructuralEdit lse = null;
135       File directory = new File(Libraries.getLibraryEditPath(_library.getName()));
136       directory.mkdirs();
137       progress.beginTask("Editing library '" + _library.getName() + "...", 100 * _operations.size());
138       while(i.hasNext() && !progress.isCanceled())
139       {
140          lse = (LibraryStructuralEdit) i.next();
141          toReturn.add(lse.perform(directory, new SubProgressMonitor(progress, 100)));
142       }
143       if(progress.isCanceled())
144       {
145          toReturn.add(Status.CANCEL_STATUS);
146       }
147       progress.done();
148       return toReturn;
149    }
150 }