ツリールートノードの内部クラスを構築するためのメソッド

 プレビュー:

コードのアイデア:

  • 木の実現
  • ルートノードの問題
  • インナークラスアプリケーション
  • パラメータを渡すための工法の適用

 ツリープロバイダー:

public class BaseTreeProvider implements TreeContentProvider, LabelProvider, IElementComparer {

	private Situation context;
	private VirtualRootNode rootNode;
	private String tableName;
	private String title;

	private class VirtualRootNode extends BaseDataObjectImpl {
		private static final long serialVersionUID = 368019880420475326L;

		private GUID recid = GUID.valueOf("00000000000000000000000000000000");

		private String text = "所有指标";

		private byte[] parents = null;

		public VirtualRootNode(ITable table) {
			super(table);
		}

		public VirtualRootNode(ITable table, String text) {
			super(table);
			if(null != text && !"".equals(text)){
				this.text = text;
			}
		}

		@Override
		public GUID getRECID() {
			return recid;
		}

		@Override
		public String getStdCode() {
			return "root";
		}

		@Override
		public String getStdName() {
			return text;
		}

		@Override
		public int getLevel() {
			return 1;
		}

		@Override
		public byte[] getParents() {
			return parents;
		}

	}

	// ****************构造*****************
	public BaseTreeProvider(Situation context, String tableName) {
		super();
		this.context = context;
		this.rootNode = new VirtualRootNode(null);
		this.tableName = tableName;
	}
	public BaseTreeProvider(Situation context, String tableName, String title) {
		super();
		this.context = context;
		this.rootNode = new VirtualRootNode(null,title);            //构造传参给内部类
		this.tableName = tableName;
		this.title = title;
	}
	// ****************构造*****************
	
	public BaseDataObjectImpl getRootNode() {
		return rootNode;
	}
	
	@Override
	public Object[] getElements(Object inputElement) {
		return getChildren(inputElement);
	}

	@Override
	public boolean equals(Object a, Object b) {
		if (a != null && b != null && a instanceof BaseDataObjectImpl && b instanceof BaseDataObjectImpl) {
			if (((BaseDataObjectImpl) a).getStdCode().equals(((BaseDataObjectImpl) b).getStdCode())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode(Object element) {
		if (element == null || !(element instanceof BaseDataObjectImpl)) {
			return 0;
		}
		BaseDataObjectImpl template = ((BaseDataObjectImpl) element);
		if (template.getStdCode() == null) {
			return 0;
		}
		return template.getStdCode().hashCode();
	}

	@Override
	public ImageDescriptor getImage(Object element) {
		if (element == null || !(element instanceof BaseDataObjectImpl)) {
			return null;
		}
		BaseDataObjectImpl template = ((BaseDataObjectImpl) element);
		if ((template.getLevel() + "").equals("1")) {
			//return context.find(ImageDescriptor.class, BapImages.ico_treenode_group_closed);
		}
		if ((template.getLevel() + "").equals("0")) {
			//return context.find(ImageDescriptor.class, BapImages.ico_treenode_file);
		}
		return null;
	}

	@Override
	public String getText(Object element) {
		if (element == null || !(element instanceof BaseDataObjectImpl)) {
			return null;
		}
		return ((BaseDataObjectImpl) element).getStdName();
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement == null || !(parentElement instanceof BaseDataObjectImpl)) {
			return new Object[] { rootNode };
		}
		BaseDataObjectImpl template = ((BaseDataObjectImpl) parentElement);
		List<BaseDataObjectImpl> list = null;
		List<FBaseDataObject> templates = new ArrayList<FBaseDataObject>();
		if(null == tableName || "".equals(tableName.trim()) ){
			//MessageDialog.alert("基础数据表名异常!");
			return null;
		}
		if (null == template.getParents()) {
			list = GetBaseDataUtil.findBaseDataByNameAndLevel(context, tableName, 1);
		} else {
			int level = template.getLevel() + 1;
			String parentId =  template.getRECID().toString() ;
			list = GetBaseDataUtil.findBaseDataByNameParentIdLevel(context, tableName, parentId, level);
		}
		if (null != list) {
			templates.addAll(list);
		}
		if (templates != null && templates.size() > 0) {
			return templates.toArray();
		}
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		if (element == null || !(element instanceof BaseDataObjectImpl)) {
			return false;
		}
		if (getChildren(element) != null && getChildren(element).length > 0) {
			return true;
		}
		return false;
	}

	@Override
	public Object getParent(Object element) {
		if (element == null || !(element instanceof BaseDataObjectImpl)) {
			return null;
		}
		if(null == tableName || "".equals(tableName.trim()) ){
			//MessageDialog.alert("基础数据表名异常!");
			return null;
		}
		BaseDataObjectImpl template = ((BaseDataObjectImpl) element);
		int level = template.getLevel() - 1;
		String parentId = template.getRECID().toString();
		List<BaseDataObjectImpl> list = GetBaseDataUtil.findBaseDataByNameParentIdLevel(context, tableName, parentId,
				level);
		if (null != list && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}

 

ツール:

/**
	 * 下一级或上一级子节点
	 * @param context
	 * @param tableName
	 * @param parentId
	 * @param level
	 * @param nextNode 
	 * @return
	 */
	public static List<BaseDataObjectImpl> findBaseDataByNameParentIdLevel(Context context,String tableName ,String parentId,int level){
		List<BaseDataObjectImpl> list = new ArrayList<BaseDataObjectImpl>();
		String sql = "define query findBaseDataByNameParentIdLevel() begin "
				+ " select t.RECID as recid,t.STDNAME as stdname,t.STDCODE as stdcode,t.LEVEL as level,t.PARENTS as parents,t.ISLEAF as isleaf    "
				+ " from "+tableName +" as t"
				+ " where t.LEVEL = "+ level
				+ " and t.PARENTS like '%" + parentId +"%'"
				+ " end ";
		DBCommand db = null;
		try {
			db = context.prepareStatement(sql);
			db.setArgumentValues();
			RecordSet rs = db.executeQuery();
			while (rs.next()) {
				BaseDataObjectImpl base = new BaseDataObjectImpl(null);
				base.setRECID(rs.getFields().get(0).getGUID());
				base.setStdName(rs.getFields().get(1).getString());
				base.setStdCode(rs.getFields().get(2).getString());
				base.setLevel(rs.getFields().get(3).getInt());
				base.setParents(rs.getFields().get(4).getBytes());
				base.setLeaf(rs.getFields().get(5).getBoolean());
				list.add(base);
			}
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			if (db != null)
				db.unuse();
		}
		return list;
	}

 

おすすめ

転載: blog.csdn.net/xiangwang2016/article/details/100020510