KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > history > RefactoringHistorySerializer


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.core.refactoring.history;
12
13 import java.net.URI JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18
19 import org.eclipse.core.filesystem.EFS;
20 import org.eclipse.core.filesystem.IFileStore;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.ResourcesPlugin;
25
26 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
27 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
28 import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryListener;
29 import org.eclipse.ltk.core.refactoring.history.RefactoringHistoryEvent;
30
31 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
32 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
33
34 /**
35  * Refactoring history listener which continuously persists the global
36  * refactoring history in the different history locations.
37  *
38  * @since 3.2
39  */

40 public final class RefactoringHistorySerializer implements IRefactoringHistoryListener {
41
42     /**
43      * {@inheritDoc}
44      */

45     public void historyNotification(final RefactoringHistoryEvent event) {
46         Assert.isNotNull(event);
47         switch (event.getEventType()) {
48             case RefactoringHistoryEvent.ADDED:
49             case RefactoringHistoryEvent.PUSHED:
50             case RefactoringHistoryEvent.POPPED: {
51                 final RefactoringDescriptorProxy proxy= event.getDescriptor();
52                 final long stamp= proxy.getTimeStamp();
53                 if (stamp >= 0) {
54                     final String JavaDoc name= proxy.getProject();
55                     final IFileStore store= EFS.getLocalFileSystem().getStore(RefactoringCorePlugin.getDefault().getStateLocation()).getChild(RefactoringHistoryService.NAME_HISTORY_FOLDER);
56                     if (name != null && !"".equals(name)) { //$NON-NLS-1$
57
final IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject(name);
58                         if (project.isAccessible()) {
59                             if (RefactoringHistoryService.hasSharedRefactoringHistory(project)) {
60                                 final URI JavaDoc uri= project.getLocationURI();
61                                 if (uri != null) {
62                                     try {
63                                         processHistoryNotification(EFS.getStore(uri).getChild(RefactoringHistoryService.NAME_HISTORY_FOLDER), event, name);
64                                     } catch (CoreException exception) {
65                                         RefactoringCorePlugin.log(exception);
66                                     } finally {
67                                         try {
68                                             project.refreshLocal(IResource.DEPTH_INFINITE, null);
69                                         } catch (CoreException exception) {
70                                             RefactoringCorePlugin.log(exception);
71                                         }
72                                     }
73                                 }
74                             } else {
75                                 try {
76                                     processHistoryNotification(store.getChild(name), event, name);
77                                 } catch (CoreException exception) {
78                                     RefactoringCorePlugin.log(exception);
79                                 }
80                             }
81                         }
82                     } else {
83                         try {
84                             processHistoryNotification(store.getChild(RefactoringHistoryService.NAME_WORKSPACE_PROJECT), event, name);
85                         } catch (CoreException exception) {
86                             RefactoringCorePlugin.log(exception);
87                         }
88                     }
89                 }
90             }
91         }
92     }
93
94     /**
95      * Processes the history event.
96      *
97      * @param store
98      * the file store
99      * @param event
100      * the history event
101      * @param name
102      * the project name, or <code>null</code>
103      * @throws CoreException
104      * if an error occurs
105      */

106     private void processHistoryNotification(final IFileStore store, final RefactoringHistoryEvent event, final String JavaDoc name) throws CoreException {
107         final RefactoringDescriptorProxy proxy= event.getDescriptor();
108         final int type= event.getEventType();
109         final RefactoringHistoryManager manager= new RefactoringHistoryManager(store, name);
110         final NullProgressMonitor monitor= new NullProgressMonitor();
111         if (type == RefactoringHistoryEvent.PUSHED || type == RefactoringHistoryEvent.ADDED) {
112             final RefactoringDescriptor descriptor= proxy.requestDescriptor(monitor);
113             if (descriptor != null)
114                 manager.addRefactoringDescriptor(descriptor, type == RefactoringHistoryEvent.ADDED, monitor);
115         } else if (type == RefactoringHistoryEvent.POPPED)
116             manager.removeRefactoringDescriptors(new RefactoringDescriptorProxy[] { proxy}, monitor, RefactoringCoreMessages.RefactoringHistoryService_updating_history);
117     }
118 }
119
Popular Tags