View Javadoc

1   /*
2    * $Id: LibraryResourceTable.java,v 1.4 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.io.File;
12  import java.net.URL;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import java.util.Map;
16  import java.util.Properties;
17  
18  import org.eclipse.core.resources.IFile;
19  import org.eclipse.jface.viewers.ITableLabelProvider;
20  import org.eclipse.jface.viewers.LabelProvider;
21  import org.eclipse.jface.viewers.TableViewer;
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.graphics.FontData;
24  import org.eclipse.swt.graphics.Image;
25  import org.eclipse.swt.layout.GridData;
26  import org.eclipse.swt.widgets.Composite;
27  import org.eclipse.swt.widgets.TableColumn;
28  import org.eclipse.ui.model.BaseWorkbenchContentProvider;
29  
30  import com.bbn.swede.core.libraries.LibraryEntryConfiguration;
31  
32  /***
33   * The LibraryResourceTable is a graphical component which is a table that 
34   * holds resources to be added or removed from a library during 
35   * library construction.  It is used exclusively by Library creation 
36   * and edit wizards, and is tightly coupled with them.
37   * 
38   * @author aperezlo
39   */
40  class LibraryResourceTable
41  {
42     private static final int NAME_INDEX = 0;
43     private static final int TYPE_INDEX = 1;
44     private static final int LOCATION_INDEX = 2;
45     
46     private TableViewer _checkBox;
47     /***
48      * Constructor.
49      * 
50      * @param parent parent component in which to place the LRT
51      * @param style default GUI styles to use.
52      */
53     public LibraryResourceTable(Composite parent, int style)
54     {
55        _checkBox = new TableViewer(parent, style | SWT.BORDER | SWT.FULL_SELECTION);
56        _checkBox.setContentProvider(new InternalContentProvider());
57        _checkBox.setLabelProvider(new InternalLabelProvider());
58        
59        _checkBox.getTable().setFont(parent.getFont());
60        
61        _checkBox.getTable().setHeaderVisible(true);
62        _checkBox.getTable().setLinesVisible(true);
63        
64        GridData tableLayoutData = new GridData(GridData.GRAB_VERTICAL | GridData.GRAB_HORIZONTAL | GridData.FILL_BOTH);
65        tableLayoutData.verticalSpan = 1;
66  
67        TableColumn nameColumn = new TableColumn(_checkBox.getTable(), _checkBox
68           .getTable().getStyle()
69           | SWT.LEFT, NAME_INDEX);
70        TableColumn typeColumn = new TableColumn(_checkBox.getTable(), _checkBox
71           .getTable().getStyle()
72           | SWT.LEFT, TYPE_INDEX);
73        TableColumn locationColumn = new TableColumn(_checkBox.getTable(),
74           _checkBox.getTable().getStyle() | SWT.LEFT, LOCATION_INDEX);
75        
76        nameColumn.setWidth(150);
77        nameColumn.setText("Resource");
78        typeColumn.setWidth(150);
79        typeColumn.setText("Type");
80        locationColumn.setWidth(150);
81        locationColumn.setText("Location");
82        
83        
84        // get the height of the tallest part of the font we're using...
85        FontData[] fd = _checkBox.getTable().getFont().getFontData();
86        int tempHeight = -1;
87        tableLayoutData.heightHint = -1;
88        for(int i = 0; i < fd.length; i++)
89        {
90           tempHeight = fd[i].getHeight();
91           if(tempHeight > tableLayoutData.heightHint)
92           {
93              tableLayoutData.heightHint = tempHeight;
94           }
95        }
96        // ... get the height of the tallest part of the font we're using
97        
98        // multiply by the number of rows we want...
99        tableLayoutData.heightHint *= 10; 
100       
101       _checkBox.getTable().setLayoutData(tableLayoutData);
102    }
103    
104    /***
105     * Accessor.
106     * 
107     * @return the TableViewer used in this component
108     */
109    TableViewer getTableViewer()
110    {
111       return _checkBox;
112    }
113    private final class InternalContentProvider extends BaseWorkbenchContentProvider
114    {
115       /* (non-Javadoc)
116        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
117        */
118       public Object[] getElements(Object element)
119       {
120          Object[] toReturn = null;
121          if(element instanceof Collection)
122          {
123             toReturn = ((Collection)element).toArray(); 
124          }
125          else
126          {
127             toReturn = super.getElements(element);
128          }
129          
130          return toReturn;
131       }
132    }
133    private final class InternalLabelProvider extends LabelProvider implements ITableLabelProvider
134    {
135       /* (non-Javadoc)
136        * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
137        */
138       public Image getColumnImage(Object element, int columnIndex)
139       {
140          return null;
141       }
142       /* (non-Javadoc)
143        * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
144        */
145       public String getColumnText(Object element, int columnIndex)
146       {
147          String toReturn = "";
148          LibraryResourceDescriptor lrd = null;
149          if(element != null)
150          {
151             lrd = (LibraryResourceDescriptor) element; 
152             switch(columnIndex)
153             {
154                case NAME_INDEX: 
155                   toReturn = lrd.getConfiguration().getName();
156                   break;
157                case TYPE_INDEX:
158                   toReturn = lrd.getType() == LibraryResourceDescriptor.ECLIPSE ? "SWeDE Resource"
159                      : lrd.getType() == LibraryResourceDescriptor.FILESYSTEM ? "Local File"
160                         : "URL";
161                   break;
162                case LOCATION_INDEX:
163                   switch(lrd.getType())
164                   {
165                      case LibraryResourceDescriptor.ECLIPSE:
166                         toReturn = lrd.getIFile().getFullPath().makeAbsolute().removeLastSegments(1).toString();
167                         break;
168                      case LibraryResourceDescriptor.FILESYSTEM:
169                         toReturn = lrd.getFile().getPath();
170                         break;
171                      default:
172                         toReturn = lrd.getConfiguration().getURL().toString();
173                   }
174                   break;
175                default:
176                   break;
177             }
178          }
179          return toReturn;
180       }
181    }
182    /***
183     * A class to encapsulate a single library resource.
184     * 
185     * @author aperezlo
186     */
187    class LibraryResourceDescriptor
188    {
189       /***
190        * <code>URL</code> - constant to indicate that the particular 
191        * resource is remote.
192        */
193       public static final int URL = 1;
194       /***
195        * <code>FILESYSTEM</code> - constant to indicate that the particular 
196        * resource is local, and is on the filesystem.
197        */
198       public static final int FILESYSTEM = 2;
199       /***
200        * <code>ECLIPSE</code> - constant to indicate that the particular 
201        * resource is local, and an Eclipse resource.
202        */
203       public static final int ECLIPSE = 3;
204       
205       private File _file;
206       private IFile _iFile;
207       private int _type;
208       
209       private LibraryEntryConfiguration _configuration;
210       
211       /***
212        * Constructor.
213        * 
214        * @param name name of the resource
215        * @param thing the resource
216        */
217       public LibraryResourceDescriptor(String name, Object thing)
218       {
219          _configuration = new LibraryEntryConfiguration();
220          _configuration.setName(name);
221          EntryMetadataSuggester mds = new EntryMetadataSuggester();
222          if(thing instanceof IFile)
223          {
224             _iFile = (IFile) thing;
225             _type = ECLIPSE;
226             _configuration.setUserProperties(mds.suggestDefaults(_iFile));
227             _configuration.setURI(mds.getURI());
228             if(_configuration.getURI() != null)
229             {
230                _configuration.setURL(_configuration.getURI());
231             }
232             _configuration.setName(mds.getName());
233          }
234          else if (thing instanceof File)
235          {
236             _file = (File) thing;
237             _type = FILESYSTEM;
238             _configuration.setUserProperties(mds.suggestDefaults(_file));
239             _configuration.setURI(mds.getURI());
240             if(_configuration.getURI() != null)
241             {
242                _configuration.setURL(_configuration.getURI());
243             }
244             _configuration.setName(mds.getName());
245          }
246          else
247          {
248             URL tempUrl = (URL) thing;
249             _configuration.setURL(tempUrl.toExternalForm());
250             _type = URL;
251          }
252       }
253       
254       
255       
256       /***
257        * Accessor.
258        * 
259        * @return the configuration object of resource
260        */
261       LibraryEntryConfiguration getConfiguration()
262       {
263          return _configuration;
264       }
265       
266       /***
267        * Mutator.
268        * 
269        * @param props the new properties for this resource.
270        */
271       void setProperties(Properties props)
272       {
273          _configuration.setUserProperties(props);
274       }
275       
276       /***
277        * Accessor.
278        * 
279        * @return the type of resource (URL, FILESYSTEM, ECLIPSE)
280        */
281       int getType()
282       {
283          return _type;
284       }
285       
286       /***
287        * Accessor.
288        * 
289        * @return the file, or null if this is an ECLIPSE or URL
290        */
291       File getFile()
292       {
293          return _file;
294       }
295       
296       /***
297        * Accessor.
298        * 
299        * @return the iFile, or null if this is a FILESYSTEM or URL
300        */
301       IFile getIFile()
302       {
303          return _iFile;
304       }
305       
306       /***
307        * Mutator.
308        * 
309        * Sets the file, and computes metadata for it.
310        * 
311        * @param file the file to set
312        */
313       void setFile(File file)
314       {
315          EntryMetadataSuggester mds = new EntryMetadataSuggester();
316          Properties tempProperties = null;
317          Map.Entry tempEntry = null;
318          Iterator i = null;
319          _file = file;
320          tempProperties = mds.suggestDefaults(file);
321          _configuration.setURI(mds.getURI());
322          _configuration.setName(mds.getName());
323          _configuration.setUserProperties(tempProperties);
324       }
325 
326       /***
327        * Mutator.
328        * 
329        * Sets the iFile and computes metadata.
330        * 
331        * @param file the iFile to set
332        */
333       void setIFile(IFile file)
334       {
335          EntryMetadataSuggester mds = new EntryMetadataSuggester();
336          Properties tempProperties = null;
337          Map.Entry tempEntry = null;
338          Iterator i = null;
339          _iFile = file;
340          tempProperties = mds.suggestDefaults(file);
341          _configuration.setURI(mds.getURI());
342          _configuration.setName(mds.getName());
343 
344          _configuration.setUserProperties(tempProperties);
345       }
346 
347       /* (non-Javadoc)
348        * @see java.lang.Object#equals(java.lang.Object)
349        */
350       public boolean equals(Object arg0)
351       {
352          boolean toReturn = false;
353          LibraryResourceDescriptor lrd = null;
354          if(arg0 instanceof LibraryResourceDescriptor)
355          {
356             lrd = (LibraryResourceDescriptor) arg0;
357 
358             if(lrd._configuration.getName().equals(_configuration.getName()))
359             {
360                toReturn = true;
361             }
362             
363             if((lrd.getConfiguration() != null) && getConfiguration() != null)
364             {
365                if((getConfiguration().getURI() != null) && (lrd.getConfiguration().getURI() != null))
366                {
367                   if(getConfiguration().getURI().equals(lrd.getConfiguration().getURI()))
368                   {
369                      toReturn = true;
370                   }
371                }
372             }
373          }
374          
375          return toReturn;
376       }
377    }
378 }