KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > RefactoringCorePlugin


1 /*******************************************************************************
2  * Copyright (c) 2000, 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;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Plugin;
15 import org.eclipse.core.runtime.Status;
16
17 import org.eclipse.core.commands.operations.IOperationHistory;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.core.commands.operations.ObjectUndoContext;
20 import org.eclipse.core.commands.operations.OperationHistoryFactory;
21
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.ltk.core.refactoring.IRefactoringCoreStatusCodes;
25 import org.eclipse.ltk.core.refactoring.IUndoManager;
26 import org.eclipse.ltk.core.refactoring.RefactoringCore;
27 import org.eclipse.ltk.core.refactoring.history.IRefactoringHistoryListener;
28
29 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistorySerializer;
30 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
31 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringContributionManager;
32
33 import org.osgi.framework.BundleContext;
34
35 public class RefactoringCorePlugin extends Plugin {
36     
37     private static RefactoringCorePlugin fgDefault;
38     private static IUndoManager fgUndoManager= null;
39     
40     private static IUndoContext fRefactoringUndoContext;
41     
42     private IRefactoringHistoryListener fRefactoringHistoryListener= null;
43     
44     public RefactoringCorePlugin() {
45         fgDefault= this;
46     }
47
48     public static RefactoringCorePlugin getDefault() {
49         return fgDefault;
50     }
51     
52     public static String JavaDoc getPluginId() {
53         return RefactoringCore.ID_PLUGIN;
54     }
55     
56     public static IUndoContext getUndoContext() {
57         if (fRefactoringUndoContext == null) {
58             fRefactoringUndoContext= new RefactoringUndoContext();
59             IUndoContext workspaceContext= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
60             if (workspaceContext instanceof ObjectUndoContext) {
61                 ((ObjectUndoContext)workspaceContext).addMatch(fRefactoringUndoContext);
62             }
63             IOperationHistory operationHistory= OperationHistoryFactory.getOperationHistory();
64             operationHistory.setLimit(fRefactoringUndoContext, 5);
65         }
66         return fRefactoringUndoContext;
67     }
68     
69     public static void log(IStatus status) {
70         getDefault().getLog().log(status);
71     }
72     
73     public static void log(Throwable JavaDoc t) {
74         IStatus status= new Status(
75             IStatus.ERROR, getPluginId(),
76             IRefactoringCoreStatusCodes.INTERNAL_ERROR,
77             RefactoringCoreMessages.RefactoringCorePlugin_internal_error,
78             t);
79         ResourcesPlugin.getPlugin().getLog().log(status);
80     }
81     
82     public static void logRemovedListener(Throwable JavaDoc t) {
83         IStatus status= new Status(
84             IStatus.ERROR, getPluginId(),
85             IRefactoringCoreStatusCodes.INTERNAL_ERROR,
86             RefactoringCoreMessages.RefactoringCorePlugin_listener_removed,
87             t);
88         ResourcesPlugin.getPlugin().getLog().log(status);
89     }
90     
91     public static void logRemovedParticipant(ParticipantDescriptor descriptor, Throwable JavaDoc t) {
92         IStatus status= new Status(
93             IStatus.ERROR, getPluginId(),
94             IRefactoringCoreStatusCodes.INTERNAL_ERROR,
95             Messages.format(
96                 RefactoringCoreMessages.RefactoringCorePlugin_participant_removed,
97                 descriptor.getId()),
98             t);
99         ResourcesPlugin.getPlugin().getLog().log(status);
100     }
101     
102     public static void logErrorMessage(String JavaDoc message) {
103         log(new Status(IStatus.ERROR, getPluginId(), IRefactoringCoreStatusCodes.INTERNAL_ERROR, message, null));
104     }
105     
106     public static IUndoManager getUndoManager() {
107         if (fgUndoManager == null)
108             fgUndoManager= createUndoManager();
109         return fgUndoManager;
110     }
111     
112     public void start(BundleContext context) throws Exception JavaDoc {
113         super.start(context);
114         RefactoringContributionManager.getInstance().connect();
115         final RefactoringHistoryService service= RefactoringHistoryService.getInstance();
116         service.connect();
117         fRefactoringHistoryListener= new RefactoringHistorySerializer();
118         service.addHistoryListener(fRefactoringHistoryListener);
119     }
120     
121     public void stop(BundleContext context) throws Exception JavaDoc {
122         if (fRefactoringUndoContext != null) {
123             IUndoContext workspaceContext= (IUndoContext)ResourcesPlugin.getWorkspace().getAdapter(IUndoContext.class);
124             if (workspaceContext instanceof ObjectUndoContext) {
125                 ((ObjectUndoContext)workspaceContext).removeMatch(fRefactoringUndoContext);
126             }
127         }
128         if (fgUndoManager != null)
129             fgUndoManager.shutdown();
130         final RefactoringHistoryService service= RefactoringHistoryService.getInstance();
131         service.disconnect();
132         if (fRefactoringHistoryListener != null)
133             service.removeHistoryListener(fRefactoringHistoryListener);
134         RefactoringContributionManager.getInstance().disconnect();
135         super.stop(context);
136     }
137     
138     /**
139      * Creates a new empty undo manager.
140      *
141      * @return a new undo manager
142      */

143     private static IUndoManager createUndoManager() {
144         return new UndoManager2();
145     }
146 }
147
Popular Tags