View Javadoc

1   /*
2    * $Id: ProjectLibraryProperties.java,v 1.6 2005/06/01 17:44:32 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.properties;
10  
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import org.eclipse.core.resources.IProject;
18  import org.eclipse.core.resources.IResource;
19  import org.eclipse.core.runtime.IAdaptable;
20  import org.eclipse.core.runtime.Platform;
21  import org.eclipse.jdt.ui.JavaUI;
22  import org.eclipse.jface.viewers.ISelectionChangedListener;
23  import org.eclipse.jface.viewers.IStructuredSelection;
24  import org.eclipse.jface.viewers.LabelProvider;
25  import org.eclipse.jface.viewers.SelectionChangedEvent;
26  import org.eclipse.jface.viewers.TableViewer;
27  import org.eclipse.swt.SWT;
28  import org.eclipse.swt.events.SelectionAdapter;
29  import org.eclipse.swt.events.SelectionEvent;
30  import org.eclipse.swt.graphics.Image;
31  import org.eclipse.swt.layout.GridData;
32  import org.eclipse.swt.layout.GridLayout;
33  import org.eclipse.swt.widgets.Button;
34  import org.eclipse.swt.widgets.Composite;
35  import org.eclipse.swt.widgets.Control;
36  import org.eclipse.swt.widgets.Label;
37  import org.eclipse.ui.IWorkbenchPropertyPage;
38  import org.eclipse.ui.dialogs.PropertyPage;
39  import org.eclipse.ui.model.WorkbenchContentProvider;
40  
41  import com.bbn.swede.core.IOWLElement;
42  import com.bbn.swede.core.IOWLLibrary;
43  import com.bbn.swede.core.IOWLProject;
44  import com.bbn.swede.core.config.SWProjectInfo;
45  import com.bbn.swede.core.libraries.ILibraryDescriptor;
46  import com.bbn.swede.core.libraries.Libraries;
47  import com.bbn.swede.core.resources.SWResourceManager;
48  //
49  
50  /***
51   * Properties projects which allows for associating and unassociating 
52   * libraries.   
53   * 
54   * @author aperezlo
55   */
56  public class ProjectLibraryProperties extends PropertyPage implements IWorkbenchPropertyPage
57  {
58     private IOWLProject _project;
59     private List _associatedLibrariesList;
60     private List _allLibrariesList;
61     private TableViewer _associatedLibrariesListViewer;
62     private TableViewer _allLibrariesListViewer;
63     private Button _toAvailableButton;
64     private Button _toAllButton;
65     
66     /***
67      * Constructor for ProjectLibraryProperties.
68      */
69     public ProjectLibraryProperties() 
70     {
71        super();
72        noDefaultAndApplyButton();
73        _associatedLibrariesList = new ArrayList();
74        _allLibrariesList = new ArrayList();
75     }
76  
77     private void addSeparator(Composite parent) 
78     {
79        Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
80        GridData gridData = new GridData();
81        gridData.horizontalAlignment = GridData.FILL;
82        gridData.grabExcessHorizontalSpace = true;
83        separator.setLayoutData(gridData);
84     }
85  
86     /*
87      *  (non-Javadoc)
88      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
89      */
90     protected Control createContents(Composite parent) 
91     {
92        Composite composite = new Composite(parent, SWT.NONE);
93        GridLayout layout = new GridLayout(3, false);
94        composite.setLayout(layout);
95        GridData data = new GridData(GridData.FILL);
96        data.grabExcessHorizontalSpace = true;
97        data.grabExcessVerticalSpace = true;
98        composite.setLayoutData(data);
99        composite.setFont(parent.getFont());
100 
101       InternalSelectionListener handler = new InternalSelectionListener();
102       
103       Label associatedLabel = new Label(composite, composite.getStyle());
104       associatedLabel.setFont(composite.getFont());
105       associatedLabel.setText("Currently associated libraries:");
106       associatedLabel.setLayoutData(new GridData (GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
107       
108       new Composite(composite, composite.getStyle());  // empty space...
109       
110       Label allLabel = new Label(composite, composite.getStyle());
111       allLabel.setFont(composite.getFont());
112       allLabel.setText("Available libraries:");
113       allLabel.setLayoutData(new GridData (GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
114       
115       
116       GridData associatedLayoutData = new GridData(GridData.GRAB_HORIZONTAL
117          | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
118       associatedLayoutData.verticalSpan = 2;
119       _associatedLibrariesListViewer = new TableViewer(composite, composite.getStyle() | SWT.BORDER | SWT.MULTI);
120       _associatedLibrariesListViewer.getControl().setLayoutData(associatedLayoutData);
121       _associatedLibrariesListViewer.getControl().setFont(composite.getFont());
122       _associatedLibrariesListViewer.setContentProvider(new InternalContentProvider());
123       _associatedLibrariesListViewer.setLabelProvider(new InternalLabelProvider());
124       _associatedLibrariesListViewer.addSelectionChangedListener(handler);
125       _associatedLibrariesListViewer.setInput(_associatedLibrariesList);
126 
127       
128       
129       _toAvailableButton = new Button(composite, composite.getStyle() | SWT.ARROW | SWT.LEFT);
130       _toAvailableButton.setFont(composite.getFont());
131       _toAvailableButton.setLayoutData(new GridData(
132          GridData.HORIZONTAL_ALIGN_CENTER | GridData.VERTICAL_ALIGN_END
133             | GridData.GRAB_VERTICAL));
134       _toAvailableButton.setText("Add");
135       _toAvailableButton.addSelectionListener(handler);
136       _toAvailableButton.setEnabled(false);
137       
138       GridData allLayoutData = new GridData (GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
139       allLayoutData.verticalSpan = 2;
140       _allLibrariesListViewer = new TableViewer(composite, composite.getStyle() | SWT.BORDER | SWT.MULTI);
141       _allLibrariesListViewer.getControl().setLayoutData(allLayoutData);
142       _allLibrariesListViewer.getControl().setFont(composite.getFont());
143       _allLibrariesListViewer.setContentProvider(new InternalContentProvider());
144       _allLibrariesListViewer.setLabelProvider(new InternalLabelProvider());
145       _allLibrariesListViewer.addSelectionChangedListener(handler);
146       _allLibrariesListViewer.setInput(_allLibrariesList);
147 
148       
149       _toAllButton = new Button(composite, composite.getStyle() | SWT.ARROW | SWT.RIGHT);
150       _toAllButton.setFont(composite.getFont());
151       _toAllButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER
152          | GridData.VERTICAL_ALIGN_BEGINNING | GridData.GRAB_VERTICAL));
153       _toAllButton.setText("Remove");
154       _toAllButton.addSelectionListener(handler);
155       _toAllButton.setEnabled(false);
156       
157       return composite;
158    }
159 
160    private Composite createDefaultComposite(Composite parent) 
161    {
162       Composite composite = new Composite(parent, SWT.NULL);
163       GridLayout layout = new GridLayout();
164       layout.numColumns = 2;
165       composite.setLayout(layout);
166 
167       GridData data = new GridData();
168       data.verticalAlignment = GridData.FILL;
169       data.horizontalAlignment = GridData.FILL;
170       composite.setLayoutData(data);
171 
172       return composite;
173    }
174    
175    /*
176     *  (non-Javadoc)
177     * @see org.eclipse.ui.IWorkbenchPropertyPage#setElement(org.eclipse.core.runtime.IAdaptable)
178     */
179    public void setElement(IAdaptable element)
180    {
181       super.setElement(element);
182       IResource project = (IResource) Platform.getAdapterManager().getAdapter(element, IResource.class);
183       IOWLProject owlProject = null;
184       boolean validProject = false;
185       if((project != null) && (project instanceof IProject))
186       {
187          owlProject = (IOWLProject) SWResourceManager.getModel().getCorrespondingElement(project);
188          if(owlProject != null)
189          {
190             _project = owlProject;
191             super.setElement(_project);
192             validProject = true;
193             _associatedLibrariesList.addAll(Arrays.asList(SWProjectInfo.getLibraries(_project.getProject())));
194             _allLibrariesList = Libraries.getAllLibraries();
195             _allLibrariesList.removeAll(_associatedLibrariesList);
196          }
197       }
198       if(!validProject)
199       {
200          _project = null;  
201       }
202    }
203    
204    /*
205     *  (non-Javadoc)
206     * @see org.eclipse.jface.preference.IPreferencePage#performOk()
207     */
208    public boolean performOk() 
209    {
210       ILibraryDescriptor ild = null;
211       Iterator iterator = _associatedLibrariesList.iterator();
212       IOWLElement[] kids = null;
213       
214       // remove the libraries that the user removed
215       kids = _project.getElementInfo().getChildren();
216       for(int i = 0; i < kids.length; i++)
217       {
218          if(kids[i] instanceof IOWLLibrary)
219          {
220             ild = ((IOWLLibrary) kids[i]).getLibrary();
221             if(_allLibrariesList.contains(ild) || !_associatedLibrariesList.contains(ild))
222             {
223                _project.getElementInfo().removeChild(kids[i]);
224             }
225          }
226       }
227       
228       // add the libraries the user selected
229       // because arrays don't have .contains, we're going 
230       // to first go through the array, and remove from the 
231       // list all those that are already added, then 
232       // go throught the list and add them all.
233       for(int i = 0; i < kids.length; i++)
234       {
235          if(kids[i] instanceof IOWLLibrary)
236          {
237             ild = ((IOWLLibrary) kids[i]).getLibrary();
238             _associatedLibrariesList.remove(ild);
239          }
240       }
241       iterator = _associatedLibrariesList.iterator();
242       while(iterator.hasNext())
243       {
244          ild = (ILibraryDescriptor) iterator.next();
245          _project.getElementInfo().addChild(SWResourceManager.create(ild));
246       }
247 
248       //_project.getCorrespondingResource().
249       SWProjectInfo.refreshInfoFile(_project.getProject());
250       
251       return true;
252    }
253 
254    private final class InternalSelectionListener extends SelectionAdapter implements ISelectionChangedListener
255    {
256       /* (non-Javadoc)
257        * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
258        */
259       public void widgetSelected(SelectionEvent event)
260       {
261          super.widgetSelected(event);
262          IStructuredSelection iss = null;
263          ILibraryDescriptor ild = null;
264          Iterator i = null;
265          if(event.getSource() == _toAvailableButton)
266          {
267             iss = (IStructuredSelection) _allLibrariesListViewer.getSelection();
268             if(!iss.isEmpty())
269             {
270                _allLibrariesList.removeAll(iss.toList());
271                _associatedLibrariesList.addAll(iss.toList());
272                
273                _associatedLibrariesListViewer.refresh();
274                _allLibrariesListViewer.refresh();
275                _associatedLibrariesListViewer.setSelection(iss);
276             }
277          }
278          else if(event.getSource() == _toAllButton)
279          {
280             iss = (IStructuredSelection) _associatedLibrariesListViewer.getSelection();
281             if(!iss.isEmpty())
282             {
283                _associatedLibrariesList.removeAll(iss.toList());
284                _allLibrariesList.addAll(iss.toList());
285                
286                _associatedLibrariesListViewer.refresh();
287                _allLibrariesListViewer.refresh();
288                _allLibrariesListViewer.setSelection(iss);
289             }
290          }
291       }
292 
293       /* (non-Javadoc)
294        * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
295        */
296       public void selectionChanged(SelectionChangedEvent event)
297       {
298          IStructuredSelection iss = (IStructuredSelection) event.getSelection();
299          ILibraryDescriptor ild = null;
300          Iterator i = null;
301          if(event.getSource() == _allLibrariesListViewer)
302          {
303             _toAvailableButton.setEnabled(!iss.isEmpty());
304             _associatedLibrariesListViewer.getTable().deselectAll();
305          }
306          else if(event.getSource() == _associatedLibrariesListViewer)
307          {
308             _toAllButton.setEnabled(!iss.isEmpty());
309             _allLibrariesListViewer.getTable().deselectAll();
310          }         
311       }
312    }
313    
314    private final class InternalContentProvider extends WorkbenchContentProvider
315    {
316       /*
317        * (non-Javadoc)
318        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
319        */
320       public Object[] getElements(Object element)
321       {
322          Object[] toReturn = null; 
323          if(element instanceof Collection)
324          {
325             ((Collection) element).remove(null);
326             toReturn = ((Collection)element).toArray();
327             
328          }
329          return toReturn;
330       }
331    }
332    
333    private final class InternalLabelProvider extends LabelProvider
334    {
335       /* (non-Javadoc)
336        * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
337        */
338       public Image getImage(Object element)
339       {
340          return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_LIBRARY);
341       }
342       /* (non-Javadoc)
343        * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
344        */
345       public String getText(Object element)
346       {
347          String toReturn = super.getText(element); 
348          if(element instanceof ILibraryDescriptor)
349          {
350             toReturn = ((ILibraryDescriptor) element).getName();
351          }
352          return toReturn;
353       }
354    }
355 }