KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > CorrectionCommandInstaller


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.jdt.internal.ui.text.correction;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.ui.IWorkbench;
20 import org.eclipse.ui.LegacyHandlerSubmissionExpression;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.commands.ICommandService;
23 import org.eclipse.ui.handlers.IHandlerActivation;
24 import org.eclipse.ui.handlers.IHandlerService;
25
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
28
29 public class CorrectionCommandInstaller {
30     
31     /**
32      * All correction commands must start with the following prefix.
33      */

34     public static final String JavaDoc COMMAND_PREFIX= "org.eclipse.jdt.ui.correction."; //$NON-NLS-1$
35

36     /**
37      * Commands for quick assist must have the following suffix.
38      */

39     public static final String JavaDoc ASSIST_SUFFIX= ".assist"; //$NON-NLS-1$
40

41     private List JavaDoc fCorrectionHandlerActivations;
42     
43     public CorrectionCommandInstaller() {
44         fCorrectionHandlerActivations= null;
45     }
46     
47     public void registerCommands(CompilationUnitEditor editor) {
48         IWorkbench workbench= PlatformUI.getWorkbench();
49         ICommandService commandService= (ICommandService) workbench.getAdapter(ICommandService.class);
50         IHandlerService handlerService= (IHandlerService) workbench.getAdapter(IHandlerService.class);
51         if (commandService == null || handlerService == null) {
52             return;
53         }
54         
55         if (fCorrectionHandlerActivations != null) {
56             JavaPlugin.logErrorMessage("correction handler activations not released"); //$NON-NLS-1$
57
}
58         fCorrectionHandlerActivations= new ArrayList JavaDoc();
59         
60         Collection JavaDoc definedCommandIds= commandService.getDefinedCommandIds();
61         for (Iterator JavaDoc iter= definedCommandIds.iterator(); iter.hasNext();) {
62             String JavaDoc id= (String JavaDoc) iter.next();
63             if (id.startsWith(COMMAND_PREFIX)) {
64                 boolean isAssist= id.endsWith(ASSIST_SUFFIX);
65                 CorrectionCommandHandler handler= new CorrectionCommandHandler(editor, id, isAssist);
66                 IHandlerActivation activation= handlerService.activateHandler(id, handler, new LegacyHandlerSubmissionExpression(null, null, editor.getSite()));
67                 fCorrectionHandlerActivations.add(activation);
68             }
69         }
70     }
71     
72     public void deregisterCommands() {
73         IHandlerService handlerService= (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
74         if (handlerService != null && fCorrectionHandlerActivations != null) {
75             handlerService.deactivateHandlers(fCorrectionHandlerActivations);
76             fCorrectionHandlerActivations= null;
77         }
78     }
79
80 }
81
Popular Tags