001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.tool.display;
018:
019: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
020: import net.refractions.udig.project.ui.tool.IToolHandler;
021: import net.refractions.udig.project.ui.tool.Tool;
022:
023: import org.eclipse.core.commands.AbstractHandler;
024: import org.eclipse.core.commands.ExecutionEvent;
025: import org.eclipse.core.commands.ExecutionException;
026: import org.eclipse.core.runtime.CoreException;
027: import org.eclipse.core.runtime.IConfigurationElement;
028:
029: /**
030: * Proxy to allow lazy loading of ToolCommandHandlers
031: *
032: * @author jeichar
033: * @since 0.6.0
034: */
035: public class HandlerProxy extends AbstractHandler {
036:
037: /**
038: * If a handler cannot be created for the Tool then this class will
039: *
040: * @author jeichar
041: * @since 0.6.0
042: */
043: public static class EmptyHandler extends AbstractHandler implements
044: IToolHandler {
045:
046: /**
047: * @see net.refractions.udig.project.tool.IToolHandler#setTool(net.refractions.udig.project.tool.Tool)
048: */
049: public void setTool(Tool tool) {
050: // do nothing
051: }
052:
053: /**
054: * @see net.refractions.udig.project.tool.IToolHandler#setCurrentCommandId(java.lang.String)
055: */
056: public void setCurrentCommandId(String currentCommandId) {
057: // do nothing.
058: }
059:
060: /**
061: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
062: */
063: public Object execute(ExecutionEvent event) {
064: return null;
065: }
066:
067: }
068:
069: private volatile IToolHandler instance;
070: private IConfigurationElement toolElement;
071: private ToolProxy tool;
072: /** the id of the extension attribute */
073: public static final String ID = "commandHandler"; //$NON-NLS-1$
074: private final String commandId;
075:
076: /**
077: * Construct <code>HandlerProxy</code>.
078: *
079: * @param toolElement
080: * @param tool
081: */
082: public HandlerProxy(IConfigurationElement toolElement,
083: ToolProxy tool, String commandId) {
084: this .toolElement = toolElement;
085: this .tool = tool;
086: this .commandId = commandId;
087: }
088:
089: private IToolHandler getToolHandler() {
090: synchronized (this ) {
091: if (instance == null) {
092: try {
093: instance = (IToolHandler) toolElement
094: .createExecutableExtension(ID);
095: instance.setTool(tool.getTool());
096: } catch (CoreException e) {
097: ProjectUIPlugin.log(null, e);
098: instance = new EmptyHandler();
099: }
100: }
101: instance.setCurrentCommandId(commandId);
102: }
103: return instance;
104: }
105:
106: // /**
107: // * @see
108: // org.eclipse.ui.commands.IHandler#addHandlerListener(org.eclipse.ui.commands.IHandlerListener)
109: // */
110: // public void addHandlerListener( IHandlerListener handlerListener ) {
111: // getToolHandler().addHandlerListener(handlerListener);
112: // }
113:
114: /**
115: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
116: */
117: public Object execute(ExecutionEvent event)
118: throws ExecutionException {
119: return getToolHandler().execute(event);
120: }
121:
122: }
|