自定义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);
}
}
自定义SWT控件六之自定义Tab的更多相关文章
- 自定义SWT控件五之自定义穿梭框
5.自定义穿梭框 package com.view.control.shuttlebox; import java.util.ArrayList; import java.util.HashMap; ...
- 自定义SWT控件二之自定义多选下拉框
2.自定义下拉多选框 package com.view.control.select; import java.util.ArrayList; import java.util.HashMap; im ...
- 自定义SWT控件一之自定义单选下拉框
一.自定义下拉控件 自定义的下拉框,是自定义样式的,其中的下拉框使用的是独立的window,非复选框的下拉框双击单机其它区域或选择完之后,独立window构成的下拉框会自动消失. package co ...
- 自定义SWT控件七之自定义Shell(可伸缩窗口)
7.可伸缩窗口 该自定义窗口可以通过鼠标随意更改窗口大小 package com.hikvision.encapsulate.view.control.shell; import org.eclips ...
- 自定义SWT控件四之其它下拉框
4.其它下拉框 4.1 添加联动二级多选择框(有添加按钮和删除按钮) package com.view.control.select; import java.util.ArrayList; impo ...
- IOS开发--自定义segment控件,方便自定义样式
系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需 这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控 ...
- 自定义SWT控件三之搜索功能下拉框
3.搜索功能下拉弹出框 package com.view.control.select; import java.util.ArrayList; import java.util.LinkedList ...
- 6、手把手教你Extjs5(六)继承自定义一个控件
Extjs的开发都可以遵循OOP的原则,其对类的封装也很完善了.自定义一个控件最简单的办法就是继承一个已有的控件.根据上一节的需要,我做了一个Button的子类.首先根据目录结构,在app目录下建立一 ...
- 【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法
原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们 ...
随机推荐
- Redis 在java中的使用(登录验证,5分钟内连续输错3次密码,锁住帐号,半小时后解封)(三)
在java中使用redis,做简单的登录帐号的验证,使用string类型,使用redis的过期时间功能 1.首先进行redis的jar包的引用,因为用的是springBoot,springBoot集成 ...
- 思维导图xmind的使用方法
什么是Xmind Xmind是一款简单好用的思维导图软件,除了可以轻松绘制基本逻辑图,还支持组织结构图(竖直).树状图(水平+竖直).思维导图(辐射).鱼骨图.二维图(表格)模型.免费版可以把思维导图 ...
- HDU 1828:Picture(扫描线+线段树 矩形周长并)
题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...
- java中session和application的用法
Session的用法 首先创建2个jsp文件t1.jsp t2.jsp 在t1.jsp <% //设置session的键与值 session.setAttribute("abc&qu ...
- Python题整理
1.Python的多态.鸭子类型? 2.Python的序列化工具有哪些? 3.Python处理Excel的工具有哪些? 4.Python处理Graph的算法库有哪些? 5.Python的dict是基于 ...
- 接口文档注解:@ApiOperation
@ApiOperation不是spring自带的注解是swagger里的 com.wordnik.swagger.annotations.ApiOperation; @ApiOperation和@Ap ...
- windows快速创建文本文档的几个方法快捷键和
1. 在平常使用电脑中要经常用到在左面创建文本文档txt最笨重的方法就是右键但是这样非常的慢,有没有什么快捷键呢 2. 快捷键 快捷键就是Win+R ,键入notepad 然后回车就可以编辑了 是不是 ...
- Docker学习第二天
CentOS 系列安装 Docker Docker 支持 CentOS6 及以后的版本. CentOS6 对于 CentOS6,可以使用 EPEL 库安装 Docker,命令如下 [root@MSJT ...
- Java编程思想:通配符(后面有两个小节,研究的不够深入)
import java.util.*; public class Test { public static void main(String[] args) { } } /* 15.9 边界 要点: ...
- weblogic10.3.6重置/修改控制台账号密码
weblogic部署服务后由于交接过程中文档不完整导致有一个域的控制台账号密码遗失, 在此整理记录一下重置控制台账号密码的过程: 注:%DOMAIN_HOME%:指WebLogic Server 域( ...