gmf logic example 代码阅读笔记

1. 创建element type的一段代码
private Element createElement(Element container, IElementType type,
            EReference containmentFeature, IProgressMonitor progressMonitor) {

        CreateElementRequest createRequest 
= new CreateElementRequest(
            getEditingDomain(), container, type, containmentFeature);

        IElementType elementType 
= ElementTypeRegistry.getInstance()
            .getElementType(createRequest.getEditHelperContext());

        
if (elementType != null) {
            ICommand createCommand 
= elementType.getEditCommand(createRequest);

            
if (createCommand != null && createCommand.canExecute()) {

                
try {
                    createCommand.execute(progressMonitor, 
null);
                    CommandResult commandResult 
= createCommand
                        .getCommandResult();

                    
if (isOK(commandResult)) {
                        Object result 
= commandResult.getReturnValue();

                        
if (result instanceof Element) {
                            
return (Element) result;
                        }
                    }

                } 
catch (ExecutionException e) {
                    Trace.catching(
                        LogicDiagramPlugin.getInstance(),
                        LogicDiagramDebugOptions.EXCEPTIONS_CATCHING,
                        getClass(), 
"createElement", e); //$NON-NLS-1$

                    Log.error(LogicDiagramPlugin.getInstance(),
                        LogicDiagramStatusCodes.COMMAND_FAILURE, e
                            .getLocalizedMessage());
                }
            }
        }
        
return null;
    }
2. element type commmand
在这个例子里面的commands包里面只有3个command类,
ConfigureLogicElementCommand继承类org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand
此类为抽象类,包含了基本类型的create方法,其中方法createElement为抽象的create方法为其他create方法所调用。
方法doExecuteWithResult()在具体的实现类中重写,并在element type 对应的helper 或 device类中使用。

3. IElementType 源代码
public interface IElementType
    
extends IAdaptable {

    
/**
     * Gets the unique identifier for this element type.
     * 
     * 
@return the unique identifier
     
*/
    
public abstract String getId();

    
/**
     * Gets the icon URL.
     * 
     * 
@return the icon URL
     
*/
    
public abstract URL getIconURL();

    
/**
     * Gets the display name.
     * 
     * 
@return the display name
     
*/
    
public abstract String getDisplayName();

    
/**
     * Gets the metaclass for this element type.
     * 
     * 
@return the metaclass
     
*/
    
public abstract EClass getEClass();

    
/**
     * Gets a command to edit an element of this type.
     * 
     * 
@param request
     *            the edit request
     * 
@return the edit command, or <code>null</code> if none is found. The
     *         command returned may not be executable, and this should be tested
     *         before it is executed.
     
*/
    
public abstract ICommand getEditCommand(IEditCommandRequest request);
    
    
/**
     * Answers whether or not the requested edit can be performed.
     * 
     * 
@param req
     *            the edit request
     * 
@return <code>true</code> if the requested edit can be performed,
     *         <code>false</code> otherwise.
     
*/
    
public boolean canEdit(IEditCommandRequest req);

    
/**
     * Gets the edit helper for this element type.
     * 
     * 
@return the edit helper
     
*/
    
public abstract IEditHelper getEditHelper();
    
    
/**
     * Gets the element supertypes for this type.
     * Ordered from furthest supertype to nearest supertype.
     * 
     * 
@return the element supertypes
     
*/
    
public IElementType[] getAllSuperTypes();

}

posted @ 2006-05-17 11:07  Android,服务端,移动互联网  阅读(741)  评论(1编辑  收藏  举报