自定义SWT控件六之自定义Tab

6、自定义tab

本章节提供的自定义tab 分为两类

  • tab上带删除按钮和添加按钮,可删除tab和添加tab
  • tab不可删除和添加

6.1 不可删除tab

package com.view.control.tab;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener; import com.util.StringUtil;
import com.view.control.DetectorNode;
import com.view.control.TreeNodeData;
import com.view.core.valid.dto.Detector;
import com.view.swt.SWTCommonUtil;
import com.view.swt.SWTResourceManager;
import com.view.swt.SWTUtil;
import com.view.swt.SWTUtil.BorderLine; /**
* <p>tabNode结点</p>
* @version V1.0
*/
public class TabNode extends DetectorNode {
/*** 序列化ID*/
private static final long serialVersionUID = 1L;
/*** 结点对应的数据 *****/
protected TreeNodeData nodeData;
/****所属tab控件***/
protected Tab tab;
protected Composite parent;
protected Composite nodeComposite;
/****需要更换的内容区域****/
protected Composite contentComposite; public TabNode(Composite parent, TreeNodeData nodeData,Tab tab,Composite contentComposite) {
this.parent = parent;
this.nodeData = nodeData;
this.tab = tab;
this.contentComposite = contentComposite;
} public void paint(){
nodeComposite = new Composite(parent,SWT.NONE);
nodeComposite.setBackground(SWTResourceManager.getWhiteColor());
GridData gd_nodeComposite = new GridData(SWT.FILL,SWT.FILL,false,true,1,1);
nodeComposite.setLayoutData(gd_nodeComposite);
GridLayout gl_nodeComposite = new GridLayout(1,true);
gl_nodeComposite.horizontalSpacing = 0;
gl_nodeComposite.verticalSpacing = 0;
gl_nodeComposite.marginWidth = 10;
gl_nodeComposite.marginHeight = 1;
nodeComposite.setLayout(gl_nodeComposite);
SWTUtil.paintBorder(nodeComposite, SWTResourceManager.getLightGreyBorderColor(), BorderLine.TOP,BorderLine.LEFT,BorderLine.BOTTOM);
nodeComposite.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND)); CLabel tab_content_label= generateNodeContentComposite();
tab_content_label.setData("data", nodeData);
this.menuCode = nodeData.getMenuCode(); nodeComposite.addListener(SWT.MouseDown, new Listener(){
@Override
public void handleEvent(Event event) {
Event newEvent = new Event();
newEvent.button = event.button;
newEvent.widget = tab_content_label;
tab_content_label.notifyListeners(SWT.MouseDown, newEvent);
}
});
nodeComposite.setData("nodeLabel", tab_content_label);
nodeComposite.layout(true);
} /**
* 生成tab区域内的内容
* @return
*/
protected CLabel generateNodeContentComposite(){
CLabel tab_content_label = new CLabel(nodeComposite,SWT.NONE);
tab_content_label.setBackground(SWTResourceManager.getWhiteColor());
GridData gd_tab_content_label = new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
tab_content_label.setLayoutData(gd_tab_content_label);
tab_content_label.setForeground(SWTResourceManager.getTextColor());
tab_content_label.setText(nodeData.getLabel());
tab_content_label.setAlignment(SWT.CENTER);
tab_content_label.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
tab_content_label.addListener(SWT.MouseDown, new Listener(){
@Override
public void handleEvent(Event event) {
if(event.button == 1){
changeTabContentPage();
}
}
});
return tab_content_label;
} /**
* 更换内容区域
* @param tabContentPage
*/
private void changeTabContentPage(){
if(StringUtil.isNotNullAndEmpty(this.nodeData.getActionPageName())){
TabNodeParam tabNodeParam = new TabNodeParam(this.nodeData.getNodeData(),this);
SWTCommonUtil.generatorPage(contentComposite, this.nodeData.getActionPageName(), tabNodeParam);
}
} @Override
public void updateErrorStatusImg(Detector detector) { } /**
* 获取结点nodeComposite
* @return
*/
public Composite getNodeComposite() {
return nodeComposite;
} public Tab getTab() {
return tab;
} /**
* 获取nodeLabel
* @return
*/
public CLabel getNodeLabel(){
if(null != nodeComposite && !nodeComposite.isDisposed()){
return (CLabel)nodeComposite.getData("nodeLabel");
}
return null;
} /***
* 触发结点事件*/
public void notifyNodeEvent(){
Event event = new Event();
event.button = 1;
event.widget = nodeComposite;
nodeComposite.notifyListeners(SWT.MouseDown, event);
} public TreeNodeData getNodeData() {
return nodeData;
} public Composite getParent() {
return parent;
}
}

