KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > history > RefactoringPropertyPage


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.ui.refactoring.history;
12
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
24 import org.eclipse.core.runtime.preferences.IScopeContext;
25
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IWorkspaceRunnable;
28 import org.eclipse.core.resources.ProjectScope;
29
30 import org.eclipse.ltk.core.refactoring.RefactoringCore;
31 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
32 import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryService;
33 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
34
35 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
36 import org.eclipse.ltk.internal.core.refactoring.RefactoringPreferenceConstants;
37 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
38 import org.eclipse.ltk.internal.ui.refactoring.IRefactoringHelpContextIds;
39 import org.eclipse.ltk.internal.ui.refactoring.Messages;
40 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
41 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin;
42 import org.eclipse.ltk.internal.ui.refactoring.WorkbenchRunnableAdapter;
43 import org.eclipse.ltk.internal.ui.refactoring.history.RefactoringHistoryEditHelper.IRefactoringHistoryProvider;
44
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Control;
53 import org.eclipse.swt.widgets.Label;
54 import org.eclipse.swt.widgets.Shell;
55
56 import org.eclipse.jface.dialogs.IDialogConstants;
57 import org.eclipse.jface.dialogs.IDialogSettings;
58 import org.eclipse.jface.dialogs.MessageDialog;
59 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
60 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
61 import org.eclipse.jface.operation.IRunnableContext;
62 import org.eclipse.jface.operation.IRunnableWithProgress;
63 import org.eclipse.jface.preference.IPreferencePageContainer;
64 import org.eclipse.jface.preference.IPreferenceStore;
65
66 import org.eclipse.ui.PlatformUI;
67 import org.eclipse.ui.dialogs.PropertyPage;
68 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
69 import org.eclipse.ui.preferences.IWorkingCopyManager;
70 import org.eclipse.ui.preferences.WorkingCopyManager;
71
72 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
73
74 import org.osgi.service.prefs.BackingStoreException;
75
76 /**
77  * Property page for a project's refactoring history.
78  *
79  * @since 3.2
80  */

