View Javadoc

1   /*
2    * $Id: OWLLibraryBuilderPageTwo.java,v 1.5 2005/06/01 17:47:43 aperezlo 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.util.Collection;
12  import java.util.Iterator;
13  import java.util.Properties;
14  
15  import org.eclipse.jface.viewers.ISelectionChangedListener;
16  import org.eclipse.jface.viewers.IStructuredSelection;
17  import org.eclipse.jface.viewers.LabelProvider;
18  import org.eclipse.jface.viewers.SelectionChangedEvent;
19  import org.eclipse.jface.viewers.TableViewer;
20  import org.eclipse.jface.wizard.WizardPage;
21  import org.eclipse.swt.SWT;
22  import org.eclipse.swt.events.ModifyEvent;
23  import org.eclipse.swt.events.ModifyListener;
24  import org.eclipse.swt.events.SelectionAdapter;
25  import org.eclipse.swt.events.SelectionEvent;
26  import org.eclipse.swt.graphics.Image;
27  import org.eclipse.swt.layout.GridData;
28  import org.eclipse.swt.layout.GridLayout;
29  import org.eclipse.swt.widgets.Button;
30  import org.eclipse.swt.widgets.Composite;
31  import org.eclipse.swt.widgets.Group;
32  import org.eclipse.swt.widgets.Label;
33  import org.eclipse.swt.widgets.Text;
34  import org.eclipse.ui.model.BaseWorkbenchContentProvider;
35  
36  import com.bbn.swede.core.libraries.AbstractConfiguration;
37  import com.bbn.swede.core.libraries.IConfigurationValidator;
38  import com.bbn.swede.ui.OWLImages;
39  
40  /***
41   * The third page of the SWeDE library creation and edit wizards.
42   * 
43   * @author aperezlo
44   */
45  public class OWLLibraryBuilderPageTwo extends WizardPage implements ISelectionChangedListener
46  {
47     private static final String NAME = "page-two";
48     private Collection _sources;
49     private LibraryResourceTable.LibraryResourceDescriptor _currentLRD;
50     private PropertyEditor _propertyEditor;
51     private TableViewer _sourcesTable;
52     
53     private Label _nameLabel;
54     private Label _uriLabel;
55     private Text _nameText;
56     private Text _uriText;
57     private Button _allowUpdateButton;
58     private Text _updateURLText;
59     
60     /***
61      * Creates the page.
62      * @param sources A collection of sources in the library
63      */
64     public OWLLibraryBuilderPageTwo(Collection sources)
65     {
66        super(NAME);
67        _sources = sources;
68        setTitle("Library Resource Metadata");
69        setMessage("Provide additional metadata about the resources in the library");
70     }
71  
72     /*
73      *  (non-Javadoc)
74      * @see org.eclipse.jface.wizard.IWizardPage#isPageComplete()
75      */
76     public boolean isPageComplete()
77     {
78        boolean completed = true;
79  
80        if(_currentLRD != null)
81        {
82           _currentLRD.getConfiguration().setUserProperties(_propertyEditor.getProperties());
83        }
84        
85        LibraryResourceTable.LibraryResourceDescriptor lrd = null;
86        IConfigurationValidator entryValidator = null;
87        Iterator i = _sources.iterator();
88        while(i.hasNext())
89        {
90           lrd = (LibraryResourceTable.LibraryResourceDescriptor) i.next();
91           entryValidator = lrd.getConfiguration().getPropertiesValidator();
92           if(!entryValidator.isValid(lrd.getConfiguration()))
93           {
94              completed = false;
95              String errorMessage = "";
96              String[] errors = entryValidator.getErrorMessages();
97              for(int index = 0; index < errors.length; errorMessage += errors[index++] + "\n")
98              {
99                 //do nothing
100             }
101          }
102       }
103       
104       return completed;
105    }
106    
107    /***
108     * Sets the collection of sources for the library.
109     * @param collection The new sources collection
110     */
111    public void setSources(Collection collection)
112    {
113       _sources.clear();
114       _sources.addAll(collection);
115       _sourcesTable.setInput(_sources);
116    }
117    
118    /*
119     *  (non-Javadoc)
120     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
121     */
122    public void createControl(Composite parent)
123    {
124       Group sourceGroup = new Group(parent, parent.getStyle());
125       GridLayout groupLayout = new GridLayout();
126       
127       sourceGroup.setLayout(groupLayout);
128       sourceGroup.setFont(parent.getFont());
129       sourceGroup.setText("Selected Resources:");
130       sourceGroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_BOTH));
131       
132       Composite topPanel = new Composite(sourceGroup, sourceGroup.getStyle());
133       topPanel.setFont(sourceGroup.getFont());
134       topPanel.setLayout(new GridLayout(2, false));
135       topPanel.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
136       
137       _sourcesTable = new TableViewer(topPanel, topPanel.getStyle() | SWT.BORDER);
138       _sourcesTable.getTable().setFont(topPanel.getFont());
139       _sourcesTable.getTable().setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
140       _sourcesTable.setContentProvider(new InternalContentProvider());
141       _sourcesTable.setLabelProvider(new InternalLabelProvider());
142       _sourcesTable.setInput(_sources);
143       _sourcesTable.addSelectionChangedListener(this);
144 
145       Composite fieldsPanel = new Composite(topPanel, topPanel.getStyle());
146       fieldsPanel.setFont(topPanel.getFont());
147       fieldsPanel.setLayout(new GridLayout(2, false));
148       fieldsPanel.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
149       
150       _nameLabel = new Label(fieldsPanel, fieldsPanel.getStyle());
151       _nameLabel.setFont(fieldsPanel.getFont());
152       _nameLabel.setText("Resource name:");
153       
154       _nameText = new Text(fieldsPanel, fieldsPanel.getStyle() | SWT.BORDER);
155       _nameText.setFont(fieldsPanel.getFont());
156       _nameText.setEnabled(false);
157       _nameText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
158          | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
159       _nameText.addModifyListener(new ModifyListener()
160       {
161          public void modifyText(ModifyEvent e)
162          {
163             if(_currentLRD != null)
164             {
165                _currentLRD.getConfiguration().setName(_nameText.getText());
166                getContainer().updateButtons();
167             }
168          }
169       });
170       
171       _uriLabel = new Label(fieldsPanel, fieldsPanel.getStyle());
172       _uriLabel.setFont(fieldsPanel.getFont());
173       _uriLabel.setText("Resource base URI:");
174       
175       _uriText = new Text(fieldsPanel, fieldsPanel.getStyle() | SWT.BORDER);
176       _uriText.setFont(fieldsPanel.getFont());
177       _uriText.setEnabled(false);
178       _uriText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
179          | GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
180       _uriText.addModifyListener(new ModifyListener()
181       {
182          public void modifyText(ModifyEvent e)
183          {
184             if(_currentLRD != null)
185             {
186                _currentLRD.getConfiguration().setURI(_uriText.getText());
187                getContainer().updateButtons();
188             }
189             
190          }
191       });
192       
193       Label allowUpdateLabel = new Label(fieldsPanel, fieldsPanel.getStyle());
194       allowUpdateLabel.setFont(fieldsPanel.getFont());
195       allowUpdateLabel.setText("Allow updates:");
196       
197       _allowUpdateButton = new Button(fieldsPanel, fieldsPanel.getStyle() | SWT.CHECK);
198       _allowUpdateButton.setFont(fieldsPanel.getFont());
199       _allowUpdateButton.setSelection(false);
200       _allowUpdateButton.setEnabled(false);
201       _allowUpdateButton.addSelectionListener(new SelectionAdapter()
202       {
203          public void widgetSelected(SelectionEvent e)
204          {
205             super.widgetSelected(e);
206             if(_currentLRD != null)
207             {
208                _currentLRD.getConfiguration().setUpdateSchedule(Boolean.toString(_allowUpdateButton.getSelection()));
209             }
210             _updateURLText.setEnabled(_allowUpdateButton.getSelection());
211             getContainer().updateButtons();
212          }
213       });
214    
215       Label updateURLLabel = new Label(fieldsPanel, fieldsPanel.getStyle());
216       updateURLLabel.setFont(fieldsPanel.getFont());
217       updateURLLabel.setText("Update URL:");
218       
219       _updateURLText = new Text(fieldsPanel, fieldsPanel.getStyle() | SWT.BORDER);
220       _updateURLText.setFont(fieldsPanel.getFont());
221       _updateURLText.setEnabled(false);
222       _updateURLText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
223       _updateURLText.addModifyListener(new ModifyListener()
224       {
225          public void modifyText(ModifyEvent e)
226          {
227             if(_currentLRD != null)
228             {
229                _currentLRD.getConfiguration().setURL(_updateURLText.getText());
230                getContainer().updateButtons();
231             }
232          }
233       });
234    
235       Group metadataGroup = new Group(sourceGroup, sourceGroup.getStyle());
236       metadataGroup.setLayout(new GridLayout());
237       metadataGroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
238       metadataGroup.setFont(sourceGroup.getFont());
239       metadataGroup.setText("Additional Metadata:");
240       
241       Properties props = new Properties();
242       _propertyEditor = new PropertyEditor(props);
243       
244       _propertyEditor.createControl(metadataGroup, metadataGroup.getStyle());
245       _propertyEditor.getControl().setLayoutData(
246          new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL
247             | GridData.FILL_BOTH));
248       _propertyEditor.setEnabled(false);
249       setControl(sourceGroup);
250    }
251 
252    /*
253     *  (non-Javadoc)
254     * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(
255     *    org.eclipse.jface.viewers.SelectionChangedEvent)
256     */
257    public void selectionChanged(SelectionChangedEvent event)
258    {
259       IStructuredSelection iss = (IStructuredSelection) event.getSelection();
260       LibraryResourceTable.LibraryResourceDescriptor lrd  = null;
261       if(!iss.isEmpty())
262       {
263          if(_currentLRD != null)
264          {
265             _currentLRD.getConfiguration().setUserProperties(_propertyEditor.getProperties());
266             _currentLRD.getConfiguration().setName(_nameText.getText());
267             _currentLRD.getConfiguration().setURL(_updateURLText.getText());
268             _currentLRD.getConfiguration().setURI(_uriText.getText());
269             _currentLRD.getConfiguration().setUpdateSchedule(Boolean.toString(_allowUpdateButton.getSelection()));
270          }
271          _currentLRD = (LibraryResourceTable.LibraryResourceDescriptor) iss.getFirstElement();
272          _propertyEditor.setProperties(_currentLRD.getConfiguration().getUserProperties());
273          _nameText
274             .setText(_currentLRD.getConfiguration().getName() == null ? ""
275                : _currentLRD.getConfiguration().getName());
276          _uriText.setText(_currentLRD.getConfiguration().getURI() == null ? ""
277             : _currentLRD.getConfiguration().getURI());
278          _updateURLText
279             .setText(_currentLRD.getConfiguration().getURL() == null ? ""
280                : _currentLRD.getConfiguration().getURL());
281          
282          _allowUpdateButton.setSelection(false);
283          if(_currentLRD.getConfiguration().getUpdateSchedule() != null)
284          {
285             if(_currentLRD.getConfiguration().getUpdateSchedule().equals(Boolean.TRUE.toString()))
286             {
287                _allowUpdateButton.setSelection(true);
288             }
289          }
290          
291          _allowUpdateButton.setEnabled(true);
292          _updateURLText.setEnabled(_allowUpdateButton.getSelection());
293          _nameText.setEnabled(true);
294          _uriText.setEnabled(true);
295          _propertyEditor.setEnabled(true);
296       }
297       else
298       {
299          _currentLRD = null;
300          _nameText.setEnabled(false);
301          _uriText.setEnabled(false);
302          _propertyEditor.setEnabled(false);
303       }
304    }
305    
306    private final class InternalContentProvider extends BaseWorkbenchContentProvider
307    {
308       /* (non-Javadoc)
309        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
310        */
311       public Object[] getElements(Object element)
312       {
313          Object[] toReturn = new Object[0];
314          if(element instanceof Collection)
315          {
316             toReturn = ((Collection) element).toArray();
317          }
318          return toReturn;
319       }
320    }
321    private final class InternalLabelProvider extends LabelProvider
322    {
323       /* (non-Javadoc)
324        * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
325        */
326       public Image getImage(Object element)
327       {
328          return OWLImages.getSharedImage(OWLImages.IMG_OWL_OBJ);
329       }
330       /* (non-Javadoc)
331        * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
332        */
333       public String getText(Object element)
334       {
335          String toReturn = super.getText(element);
336          LibraryResourceTable.LibraryResourceDescriptor lrd = null;
337          if(element instanceof LibraryResourceTable.LibraryResourceDescriptor)
338          {
339             toReturn = ((LibraryResourceTable.LibraryResourceDescriptor) element).getConfiguration().getName();
340          }
341          return toReturn;
342       }
343    }
344    private final class InternalConfiguration extends AbstractConfiguration
345    {
346       /* (non-Javadoc)
347        * @see com.bbn.swede.core.libraries.IConfiguration#getName()
348        */
349       public String getName()
350       {
351          return null;
352       }
353       /* (non-Javadoc)
354        * @see com.bbn.swede.core.libraries.IConfiguration#getPropertiesValidator()
355        */
356       public IConfigurationValidator getPropertiesValidator()
357       {
358          return null;
359       }
360    }
361    
362    
363 }