View Javadoc

1   /*
2    * $Id: OWLLibraryEditorPageOne.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.BufferedInputStream;
12  import java.io.BufferedOutputStream;
13  import java.io.File;
14  import java.io.FileNotFoundException;
15  import java.io.FileOutputStream;
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.util.Iterator;
19  
20  import org.eclipse.swt.widgets.Composite;
21  
22  import com.bbn.swede.core.libraries.ILibraryDescriptor;
23  import com.bbn.swede.core.libraries.ILibraryEntryDescriptor;
24  import com.bbn.swede.core.libraries.Libraries;
25  
26  /***
27   * The second page of the Edit Library wizard (note that this wizard 
28   * starts with this (the second) page), but it is possible to go 'Back' to
29   * the first page).
30   * 
31   * @author aperezlo
32   */
33  public class OWLLibraryEditorPageOne extends OWLLibraryBuilderPageOne
34  {
35     private ILibraryDescriptor _library;
36     
37     /***
38      * Constructs a page to edit an existing library.
39      * @param iss The descriptor of the library to edit
40      */
41     public OWLLibraryEditorPageOne(ILibraryDescriptor iss)
42     {
43        super(null);
44        _library = iss;
45     }
46     
47     
48     private File unzip(InputStream is, String name)
49     {
50        final int iBufferSize = 1024;
51        byte[] buffer = new byte[iBufferSize];
52        int length = iBufferSize;
53        File outputDirectory = new File(Libraries.getLibraryEditPath(_library.getName()));
54        File output = new File(outputDirectory, name);
55        BufferedInputStream in = null;
56        BufferedOutputStream out = null;
57        try
58        {
59           outputDirectory.mkdirs();
60           output.createNewFile();
61           output.deleteOnExit();
62        }
63        catch (IOException e)
64        {
65           e.printStackTrace();
66           output = null;
67        }
68        if(output != null)
69        {
70           try
71           {
72              in = new BufferedInputStream(is);
73              out = new BufferedOutputStream(new FileOutputStream(output));
74              while(length > -1)
75              {
76                 length = in.read(buffer, 0, iBufferSize);
77                 if(length > -1)
78                 {
79                    out.write(buffer, 0, length);
80                 }
81              }
82           }
83           catch (FileNotFoundException e1)
84           {
85              e1.printStackTrace();
86           }
87           catch (IOException e1)
88           {
89              e1.printStackTrace();
90           }
91           finally
92           {
93              try
94              {
95                 out.flush();
96                 out.close();
97                 in.close();
98              }
99              catch (IOException e2)
100             {
101                e2.printStackTrace();
102             }
103          }
104       }
105       
106       return output;
107    }
108    
109    /*
110     *  (non-Javadoc)
111     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
112     */
113    public void createControl(Composite parent)
114    {
115       super.createControl(parent);
116       LibraryResourceTable.LibraryResourceDescriptor lrd = null;
117       ILibraryEntryDescriptor iled = null;
118       Iterator i = _library.getEntryDescriptors().iterator();
119       File localFile = null;
120       
121       while(i.hasNext())
122       {
123          iled = (ILibraryEntryDescriptor) i.next();
124          localFile = unzip(iled.getFileInputStream(), iled.getConfiguration().getName());
125          lrd = _libraryResourceTable.new LibraryResourceDescriptor(iled.getConfiguration().getName(), localFile);
126          lrd.getConfiguration().setProperties(iled.getConfiguration().getProperties());
127          _libraryResources.add(lrd);
128       }
129       
130       _libraryResourceTable.getTableViewer().setInput(_libraryResources);
131       
132    }
133    
134 }