Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TextAndDialogCellEditor(events in a JFace Dialog problem)
TextAndDialogCellEditor [message #946456] Tue, 16 October 2012 08:05
Joaquin Morcate is currently offline Joaquin Morcate
Messages: 49
Registered: March 2010
Member
Hi all,

I'm trying to use the following editor that combines a DialogCellEditor with a text editor:
/*******************************************************************************
 * Copyright (c) 2006, 2009 Eric Rizzo and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eric Rizzo - initial implementation
 *******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import .....

/**
 * A CellEditor that is a blending of DialogCellEditor and TextCellEditor. The user can either type
 * directly into the Text or use the button to open a Dialog for editing the cell's value.
 * 
 */
public class TextAndDialogCellEditor extends DialogCellEditor {

	private Text textField;
	private String dialogMessage;
	private String dialogTitle;


	public TextAndDialogCellEditor(Composite parent) {
		super(parent);
	}


	public void setDialogMessage(String dialogMessage) {
		this.dialogMessage = dialogMessage;
	}


	public void setDialogTitle(String dialogTitle) {
		this.dialogTitle = dialogTitle;
	}


	protected Control createContents(Composite cell) {
		textField = new Text(cell, SWT.NONE);
		textField.setFont(cell.getFont());
		textField.setBackground(cell.getBackground());
		textField.addFocusListener(new FocusAdapter() {
				public void focusLost(FocusEvent event) {
					 setValueToModel();
				}
			});

		textField.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent event) {
					keyReleaseOccured(event);
				}
			});

		return textField;
	}

	protected void keyReleaseOccured(KeyEvent keyEvent) {
		if (keyEvent.keyCode == SWT.CR || keyEvent.keyCode == SWT.KEYPAD_CR) { // Enter key
			setValueToModel();
		}
		super.keyReleaseOccured(keyEvent);
	}

	protected void setValueToModel() {
	 	String newValue = textField.getText();
        boolean newValidState = isCorrect(newValue);
        if (newValidState) {
            markDirty();
            doSetValue(newValue);
        } else {
            // try to insert the current value into the error message.
            setErrorMessage(MessageFormat.format(getErrorMessage(), new Object[] { newValue.toString() }));
        }
	}

	protected void updateContents(Object value) {
		if (textField == null) {
			return;
		}

        String text = "";
        if (value != null) {
			text = value.toString();
		}
        textField.setText(text);
		
	}

	protected void doSetFocus() {
		// Overridden to set focus to the Text widget instead of the Button.
		textField.setFocus();
		textField.selectAll();
	}


	protected Object openDialogBox(Control cellEditorWindow) {
		InputDialog dialog = new InputDialog(cellEditorWindow.getShell(), dialogTitle, dialogMessage, getDialogInitialValue(), null);
		if (dialog.open() == Window.OK) {
			return dialog.getValue();
		} else {
			return null;
		}
	}

	protected String getDialogInitialValue() {
		Object value = getValue();
		if (value == null) {
			return null;
		} else {
			return value.toString();
		}
	}
}


I have the following problem. If I modify the value using the text field the editor works fine. When I press CR the editor is deactivated and my model is updated. If I open the dialog to modify the value it works also fine. But after that if I activate the editor again and I press CR on the text field, it doesn't work any more. Actually the event never arrives to keyReleaseOccured. All the key are processed properly, so I can modify the text in the Text widget, I can even press ESCAPE to cancel the application but not CR. I looks like if the event is consumed somewhere before, maybe in the dialog that disappeared. I'm lost. I don't understand how the dialog register its listeners. The CR will fire the action for the default value, but where is that association made? Finally, any idea about it?

Thank you.
Previous Topic:[Databinding] Problems with multiple actions in a checkbox
Next Topic:FormText inside ScrolledFormText
Goto Forum:
  


Current Time: Wed Oct 01 04:13:54 GMT 2014

Powered by FUDForum. Page generated in 0.01536 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software