/**
* 文件名:ComboBoxDialogCellEditor.java
*
* 版本信息:
* 日期:2010-7-19
* Copyright huawei Corporation 2010
* 版权所有
*
*/
package com.huawei.adt;
import java.text.MessageFormat;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
public abstract class ComboBoxDialogCellEditor extends CellEditor
{
// Combo Items
private String[] items;
private Composite editor;
private CCombo comboBox;
private Control contents;
private Button button;
private FocusListener buttonFocusListener;
private ModifyListener modifyListener;
private Object value = null;
/**
* Internal class for laying out the dialog.
*/
private class DialogCellLayout extends Layout
{
@Override
public void layout(Composite editor, boolean force)
{
Rectangle bounds = editor.getClientArea();
Point size = ComboBoxDialogCellEditor.this.button.computeSize(
SWT.DEFAULT, SWT.DEFAULT, force);
if (ComboBoxDialogCellEditor.this.contents != null)
{
ComboBoxDialogCellEditor.this.contents.setBounds(0, 0,
bounds.width - size.x, bounds.height);
}
ComboBoxDialogCellEditor.this.button.setBounds(bounds.width
- size.x, 0, size.x, bounds.height);
}
@Override
public Point computeSize(Composite editor, int wHint, int hHint,
boolean force)
{
if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT)
{
return new Point(wHint, hHint);
}
Point contentsSize = ComboBoxDialogCellEditor.this.contents
.computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
Point buttonSize = ComboBoxDialogCellEditor.this.button
.computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
// Just return the button width to ensure the button is not clipped
// if the label is long.
// The label will just use whatever extra width there is
Point result = new Point(buttonSize.x, Math.max(contentsSize.y,
buttonSize.y));
return result;
}
}
// Combo default style
private static final int defaultStyle = SWT.NONE;
public ComboBoxDialogCellEditor()
{
setStyle(defaultStyle);
}
public ComboBoxDialogCellEditor(Composite parent, String[] items)
{
this(parent, items, defaultStyle);
}
public ComboBoxDialogCellEditor(Composite parent, String[] items, int style)
{
super(parent, style);
setItems(items);
}
public String[] getItems()
{
return this.items;
}
public void setItems(String[] items)
{
Assert.isNotNull(items);
this.items = items;
populateComboBoxItems();
}
protected Button createButton(Composite parent)
{
Button result = new Button(parent, SWT.DOWN);
result.setText(""); //$NON-NLS-1$
return result;
}
protected Control createContents(Composite cell)
{
this.comboBox = new CCombo(cell, getStyle());
this.comboBox.setFont(cell.getFont());
populateComboBoxItems();
this.comboBox.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
keyReleaseOccured(e);
}
});
this.comboBox.addModifyListener(getModifyListener());
this.comboBox.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetDefaultSelected(SelectionEvent event)
{
applyEditorValueAndDeactivate();
}
@Override
public void widgetSelected(SelectionEvent event)
{
ComboBoxDialogCellEditor.this.value = ComboBoxDialogCellEditor.this.comboBox.getText();
}
});
this.comboBox.addTraverseListener(new TraverseListener()
{
public void keyTraversed(TraverseEvent e)
{
if (e.detail == SWT.TRAVERSE_ESCAPE
|| e.detail == SWT.TRAVERSE_RETURN)
{
e.doit = false;
}
}
});
this.comboBox.addFocusListener(new FocusAdapter()
{
@Override
public void focusLost(FocusEvent e)
{
if (!ComboBoxDialogCellEditor.this.button.isFocusControl())
{
ComboBoxDialogCellEditor.this.focusLost();
}
}
});
return this.comboBox;
}
@Override
protected Control createControl(Composite parent)
{
Font font = parent.getFont();
Color bg = parent.getBackground();
this.editor = new Composite(parent, getStyle());
this.editor.setFont(font);
this.editor.setBackground(bg);
this.editor.setLayout(new DialogCellLayout());
this.contents = createContents(this.editor);
updateContents(this.value);
this.button = createButton(this.editor);
this.button.setFont(font);
this.button.addKeyListener(new KeyAdapter()
{
@Override
public void keyReleased(KeyEvent e)
{
if (e.character == '\u001b')
{ // Escape
fireCancelEditor();
}
}
});
this.button.addFocusListener(getButtonFocusListener());
this.button.addSelectionListener(new SelectionAdapter()
{
@Override
public void widgetSelected(SelectionEvent event)
{
ComboBoxDialogCellEditor.this.button.removeFocusListener(getButtonFocusListener());
Object newValue = openDialogBox(ComboBoxDialogCellEditor.this.editor);
ComboBoxDialogCellEditor.this.button.addFocusListener(getButtonFocusListener());
if (newValue != null)
{
boolean newValidState = isCorrect(newValue);
if (newValidState)
{
markDirty();
doSetValue(newValue);
} else
{
setErrorMessage(MessageFormat.format(getErrorMessage(),
new Object[]
{ newValue.toString() }));
}
fireApplyEditorValue();
}
}
});
setValueValid(true);
return this.editor;
}
@Override
public void deactivate()
{
if (this.button != null && !this.button.isDisposed())
{
this.button.removeFocusListener(getButtonFocusListener());
}
super.deactivate();
}
@Override
protected Object doGetValue()
{
return this.value;
}
@Override
protected void doSetFocus()
{
if (this.comboBox != null)
{
this.comboBox.setFocus();
}
}
@Override
protected void doSetValue(Object value)
{
this.value = value;
updateContents(value);
}
private void populateComboBoxItems()
{
if (this.comboBox != null && this.items != null)
{
this.comboBox.removeAll();
for (int i = 0; i < this.items.length; i++)
{
this.comboBox.add(this.items[i], i);
}
this.comboBox.setText("");
}
}
void applyEditorValueAndDeactivate()
{
Object newValue = this.comboBox.getText();
if (newValue != null && !newValue.equals(this.value.toString()))
{
boolean newValidState = isCorrect(newValue);
if (newValidState)
{
markDirty();
doSetValue(newValue);
} else
{
setErrorMessage(MessageFormat.format(getErrorMessage(),
new Object[]
{ newValue.toString() }));
}
}
fireApplyEditorValue();
deactivate();
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.CellEditor#focusLost()
*/
@Override
protected void focusLost()
{
if (isActivated())
{
applyEditorValueAndDeactivate();
}
}
/*
* (non-Javadoc)
* @see org.eclipse.jface.viewers.CellEditor#keyReleaseOccured(org.eclipse.swt.events.KeyEvent)
*/
@Override
protected void keyReleaseOccured(KeyEvent keyEvent)
{
if (keyEvent.character == '\r')
{ // Return key
if (this.comboBox != null && !this.comboBox.isDisposed())
{
fireCancelEditor();
}
} else if (keyEvent.character == '\t')
{ // tab key
applyEditorValueAndDeactivate();
}
}
protected void editOccured(ModifyEvent e)
{
String value = this.comboBox.getText();
if (value == null)
{
value = "";//$NON-NLS-1$
}
Object typedValue = value;
boolean oldValidState = isValueValid();
boolean newValidState = isCorrect(typedValue);
if (typedValue == null && newValidState)
{
Assert.isTrue(false,
"Validator isn't limiting the cell editor's type range");//$NON-NLS-1$
}
if (!newValidState)
{
// try to insert the current value into the error message.
setErrorMessage(MessageFormat.format(getErrorMessage(),
new Object[]
{ value }));
}
valueChanged(oldValidState, newValidState);
}
private ModifyListener getModifyListener()
{
if (this.modifyListener == null)
{
this.modifyListener = new ModifyListener()
{
public void modifyText(ModifyEvent e)
{
editOccured(e);
}
};
}
return this.modifyListener;
}
private FocusListener getButtonFocusListener()
{
if (this.buttonFocusListener == null)
{
this.buttonFocusListener = new FocusListener()
{
public void focusGained(FocusEvent e)
{
};
public void focusLost(FocusEvent e)
{
ComboBoxDialogCellEditor.this.focusLost();
}
};
}
;
return this.buttonFocusListener;
}
private void updateContents(Object value)
{
Assert.isTrue(this.comboBox != null);
if (value != null && value instanceof String)
{
this.comboBox.removeModifyListener(getModifyListener());
this.comboBox.setText((String) value);
this.comboBox.addModifyListener(getModifyListener());
}
}
protected abstract Object openDialogBox(Control cellEditorWindow);
}
http://wiki.eclipse.org/index.php/JFaceSnippets
分享到:
相关推荐
基于springboot大学生就业信息管理系统源码数据库文档.zip
基于java的驾校收支管理可视化平台的开题报告
时间序列 原木 间隔5秒钟 20241120
毕业设计&课设_基于 Vue 的电影在线预订与管理系统:后台 Java(SSM)代码,为毕业设计项目.zip
基于springboot课件通中小学教学课件共享平台源码数据库文档.zip
基于java的网上购物商城的开题报告
Delphi人脸检测与识别Demo1fdef-main.zip
基于java的咖啡在线销售系统的开题报告
基于java的自助医疗服务系统的开题报告.docx
内容概要:本文档全面介绍了Visual Basic(VB)编程语言的基础知识和高级应用。首先概述了VB的基本特性和开发环境,随后详细讲述了VB的数据类型、变量、运算符、控制结构、数组、过程与函数、变量作用域等内容。接着介绍了窗体设计、控件使用、菜单与工具栏的设计,文件操作、数据库访问等关键知识点。最后讨论了VB的学习方法、发展历史及其在桌面应用、Web应用、数据库应用、游戏开发和自动化脚本编写等领域的广泛应用前景。 适合人群:初学者和中级程序员,尤其是希望快速掌握Windows桌面应用开发的人群。 使用场景及目标:①掌握VB的基础语法和开发环境;②学会使用VB创建复杂的用户界面和功能完整的应用程序;③理解数据库操作、文件管理和网络编程等高级主题。 其他说明:Visual Basic是一种简单易学且功能强大的编程语言,尤其适合用于开发Windows桌面应用。文中不仅覆盖了基础知识,还包括了大量的实用案例和技术细节,帮助读者快速提升编程技能。
基于java的疫情期间高校防控系统开题报告.docx
基于springboot+vue社区老年人帮扶系统源码数据库文档.zip
基于java的超市商品管理系统的开题报告.docx
基于SpringBoot房屋买卖平台源码数据库文档.zip
xdu限通院23微处理器系统与应用大作业(两只老虎),适应于汇编语言keil软件,
<项目介绍> - 新闻类网站系统,基于SSM(Spring、Spring MVC、MyBatis)+MySQL开发,高分成品毕业设计,附带往届论文 - 不懂运行,下载完可以私聊问,可远程教学 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------
基于java的学生网上请假系统的开题报告.docx
社会经济繁荣发展的今天,电子商务得到了飞速发展,网上交易越来越彰显出其独特的优越性,在人们的日常生活中,出现了各种类型的交易网站。其中一个就是车辆易主交易网站,它是一个服务于用户买卖二手车辆的交易网站,为用户提供了平等互利、方便快捷的网上交易平台,通过这一类型的网站,用户可自由出售和购买车辆。 本课题主要根据车辆本身的特性,充分发挥互联网的特点与优势,构建一个以二手车辆为商品、基于互联网平台的车辆易主业务交易管理系统,并根据车辆易主业务交易管理系统的应用需求,进行需求分析,进而对网站系统作规划设计。采用IDEA为运行平台,以SSH为框架,运用HTML语言、JSP技术、MySql数据库、JSP与后台数据库链接等关键技术建设二手车网上交易系统,构建车辆易主交易系统的会员注册与登录,网站首页展示、用户发布商品车辆,用户求购商品车辆,分页浏览、购物系统、用户后台管理、管理员用户后台管理等功能,并使这些功能得以实现并更好为用户服务。网站整体构建完成且测试成功后,用户可以进入网站进行注册、登录,登录后,用户可以在网站上发布自己的闲置车辆或者寻找想要购买的车辆,还可以收藏车辆,管理发布和收藏的车辆,
SQLite3的向量扩展库,windows dll,版本0.1.5