gmf logic example 代码阅读笔记

本文介绍了一种用于创建elementtype的方法,通过具体的代码示例展示了如何利用IElementType接口来生成新的元素实例。此外,还讨论了与elementtype相关的命令类及其继承结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值