81 public final class RefactoringPropertyPage extends PropertyPage {
82
83     /** The dialog settings key */
84     private static final String JavaDoc DIALOG_SETTINGS_KEY= "RefactoringPropertyPage"; //$NON-NLS-1$
85

86     /** The empty descriptors constant */
87     static final RefactoringDescriptorProxy[] EMPTY_DESCRIPTORS= new RefactoringDescriptorProxy[0];
88
89     /** Preference key for the warn delete all preference */
90     private static final String JavaDoc PREFERENCE_DO_NOT_WARN_DELETE_ALL= RefactoringUIPlugin.getPluginId() + ".do.not.warn.delete.history"; //$NON-NLS-1$;
91

92     /** The sort dialog setting */
93     private static final String JavaDoc SETTING_SORT= "org.eclipse.ltk.ui.refactoring.sortRefactorings"; //$NON-NLS-1$
94

95     /** The refactoring preference */
96     private boolean fHasProjectHistory= false;
97
98     /** The refactoring history control */
99     private ShowRefactoringHistoryControl fHistoryControl;
100
101     /** The preferences working copy manager, or <code>null</code> */
102     private IWorkingCopyManager fManager= null;
103
104     /** Has the property page new dialog settings? */
105     private boolean fNewSettings;
106
107     /** The dialog settings, or <code>null</code> */
108     private IDialogSettings fSettings= null;
109
110     /** The share history button, or <code>null</code> */
111     private Button fShareHistoryButton= null;
112
113     /**
114      * Creates a new refactoring property page.
115      */

116     public RefactoringPropertyPage() {
117         noDefaultAndApplyButton();
118         final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings();
119         final IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
120         if (section == null)
121             fNewSettings= true;
122         else {
123             fNewSettings= false;
124             fSettings= section;
125         }
126     }
127
128     /**
129      * {@inheritDoc}
130      */

131     protected Control createContents(final Composite parent) {
132         initializeDialogUnits(parent);
133
134         final IPreferencePageContainer container= getContainer();
135         if (container instanceof IWorkbenchPreferenceContainer)
136             fManager= ((IWorkbenchPreferenceContainer) container).getWorkingCopyManager();
137         else
138             fManager= new WorkingCopyManager();
139
140         final Composite composite= new Composite(parent, SWT.NONE);
141         final GridLayout layout= new GridLayout();
142         layout.marginWidth= 0;
143         composite.setLayout(layout);
144
145         fHistoryControl= new ShowRefactoringHistoryControl(composite, new RefactoringHistoryControlConfiguration(getCurrentProject(), true, false) {
146
147             public String JavaDoc getProjectPattern() {
148                 return RefactoringUIMessages.RefactoringPropertyPage_project_pattern;
149             }
150         }) {
151
152             protected void createEditButton(final Composite control) {
153                 // No edit button so far
154
}
155         };
156         fHistoryControl.createControl();
157         boolean sortProjects= true;
158         final IDialogSettings settings= fSettings;
159         if (settings != null)
160             sortProjects= settings.getBoolean(SETTING_SORT);
161         if (sortProjects)
162             fHistoryControl.sortByProjects();
163         else
164             fHistoryControl.sortByDate();
165
166         fHistoryControl.getDeleteAllButton().addSelectionListener(new SelectionAdapter() {
167
168             public final void widgetSelected(final SelectionEvent event) {
169                 final IProject project= getCurrentProject();
170                 if (project != null) {
171                     final IRunnableContext context= new ProgressMonitorDialog(getShell());
172                     final IPreferenceStore store= RefactoringUIPlugin.getDefault().getPreferenceStore();
173                     MessageDialogWithToggle dialog= null;
174                     if (!store.getBoolean(PREFERENCE_DO_NOT_WARN_DELETE_ALL) && !fHistoryControl.getInput().isEmpty()) {
175                         dialog= MessageDialogWithToggle.openYesNoQuestion(getShell(), RefactoringUIMessages.RefactoringPropertyPage_confirm_delete_all_caption, Messages.format(RefactoringUIMessages.RefactoringPropertyPage_confirm_delete_all_pattern, project.getName()), RefactoringUIMessages.RefactoringHistoryWizard_do_not_show_message, false, null, null);
176                         store.setValue(PREFERENCE_DO_NOT_WARN_DELETE_ALL, dialog.getToggleState());
177                     }
178                     if (dialog == null || dialog.getReturnCode() == IDialogConstants.YES_ID)
179                         promptDeleteHistory(context, project);
180                 }
181             }
182         });
183         fHistoryControl.getDeleteButton().addSelectionListener(new SelectionAdapter() {
184
185             public final void widgetSelected(final SelectionEvent event) {
186                 final RefactoringDescriptorProxy[] selection= fHistoryControl.getCheckedDescriptors();
187                 if (selection.length > 0) {
188                     final IRunnableContext context= new ProgressMonitorDialog(getShell());
189                     final IProject project= getCurrentProject();
190                     if (project != null) {
191                         final Shell shell= getShell();
192                         RefactoringHistoryEditHelper.promptRefactoringDelete(shell, context, fHistoryControl, new RefactoringDescriptorDeleteQuery(shell, getCurrentProject(), selection.length), new IRefactoringHistoryProvider() {
193
194                             public RefactoringHistory getRefactoringHistory(final IProgressMonitor monitor) {
195                                 return RefactoringHistoryService.getInstance().getProjectHistory(project, monitor);
196                             }
197                         }, selection);
198                     }
199                 }
200             }
201         });
202         final Button button= fHistoryControl.getEditButton();
203         if (button != null) {
204             button.addSelectionListener(new SelectionAdapter() {
205
206                 public final void widgetSelected(final SelectionEvent event) {
207                     final RefactoringDescriptorProxy[] selection= fHistoryControl.getSelectedDescriptors();
208                     if (selection.length > 0) {
209                         final IRunnableContext context= new ProgressMonitorDialog(getShell());
210                         RefactoringHistoryEditHelper.promptRefactoringDetails(context, fHistoryControl, selection[0]);
211                     }
212                 }
213             });
214         }
215         fShareHistoryButton= new Button(composite, SWT.CHECK);
216         fShareHistoryButton.setText(RefactoringUIMessages.RefactoringPropertyPage_share_message);
217         fShareHistoryButton.setData(RefactoringPreferenceConstants.PREFERENCE_SHARED_REFACTORING_HISTORY);
218
219         final GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
220         data.verticalIndent= convertHeightInCharsToPixels(1) / 2;
221         fShareHistoryButton.setLayoutData(data);
222         fShareHistoryButton.setSelection(hasSharedRefactoringHistory());
223
224         new Label(composite, SWT.NONE);
225
226         final IProject project= getCurrentProject();
227         if (project != null) {
228             IRunnableContext context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
229             if (context == null)
230                 context= PlatformUI.getWorkbench().getProgressService();
231             handleInputEvent(context, project);
232         }
233         applyDialogFont(composite);
234         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IRefactoringHelpContextIds.REFACTORING_PROPERTY_PAGE);
235
236         return composite;
237     }
238
239     /**
240      * Returns the project currently associated with this property page.
241      *
242      * @return the currently associated project, or <code>null</code>
243      */

