View Javadoc

1   /*
2    * $Id: OWLLibraryBuilderPageOne.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.MalformedURLException;
13  import java.net.URL;
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.regex.Matcher;
19  import java.util.regex.Pattern;
20  
21  import org.eclipse.core.resources.IFile;
22  import org.eclipse.jface.dialogs.MessageDialog;
23  import org.eclipse.jface.dialogs.ProgressMonitorDialog;
24  import org.eclipse.jface.viewers.IStructuredSelection;
25  import org.eclipse.swt.SWT;
26  import org.eclipse.swt.events.ModifyEvent;
27  import org.eclipse.swt.events.ModifyListener;
28  import org.eclipse.swt.events.SelectionAdapter;
29  import org.eclipse.swt.events.SelectionEvent;
30  import org.eclipse.swt.events.VerifyEvent;
31  import org.eclipse.swt.events.VerifyListener;
32  import org.eclipse.swt.layout.GridData;
33  import org.eclipse.swt.layout.GridLayout;
34  import org.eclipse.swt.widgets.Button;
35  import org.eclipse.swt.widgets.Composite;
36  import org.eclipse.swt.widgets.Event;
37  import org.eclipse.swt.widgets.Group;
38  import org.eclipse.swt.widgets.Label;
39  import org.eclipse.swt.widgets.Text;
40  import org.eclipse.ui.dialogs.WizardExportResourcesPage;
41  
42  import com.bbn.swede.core.libraries.DownloadFileOperation;
43  
44  /***
45   * The second wizard page for the Library creation wizard.
46   * 
47   * @author aperezlo
48   */
49  public class OWLLibraryBuilderPageOne extends WizardExportResourcesPage
50  {
51     /***
52      * List of resources to include in the library.
53      */
54     protected List _libraryResources;
55     /***
56      * Widget containing resources added to the library.
57      */
58     protected LibraryResourceTable _libraryResourceTable;
59     private IStructuredSelection _initialSelection;
60     
61     /***
62      * Creates the page.
63      * @param iss The current selection
64      */
65     public OWLLibraryBuilderPageOne(IStructuredSelection iss)
66     {
67        super("Library Builder", iss);
68        _initialSelection = iss;
69        setTitle("SWeDE Library Wizard");
70        setMessage("Select resources");
71        _libraryResources = new ArrayList();
72     }
73     
74     /***
75      * Creates controls for adding resources from SWeDE projects to the library.
76      * @param parent The parent for the controls
77      */
78     protected void createSWeDEResourcesGroup(Composite parent)
79     {
80        Group swedeResourcesGroup = new Group(parent, parent.getStyle());
81        GridLayout groupLayout = new GridLayout();
82        Button addButton = null;
83        
84        groupLayout.numColumns = 2;
85        
86        swedeResourcesGroup.setFont(parent.getFont());
87        swedeResourcesGroup.setLayout(groupLayout);
88        swedeResourcesGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL));
89        swedeResourcesGroup.setText("Select SWeDE Resources:");
90        
91        createResourcesGroup(swedeResourcesGroup);
92        if(_initialSelection != null)
93        {
94           setupBasedOnInitialSelections();         
95        }
96        
97        addButton = new Button(swedeResourcesGroup, swedeResourcesGroup.getStyle() | SWT.PUSH);
98        addButton.setFont(swedeResourcesGroup.getFont());
99        addButton.setText("Add");
100       addButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
101       addButton.addSelectionListener(new SelectionAdapter()
102       {
103          public void widgetSelected(SelectionEvent e)
104          {
105             super.widgetSelected(e);
106             List resourcesList = getSelectedResources();
107             LibraryResourceTable.LibraryResourceDescriptor lrd = null;
108             Iterator i = resourcesList.iterator();
109             IFile resource = null;
110             
111             int numRejected = 0;
112             String rejectedResourcesString = "\n";
113 
114             while(i.hasNext())
115             {
116                resource = (IFile) i.next();
117                if(resource.getFileExtension().equals("owl"))
118                {
119                   lrd = _libraryResourceTable.new LibraryResourceDescriptor(resource.getName(), resource);
120                   if(!_libraryResources.contains(lrd))
121                   {
122                      _libraryResources.add(lrd);
123                   }
124                }
125                else
126                {
127                   numRejected++;
128                   rejectedResourcesString += resource.getName() + "\n";
129                }
130             }
131             if(numRejected > 0)
132             {
133                MessageDialog
134                   .openWarning(
135                      getShell(),
136                      "Invalid file type",
137                      "Warning: "
138                         + ((numRejected > 1) ? (numRejected + " of t") : "T")
139                         + "he resource"
140                         + ((numRejected > 1) ? "s" : "")
141                         + " that you selected "
142                         + ((numRejected > 1) ? "are" : "is")
143                         + " not "
144                         + ((numRejected > 1) ? "" : "an ")
145                         + ".owl file"
146                         + ((numRejected > 1) ? "s" : "")
147                         + ".  Only OWL files can be added to libraries.\n\nThe invalid file"
148                         + ((numRejected > 1) ? "s are" : " is") + ":\n"
149                         + rejectedResourcesString);
150             }
151                
152             _libraryResourceTable.getTableViewer().setInput(_libraryResources);
153             updatePageCompletion();
154          }
155       });
156    }
157    
158    /*
159     *  (non-Javadoc)
160     * @see org.eclipse.ui.dialogs.WizardDataTransferPage#updatePageCompletion()
161     */
162    protected void updatePageCompletion()
163    {
164       setPageComplete(isPageComplete());
165    }
166    
167    /*
168     *  (non-Javadoc)
169     * @see org.eclipse.jface.wizard.IWizardPage#isPageComplete()
170     */
171    public boolean isPageComplete()
172    {
173       Collection col = (Collection) _libraryResourceTable.getTableViewer().getInput();
174       return ((col != null) && (col.size() > 0)); 
175    }
176    
177    /***
178     * Creates the controls for importing resources from the Internet.
179     * @param parent The parent for the import controls
180     */
181    protected void createURLResourcesGroup(Composite parent)
182    {
183       Group urlResourcesGroup = new Group(parent, parent.getStyle());
184       GridLayout groupLayout = new GridLayout();
185       Button addButton = null;
186 
187       urlResourcesGroup.setFont(parent.getFont());
188       urlResourcesGroup.setText("Select Internet resources:");
189       
190       groupLayout.numColumns = 3;
191       groupLayout.makeColumnsEqualWidth = false;
192       
193       urlResourcesGroup.setLayout(groupLayout);
194       urlResourcesGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL));
195             
196       final Text urlText = new Text(urlResourcesGroup, urlResourcesGroup.getStyle() | SWT.BORDER);
197       GridData urlTextLayoutData = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
198       urlTextLayoutData.horizontalSpan = 2;
199       urlText.setFont(urlResourcesGroup.getFont());
200       urlText.setLayoutData(urlTextLayoutData);
201       urlText.setText("http://");
202 
203       addButton = new Button(urlResourcesGroup, urlResourcesGroup.getStyle() | SWT.PUSH);
204       addButton.setFont(urlResourcesGroup.getFont());
205       addButton.setText("Add");
206 
207       final Label nameLabel = new Label(urlResourcesGroup, urlResourcesGroup.getStyle());
208       nameLabel.setFont(urlResourcesGroup.getFont());
209       nameLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
210       nameLabel.setText("Enter a local name for this resource:");
211       
212       final Text nameText = new Text(urlResourcesGroup, urlResourcesGroup.getStyle() | SWT.BORDER);
213       nameText.setFont(urlResourcesGroup.getFont());
214       nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
215          | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
216       nameText.addVerifyListener(new VerifyListener()
217       {
218          public void verifyText(VerifyEvent e)
219          {
220             Pattern p = Pattern.compile("[a-zA-Z0-9//-_//.]*?");
221             e.doit = (p.matcher(e.text).matches());
222          }
223       });
224       
225       urlText.addModifyListener(new ModifyListener()
226       {
227          public void modifyText(ModifyEvent e)
228          {
229             String text = urlText.getText();
230             String currentName = nameText.getText();
231             Matcher m = null;
232             String fileName = null;
233             if((text != null))
234             {
235                m = Pattern.compile("/[a-zA-Z0-9//.//-]++").matcher(text);
236                while(m.find())
237                {
238                   fileName = m.group();
239                }
240                if(fileName != null)
241                {
242                   nameText.setText(fileName.substring(1) + (fileName.substring(1).length() > 0 ? ".owl" : ""));
243                }
244             }
245          }
246       });
247       
248       addButton.addSelectionListener(new SelectionAdapter()
249       {
250          public void widgetSelected(SelectionEvent e)
251          {
252             super.widgetSelected(e);
253             boolean wasError = false;
254             String fileName = nameText.getText();
255             URL newURL = null;
256             try
257             {
258                newURL = new URL(urlText.getText());
259                new File(fileName);
260                if(!fileName.endsWith(".owl"))
261                {
262                   if(MessageDialog.openConfirm(getShell(), "Invalid name",
263                      "The name you entered, '" + nameText.getText()
264                         + "', does not end in '.owl', as is required.  "
265                         + "Would you like it to be added automatically?"))
266                   {
267                      fileName += ".owl";
268                      new File(fileName);
269                   }
270                   else
271                   {
272                      wasError = true;
273                   }
274                }
275             }
276             catch (MalformedURLException e1)
277             {
278                MessageDialog
279                   .openError(
280                      getShell(),
281                      "Invalid URL",
282                      "The URL you entered, '"
283                         + urlText.getText()
284                         + "', is improperly formatted.\nPlease enter a valid URL and try adding this resource again.");
285                wasError = true;
286             }
287             catch(Exception e2)
288             {
289                MessageDialog
290                   .openError(
291                      getShell(),
292                      "Invalid name",
293                      "The name you entered, '"
294                         + nameText.getText()
295                         + "' is improperly formated.\nPlease enter a valid name and try adding this resource again.");
296                wasError = true;
297             }
298             
299             if(!wasError)
300             {
301                LibraryResourceTable.LibraryResourceDescriptor lrd = _libraryResourceTable.new LibraryResourceDescriptor(
302                   fileName, newURL);
303                if(!_libraryResources.contains(lrd))
304                {
305                   DownloadFileOperation dfo = null;
306                   try
307                   {
308                      dfo = new DownloadFileOperation(new URL(lrd.getConfiguration().getURL()), fileName, true);
309                   }
310                   catch (MalformedURLException e4)
311                   {
312                      e4.printStackTrace();
313                   }
314                   try
315                   {
316                      new ProgressMonitorDialog(getShell()).run(true, true, dfo);
317                   }
318                   catch(Exception e3)
319                   {
320                      e3.printStackTrace();
321                   }
322                   if(dfo.getExceptions().size() > 0)
323                   {
324                      MessageDialog
325                         .openError(getShell(), "Download error",
326                            "The file could not be downloaded.  Please check the URL and try again.");
327                   }
328                   else
329                   {
330                      if(!dfo.getWasCanceled())
331                      {
332                         lrd.setFile(dfo.getFile());
333                         _libraryResources.add(lrd);
334                         _libraryResourceTable.getTableViewer().setInput(_libraryResources);
335                         urlText.setText("");
336                         nameText.setText("");
337                      }
338                   }
339                }
340             }
341 
342             updatePageCompletion();
343          }
344       });
345    }
346 
347    /***
348     * Creates controls for viewing and interacting with the resources already
349     * selected for inclusion in the library.
350     * @param parent The parent for the controls
351     */
352    protected void createSelectedResourcesGroup(Composite parent)
353    {
354       Group selectedResourcesGroup = new Group(parent, parent.getStyle());
355       GridLayout groupLayout = new GridLayout();
356       groupLayout.numColumns = 2;
357       Button removeButton = null;
358       
359       selectedResourcesGroup.setFont(parent.getFont());
360       selectedResourcesGroup.setLayout(groupLayout);
361       selectedResourcesGroup.setText("Selected Resources:");
362       selectedResourcesGroup.setLayoutData(new GridData(GridData.FILL_BOTH
363          | GridData.GRAB_VERTICAL | GridData.GRAB_VERTICAL));
364       
365       setupTable(selectedResourcesGroup);
366       
367       removeButton = new Button(selectedResourcesGroup, selectedResourcesGroup.getStyle() | SWT.PUSH);
368       removeButton.setFont(selectedResourcesGroup.getFont());
369       removeButton.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
370       removeButton.setText("Remove");
371       removeButton.addSelectionListener(new SelectionAdapter()
372       {
373          public void widgetSelected(SelectionEvent e)
374          {
375             super.widgetSelected(e);
376             IStructuredSelection iss = (IStructuredSelection) _libraryResourceTable.getTableViewer().getSelection();
377             Iterator iter = null;
378             LibraryResourceTable.LibraryResourceDescriptor lrd = null;
379             if(!iss.isEmpty())
380             {
381                iter = iss.iterator();
382                while(iter.hasNext())
383                {
384                   lrd = (LibraryResourceTable.LibraryResourceDescriptor) iter.next();
385                   _libraryResources.remove(lrd);
386                }
387                _libraryResourceTable.getTableViewer().setInput(_libraryResources);
388             }
389             updatePageCompletion();
390          }
391       });
392    }
393 
394    /***
395     * Retrieves the sources for the library.
396     * @return The list of sources
397     */
398    public List getSources()
399    {
400       return (List) _libraryResourceTable.getTableViewer().getInput();
401    }
402    
403    private void setupTable(Composite parent)
404    {
405       _libraryResourceTable = new LibraryResourceTable(parent, parent.getStyle() | SWT.MULTI);
406    }
407    
408    /*
409     *  (non-Javadoc)
410     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
411     */
412    public void createControl(Composite parent)
413    {
414       Composite thisComposite = new Composite(parent, parent.getStyle());
415       thisComposite.setFont(parent.getFont());
416       thisComposite.setLayout(new GridLayout());
417       thisComposite.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
418       createSWeDEResourcesGroup(thisComposite);
419 //      createFilesystemResourcesGroup(thisComposite);
420       createURLResourcesGroup(thisComposite);
421       createSelectedResourcesGroup(thisComposite);
422       setControl(thisComposite);
423    }
424 
425    /*
426     *  (non-Javadoc)
427     * @see org.eclipse.ui.dialogs.WizardExportResourcesPage#createDestinationGroup(org.eclipse.swt.widgets.Composite)
428     */
429    protected void createDestinationGroup(Composite parent)
430    {
431    }
432 
433    /*
434     *  (non-Javadoc)
435     * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
436     */
437    public void handleEvent(Event event)
438    {
439    }
440 
441 }