01: /*******************************************************************************
02: * Copyright (c) 2000, 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.ui.text.correction;
11:
12: import java.util.ArrayList;
13: import java.util.Collection;
14: import java.util.Iterator;
15: import java.util.List;
16:
17: import org.eclipse.ui.IWorkbench;
18: import org.eclipse.ui.LegacyHandlerSubmissionExpression;
19: import org.eclipse.ui.PlatformUI;
20: import org.eclipse.ui.commands.ICommandService;
21: import org.eclipse.ui.handlers.IHandlerActivation;
22: import org.eclipse.ui.handlers.IHandlerService;
23:
24: import org.eclipse.jdt.internal.ui.JavaPlugin;
25: import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
26:
27: public class CorrectionCommandInstaller {
28:
29: /**
30: * All correction commands must start with the following prefix.
31: */
32: public static final String COMMAND_PREFIX = "org.eclipse.jdt.ui.correction."; //$NON-NLS-1$
33:
34: /**
35: * Commands for quick assist must have the following suffix.
36: */
37: public static final String ASSIST_SUFFIX = ".assist"; //$NON-NLS-1$
38:
39: private List fCorrectionHandlerActivations;
40:
41: public CorrectionCommandInstaller() {
42: fCorrectionHandlerActivations = null;
43: }
44:
45: public void registerCommands(CompilationUnitEditor editor) {
46: IWorkbench workbench = PlatformUI.getWorkbench();
47: ICommandService commandService = (ICommandService) workbench
48: .getAdapter(ICommandService.class);
49: IHandlerService handlerService = (IHandlerService) workbench
50: .getAdapter(IHandlerService.class);
51: if (commandService == null || handlerService == null) {
52: return;
53: }
54:
55: if (fCorrectionHandlerActivations != null) {
56: JavaPlugin
57: .logErrorMessage("correction handler activations not released"); //$NON-NLS-1$
58: }
59: fCorrectionHandlerActivations = new ArrayList();
60:
61: Collection definedCommandIds = commandService
62: .getDefinedCommandIds();
63: for (Iterator iter = definedCommandIds.iterator(); iter
64: .hasNext();) {
65: String id = (String) iter.next();
66: if (id.startsWith(COMMAND_PREFIX)) {
67: boolean isAssist = id.endsWith(ASSIST_SUFFIX);
68: CorrectionCommandHandler handler = new CorrectionCommandHandler(
69: editor, id, isAssist);
70: IHandlerActivation activation = handlerService
71: .activateHandler(id, handler,
72: new LegacyHandlerSubmissionExpression(
73: null, null, editor.getSite()));
74: fCorrectionHandlerActivations.add(activation);
75: }
76: }
77: }
78:
79: public void deregisterCommands() {
80: IHandlerService handlerService = (IHandlerService) PlatformUI
81: .getWorkbench().getAdapter(IHandlerService.class);
82: if (handlerService != null
83: && fCorrectionHandlerActivations != null) {
84: handlerService
85: .deactivateHandlers(fCorrectionHandlerActivations);
86: fCorrectionHandlerActivations = null;
87: }
88: }
89:
90: }
|