244     private IProject getCurrentProject() {
245         return (IProject) getElement().getAdapter(IProject.class);
246     }
247
248     /**
249      * Returns the preferences for the specified context.
250      *
251      * @param manager
252      * the working copy manager
253      * @param context
254      * the scope context
255      * @return the preferences
256      */

257     private IEclipsePreferences getPreferences(final IWorkingCopyManager manager, final IScopeContext context) {
258         final IEclipsePreferences preferences= context.getNode(RefactoringCore.ID_PLUGIN);
259         if (manager != null)
260             return manager.getWorkingCopy(preferences);
261         return preferences;
262     }
263
264     /**
265      * Handles the input event.
266      *
267      * @param context
268      * the runnable context to use
269      * @param project
270      * the project
271      */

272     private void handleInputEvent(final IRunnableContext context, final IProject project) {
273         Assert.isNotNull(context);
274         Assert.isNotNull(project);
275         try {
276             context.run(false, false, new IRunnableWithProgress() {
277
278                 public void run(final IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
279                     final IRefactoringHistoryService service= RefactoringCore.getHistoryService();
280                     try {
281                         service.connect();
282                         fHistoryControl.setInput(service.getProjectHistory(project, monitor));
283                     } finally {
284                         service.disconnect();
285                     }
286                 }
287             });
288         } catch (InvocationTargetException JavaDoc exception) {
289             RefactoringUIPlugin.log(exception);
290         } catch (InterruptedException JavaDoc exception) {
291             // Do nothing
292
}
293     }
294
295     /**
296      * Returns whether a project has an shared refactoring history.
297      *
298      * @return <code>true</code> if the project contains an shared project
299      * history, <code>false</code> otherwise
300      */

301     private boolean hasSharedRefactoringHistory() {
302         final IProject project= getCurrentProject();
303         if (project != null)
304             return RefactoringHistoryService.hasSharedRefactoringHistory(project);
305         return false;
306     }
307
308     /**
309      * {@inheritDoc}
310      */

311     protected void performDefaults() {
312         super.performDefaults();
313         final IProject project= getCurrentProject();
314         if (project != null)
315             setPreference(fManager, new ProjectScope(project), RefactoringPreferenceConstants.PREFERENCE_SHARED_REFACTORING_HISTORY, null);
316     }
317
318     /**
319      * {@inheritDoc}
320      */

