Simplify tableViewer development with annotations

I haven't used swt for development in two years. Looking back at the code writing two years ago, there are indeed some terrible places. I used my spare time to make small repairs, but the general writing design of the project still has some problems, but this book The secondary focus is only on simplifying the development configuration. This time, the annotation method is used to process the configuration, which is much more convenient than the previous string method.

Added annotation class:

@Retention(RetentionPolicy.RUNTIME) // The annotation will exist in the class bytecode file and can be obtained by reflection at runtime  
@Target({ElementType.FIELD, ElementType.METHOD})//Define the target of the annotation **Scope field, constant/method of the enumeration  
@Documented//Indicates that the annotation will be included in the javadoc
public @interface YtTableItemAnno {

	/**Whether it can be edited*/
	boolean editor() default false;
	
	/** The text of the header column of the table */
	String itemText() ;
	
	/** The width of the header column of the table */
	int width() default 80 ;
	
	/** Header sorting */
	int itemIndex() ;
	
	
}

The annotation description class handles the column functions of the commonly used tableviewer, such as width, editable, etc. 

 

Added javabean to display the column elements corresponding to the tableviewer.

public class InOrderDesc {
	private int inOrderId;
	private String no;
	private String supName;
	private float allMoney = 0.00f;

	// order id|contract number|supplier|total amount
	
	@YtTableItemAnno(editor = false, itemText = "订单id", itemIndex = 0 , width = 50)
	public int getInOrderId() {
		return inOrderId;
	}

	public void setInOrderId(int inOrderId) {
		this.inOrderId = inOrderId;
	}
	@YtTableItemAnno(editor = false, itemText = "合同号", itemIndex = 1 , width = 50)
	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}
	@YtTableItemAnno(editor = true, itemText = "Supplier(editable)", itemIndex = 2 , width = 100)
	public String getSupName() {
		return supName;
	}

	public void setSupName(String supName) {
		this.supName = supName;
	}

	@YtTableItemAnno(editor = true, itemText = "Total amount (editable)", itemIndex = 3 , width = 100)
	public float getAllMoney() {
		return allMoney;
	}

	public void setAllMoney(float allMoney) {
		this.allMoney = allMoney;
	}

	@Override
	public String toString() {
		return "InOrderDesc [inOrderId=" + inOrderId + ", no=" + no
				+ ", supName=" + supName + ", allMoney=" + allMoney + "]";
	}
	@YtTableItemAnno(editor = true, itemText = "Whether there are more than 20 blocks", itemIndex = 4 , width = 50)
	public String getFlag() {
		if (allMoney > 20) {
			return "是";
		}
		return "否";
	}

}

 It can be seen that the current configuration is directly written in javabean, and does not need to be processed separately

 

And the way to generate a tableviewer, only need to write the following

 

	private void initTable2() {
		List<InOrderDesc> list = new ArrayList<InOrderDesc>();
		//create random data
		for(int i = 0 ; i < 10 ; i++){
			InOrderDesc gb = new InOrderDesc();
			gb.setInOrderId(i);
			gb.setNo(r.getEnRandom(5, 10));
			gb.setSupName(r.getChRandom(1, 3));
			gb.setAllMoney(Float.valueOf(r.getNumFixed(4))/100);
			list.add(gb);
		}
		//Initialize tableViewer
		YtCheckBoxTable tableViewer = YtCheckBoxTable.newSimpleTable(this, InOrderDesc.class);
		//Set the data display list
		tableViewer.setEoList(list);
		
		
	}

 

 The effect diagram is as follows:



 

The general approach is that when you create a bean, write the processing you need in an annotation, and you can generate a tableviewer. git code down

https://github.com/JavaRui/pers.crwu.swt.tableviewer.git

or download the attachment

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326079277&siteId=291194637