📄 .#pcombo.java.1.11
字号:
private void showDropDown2(){
if (dropdownShell == null)
createShell();
Point size;
if (!dropdownResized){
//comboWidth is really the width of the combo minus trimmings around
//the dropdown shell,
//The intention is that if the strategy wants to set its width equal
//to the combo it would just use this value
int comboWidth = dropdownShell.computeTrim(0,0,1,1).width -1;
comboWidth = this.getSize().x - comboWidth;
size = dropdownStrategy.getSize(comboWidth);
Rectangle bounds = dropdownShell.computeTrim(0,0,size.x,size.y);
dropdownShell.setSize(bounds.width,bounds.height);
dropdownResized = false;
} else {
size = dropdownShell.getSize();
}
Point loc = getDisplay().map(this.getParent(),null,this.getLocation());
if (size.x < getSize().x){
loc.x += getSize().x - dropdownShell.getSize().x;
}
if (loc.x < 0)
loc.x = 0;
if ((loc.x + size.x) > getDisplay().getClientArea().width)
loc.x = getDisplay().getClientArea().width - size.x;
loc.y += getSize().y;
if ((loc.y + size.y) > getDisplay().getClientArea().height)
loc.y -= getSize().y + size.y;
//For whatever reaons, sometimes the set loc and visible call triggers
//a resize event
boolean resizeTempSave = dropdownResized;
dropdownShell.setLocation(loc);
dropdownShell.setVisible(true);
dropdownResized = resizeTempSave;
setToolTipText(null);
}
private void createShell(){
int resizeBit = SWT.NONE;
if (resizable){
resizeBit = SWT.RESIZE | SWT.BORDER;
}
dropdownShell = new Shell(getShell(),SWT.NO_FOCUS | SWT.ON_TOP | resizeBit);
dropdownShell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event e) {
if (e.detail == SWT.TRAVERSE_ESCAPE) {
e.doit = false;
closeDropDown();
}
}
});
if (resizable){
dropdownShell.addControlListener(new ControlListener(){
public void controlMoved(ControlEvent e) {
}
public void controlResized(ControlEvent e) {
int w = dropdownShell.getSize().x;
int h = dropdownShell.getSize().y;
if (w < 60 || h < 60){
if (w < 60)
w = 60;
if (h < 60)
h = 60;
dropdownShell.setSize(w,h);
}
dropdownResized = true;
}
});
}
parentShellShellListener = new ShellListener(){
public void shellActivated(ShellEvent arg0) {
}
public void shellClosed(ShellEvent arg0) {
}
public void shellDeactivated(ShellEvent arg0) {
getDisplay().asyncExec(new Runnable(){
public void run() {
if (isDropDownOpen() && dropdownShell != getDisplay().getActiveShell()){
getShell().setActive();
closeDropDown();
}
}}
);
}
public void shellDeiconified(ShellEvent arg0) {
}
public void shellIconified(ShellEvent arg0) {
closeDropDown();
}
};
getShell().addShellListener(parentShellShellListener);
parentShellControlListener = new ControlListener(){
public void controlMoved(ControlEvent e) {
closeDropDown();
}
public void controlResized(ControlEvent e) {
closeDropDown();
}
};
getShell().addControlListener(parentShellControlListener);
dropdownShell.setLayout(new FillLayout());
dropdownStrategy.createContents(dropdownShell,updater);
dropdownShell.addShellListener(new ShellListener(){
public void shellActivated(ShellEvent e) {
}
public void shellClosed(ShellEvent e) {
}
public void shellDeactivated(ShellEvent e) {
if (!ignoreDeactivate)
closeDropDown();
}
public void shellDeiconified(ShellEvent e) {
}
public void shellIconified(ShellEvent e) {
}}
);
mouseDownFilter = new Listener(){
public void handleEvent(Event event) {
if (isDropDownOpen()){
if (dropdownStrategy.canReturnFocusOnMouseDown()){
getDisplay().asyncExec(new Runnable(){
public void run() {
ignoreDeactivate = true;
if (!isDisposed())
getShell().setActive();
ignoreDeactivate = false;
}}
);
}
if (event.widget instanceof Control){
Control c = (Control) event.widget;
if (!dropdownShell.getBounds().contains(getDisplay().map(c,null,event.x,event.y))){
closeDropDown();
event.type = SWT.NONE;
}
}
}
}
};
getDisplay().addFilter(SWT.MouseDown,mouseDownFilter);
}
private void closeDropDown(){
if (!isDropDownOpen())
return;
getDisplay().asyncExec(new Runnable(){
public void run() {
if (!isDisposed()){
getShell().setActive();
PCombo.this.forceFocus();
dropdownShell.setVisible(false);
if (!isDisposed())
setToolTipText(tooltip);
if (!isDisposed())
PCombo.this.forceFocus();
if (!isDisposed())
focusIn();
if (!isDisposed())
redraw();
}
}}
);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setToolTipText(java.lang.String)
*/
public void setToolTipText(String string) {
super.setToolTipText(string);
tooltip = string;
}
private boolean isDropDownOpen(){
return (!dropdownShell.isDisposed() && dropdownShell.isVisible());
}
private void dispose2() {
dropdownShell.dispose();
display.removeFilter(SWT.MouseDown,mouseDownFilter);
if (getShell() != null){
getShell().removeControlListener(parentShellControlListener);
getShell().removeShellListener(parentShellShellListener);
}
dropdownStrategy.dispose();
paintingStrategy.dispose();
}
/**
* Returns the current value Object. The actual type of this value
* is determined by the IComboStrategy. If this PCombo is editable
* and the user has typed a value rather than selecting one from the
* drop down, this value will be of type String.
* @return the value object
*/
public Object getValue() {
return value;
}
/**
* Sets the current value Object. Acceptable types of value objects
* are determined by the IComboStrategy.
*
* @param value the value object to set
*/
public void setValue(Object value) {
setValue(value,false);
}
private void setValue(Object value,boolean byTyping){
int x = paintingStrategy.getTextXStartPosition();
this.value = value;
paintingStrategy.valueChanged(value);
redraw();
if (!ignoreUpdates)
dropdownStrategy.selectValue(value);
setToolTipText(labelProvider.getToolTip(value));
if (paintingStrategy.getTextXStartPosition() != x){
resize();
}
if (text.isVisible() && !byTyping){
ignoreModification = true;
if (value == null){
text.setText("");
} else {
text.setText(labelProvider.getText(value));
}
ignoreModification = false;
}
}
private void focusIn() {
if (!readOnly && !isDropDownOpen()){
resize();
ignoreModification = true;
if (value == null){
text.setText("");
} else {
text.setText(labelProvider.getText(value));
}
ignoreModification = false;
text.setBackground(this.getBackground());
text.setForeground(this.getForeground());
text.setFont(this.getFont());
//textModified = false;
text.selectAll();
text.setVisible(true);
getDisplay().asyncExec(new Runnable() {
public void run() {
if (text != null && !text.isDisposed())
text.setFocus();
}
});
}
}
private void focusOut() {
if (!readOnly){
// if (textModified)
// setValue(text.getText());
text.setVisible(false);
}
}
/**
* Adds the listener to the collection of listeners who will be
* notified when the drop down's selection changes, by sending it one of
* the messages defined in the SelectionListener interface.
* <p>
* widgetSelected is called when the drop down's selection changes.
* widgetDefaultSelected is not called.
*
* @param listener the listener which should be notified
*/
public void addSelectionListener(SelectionListener listener){
selectListeners.add(listener);
}
/**
* Removes the listener from the collection of listeners who
* will be notified when the drop down's selection changes.
*
* @param listener the listener which should no longer be notified
*/
public void removeSelectionListener(SelectionListener listener){
selectListeners.remove(listener);
}
/**
* Adds the listener to the collection of listeners who will be
* notified when the combo's value is modified.
* <p>
* modifyText is called when the value is modified.
*
* @param listener the listener which should be notified
*/
public void addModifyListener(ModifyListener listener){
modifyListeners.add(listener);
}
/**
* Removes the listener from the collection of listeners who
* will be notified when the combo's value is modified.
*
* @param listener the listener which should no longer be notified
*/
public void removeModifyListener(ModifyListener listener){
modifyListeners.remove(listener);
}
private void fireSelectListeners(){
Event e = new Event();
e.display = this.getDisplay();
e.widget = this;
SelectionEvent se = new SelectionEvent(e);
this.notifyListeners(SWT.Selection,e);
for (Iterator iter = selectListeners.iterator(); iter.hasNext();) {
SelectionListener listener = (SelectionListener) iter.next();
listener.widgetSelected(se);
}
}
private void fireModifyListeners(){
Event e = new Event();
e.display = this.getDisplay();
e.widget = this;
ModifyEvent me = new ModifyEvent(e);
this.notifyListeners(SWT.Modify,e);
for (Iterator iter = modifyListeners.iterator(); iter.hasNext();) {
ModifyListener listener = (ModifyListener) iter.next();
listener.modifyText(me);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -