Eclipse插件开发 swt ComboBoxCellEditor CCombo 下拉框高度
效果图:
代码如下
bindingPageTableViewer.setCellModifier(new ICellModifier() { public boolean canModify(Object element, String property) { IBindingPageModel bindingPageModel = (IBindingPageModel) element; if (property.equals(bindingPageColumnNameArray[3])) { String[] items = null; if (null == bindingPageModel.getBindingPage() || bindingPageModel.getBindingPage() instanceof IFormPageModel) { items = getPageNameArray(0); } else { items = getPageNameArray(1); } for(int i=0;i<items.length;i++){ System.out.println("items : " + items[i]); } cellEditors[3] = new ComboBoxCellEditor(table, items, SWT.READ_ONLY ); //下拉框高度设置,根据items数组除以2,进行设置 CCombo cc = (CCombo)cellEditors[3].getControl(); cc.setVisibleItemCount(items.length/2); } return true; } public Object getValue(Object element, String property) { IBindingPageModel model = (IBindingPageModel) element; if (property.equals(bindingPageColumnNameArray[2])) { if (null == model.getBindingPage() || model.getBindingPage() instanceof IFormPageModel) { return new Integer(0); } else { return new Integer(1); } } if (property.equals(bindingPageColumnNameArray[3])) { String[] pageItems = ((ComboBoxCellEditor) cellEditors[3]) .getItems(); System.out.println("cellEditors[3] : " + cellEditors[3]); String pageName = ""; if (null != model.getBindingPage()) { pageName = model.getBindingPage().getName(); System.out.println("pageName:" + pageName); } for (int i = 0, length = pageItems.length; i < length; i++) { if (pageItems[i].equals(pageName)) { return new Integer(i); } } return new Integer(0); } return null; } public void modify(Object element, String property, Object value) { IBindingPageModel model = null; IPageModel pageModel = null; if (element instanceof Item) model = (IBindingPageModel) ((Item) element).getData(); if (property.equals(bindingPageColumnNameArray[2])) { int pageTypeValue = ((Integer) value).intValue(); ComboBoxCellEditor pageCellEditor = (ComboBoxCellEditor) cellEditors[3]; String[] pageNameArray = getPageNameArray(pageTypeValue); pageCellEditor.setItems(pageNameArray); if (pageTypeValue == 0 && !(model.getBindingPage() instanceof IFormPageModel)) { if (pageNameArray.length > 0) { String pageName = pageNameArray[0]; pageModel = getFormModel().getPageByName(pageName); model.setBindingPage(pageModel); } else { model.setBindingPage(ModelFactory.getInstance() .newFormPageModel()); } } if (pageTypeValue == 1 && !(model.getBindingPage() instanceof IInterfacePageModel)) { if (pageNameArray.length > 0) { String pageName = pageNameArray[0]; pageModel = getInterfacePageByName(pageName); model.setBindingPage(pageModel); } else { model.setBindingPage(ModelFactory.getInstance() .newInterfacePageModel()); } } } if (property.equals(bindingPageColumnNameArray[3])) { int pageValue = ((Integer) value).intValue(); String[] cellItems = ((ComboBoxCellEditor) cellEditors[3]) .getItems(); String pageName = null; if (pageValue >= 0 && cellItems.length > 0) { pageName = cellItems[pageValue]; System.out.println("pageName: " + pageName); } if (model.getBindingPage() instanceof IFormPageModel) { pageModel = getFormModel().getPageByName(pageName); if (pageModel == null || pageModel.equals(model.getBindingPage())) { pageModel = null; } else { model.setBindingPage(pageModel); } } else if (model.getBindingPage() instanceof IInterfacePageModel) { pageModel = getInterfacePageByName(pageName); if (pageModel == null || pageModel.equals(model.getBindingPage())) { pageModel = null; } else { model.setBindingPage(pageModel); } } } if (null != pageModel) { model.getBindingParameterList().clear(); model.getBindingOperationList().clear(); } bindingPageTableViewer.refresh(); updatePageStates(model); } });
核心思想:
CellEditor cellEditor = new ComboBoxCellEditor(table, items,SWT.READ_ONLY);
//拿到control,进行高度设置即可,如果直接使用的是CCombo下拉框,上面那行代码都不用要,就可以设置下来高度。
CCombo cc = (CCombo)cellEditor.getControl();
cc.setVisibleItemCount(items.length/2);
落雨 ae6623
qq 394263788