TabNode.java 为tab结点。

package com.view.control.tab;

import com.view.core.param.Param;

/**
* <p>tab结点包含的特有参数</p>
* @version V1.0
*/
public class TabNodeParam extends Param { /*** 序列化ID*/
private static final long serialVersionUID = 1L;
/***tab结点上本来绑定的参数***/
private Param param;
/***tabNode结点****/
private TabNode tabNode; public TabNodeParam(Param param,TabNode tabNode){
this.param = param;
this.tabNode = tabNode;
} public Param getParam() {
return param;
} public void setParam(Param param) {
this.param = param;
} public TabNode getTabNode() {
return tabNode;
} public void setTabNode(TabNode tabNode) {
this.tabNode = tabNode;
}
}

TabNodeParam.java 为TabNode中额外绑定的参数。

package com.view.control.tab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List; import org.apache.commons.collections.map.MultiValueMap;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell; import com.util.CollectionUtil;
import com.view.control.TreeNodeData;
import com.view.swt.SWTResourceManager;
import com.view.swt.SWTUtil;
import com.view.swt.SWTUtil.BorderLine; /**
* <p>tab控件(默认打开第一个tab页)</p>
* @version V1.0
*/
public class Tab {
/***采用gridLayout布局**/
protected Composite parent;
/***所有的根孩子节点数据*******/
protected List<TreeNodeData> allNodeDataList = new ArrayList<TreeNodeData>();
protected List<TabNode> tabNodeList = new LinkedList<TabNode>();
/***tab高度***/
protected int tabHeight = 25;
protected Composite tabComposite;
/***选中的tab页***/
protected TabNode chooseTab;
/***适用于所有tab结点监听器*****/
protected MultiValueMap nodeListener = MultiValueMap.decorate(new HashMap<Integer,Listener>());
protected Composite content_composite;
protected boolean hasEmpty = true;
/***tab内容区域是否出现滚动条***/
protected boolean hasScroll = true; public Tab(Composite parent,List<TreeNodeData> allNodeDataList){
this.parent = parent;
this.allNodeDataList = allNodeDataList;
} public void paint(){
tabComposite = new Composite(parent,SWT.NONE);
GridData gd_top_tab_composite = new GridData(SWT.FILL,SWT.FILL,true,false,1,1);
gd_top_tab_composite.heightHint = this.tabHeight;
tabComposite.setLayoutData(gd_top_tab_composite);
tabComposite.setBackground(SWTResourceManager.getWhiteColor());
GridLayout gl_top_tab_composite = new GridLayout(hasEmpty?allNodeDataList.size()+1:allNodeDataList.size(),false);
gl_top_tab_composite.horizontalSpacing = 0;
gl_top_tab_composite.verticalSpacing = 0;
gl_top_tab_composite.marginWidth = 0;
gl_top_tab_composite.marginHeight = 0;
tabComposite.setLayout(gl_top_tab_composite); if(hasScroll){
ScrolledComposite scroll_content_composite = new ScrolledComposite(parent, SWT.V_SCROLL|SWT.H_SCROLL);
GridData gd_scroll_content_composite = new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
scroll_content_composite.setLayoutData(gd_scroll_content_composite);
scroll_content_composite.setExpandHorizontal(true);
scroll_content_composite.setExpandVertical(true);
scroll_content_composite.setData("shink", true); content_composite = new Composite(scroll_content_composite, SWT.NONE);
scroll_content_composite.setContent(content_composite);
}else{
content_composite = new Composite(parent, SWT.NONE);
GridData gd_content_composite = new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
content_composite.setLayoutData(gd_content_composite);
} content_composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
generateToTabComposite(); content_composite.layout(true);
parent.layout(true); /***默认展开第一个***/
if(CollectionUtil.isNotEmpty(tabNodeList)){
tabNodeList.get(0).notifyNodeEvent();
}
} /**
* 绘制顶部tab选项页
* @param contentComposite
* @return 返回第一个tab页标签
*/
protected void generateToTabComposite(){
if(CollectionUtil.isNotEmpty(allNodeDataList)){
for(TreeNodeData nodeData:allNodeDataList){
tabNodeList.add(createTab(nodeData));
}
}
if(hasEmpty){
Composite empty_composite = new Composite(tabComposite,SWT.NONE);
empty_composite.setBackground(SWTResourceManager.getWhiteColor());
GridData gd_empty_composite = new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
empty_composite.setLayoutData(gd_empty_composite);
SWTUtil.paintBorder(empty_composite, SWTResourceManager.getColor(229,229,229),BorderLine.BOTTOM,BorderLine.LEFT);
}
tabComposite.layout(true);
} /**
* 创建一个tab
* @param top_tab_composite
* @return
*/
@SuppressWarnings("unchecked")
protected TabNode createTab(TreeNodeData treeNodeData){
TabNode tabNode = new TabNode(tabComposite,treeNodeData,this,content_composite);
tabNode.paint();
CLabel nodeLabel = tabNode.getNodeLabel();
if(null != nodeLabel){
//添加选中样式和非选中样式
nodeLabel.addListener(SWT.MouseDown, new Listener(){
@Override
public void handleEvent(Event event) {
if(event.button == 1){
if(chooseTab == tabNode){
return;
}
if(chooseTab != null){
changeTabToUnChoose(chooseTab);
}
changeTabToChoose(tabNode);
chooseTab = tabNode;
}
}
});
if(CollectionUtil.isNotMultiEmpty(nodeListener)){
Iterator<Integer> it = nodeListener.keySet().iterator();
Integer mouseType;
Listener listener = null;
while(it.hasNext()){
mouseType = it.next();
if(CollectionUtil.isNotEmpty(nodeListener.getCollection(mouseType))){
for(Object object:nodeListener.getCollection(mouseType)){
listener = (Listener)object;
nodeLabel.addListener(mouseType, listener);
}
}
}
}
}
return tabNode;
} /**
* 将tab更改成未选中状态
* @param tab_content_label
*/
protected void changeTabToUnChoose(TabNode chooseTab){
CLabel nodeLabel = chooseTab.getNodeLabel();
if(null != nodeLabel){
nodeLabel.setForeground(SWTResourceManager.getTextColor());
PaintListener paintListener = (PaintListener)nodeLabel.getParent().getData("paintListener");
if(null != paintListener){
nodeLabel.getParent().removePaintListener(paintListener);
}
SWTUtil.paintBorder(nodeLabel.getParent(), SWTResourceManager.getColor(229,229,229),BorderLine.LEFT,BorderLine.TOP);
SWTUtil.paintBorder(nodeLabel.getParent(), SWTResourceManager.getColor(229,229,229),BorderLine.BOTTOM);
nodeLabel.getParent().redraw();
}
} /**
* 将tab更改成选中状态
* @param tab_content_label
*/
protected void changeTabToChoose(TabNode chooseTab){
CLabel nodeLabel = chooseTab.getNodeLabel();
if(null != nodeLabel){
nodeLabel.setForeground(SWTResourceManager.getColor(83,157,249));
PaintListener paintListener = (PaintListener)nodeLabel.getParent().getData("paintListener");
if(null != paintListener){
nodeLabel.getParent().removePaintListener(paintListener);
}
SWTUtil.paintBorder(nodeLabel.getParent(), SWTResourceManager.getColor(229,229,229),BorderLine.LEFT,BorderLine.TOP);
SWTUtil.paintBorder(nodeLabel.getParent(), SWTResourceManager.getWhiteColor(),BorderLine.BOTTOM);
nodeLabel.getParent().redraw();
}
} public int getTabHeight() {
return tabHeight;
} public void setTabHeight(int tabHeight) {
this.tabHeight = tabHeight;
} /**
* 添加结点监听器
* @param listener
*/
public void addListener(int mouseEventType,Listener listener){
nodeListener.put(mouseEventType, listener);
} public boolean isHasEmpty() {
return hasEmpty;
} /**
* 当tab标签的数量不能够完全撑住一行时,是否剩下的用一个空tab来做支撑
* @param hasEmpty
*/
public void setHasEmpty(boolean hasEmpty) {
this.hasEmpty = hasEmpty;
} /**
* 所有的重新绘制
* @param composite
*/
protected void reLayoutAllParent(Composite composite) {
Composite contentComposite = composite;
while(contentComposite != this.parent){
contentComposite.layout(true);
contentComposite = contentComposite.getParent();
}
contentComposite.layout(true);
Composite parentComposite = contentComposite.getParent();
while(!(parentComposite instanceof ScrolledComposite) && !(parentComposite instanceof Shell)){
parentComposite.layout(true);
contentComposite = parentComposite;
parentComposite = parentComposite.getParent();
}
if(parentComposite instanceof ScrolledComposite){
((ScrolledComposite)parentComposite).setMinSize(contentComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
} public boolean isHasScroll() {
return hasScroll;
} /**
* 设置内容区域是否需要滚动条
* @param hasScroll
*/
public void setHasScroll(boolean hasScroll) {
this.hasScroll = hasScroll;
}
}

6.2 可删除和添加tab

package com.view.control.tab;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener; import com.view.control.TreeNodeData;
import com.view.control.tab.OptTab.OptTabEvent;
import com.view.swt.CustomMessageBox;
import com.view.swt.SWTResourceManager;
import com.view.swt.CustomMessageBox.Result; public class OptTabNode extends TabNode {
private OptTabEvent optTabEvent;
private OptTabNode optTabNode; /** * 序列化ID */
private static final long serialVersionUID = 1L; public OptTabNode(Composite parent, TreeNodeData nodeData, OptTab tab, Composite contentComposite) {
super(parent, nodeData, tab, contentComposite);
} @Override
protected CLabel generateNodeContentComposite(){
CLabel tab_content_label;
optTabNode = this;
if(optTabEvent != null){
GridLayout gl_nodeComposite = new GridLayout(2,false);
gl_nodeComposite.horizontalSpacing = 0;
gl_nodeComposite.verticalSpacing = 0;
gl_nodeComposite.marginWidth = 10;
gl_nodeComposite.marginHeight = 1;
nodeComposite.setLayout(gl_nodeComposite); tab_content_label = super.generateNodeContentComposite(); CLabel deleteLabel = new CLabel(nodeComposite, SWT.NONE);
GridData gd_deleteLabel = new GridData(SWT.TOP,SWT.CENTER,true,true,1,1);
deleteLabel.setLayoutData(gd_deleteLabel);
deleteLabel.setText("X");
deleteLabel.setAlignment(SWT.RIGHT);
deleteLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
deleteLabel.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
deleteLabel.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
if (new CustomMessageBox(deleteLabel.getShell(),"确认要删除?").open() == Result.YES.getResult()) {
//删除一个tab
((OptTab)tab).delTab(optTabNode);
}
}
});
}else{
tab_content_label = super.generateNodeContentComposite();
}
return tab_content_label;
} public OptTabEvent getOptTabEvent() {
return optTabEvent;
} public void setOptTabEvent(OptTabEvent optTabEvent) {
this.optTabEvent = optTabEvent;
}
}
package com.view.control.tab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import log.Message; import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSON;
import com.util.CollectionUtil;
import com.util.StringUtil;
import com.view.control.DefindFormAddCombo;
import com.view.control.TreeNodeData;
import com.view.control.select.DropDownBox;
import com.view.swt.SWTCommonUtil;
import com.view.swt.SWTResourceManager;
import com.view.swt.SWTUtil;
import com.view.swt.SWTUtil.BorderLine; /**
* <p>可删除和新增tab控件</p>
* @version V1.0
*/
public class OptTab extends Tab {
private static final Logger log = LoggerFactory.getLogger(OptTab.class);
private DefindFormAddCombo addCombo;
private List<DropDownBox.Data> comboDataList;
private Map<DropDownBox.Data,TreeNodeData> allNodeDataMap;
private List<DropDownBox.Data> defaultValueList;
/****添加combo显示名称****/
private String addComboLabelName;
/****combo文本框的宽度****/
private int comboLabelWidth = 24;
/*****设置显示名称控件的宽度*****/
private int nameWidth = 50;
private OptTabEvent optTabEvent;
/****占空间*****/
private List<Composite> tabNodeAllComposite = new LinkedList<Composite>(); public OptTab(Composite parent, List<TreeNodeData> allNodeDataList,String addComboLabelName) {
super(parent, allNodeDataList);
if(CollectionUtil.isNotEmpty(allNodeDataList)){
comboDataList = new ArrayList<DropDownBox.Data>(allNodeDataList.size());
allNodeDataMap = new HashMap<>(allNodeDataList.size());
DropDownBox.Data data = null;
for(TreeNodeData treeNodeData:allNodeDataList){
if(StringUtil.isNotNullAndEmpty(treeNodeData.getLabel()) && StringUtil.isNotNullAndEmpty(treeNodeData.getMenuCode())){
data = new DropDownBox.Data(treeNodeData.getLabel(),treeNodeData.getMenuCode());
allNodeDataMap.put(data, treeNodeData);
comboDataList.add(data);
}else{
log.trace(HikLog.toLog(new Message("has no label or menuCode","label","menuCode")),treeNodeData.getLabel(),treeNodeData.getMenuCode());
}
}
}
this.addComboLabelName = addComboLabelName;
} /**
* 创建结点占位符
* @param parentComposite 相对布局
* @return
*/
private Composite createTabNodePlaceHolder(Composite parentComposite){
Composite nodePlaceHolderComposite = new Composite(parentComposite,SWT.NONE);
nodePlaceHolderComposite.setBackground(SWTResourceManager.getWhiteColor());
GridData gd_nodePlaceHolderComposite = new GridData(SWT.FILL,SWT.FILL,false,true,1,1);
gd_nodePlaceHolderComposite.exclude = true;
nodePlaceHolderComposite.setLayoutData(gd_nodePlaceHolderComposite);
GridLayout gl_nodePlaceHolderComposite = new GridLayout(1,true);
gl_nodePlaceHolderComposite.horizontalSpacing = 0;
gl_nodePlaceHolderComposite.verticalSpacing = 0;
gl_nodePlaceHolderComposite.marginWidth = 0;
gl_nodePlaceHolderComposite.marginHeight = 0;
nodePlaceHolderComposite.setLayout(gl_nodePlaceHolderComposite);
nodePlaceHolderComposite.setVisible(false);
tabNodeAllComposite.add(nodePlaceHolderComposite);
return nodePlaceHolderComposite;
} @Override
protected void generateToTabComposite(){
GridLayout gl_top_tab_composite = new GridLayout(hasEmpty?allNodeDataList.size()+2:allNodeDataList.size()+1,false);
gl_top_tab_composite.horizontalSpacing = 0;
gl_top_tab_composite.verticalSpacing = 0;
gl_top_tab_composite.marginWidth = 0;
gl_top_tab_composite.marginHeight = 0;
tabComposite.setLayout(gl_top_tab_composite); //若有默认值,则创建默认tab
if(CollectionUtil.isNotEmpty(defaultValueList)){
for(DropDownBox.Data defaultValue:defaultValueList){
if(allNodeDataMap.containsKey(defaultValue)){
tabNodeList.add(createTab(allNodeDataMap.get(defaultValue)));
}else{
log.error(HikLog.toLog("", "defaultValue is not in allNodeDataList", "defaultValue","allNodeDataList"),JSON.toJSONString(defaultValue),JSON.toJSONString(allNodeDataList));
}
}
comboDataList.removeAll(defaultValueList);
}
//预占用tabCompoiste的tab
if(CollectionUtil.isNotEmpty(comboDataList)){
for(int i = 0;i<comboDataList.size();i++){
createTabNodePlaceHolder(tabComposite);
}
}
Composite addComboComposite = new Composite(tabComposite,SWT.NONE);
addComboComposite.setBackground(SWTResourceManager.getWhiteColor());
GridData gd_addComboComposite = new GridData(SWT.FILL,SWT.FILL,false,true,1,1);
addComboComposite.setLayoutData(gd_addComboComposite);
GridLayout gl_addComboComposite = new GridLayout(1,true);
gl_addComboComposite.horizontalSpacing = 0;
gl_addComboComposite.verticalSpacing = 0;
gl_addComboComposite.marginWidth = 10;
gl_addComboComposite.marginHeight = 1;
addComboComposite.setLayout(gl_addComboComposite);
SWTUtil.paintBorder(addComboComposite, SWTResourceManager.getColor(229,229,229)); addCombo = new DefindFormAddCombo(addComboComposite, addComboLabelName, comboDataList, comboLabelWidth, nameWidth, this.tabHeight-5);
addCombo.setNameShowInComboLabel(true);
addCombo.addSelectListener(new Listener() {
@Override
public void handleEvent(Event event) {
CLabel itemLabel = (CLabel)event.widget;
DropDownBox.Data data = (DropDownBox.Data)itemLabel.getData("value");
addTab(allNodeDataMap.get(data));
}
});
addCombo.setComboRowWidth(100);
addCombo.paint(); if(hasEmpty){
Composite empty_composite = new Composite(tabComposite,SWT.NONE);
empty_composite.setBackground(SWTResourceManager.getWhiteColor());
GridData gd_empty_composite = new GridData(SWT.FILL,SWT.FILL,true,true,1,1);
empty_composite.setLayoutData(gd_empty_composite);
SWTUtil.paintBorder(empty_composite, SWTResourceManager.getColor(229,229,229),BorderLine.BOTTOM);
}
addComboComposite.layout(true);
tabComposite.layout(true);
SWTUtil.paintBorder(this.content_composite,SWTResourceManager.getLightGreyBorderColor(),BorderLine.LEFT,BorderLine.RIGHT,BorderLine.BOTTOM);
} /**
* 查找可用的tab
* @return
*/
private Composite findAvailablePlaceHolderComposite(){
if(CollectionUtil.isNotEmpty(tabNodeAllComposite)){
GridData gd_placeHolderComposite = null;
Composite availabelComposite = null;
for(Composite placeHolderComposite:tabNodeAllComposite){
gd_placeHolderComposite = (GridData)placeHolderComposite.getLayoutData();
if(gd_placeHolderComposite.exclude){
availabelComposite = placeHolderComposite;
gd_placeHolderComposite.exclude = false;
placeHolderComposite.setVisible(true);
break;
}
}
return availabelComposite;
}
return null;
} /**
* 创建一个tab
* @param top_tab_composite
* @return
*/
@SuppressWarnings("unchecked")
protected TabNode createTab(TreeNodeData treeNodeData){
Composite placeHolderComposite = findAvailablePlaceHolderComposite();
if(null == placeHolderComposite){
placeHolderComposite = createTabNodePlaceHolder(tabComposite);
GridData gd_placeHolderComposite = (GridData)placeHolderComposite.getLayoutData();
gd_placeHolderComposite.exclude = false;
placeHolderComposite.setVisible(true);
}
OptTabNode tabNode = new OptTabNode(placeHolderComposite,treeNodeData,this,content_composite);
tabNode.setOptTabEvent(optTabEvent);
tabNode.paint();
placeHolderComposite.layout(true);
placeHolderComposite.getParent().layout(true);
CLabel nodeLabel = tabNode.getNodeLabel();
if(null != nodeLabel){
//添加选中样式和非选中样式
nodeLabel.addListener(SWT.MouseDown, new Listener(){
@Override
public void handleEvent(Event event) {
if(event.button == 1){
if(chooseTab == tabNode){
return;
}
if(chooseTab != null){
changeTabToUnChoose(chooseTab);
}
changeTabToChoose(tabNode);
chooseTab = tabNode;
}
}
});
if(CollectionUtil.isNotMultiEmpty(nodeListener)){
Iterator<Integer> it = nodeListener.keySet().iterator();
Integer mouseType;
Listener listener = null;
while(it.hasNext()){
mouseType = it.next();
if(CollectionUtil.isNotEmpty(nodeListener.getCollection(mouseType))){
for(Object object:nodeListener.getCollection(mouseType)){
listener = (Listener)object;
nodeLabel.addListener(mouseType, listener);
}
}
}
}
}
return tabNode;
} /**
* 删除tab
* @param tabNode
* @return
*/
public boolean delTab(OptTabNode tabNode){
if(optTabEvent != null && !optTabEvent.delete(tabNode.getNodeData())){
return false;
}
tabNode.getNodeComposite().dispose();
TreeNodeData treeNode = tabNode.getNodeData();
DropDownBox.Data data = new DropDownBox.Data(treeNode.getLabel(),treeNode.getMenuCode());
addCombo.addData(data);
//默认展开下一个
int index = tabNodeList.indexOf(tabNode);
if(index < tabNodeList.size()-1){
tabNodeList.get(index+1).notifyNodeEvent();
}else if(index == tabNodeList.size()-1 && tabNodeList.size()>1){
tabNodeList.get(0).notifyNodeEvent();
}else{
for(Control child:content_composite.getChildren()){
child.dispose();
}
reLayoutContentComposite();
}
tabNodeList.remove(tabNode);
Composite placeHolderComposite = tabNode.getParent();
GridData gd_placeHolderComposite = (GridData)placeHolderComposite.getLayoutData();
gd_placeHolderComposite.exclude = true;
placeHolderComposite.setVisible(false);
placeHolderComposite.getParent().layout(true);
return true;
} /**
* 添加tab
* @param nodeData
* @return
*/
public boolean addTab(TreeNodeData nodeData){
if(nodeData == null){
return false;
}
if(optTabEvent != null && !optTabEvent.add(nodeData)){
return false;
}
//创建tab
TabNode tabNode = createTab(nodeData);
tabNodeList.add(tabNode);
tabNode.notifyNodeEvent();
DropDownBox.Data data = new DropDownBox.Data(nodeData.getLabel(),nodeData.getMenuCode());
addCombo.removeData(data);
return true;
} /**
* 重新绘制content区域*/
private void reLayoutContentComposite(){
Set<ScrolledComposite> scrolledCompositeSet = new HashSet<>();
SWTCommonUtil.validScrollComposite(content_composite, scrolledCompositeSet);
if(content_composite.getParent() instanceof ScrolledComposite){
ScrolledComposite scroll_content_composite = (ScrolledComposite)content_composite.getParent();
if(CollectionUtil.isEmpty(scrolledCompositeSet)){
scroll_content_composite.setMinSize(content_composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}else{
scroll_content_composite.setMinSize(0,0);
}
}else{
reLayoutAllParent(content_composite);
}
} /**
* 设置默认tab值,这个 defaultValueList 必须在 allNodeDataList中,否则创建不出来对应的tab
* @param defaultValueList
*/
public void setDefaultValueList(List<DropDownBox.Data> defaultValueList) {
this.defaultValueList = defaultValueList;
} public void setComboLabelWidth(int comboLabelWidth) {
this.comboLabelWidth = comboLabelWidth;
} public void setNameWidth(int nameWidth) {
this.nameWidth = nameWidth;
} public void setOptTabEvent(OptTabEvent optTabEvent) {
this.optTabEvent = optTabEvent;
} /**
*
* <p>删除一个tab发生的事件</p>
* @version V1.0
*/
public interface OptTabEvent{
/**
* 删除一个tab发生的事件
* @param nodeData
* @return
*/
boolean delete(TreeNodeData nodeData);
/***
* 添加一个tab发生的事件
* @param nodeData
* @return
*/
boolean add(TreeNodeData nodeData);
}
}
上一篇:Bash的自动补全


下一篇:记一次按需加载和npm模块发布实践