321     public boolean performOk() {
322         final IProject project= getCurrentProject();
323         if (project == null || fManager == null)
324             return true; // not contributed on a project or no control created
325

326         if (fNewSettings) {
327             final IDialogSettings settings= RefactoringUIPlugin.getDefault().getDialogSettings();
328             IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
329             section= settings.addNewSection(DIALOG_SETTINGS_KEY);
330             fSettings= section;
331         }
332         fSettings.put(SETTING_SORT, fHistoryControl.isSortByProjects());
333
334         setPreference(fManager, new ProjectScope(project), RefactoringPreferenceConstants.PREFERENCE_SHARED_REFACTORING_HISTORY, Boolean.valueOf(fShareHistoryButton.getSelection()).toString());
335         try {
336             fManager.applyChanges();
337             final boolean history= RefactoringHistoryService.hasSharedRefactoringHistory(project);
338             if (history != fHasProjectHistory) {
339                 final Job job= new Job(history ? RefactoringUIMessages.RefactoringPropertyPage_sharing_refactoring_history : RefactoringUIMessages.RefactoringPropertyPage_unsharing_refactoring_history) {
340
341                     public final IStatus run(final IProgressMonitor monitor) {
342                         try {
343                             RefactoringHistoryService.setSharedRefactoringHistory(project, history, monitor);
344                         } catch (CoreException exception) {
345                             RefactoringUIPlugin.log(exception);
346                             return exception.getStatus();
347                         }
348                         return Status.OK_STATUS;
349                     }
350                 };
351                 job.setRule(project);
352                 job.setPriority(Job.SHORT);
353                 job.schedule();
354             }
355         } catch (BackingStoreException exception) {
356             RefactoringUIPlugin.log(exception);
357         }
358         return super.performOk();
359     }
360
361     /**
362      * Prompts the user to delete the project refactoring history.
363      *
364      * @param context
365      * the runnable context to use
366      * @param project
367      * the project to delete its refactoring history
368      */

369     protected void promptDeleteHistory(final IRunnableContext context, final IProject project) {
370         Assert.isNotNull(context);
371         Assert.isNotNull(project);
372         final RefactoringHistoryService service= RefactoringHistoryService.getInstance();
373         try {
374             service.connect();
375             try {
376                 final Shell shell= getShell();
377                 context.run(false, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
378
379                     public void run(final IProgressMonitor monitor) throws CoreException {
380                         try {
381                             monitor.beginTask(RefactoringCoreMessages.RefactoringHistoryService_deleting_refactorings, 100);
382                             try {
383                                 service.deleteRefactoringHistory(project, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
384                             } catch (CoreException exception) {
385                                 final Throwable JavaDoc throwable= exception.getStatus().getException();
386                                 if (throwable instanceof IOException JavaDoc) {
387                                     shell.getDisplay().syncExec(new Runnable JavaDoc() {
388
389                                         public void run() {
390                                             MessageDialog.openError(shell, RefactoringUIMessages.ChangeExceptionHandler_refactoring, throwable.getLocalizedMessage());
391                                         }
392                                     });
393                                 } else
394                                     throw exception;
395                             }
396                             final RefactoringHistory history= service.getProjectHistory(project, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
397                             shell.getDisplay().syncExec(new Runnable JavaDoc() {
398
399                                 public void run() {
400                                     fHistoryControl.setInput(history);
401                                     fHistoryControl.setCheckedDescriptors(EMPTY_DESCRIPTORS);
402                                 }
403                             });
404                         } finally {
405                             monitor.done();
406                         }
407                     }
408                 }, project));
409             } catch (InvocationTargetException JavaDoc exception) {
410                 RefactoringUIPlugin.log(exception);
411             } catch (InterruptedException JavaDoc exception) {
412                 // Do nothing
413
}
414         } finally {
415             service.disconnect();
416         }
417     }
418
419     /**
420      * Sets the preference for a certain context.
421      *
422      * @param manager
423      * the working copy manager
424      * @param context
425      * the scope context
426      * @param key
427      * the key of the preference
428      * @param value
429      * the value of the preference
430      */

431     private void setPreference(final IWorkingCopyManager manager, final IScopeContext context, final String JavaDoc key, final String JavaDoc value) {
432         final IEclipsePreferences preferences= getPreferences(manager, context);
433         if (value != null)
434             preferences.put(key, value);
435         else
436             preferences.remove(key);
437     }
438
439     /**
440      * {@inheritDoc}
441      */

442     public void setVisible(final boolean visible) {
443         fHasProjectHistory= hasSharedRefactoringHistory();
444         super.setVisible(visible);
445     }
446 }
447
Popular Tags