<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    獨自等待
    那曾經從自己身邊溜走的人……
    posts - 0,comments - 3,trackbacks - 0
    在TableViewer或TreeViewer編輯時候,Eclipse提供了基本的CellEditor,如TextCellEditor、CheckboxCellEditor、ComboBoxCellEditor、DialogCellEditor等,但在實際應用過程中,我們通常有特殊需要,如下圖類型的單元格編輯器:

    實現的方式相當簡單,我組合了ComboBoxCellEditor、DialogCellEditor,其中ComboBox支持手工輸入,返回為String類型值(原ComboBoxCellEditor為Integer類型),貼下具體代碼吧:
    package com.test.ui.properties.invoke;

    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 {
            
    public void layout(Composite editor, boolean force) {
                Rectangle bounds 
    = editor.getClientArea();
                Point size 
    = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
                
    if (contents != null{
                    contents.setBounds(
    00, bounds.width - size.x, bounds.height);
                }

                button.setBounds(bounds.width 
    - size.x, 0, size.x, bounds.height);
            }


            
    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 
    = contents.computeSize(SWT.DEFAULT, SWT.DEFAULT,
                        force);
                Point buttonSize 
    = 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 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) {
            comboBox 
    = new CCombo(cell, getStyle());
            comboBox.setFont(cell.getFont());
            populateComboBoxItems();

            comboBox.addKeyListener(
    new KeyAdapter() {
                
    public void keyPressed(KeyEvent e) {
                    keyReleaseOccured(e);
                }

            }
    );

            comboBox.addModifyListener(getModifyListener());

            comboBox.addSelectionListener(
    new SelectionAdapter() {
                
    public void widgetDefaultSelected(SelectionEvent event) {
                    applyEditorValueAndDeactivate();
                }

                
                
    public void widgetSelected(SelectionEvent event) {
                    value 
    = comboBox.getText();
                }

            }
    );
            
            comboBox.addTraverseListener(
    new TraverseListener() {
                
    public void keyTraversed(TraverseEvent e) {
                    
    if (e.detail == SWT.TRAVERSE_ESCAPE
                            
    || e.detail == SWT.TRAVERSE_RETURN) {
                        e.doit 
    = false;
                    }

                }

            }
    );

            comboBox.addFocusListener(
    new FocusAdapter() {
                
    public void focusLost(FocusEvent e) {
                    
    if(!button.isFocusControl()) {
                        ComboBoxDialogCellEditor.
    this.focusLost();
                    }

                }

            }
    );

            
    return comboBox;
        }

        
        @Override
        
    protected Control createControl(Composite parent) {
            Font font 
    = parent.getFont();
            Color bg 
    = parent.getBackground();

            editor 
    = new Composite(parent, getStyle());
            editor.setFont(font);
            editor.setBackground(bg);
            editor.setLayout(
    new DialogCellLayout());

            contents 
    = createContents(editor);
            updateContents(value);
            
            button 
    = createButton(editor);
            button.setFont(font);

            button.addKeyListener(
    new KeyAdapter() {
                
    public void keyReleased(KeyEvent e) {
                    
    if (e.character == '\u001b'// Escape
                        fireCancelEditor();
                    }

                }

            }
    );
            button.addFocusListener(getButtonFocusListener());
            button.addSelectionListener(
    new SelectionAdapter() {
                
    public void widgetSelected(SelectionEvent event) {
                    button.removeFocusListener(getButtonFocusListener());
                    Object newValue 
    = openDialogBox(editor);
                    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 editor;
        }


        
    public void deactivate() {
            
    if (button != null && !button.isDisposed()) {
                button.removeFocusListener(getButtonFocusListener());
            }

            
            
    super.deactivate();
        }


        @Override
        
    protected Object doGetValue() {
            
    return value;
        }


        @Override
        
    protected void doSetFocus() {
            
    if (comboBox != null)
                comboBox.setFocus();
        }


        @Override
        
    protected void doSetValue(Object value) {
            
    this.value = value;
            updateContents(value);
        }


        
    private void populateComboBoxItems() {
            
    if (comboBox != null && items != null{
                comboBox.removeAll();
                
    for (int i = 0; i < items.length; i++{
                    comboBox.add(items[i], i);
                }

                comboBox.setText(
    "");
            }

        }


        
    void applyEditorValueAndDeactivate() {
            Object newValue 
    = comboBox.getText();
            
    if (newValue != null && !newValue.equals(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()
         
    */

        
    protected void focusLost() {
            
    if (isActivated()) {
                applyEditorValueAndDeactivate();
            }

        }


        
        
    /*
         * (non-Javadoc)
         * @see org.eclipse.jface.viewers.CellEditor#keyReleaseOccured(org.eclipse.swt.events.KeyEvent)
         
    */

        
    protected void keyReleaseOccured(KeyEvent keyEvent) {
            
    if (keyEvent.character == '\r'// Return key
                if (comboBox != null && !comboBox.isDisposed())
                    fireCancelEditor();
            }
     else if (keyEvent.character == '\t'// tab key
                applyEditorValueAndDeactivate();
            }

        }


        
    protected void editOccured(ModifyEvent e) {
            String value 
    = 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 (modifyListener == null{
                modifyListener 
    = new ModifyListener() {
                    
    public void modifyText(ModifyEvent e) {
                        editOccured(e);
                    }

                }
    ;
            }

            
    return modifyListener;
        }


        
    private FocusListener getButtonFocusListener() {
            
    if (buttonFocusListener == null{
                buttonFocusListener 
    = new FocusListener() {
                    
    public void focusGained(FocusEvent e) {};
                    
    public void focusLost(FocusEvent e) {
                        ComboBoxDialogCellEditor.
    this.focusLost();
                    }

                }
    ;
            }
    ;
            
    return buttonFocusListener;
        }


        
    private void updateContents(Object value) {
            Assert.isTrue(comboBox 
    != null);
            
            
    if (value != null &&  value instanceof String) {
                comboBox.removeModifyListener(getModifyListener());
                comboBox.setText((String) value);
                comboBox.addModifyListener(getModifyListener());
            }

        }

        
        
    protected abstract Object openDialogBox(Control cellEditorWindow);

    }

    posted on 2008-06-02 13:46 自由 閱讀(3932) 評論(2)  編輯  收藏 所屬分類: SWT

    FeedBack:
    # re: 自定義 CellEditor
    2012-12-01 18:41 | ljj
    怎么調用呢?能給個例子么?  回復  更多評論
      
    # re: 自定義 CellEditor[未登錄]
    2016-02-16 11:57 |
    有個問題請教一下,我使用swt table開發了一個小程序自己用,沒想到特別麻煩,table里用了TextCellEditor,在輸入編輯的時候能做到換行,輸入完以后表格里的文本只能顯示一行。求問高手,怎么才能實現表格里文本多行顯示?(不通過換行符,通過wrap這種方式)
    謝謝  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲午夜福利717| 亚洲a无码综合a国产av中文| 亚洲日本一线产区和二线 | 日韩在线看片免费人成视频播放| 亚洲人xxx日本人18| 好男人视频在线观看免费看片 | 亚洲av最新在线网址| 深夜国产福利99亚洲视频| 99久久免费国产精品特黄| 亚在线观看免费视频入口| 一级一级一片免费高清| 久久香蕉国产线看观看亚洲片| 四虎影视免费永久在线观看| AV免费网址在线观看| 99在线精品视频观看免费| 伊人久久免费视频| a级毛片毛片免费观看久潮喷| 亚洲国产福利精品一区二区| 国产亚洲综合网曝门系列| 国产午夜亚洲精品理论片不卡| 中国xxxxx高清免费看视频| 国产免费一区二区三区不卡| 99亚偷拍自图区亚洲| 国产亚洲精品国看不卡| 亚洲av片一区二区三区| 最好免费观看韩国+日本| 一级片在线免费看| 手机永久免费的AV在线电影网| 亚洲理论片中文字幕电影| 亚洲福利在线视频| 亚洲综合自拍成人| 久久久久久亚洲精品成人| 4444亚洲国产成人精品| 国产精品自在自线免费观看| 久久ww精品w免费人成| 日本免费大黄在线观看| 97青青草原国产免费观看| 久久免费视频网站| 99久久综合精品免费| 一级毛片免费毛片一级毛片免费 | 亚洲欧洲国产精品你懂的|