GEF属性页的常见3类属性实现

1 篇文章 0 订阅

目标效果:

 

前提:

1.对象必须实现IPropertySource接口并且实现相关方法

2.对象必须为属性视图提供属性

属性分类:

  • PropertyDescriptor 可以实现不可编辑的属性
  • StandardComboBoxPropertyDescriptor
  • ColorPropertyDescriptor 会弹出颜色选择对话框
  • ComboBoxPropertyDescriptor 可以通过下拉框选择需要的属性
  • TextPropertyDescriptor 实现可编辑的属性

后三种为常用类型,此次介绍也为此

基础:

步骤1:为属性视图提供属性项

(实现getPropertyDescriptors方法)

例:

基础数据部分:

    private String text = "Hello world";
//	约束
	private Rectangle constraint;
	private boolean flag = true;
	private Color color = ColorConstants.orange;

	public static final String P_CONSTRAINT = "_constraint";
	// 添加字符串的ID 这样改变图形的文本时可通知其Editpart
	public static final String P_TEXT = "_text";
	public static final String P_FLAG = "_flag";
	public static final String P_COLOR = "_color";

    public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
		firePropertyChange(P_COLOR, null, color);
	}
//重载setColor,由于ColorPropertyDescriptor只接收RGB类型,所以必须重载
	public void setColor(RGB color) {
		this.color = new Color(null, color);
		firePropertyChange(P_COLOR, null, this.color);
	}

    public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		firePropertyChange(P_TEXT, null, text);
	}

	public Rectangle getConstraint() {
		return constraint;
	}

	public void setConstraint(Rectangle constraint) {
		this.constraint = constraint;
		firePropertyChange(P_CONSTRAINT, null, constraint);
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
		firePropertyChange(P_FLAG, null, flag);
	}

 正文:

    // 其实属性视图中用tableview来显示属性,第一列是属性名称,第2列是属性值.
	// IPropertyDescriptor[]数组顾名思义就是用来设置属性名称的.

    @Override
	public IPropertyDescriptor[] getPropertyDescriptors() {

		List<IPropertyDescriptor> properties = new ArrayList<>();//定义数组用来存储属性项

		String group1 = "1.位置信息";//最前面的1是用来排序的,目前没找到好的属性组排序方法
		PropertyDescriptor pd1 = new TextPropertyDescriptor("x", "x轴");
		PropertyDescriptor pd2 = new TextPropertyDescriptor("y", "y轴");

        pd1.setCategory(group1);//		定义分组
		pd2.setCategory(group1);

        properties.add(pd1);//      添加进数组
		properties.add(pd2);

		String group2 = "2.尺寸信息";
		PropertyDescriptor pd3 = new TextPropertyDescriptor("width", "宽度");
		PropertyDescriptor pd4 = new TextPropertyDescriptor("height", "长度");

        pd3.setCategory(group2);
		pd4.setCategory(group2);

        properties.add(pd3);
		properties.add(pd4);

		String group3 = "3.基础信息";

		PropertyDescriptor pd5 = new TextPropertyDescriptor(P_TEXT, "标题");
//可编辑文本属性(需要注意的地方:此属性项需要传输String类型数据)

		PropertyDescriptor pd6 = new ComboBoxPropertyDescriptor(P_FLAG, "标志", new             String[] { "true", "false" });

//多选框属性(需要注意的地方:此属性项需要传输int类型数据,用来定位字符串数组数据)

		PropertyDescriptor pd7 = new ColorPropertyDescriptor(P_COLOR, "颜色");

//颜色属性(需要注意的地方:此属性项需要传输RGB类型数据,但通常控件需要Color类型,所以需要调用Color类中getRGB方法获取到RGB类)
		
		pd5.setCategory(group3);
		pd6.setCategory(group3);
		pd7.setCategory(group3);
	
		
		properties.add(pd5);
		properties.add(pd6);
		properties.add(pd7);

		return properties.toArray(new IPropertyDescriptor[properties.size()]);//将内容传给属性视图
	}

步骤2:填充方法(Model部分)

// 使用属性的ID来获得该属性在属性视图的值
	@Override
	public Object getPropertyValue(Object id) {
		if (id.equals(P_TEXT)) {
			return getText();
		} else if (id.equals(P_FLAG)) {
			return isFlag() ? 0 : 1;
		} else if (id.equals("x")) {
			return constraint.x + "";
		} else if (id.equals("y")) {
			return constraint.y + "";
		} else if (id.equals("width")) {
			return constraint.width + "";
		} else if (id.equals("height")) {
			return constraint.height + "";
		} else if (id.equals(P_COLOR)) {
			return getColor().getRGB();
		}
		return null;
	}

	// 判断属性视图中的属性值是否改变,如果没有指定属性值则返回false
	@Override
	public boolean isPropertySet(Object id) {
		if (id.equals(P_TEXT)) {
			return true;
		} else if (id.equals(P_FLAG)) {
			return true;
		} else if (id.equals("x")) {
			return true;
		} else if (id.equals("y")) {
			return true;
		} else if (id.equals("width")) {
			return true;
		} else if (id.equals("height")) {
			return true;
		} else if (id.equals(P_COLOR)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		if (id.equals(P_TEXT)) {
			setText((String) value);
		} else if (id.equals(P_FLAG)) {
			setFlag(((Integer) value).intValue() == 0);
		} else if (id.equals(P_COLOR)) {
			setColor((RGB) value);
		} else {
			int parseInt = Integer.parseInt(value.toString());
			if (id.equals("x")) {
				constraint = new Rectangle(parseInt, constraint.y, constraint.width, constraint.height);
			} else if (id.equals("y")) {
				constraint = new Rectangle(constraint.x, parseInt, constraint.width, constraint.height);
			} else if (id.equals("width")) {
				constraint = new Rectangle(constraint.x, constraint.y, parseInt, constraint.height);
			} else if (id.equals("height")) {
				constraint = new Rectangle(constraint.x, constraint.y, constraint.width, parseInt);
			}
			setConstraint(constraint);
		}

	}

步骤3:填充方法(EditPart部分)

@Override
	public void propertyChange(PropertyChangeEvent evt) {
		String propertyName = evt.getPropertyName();
		if (propertyName.equals(HelloModel.P_TEXT)) {
			Label label = (Label) getFigure();
			label.setText((String) evt.getNewValue());
		} else if (propertyName.equals(HelloModel.P_FLAG)) {
			Label label = (Label) getFigure();
			label.setOpaque((Boolean) evt.getNewValue());
		} else if (propertyName.equals(HelloModel.P_COLOR)) {
			Label label = (Label) getFigure();
			label.setBackgroundColor((Color) evt.getNewValue());
		} else if (propertyName.equals("x")) {
			HelloModel model = (HelloModel) getModel();
			model.getConstraint().x = (Integer) evt.getNewValue();
		} else if (propertyName.equals("y")) {
			HelloModel model = (HelloModel) getModel();
			model.getConstraint().y = (Integer) evt.getNewValue();
		} else if (propertyName.equals("width")) {
			HelloModel model = (HelloModel) getModel();
			model.getConstraint().width = (Integer) evt.getNewValue();
		} else if (propertyName.equals("height")) {
			HelloModel model = (HelloModel) getModel();
			model.getConstraint().height = (Integer) evt.getNewValue();
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值