View Javadoc

1   /*
2    * $Id: LibraryEdit.java,v 1.3 2005/05/31 16:04: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.core.libraries;
10  
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.eclipse.core.runtime.IProgressMonitor;
16  import org.eclipse.core.runtime.IStatus;
17  import org.eclipse.core.runtime.MultiStatus;
18  import org.eclipse.core.runtime.Status;
19  import org.eclipse.core.runtime.SubProgressMonitor;
20  
21  import com.bbn.swede.core.OWLCore;
22  
23  /***
24   * This class represents a series of edits to a library.  Edits can include 
25   * files that were added to, or deleted from, the library, as well as 
26   * metadata changes for any of the files in the library or the library 
27   * itself, or indications that files should be updated from the web.
28   * 
29   * @author aperezlo
30   */
31  public class LibraryEdit
32  {
33     /***
34      * Edit type constant indicating the addition of one or more entries.
35      */
36     public static final int ADDITION      = 1;
37     /***
38      * Edit type constant indicating the deletion of one or more entries.
39      */
40     public static final int DELETION      = 2;
41     /***
42      * Edit type constant indicating that entries were refreshed.
43      */
44     public static final int REFRESH       = 4;
45     /***
46      * Edit type constant indicating that the library's configuartion changed.
47      */
48     public static final int CONFIGURATION = 8;
49     /***
50      * Edit type constant.
51      */
52     public static final int LIBRARY_LEVEL = 16;
53     
54     private List _operations;
55     private ILibraryDescriptor _library;
56     
57     /***
58      * Creates a library edit operation.
59      * @param library The library with which this edit is associated
60      */
61     public LibraryEdit(ILibraryDescriptor library)
62     {
63        _operations = new ArrayList();
64        _library = library;
65     }
66  
67     /***
68      * Add this composite edit to this LibraryEdit.  Operations are 
69      * performed in the order they were added.
70      * 
71      * @param lsce a composite edit
72      */
73     public void add(LibraryStructuralCompositeEdit lsce)
74     {
75        _operations.add(lsce);
76     }
77     
78     /***
79      * Performs the operations in this LibraryEdit upon the library supplied 
80      * at object construction.
81      * <br /> <br />
82      * This operation supports cancelation
83      * 
84      * @param monitor an object to receive notifications of the progress of this operation
85      * @return IStatus.OK if the operation was a success, IStatus.CANCEL if it was canceled, and IStatus.ERROR otherwise
86      */
87     public IStatus perform(IProgressMonitor monitor)
88     {
89        MultiStatus toReturn = new MultiStatus(OWLCore.getID(), IStatus.OK, "Edit succeeded", null);
90        Iterator i = _operations.iterator();
91        LibraryStructuralCompositeEdit lsce = null;
92        monitor.beginTask("Editing '" + _library.getName() + "'", 100 * _operations.size());
93        while(i.hasNext())
94        {
95           lsce = (LibraryStructuralCompositeEdit) i.next();
96           toReturn.add(lsce.perform(new SubProgressMonitor(monitor, 100)));
97        }
98        monitor.done();
99        if(monitor.isCanceled())
100       {
101          toReturn.add(Status.CANCEL_STATUS);
102       }
103       return toReturn;
104    }
105    
106    /***
107     * Performs the configuration operations in this LibraryEdit upon the 
108     * library supplied at object construction.
109     * <br /> <br />
110     * This operation supports cancelation
111     * 
112     * @param monitor an object to receive notifications of the progress of this operation
113     * @return IStatus.OK if the operation was a success, IStatus.CANCEL if it was canceled, and IStatus.ERROR otherwise
114     */
115    public IStatus configure(IProgressMonitor monitor)
116    {
117       MultiStatus toReturn = new MultiStatus(OWLCore.getID(), IStatus.OK, "Configuration succeeded", null);
118       Iterator i = _operations.iterator();
119       LibraryStructuralCompositeEdit lsce = null;
120       monitor.beginTask("Configuring '" + _library.getName() + "'", 100 * _operations.size());
121       while(i.hasNext())
122       {
123          lsce = (LibraryStructuralCompositeEdit) i.next();
124          toReturn.add(lsce.configure(_library, new SubProgressMonitor(monitor, 100)));
125       }
126       monitor.done();
127       if(monitor.isCanceled())
128       {
129          toReturn.add(Status.CANCEL_STATUS);
130       }
131       return toReturn;
132    }
133    
134 }