好的算法

 

用数组代替Map---填入过程

 

public void installEditPolicy(Object key, EditPolicy editPolicy) {
	Assert.isNotNull(key, "Edit Policies must be installed with keys");//$NON-NLS-1$
	if (policies == null) {
		policies = new Object[2];
		policies[0] = key;
		policies[1] = editPolicy;
	} else {
		int index = 0;
		while (index < policies.length && !key.equals(policies[index]))
			index += 2;
		if (index < policies.length) {
			index++;
			EditPolicy old = (EditPolicy)policies[index];
			if (old != null && isActive())
				old.deactivate();
			policies[index] = editPolicy;
		} else {
			Object newPolicies[] = new Object[policies.length + 2];
			System.arraycopy(policies, 0, newPolicies, 0, policies.length);
			policies = newPolicies;
			policies[index] = key;
			policies[index + 1] = editPolicy;
		}
	}
	
	if (editPolicy != null) {
		editPolicy.setHost(this);
		if (isActive())
			editPolicy.activate();
	}
}

 

 

 

用数组代替Map---获取过程

 

public EditPolicy getEditPolicy(Object key) {
	if (policies != null)
		for (int i = 0; i < policies.length; i += 2) {
			if (key.equals(policies[i]))
				return (EditPolicy) policies[i + 1];
		}
	return null;
}
 

 

ModelEditPart存在着一一对应的关系,同步功能

protected void refreshChildren() {
	int i;
	EditPart editPart;
	Object model;

	Map modelToEditPart = new HashMap();
	List children = getChildren();

	for (i = 0; i < children.size(); i++) {
		editPart = (EditPart)children.get(i);
		modelToEditPart.put(editPart.getModel(), editPart);
	}

	List modelObjects = getModelChildren();

	for (i = 0; i < modelObjects.size(); i++) {
		model = modelObjects.get(i);

		//Do a quick check to see if editPart[i] == model[i]
		if (i < children.size()
			&& ((EditPart) children.get(i)).getModel() == model)
				continue;

		//Look to see if the EditPart is already around but in the wrong location
		editPart = (EditPart)modelToEditPart.get(model);

		if (editPart != null)
			reorderChild (editPart, i);
		else {
			//An editpart for this model doesn't exist yet.  Create and insert one.
			editPart = createChild(model);
			addChild(editPart, i);
		}
	}
	List trash = new ArrayList();
	for (; i < children.size(); i++)
		trash.add(children.get(i));
	for (i = 0; i < trash.size(); i++) {
		EditPart ep = (EditPart)trash.get(i);
		removeChild(ep);
	}
}
 

存在接口EditPart,定义addNotify方法

采用多态来实现递归调用---EditPart实现类在实现addNotify方法时候需要递归遍历其children,使它们也进行addNotify动作。

public void addNotify() {
	register();
	createEditPolicies();
	List children = getChildren();
	for (int i = 0; i < children.size(); i++)
		((EditPart)children.get(i))
			.addNotify();
	refresh();
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值