View Javadoc

1   /*
2    * $Id: LibrariesPreferencesPage.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;
10  
11  import java.util.Collection;
12  import java.util.Iterator;
13  import java.util.regex.Pattern;
14  
15  import org.eclipse.core.runtime.IProgressMonitor;
16  import org.eclipse.core.runtime.IStatus;
17  import org.eclipse.core.runtime.Preferences;
18  import org.eclipse.core.runtime.Status;
19  import org.eclipse.core.runtime.jobs.Job;
20  import org.eclipse.jdt.ui.JavaUI;
21  import org.eclipse.jface.dialogs.MessageDialog;
22  import org.eclipse.jface.preference.PreferencePage;
23  import org.eclipse.jface.viewers.IStructuredSelection;
24  import org.eclipse.jface.viewers.LabelProvider;
25  import org.eclipse.jface.viewers.TableViewer;
26  import org.eclipse.swt.SWT;
27  import org.eclipse.swt.events.SelectionAdapter;
28  import org.eclipse.swt.events.SelectionEvent;
29  import org.eclipse.swt.events.VerifyEvent;
30  import org.eclipse.swt.events.VerifyListener;
31  import org.eclipse.swt.graphics.Image;
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.Control;
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.IActionDelegate;
41  import org.eclipse.ui.IWorkbench;
42  import org.eclipse.ui.IWorkbenchPreferencePage;
43  import org.eclipse.ui.model.BaseWorkbenchContentProvider;
44  
45  import com.bbn.swede.core.OWLCore;
46  import com.bbn.swede.core.libraries.ILibraryDescriptor;
47  import com.bbn.swede.core.libraries.Libraries;
48  import com.bbn.swede.ui.wizards.LibraryWizardActionFactory;
49  
50  /***
51   * A preferences page for the configuration of SWeDE Libraries.
52   * 
53   * @author aperezlo
54   */
55  public class LibrariesPreferencesPage extends PreferencePage implements
56     IWorkbenchPreferencePage
57  {
58     private IWorkbench _workbench;
59     private Preferences _preferences;
60     private TableViewer _listViewer;
61     
62     private Text _updateFrequencyText;
63     private Button _updateButton;
64     
65     /*
66      *  (non-Javadoc)
67      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
68      */
69     protected Control createContents(Composite parent)
70     {
71        Group mainGroup = new Group(parent, parent.getStyle());
72        mainGroup.setFont(parent.getFont());
73        mainGroup.setLayout(new GridLayout(2, false));
74        mainGroup.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH));
75        mainGroup.setText("Ontology Libraries");
76        
77        GridData listViewerLayoutData = new GridData(GridData.GRAB_HORIZONTAL
78           | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
79        listViewerLayoutData.verticalSpan = 5;
80        _listViewer = new TableViewer(mainGroup, mainGroup.getStyle() | SWT.BORDER | SWT.MULTI);
81        _listViewer.getControl().setFont(mainGroup.getFont());
82        _listViewer.setContentProvider(new InternalContentProvider());
83        _listViewer.setLabelProvider(new InternalLabelProvider());
84        _listViewer.setInput(Libraries.getAllLibraries());
85        _listViewer.getControl().setLayoutData(listViewerLayoutData);
86  
87        
88        Button addButton = new Button(mainGroup, mainGroup.getStyle() | SWT.PUSH);
89        addButton.setFont(mainGroup.getFont());
90        addButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
91        addButton.setText("Add new library");
92        addButton.setToolTipText("Create a new library and make it available to SWeDE");
93        addButton.addSelectionListener(new SelectionAdapter()
94        {
95           public void widgetSelected(SelectionEvent e)
96           {
97              super.widgetSelected(e);
98              IActionDelegate lwa = LibraryWizardActionFactory.getAction(LibraryWizardActionFactory.CREATE);
99              lwa.run(null);
100             _listViewer.setInput(Libraries.getAllLibraries());
101          }
102       });
103 
104       Button editButton = new Button(mainGroup, mainGroup.getStyle() | SWT.PUSH);
105       editButton.setFont(mainGroup.getFont());
106       editButton.setText("Edit library");
107       editButton.setToolTipText("Add, remove, or alter resources or metadata from an existing library");
108       editButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
109       editButton.addSelectionListener(new SelectionAdapter()
110       {
111          public void widgetSelected(SelectionEvent e)
112          {
113             super.widgetSelected(e);
114             LibraryWizardActionFactory.Edit lwa = (LibraryWizardActionFactory.Edit)LibraryWizardActionFactory
115                .getAction(LibraryWizardActionFactory.EDIT);
116             IStructuredSelection iss =  (IStructuredSelection) _listViewer.getSelection();
117             if(!iss.isEmpty())
118             {
119                lwa.setLibrary((ILibraryDescriptor) iss.getFirstElement());
120                lwa.run(null);
121                _listViewer.setInput(Libraries.getAllLibraries());
122             }
123          }
124       });
125 
126       Button rebuildButton = new Button(mainGroup, mainGroup.getStyle() | SWT.PUSH);
127       rebuildButton.setFont(mainGroup.getFont());
128       rebuildButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
129       rebuildButton.setText("Refresh all libraries");
130       rebuildButton.setToolTipText("Re-load each element of the library and rebuild it");
131       rebuildButton.addSelectionListener(new SelectionAdapter()
132       {
133          public void widgetSelected(SelectionEvent e)
134          {
135             Libraries.refreshAllLibraries();
136          }
137       });
138       
139       Button removeButton = new Button(mainGroup, mainGroup.getStyle() | SWT.PUSH);
140       removeButton.setFont(mainGroup.getFont());
141       removeButton.setText("Remove library");
142       removeButton.setToolTipText("Make the library permanently unavailable to SWeDE");
143       removeButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
144       removeButton.addSelectionListener(new SelectionAdapter()
145       {
146          public void widgetSelected(SelectionEvent e)
147          {
148             super.widgetSelected(e);
149             IStructuredSelection iss = (IStructuredSelection) _listViewer.getSelection();
150             Iterator i = iss.iterator();
151             ILibraryDescriptor ild = null;
152             if(!iss.isEmpty())
153             {
154                String confirmMessageBegin = "You are about to permanently remove the following librar"
155                   + ((iss.size() > 1) ? "ies" : "y") + ":\n\n";
156                String confirmMessageEnd = "\nAfter this operation, "
157                   + ((iss.size() > 1) ? "they" : "it")
158                   + " will not be available to any project, and any project that is currently using "
159                   + ((iss.size() > 1) ? "them" : "it")
160                   + " could be made invalid.  \n\nAre you sure you want to do this?";
161                while(i.hasNext())
162                {
163                   ild = (ILibraryDescriptor) i.next();
164                   confirmMessageBegin += ild.getName() + "\n";
165                }
166                if(MessageDialog.openConfirm(getShell(), "Are you sure?", confirmMessageBegin + confirmMessageEnd))
167                {
168                   i = iss.iterator();
169                   while (i.hasNext())
170                   {
171                      ild = (ILibraryDescriptor) i.next();
172                      if(!ild.isAvailable())
173                      {
174                         MessageDialog
175                            .openError(
176                               getShell(),
177                               "Unable to remove",
178                               "This library cannot be removed right now "
179                                  + "because it is currently being used by another "
180                                  + "operation, most likely a refresh.  Please try "
181                                  + "again once the operation is finished.");
182                      }
183                      else if(!Libraries.deleteLibrary(ild))
184                      {
185                         MessageDialog.openError(getShell(), "Unable to remove",
186                            "Unable to delete the library, '" + ild.getName()
187                               + "', at this time.  Please try again later.");
188                      }
189                   }
190                   _listViewer.setInput(Libraries.getAllLibraries());
191                }
192             }
193          }
194       });
195       
196       
197       Group optionsGroup = new Group(mainGroup, mainGroup.getStyle() | SWT.SHADOW_NONE);
198       GridData optionsLayoutData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
199       
200       optionsLayoutData.horizontalSpan = 2;
201       optionsGroup.setFont(mainGroup.getFont());
202       optionsGroup.setLayoutData(optionsLayoutData);
203       optionsGroup.setLayout(new GridLayout(3, false));
204       optionsGroup.setText("Options:");
205       
206       Label updateLabel = new Label(optionsGroup, optionsGroup.getStyle());
207       GridData updateLabelLayoutData = new GridData();
208       updateLabelLayoutData.horizontalSpan = 2;
209       updateLabel.setLayoutData(updateLabelLayoutData);
210       updateLabel.setFont(optionsGroup.getFont());
211       updateLabel.setText("Automatically update libraries:");
212       
213       _updateButton = new Button(optionsGroup, optionsGroup.getStyle() | SWT.CHECK);
214       _updateButton.setSelection(_preferences.getBoolean(OWLCore.PREFERENCE_LIBRARY_AUTOMATIC_UPDATE));
215       
216       Label updateFrequencyLabel = new Label(optionsGroup, optionsGroup.getStyle());
217       updateFrequencyLabel.setFont(optionsGroup.getFont());
218       updateFrequencyLabel.setText("Update every:");
219       
220       _updateFrequencyText = new Text(optionsGroup, optionsGroup.getStyle() | SWT.BORDER);
221       _updateFrequencyText.setFont(optionsGroup.getFont());
222       _updateFrequencyText.addVerifyListener(new VerifyListener()
223       {
224          public void verifyText(VerifyEvent e)
225          {
226             e.doit = Pattern.matches("([0-9]++)?", e.text);
227          }
228       });
229       _updateFrequencyText.setEnabled(_updateButton.getSelection());
230       _updateFrequencyText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
231       _updateFrequencyText.setTextLimit(3);
232       _updateFrequencyText.setText(Long.toString(_preferences
233          .getLong(OWLCore.PREFERENCE_LIBRARY_UPDATE_SCHEDULE_FREQUENCY_STR)
234          / (24 * 60 * 60 * 1000)));
235       
236       Label daysLabel = new Label(optionsGroup, optionsGroup.getStyle());
237       daysLabel.setFont(optionsGroup.getFont());
238       daysLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
239          | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING));
240       daysLabel.setText("days");
241       
242       _updateButton.addSelectionListener(new SelectionAdapter() 
243       {
244          public void widgetSelected(SelectionEvent e)
245          {
246             super.widgetSelected(e);
247             _updateFrequencyText.setEnabled(_updateButton.getSelection());
248          }
249       });    
250       
251       return mainGroup;
252    }
253 
254    /*
255     *  (non-Javadoc)
256     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
257     */
258    public void init(IWorkbench workbench)
259    {
260       _workbench = workbench;
261       _preferences = OWLCore.getPlugin().getPluginPreferences();
262    }
263 
264    /*
265     *  (non-Javadoc)
266     * @see org.eclipse.jface.preference.IPreferencePage#performOk()
267     */
268    public boolean performOk()
269    {
270       _preferences.setValue(OWLCore.PREFERENCE_LIBRARY_AUTOMATIC_UPDATE, _updateButton.getSelection());
271       _preferences.setValue(
272          OWLCore.PREFERENCE_LIBRARY_UPDATE_SCHEDULE_FREQUENCY_STR, Long
273             .parseLong(_updateFrequencyText.getText())
274             * 24 * 60 * 60 * 1000);
275       OWLCore.getPlugin().savePluginPreferences();
276       if(Boolean.TRUE.toString().equals(_preferences.getString(OWLCore.PREFERENCE_LIBRARY_AUTOMATIC_UPDATE)))
277       {
278          (new Job("Refreshing All Libraries")
279          {
280             protected IStatus run(IProgressMonitor monitor)
281             {
282                Libraries.refreshAllLibraries();
283                return Status.OK_STATUS;
284             } 
285          }).schedule(_preferences.getLong(OWLCore.PREFERENCE_LIBRARY_UPDATE_SCHEDULE_FREQUENCY_STR) 
286             + _preferences.getLong(OWLCore.PREFERENCE_LIBRARY_UPDATE_SCHEDULE_LAST_TIME_STR)
287             - System.currentTimeMillis());
288       }
289       return true;
290    }
291    
292    /*
293     *  (non-Javadoc)
294     * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
295     */
296    protected void performDefaults()
297    {
298       super.performDefaults();
299       _updateButton.setSelection(_preferences.getDefaultBoolean(OWLCore.PREFERENCE_LIBRARY_AUTOMATIC_UPDATE));
300       _updateFrequencyText
301          .setText(Long
302             .toString(_preferences
303                .getDefaultLong(OWLCore.PREFERENCE_LIBRARY_UPDATE_SCHEDULE_FREQUENCY_STR)
304                / (24 * 60 * 60 * 1000)));
305       _updateFrequencyText.setEnabled(_updateButton.getSelection());
306    }
307    
308    private final class InternalLabelProvider extends LabelProvider
309    {
310       /* (non-Javadoc)
311        * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
312        */
313       public String getText(Object element)
314       {
315          String toReturn = super.getText(element);
316          ILibraryDescriptor ild = null;
317          if(element instanceof ILibraryDescriptor)
318          {
319             ild = (ILibraryDescriptor) element;
320             toReturn = ild.getName();
321          }
322          return toReturn;
323       }
324 
325       /* (non-Javadoc)
326        * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
327        */
328       public Image getImage(Object element)
329       {
330          return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_LIBRARY);
331       }
332    }
333    private final class InternalContentProvider extends BaseWorkbenchContentProvider
334    {
335       /* (non-Javadoc)
336        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
337        */
338       public Object[] getElements(Object element)
339       {
340          Object[] toReturn = new Object[0];
341          if(element instanceof Collection)
342          {
343             toReturn = ((Collection) element).toArray();
344          }
345          return toReturn;
346       }
347    }
348    
349    
350 }