Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » (TableViewer) How to have all gui of celleditor be displayed all the time
(TableViewer) How to have all gui of celleditor be displayed all the time [message #333809] Tue, 06 January 2009 23:23 Go to next message
zubin Missing name is currently offline zubin Missing nameFriend
Messages: 32
Registered: July 2009
Member
Hi,

I am using tableViewer and ComboBoxCellEditor and DialogCellEditor to
display the data. Everything works ok except that the gui of celleditor
only shows up if I double click on the cell. For example,

1. I click on a cell with DialogCellEditor attached to it.
2. A dialog will show up.

Can I have those dialogs show up all the time(without click on cell)
without resource leak(I tried to use swt table, but when the table become
large like 30rows*40 columns, application becomes extremly slow)?
Re: (TableViewer) How to have all gui of celleditor be displayed all the time [message #333810 is a reply to message #333809] Tue, 06 January 2009 23:28 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

Short answer no (you mean the Button displayed right?). Longer answer is
that you could take a Screenshot and display this instead of the real
control I've used this approach for emulation of native looking checkboxes.

Tom

zubin schrieb:
> Hi,
>
> I am using tableViewer and ComboBoxCellEditor and DialogCellEditor to
> display the data. Everything works ok except that the gui of celleditor
> only shows up if I double click on the cell. For example,
> 1. I click on a cell with DialogCellEditor attached to it.
> 2. A dialog will show up.
> Can I have those dialogs show up all the time(without click on cell)
> without resource leak(I tried to use swt table, but when the table
> become large like 30rows*40 columns, application becomes extremly slow)?
>
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: (TableViewer) How to have all gui of celleditor be displayed all the time [message #333825 is a reply to message #333810] Wed, 07 January 2009 17:24 Go to previous messageGo to next message
zubin Missing name is currently offline zubin Missing nameFriend
Messages: 32
Registered: July 2009
Member
Thanks, Tom.

There is another problem,
how can a user one click on dialog button to bring up(pop up) the dialog
if a cell has 2 controllers? for example, if user click on the text bar,
the value on the text bar will become modifiable; otherwise if the dialog
button is clicked, a dialog will be poped up. how can we detect where the
user is clicked(the width of cell is dynamic also)?




here is the snipet for the DialogCellEditor that allow user to modify the
value directly from text bar.

import java.text.MessageFormat;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.FileDialog;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Text;

public class DialogCellEditor extends CellEditor {

/**
* The editor control.
*/
private Composite editor;

/**
* The button.
*/
private Button button;

private Composite parent;

private static Color readOnlyBgColor;
/**
* The text control; initially <code>null</code>.
*/
protected Text text;

/**
* State information for updating action enablement
*/
private boolean isSelection = false;
private boolean isDeleteable = false;
private boolean isSelectable = false;

/**
* Default TextCellEditor style specify no borders on text widget as
cell
* outline in table already provides the look of a border.
*/
private static final int defaultStyle = SWT.SINGLE;

/**
* Creates a new text string cell editor with no control The cell
editor
* value is the string itself, which is initially the empty string.
* Initially, the cell editor has no cell validator.
*
* @since 2.1
*/
public DialogCellEditor() {
setStyle(defaultStyle);
}

/**
* Creates a new text string cell editor parented under the given
control.
* The cell editor value is the string itself, which is initially the
empty
* string. Initially, the cell editor has no cell validator.
*
* @param parent
* the parent control
*/
public DialogCellEditor(Composite parent) {
this(parent, defaultStyle);
this.parent = parent;
}

/**
* Creates a new text string cell editor parented under the given
control.
* The cell editor value is the string itself, which is initially the
empty
* string. Initially, the cell editor has no cell validator.
*
* @param parent
* the parent control
* @param style
* the style bits
* @since 2.1
*/
public DialogCellEditor(Composite parent, int style) {
super(parent, style);
this.parent = parent;
}

/**
* Checks to see if the "deleteable" state (can delete/ nothing to
delete)
* has changed and if so fire an enablement changed notification.
*/
private void checkDeleteable() {
boolean oldIsDeleteable = isDeleteable;
isDeleteable = isDeleteEnabled();
if (oldIsDeleteable != isDeleteable) {
fireEnablementChanged(DELETE);
}
}

/**
* Checks to see if the "selectable" state (can select) has changed
and if
* so fire an enablement changed notification.
*/
private void checkSelectable() {
boolean oldIsSelectable = isSelectable;
isSelectable = isSelectAllEnabled();
if (oldIsSelectable != isSelectable) {
fireEnablementChanged(SELECT_ALL);
}
}

/**
* Checks to see if the selection state (selection / no selection) has
* changed and if so fire an enablement changed notification.
*/
private void checkSelection() {
boolean oldIsSelection = isSelection;
isSelection = text.getSelectionCount() > 0;
if (oldIsSelection != isSelection) {
fireEnablementChanged(COPY);
fireEnablementChanged(CUT);
}
}

/**
* 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 = button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
force);
if (text != null)
text.setBounds(0, 0, bounds.width - size.x, bounds.height);
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 = text.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;
}
}

/**
* Creates the button for this cell editor under the given parent
control.
* <p>
* The default implementation of this framework method creates the
button
* display on the right hand side of the dialog cell editor.
Subclasses may
* extend or reimplement.
* </p>
*
* @param parent
* the parent control
* @return the new button control
*/
protected Button createButton(Composite parent) {
Button result = new Button(parent, SWT.DOWN);
result.setText("..."); //$NON-NLS-1$
return result;
}

/*
* (non-Javadoc) Method declared on CellEditor.
*/
@Override
protected Control createControl(Composite parent) {
Font font = parent.getFont();
Color bg = parent.getBackground();

if (readOnlyBgColor != null) {
readOnlyBgColor.dispose();
readOnlyBgColor = null;
}

if (getStyle() == SWT.READ_ONLY) {
readOnlyBgColor = new Color(parent.getDisplay(), 238, 250,
209);
bg = readOnlyBgColor;
} else {
bg = parent.getBackground();
}

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

text = new Text(editor, getStyle());
text.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
handleDefaultSelection(e);
}
});
text.addKeyListener(new KeyAdapter() {
// hook key pressed - see PR 14201
@Override
public void keyPressed(KeyEvent e) {
keyReleaseOccured(e);

// as a result of processing the above call, clients may
have
// disposed this cell editor
if ((getControl() == null) || getControl().isDisposed())
return;
checkSelection(); // see explaination below
checkDeleteable();
checkSelectable();
}
});
text.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_ESCAPE || e.detail ==
SWT.TRAVERSE_RETURN) {
e.doit = false;
}
}
});
// We really want a selection listener but it is not supported so
we
// use a key listener and a mouse listener to know when selection
// changes
// may have occured
text.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
checkSelection();
checkDeleteable();
checkSelectable();
}
});

text.setFont(parent.getFont());
text.setBackground(bg);
text.setText("");//$NON-NLS-1$

button = createButton(editor);
button.setFont(font);

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

button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Object newValue = openDialogBox(editor);
if (newValue != null) {
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() }));
}
// fireApplyEditorValue();
focusLost();
}
}
});

setValueValid(true);

return editor;
}

/**
* @see
org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(Con trol)
*/
protected Object openDialogBox(@SuppressWarnings("unused") Control
cellEditorWindow) {
// Constucts a custom FileChooser
FileDialog fDialog = new FileDialog(parent.getShell(), SWT.OPEN);
/*
* Filters if needed fDialog.setFilterNames(...);
* fDialog.setFilterExtensions(...);
*/

String value = (String) getValue();

if ((value != null) && (value.length() > 0)) {
fDialog.setFileName(value);
}
String path = fDialog.open();

// Returns a path, a string, what you want
return path;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> framework method returns the text string.
*
* @return the text string
*/
@Override
protected Object doGetValue() {
return text.getText();
}

/*
* (non-Javadoc) Method declared on CellEditor.
*/
@Override
protected void doSetFocus() {
if (text != null) {
text.selectAll();
text.setFocus();
checkSelection();
checkDeleteable();
checkSelectable();
}
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> framework method accepts a text string (type
* <code>String</code>).
*
* @param value
* a text string (type <code>String</code>)
*/
@Override
protected void doSetValue(Object value) {
text.setText(value.toString());
}

/**
* Since a text editor field is scrollable we don't set a minimumSize.
*/
@Override
public LayoutData getLayoutData() {
return new LayoutData();
}

/**
* Handles a default selection event from the text control by applying
the
* editor value and deactivating this cell editor.
*
* @param event
* the selection event
*
* @since 3.0
*/
protected void handleDefaultSelection(@SuppressWarnings("unused")
SelectionEvent event) {
// same with enter-key handling code in keyReleaseOccured(e);
fireApplyEditorValue();
deactivate();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method returns <code>true</code> if the
current
* selection is not empty.
*/
@Override
public boolean isCopyEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getSelectionCount() > 0;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method returns <code>true</code> if the
current
* selection is not empty.
*/
@Override
public boolean isCutEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getSelectionCount() > 0;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method returns <code>true</code> if there
is a
* selection or if the caret is not positioned at the end of the text.
*/
@Override
public boolean isDeleteEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getSelectionCount() > 0 || text.getCaretPosition() <
text.getCharCount();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method always returns <code>true</code>.
*/
@Override
public boolean isPasteEnabled() {
if (text == null || text.isDisposed())
return false;
return true;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method always returns <code>true</code>.
*/
public boolean isSaveAllEnabled() {
if (text == null || text.isDisposed())
return false;
return true;
}

/**
* Returns <code>true</code> if this cell editor is able to perform the
* select all action.
* <p>
* This default implementation always returns <code>false</code>.
* </p>
* <p>
* Subclasses may override
* </p>
*
* @return <code>true</code> if select all is possible,
<code>false</code>
* otherwise
*/
@Override
public boolean isSelectAllEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getCharCount() > 0;
}

/**
* Processes a key release event that occurred in this cell editor.
* <p>
* The <code>TextCellEditor</code> implementation of this framework
method
* ignores when the RETURN key is pressed since this is handled in
* <code>handleDefaultSelection</code>. An exception is made for
Ctrl+Enter
* for multi-line texts, since a default selection event is not sent
in this
* case.
* </p>
*
* @param keyEvent
* the key event
*/
@Override
protected void keyReleaseOccured(KeyEvent keyEvent) {
if (keyEvent.character == '\r') { // Return key
// Enter is handled in handleDefaultSelection.
// Do not apply the editor value in response to an Enter key
event
// since this can be received from the IME when the intent is
-not-
// to apply the value.
// See bug 39074 [CellEditors] [DBCS] canna input mode fires
bogus
// event
// from Text Control
//
// An exception is made for Ctrl+Enter for multi-line texts,
since
// a default selection event is not sent in this case.
if (text != null && !text.isDisposed() && (text.getStyle() &
SWT.MULTI) != 0) {
if ((keyEvent.stateMask & SWT.CTRL) != 0) {
super.keyReleaseOccured(keyEvent);
}
}
return;
}
super.keyReleaseOccured(keyEvent);
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method copies the current selection to the
* clipboard.
*/
@Override
public void performCopy() {
text.copy();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method cuts the current selection to the
* clipboard.
*/
@Override
public void performCut() {
text.cut();
checkSelection();
checkDeleteable();
checkSelectable();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method deletes the current selection or, if
there
* is no selection, the character next character from the current
position.
*/
@Override
public void performDelete() {
if (text.getSelectionCount() > 0)
// remove the contents of the current selection
text.insert(""); //$NON-NLS-1$
else {
// remove the next character
int pos = text.getCaretPosition();
if (pos < text.getCharCount()) {
text.setSelection(pos, pos + 1);
text.insert(""); //$NON-NLS-1$
}
}
checkSelection();
checkDeleteable();
checkSelectable();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method pastes the the clipboard contents
over the
* current selection.
*/
@Override
public void performPaste() {
text.paste();
checkSelection();
checkDeleteable();
checkSelectable();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method selects all of the current text.
*/
@Override
public void performSelectAll() {
text.selectAll();
checkSelection();
checkDeleteable();
}

}
Re: (TableViewer) How to have all gui of celleditor be displayed all the time [message #333826 is a reply to message #333825] Wed, 07 January 2009 17:34 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi Zubin,

I'm not able to make up a Snippet for every question I answer.

To move a thread up in my
newsgroup-I'll-answer-as-fast-as-possible-work-queue:

a) Create fully runnable snippet I can use to start working with
immediately

b) Even say that you release the code under EPL so that we can add it to
our snippet collection ([1]). Immediately puts your request at the
top of my queue :-)

I hope you understand this.

Tom

[1]http://wiki.eclipse.org/JFaceSnippets
Re: (TableViewer) How to have all gui of celleditor be displayed all the time [message #333828 is a reply to message #333826] Wed, 07 January 2009 19:03 Go to previous message
zubin Missing name is currently offline zubin Missing nameFriend
Messages: 32
Registered: July 2009
Member
Here is the runnable snippet(in the snippet, the dialog image is still on
the left hand side instead right hand side. I have no idea how to fix
this yet. )


import java.text.MessageFormat;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

public class OneClickSnippet {

public OneClickSnippet(final Shell shell) {
final Table table = new Table(shell, SWT.BORDER |
SWT.FULL_SELECTION);

final TableViewer v = new TableViewer(table);
v.getTable().setLinesVisible(true);
v.getTable().setHeaderVisible(true);

final TableViewerColumn dialogEditor = new TableViewerColumn(v,
SWT.NONE);
dialogEditor.getColumn().setWidth(200);
dialogEditor.getColumn().setText("Dialog & Text Editor");
dialogEditor.setLabelProvider(new ColumnLableProvider(v));

dialogEditor.setEditingSupport(new DialogEdittingSupport(v));

v.setContentProvider(new MyContentProvider());

MyModel[] model = createModel();
v.setInput(model);

}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new OneClickSnippet(shell);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();
}

private MyModel[] createModel() {
MyModel[] elements = new MyModel[20];

for (int i = 0; i < 10; i++) {
elements[i] = new MyModel(i);
}

for (int i = 0; i < 10; i++) {
elements[i + 10] = new MyModel2(i);
}

return elements;
}

private class MyContentProvider implements IStructuredContentProvider {

/*
* (non-Javadoc)
*
* @see
*
org.eclipse.jface.viewers.IStructuredContentProvider#getElem ents(
* java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
return (MyModel[]) inputElement;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {

}

/*
* (non-Javadoc)
*
* @see
*
org.eclipse.jface.viewers.IContentProvider#inputChanged(org. eclipse
* .jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {

}

}

public class MyModel {
public int counter;

public MyModel(int counter) {
this.counter = counter;
}

public String toString() {
return "Item " + this.counter;
}
}

public class MyModel2 extends MyModel {

public MyModel2(int counter) {
super(counter);
}

public String toString() {
return "Special Item " + this.counter;
}
}

private class DialogEdittingSupport extends EditingSupport {

private CellEditor dialogTextCellEditor;

public DialogEdittingSupport(TableViewer viewer) {
super(viewer);
dialogTextCellEditor = new ComboCellEditor(viewer.getTable(),
SWT.NONE);
}

@Override
protected boolean canEdit(Object element) {
return true;
}

@Override
protected CellEditor getCellEditor(Object element) {
return dialogTextCellEditor;
}

@Override
protected Object getValue(Object element) {
return element.toString();
}

@Override
protected void setValue(Object element, Object value) {

}
}

private class DialogCellEditor extends CellEditor {

/**
* The editor control.
*/
private Composite editor;

/**
* The button.
*/
private Button button;

private Composite parent;

/**
* The text control; initially <code>null</code>.
*/
protected Text text;

/**
* State information for updating action enablement
*/
private boolean isSelection = false;
private boolean isDeleteable = false;
private boolean isSelectable = false;

/**
* Default TextCellEditor style specify no borders on text widget
as
* cell outline in table already provides the look of a border.
*/
private static final int defaultStyle = SWT.SINGLE;

/**
* Creates a new text string cell editor with no control The cell
editor
* value is the string itself, which is initially the empty string.
* Initially, the cell editor has no cell validator.
*
* @since 2.1
*/
public DialogCellEditor() {
setStyle(defaultStyle);
}

/**
* Creates a new text string cell editor parented under the given
* control. The cell editor value is the string itself, which is
* initially the empty string. Initially, the cell editor has no
cell
* validator.
*
* @param parent
* the parent control
*/
public DialogCellEditor(Composite parent) {
this(parent, defaultStyle);
this.parent = parent;
}

/**
* Creates a new text string cell editor parented under the given
* control. The cell editor value is the string itself, which is
* initially the empty string. Initially, the cell editor has no
cell
* validator.
*
* @param parent
* the parent control
* @param style
* the style bits
* @since 2.1
*/
public DialogCellEditor(Composite parent, int style) {
super(parent, style);
this.parent = parent;
}

/**
* Checks to see if the "deleteable" state (can delete/ nothing to
* delete) has changed and if so fire an enablement changed
* notification.
*/
private void checkDeleteable() {
boolean oldIsDeleteable = isDeleteable;
isDeleteable = isDeleteEnabled();
if (oldIsDeleteable != isDeleteable) {
fireEnablementChanged(DELETE);
}
}

/**
* Checks to see if the "selectable" state (can select) has
changed and
* if so fire an enablement changed notification.
*/
private void checkSelectable() {
boolean oldIsSelectable = isSelectable;
isSelectable = isSelectAllEnabled();
if (oldIsSelectable != isSelectable) {
fireEnablementChanged(SELECT_ALL);
}
}

/**
* Checks to see if the selection state (selection / no selection)
has
* changed and if so fire an enablement changed notification.
*/
private void checkSelection() {
boolean oldIsSelection = isSelection;
isSelection = text.getSelectionCount() > 0;
if (oldIsSelection != isSelection) {
fireEnablementChanged(COPY);
fireEnablementChanged(CUT);
}
}

/**
* 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 = button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
force);
if (text != null)
text.setBounds(0, 0, bounds.width - size.x,
bounds.height);
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 = text.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;
}
}

/**
* Creates the button for this cell editor under the given parent
* control.
* <p>
* The default implementation of this framework method creates the
* button display on the right hand side of the dialog cell editor.
* Subclasses may extend or reimplement.
* </p>
*
* @param parent
* the parent control
* @return the new button control
*/
protected Button createButton(Composite parent) {
Button result = new Button(parent, SWT.DOWN);
result.setText("..."); //$NON-NLS-1$
return result;
}

/*
* (non-Javadoc) Method declared on CellEditor.
*/
@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());

text = new Text(editor, getStyle());
text.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
handleDefaultSelection(e);
}
});
text.addKeyListener(new KeyAdapter() {
// hook key pressed - see PR 14201
@Override
public void keyPressed(KeyEvent e) {
keyReleaseOccured(e);

// as a result of processing the above call, clients
may
// have
// disposed this cell editor
if ((getControl() == null) ||
getControl().isDisposed())
return;
checkSelection(); // see explaination below
checkDeleteable();
checkSelectable();
}
});
text.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_ESCAPE || e.detail ==
SWT.TRAVERSE_RETURN) {
e.doit = false;
}
}
});
// We really want a selection listener but it is not supported
so we
// use a key listener and a mouse listener to know when
selection
// changes
// may have occured
text.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
checkSelection();
checkDeleteable();
checkSelectable();
}
});

text.setFont(parent.getFont());
text.setBackground(bg);
text.setText("");//$NON-NLS-1$

button = createButton(editor);
button.setFont(font);

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

button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
Object newValue = openDialogBox(editor);
if (newValue != null) {
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() }));
}
// fireApplyEditorValue();
focusLost();
}
}
});

setValueValid(true);

return editor;
}

/**
* @see
org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(Con trol)
*/
protected Object openDialogBox(@SuppressWarnings("unused") Control
cellEditorWindow) {
// Constucts a custom FileChooser
FileDialog fDialog = new FileDialog(parent.getShell(),
SWT.OPEN);
/*
* Filters if needed fDialog.setFilterNames(...);
* fDialog.setFilterExtensions(...);
*/

String value = (String) getValue();

if ((value != null) && (value.length() > 0)) {
fDialog.setFileName(value);
}
String path = fDialog.open();

// Returns a path, a string, what you want
return path;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> framework method returns the text
string.
*
* @return the text string
*/
@Override
protected Object doGetValue() {
return text.getText();
}

/*
* (non-Javadoc) Method declared on CellEditor.
*/
@Override
protected void doSetFocus() {
if (text != null) {
text.selectAll();
text.setFocus();
checkSelection();
checkDeleteable();
checkSelectable();
}
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> framework method accepts a text string
(type
* <code>String</code>).
*
* @param value
* a text string (type <code>String</code>)
*/
@Override
protected void doSetValue(Object value) {
text.setText(value.toString());
}

/**
* Since a text editor field is scrollable we don't set a
minimumSize.
*/
@Override
public LayoutData getLayoutData() {
return new LayoutData();
}

/**
* Handles a default selection event from the text control by
applying
* the editor value and deactivating this cell editor.
*
* @param event
* the selection event
*
* @since 3.0
*/
protected void handleDefaultSelection(@SuppressWarnings("unused")
SelectionEvent event) {
// same with enter-key handling code in keyReleaseOccured(e);
fireApplyEditorValue();
deactivate();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method returns <code>true</code> if the
* current selection is not empty.
*/
@Override
public boolean isCopyEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getSelectionCount() > 0;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method returns <code>true</code> if the
* current selection is not empty.
*/
@Override
public boolean isCutEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getSelectionCount() > 0;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method returns <code>true</code> if
there is
* a selection or if the caret is not positioned at the end of the
text.
*/
@Override
public boolean isDeleteEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getSelectionCount() > 0 || text.getCaretPosition()
< text.getCharCount();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method always returns <code>true</code>.
*/
@Override
public boolean isPasteEnabled() {
if (text == null || text.isDisposed())
return false;
return true;
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method always returns <code>true</code>.
*/
public boolean isSaveAllEnabled() {
if (text == null || text.isDisposed())
return false;
return true;
}

/**
* Returns <code>true</code> if this cell editor is able to
perform the
* select all action.
* <p>
* This default implementation always returns <code>false</code>.
* </p>
* <p>
* Subclasses may override
* </p>
*
* @return <code>true</code> if select all is possible,
* <code>false</code> otherwise
*/
@Override
public boolean isSelectAllEnabled() {
if (text == null || text.isDisposed())
return false;
return text.getCharCount() > 0;
}

/**
* Processes a key release event that occurred in this cell editor.
* <p>
* The <code>TextCellEditor</code> implementation of this framework
* method ignores when the RETURN key is pressed since this is
handled
* in <code>handleDefaultSelection</code>. An exception is made for
* Ctrl+Enter for multi-line texts, since a default selection
event is
* not sent in this case.
* </p>
*
* @param keyEvent
* the key event
*/
@Override
protected void keyReleaseOccured(KeyEvent keyEvent) {
if (keyEvent.character == '\r') { // Return key
// Enter is handled in handleDefaultSelection.
// Do not apply the editor value in response to an Enter
key
// event
// since this can be received from the IME when the intent
is
// -not-
// to apply the value.
// See bug 39074 [CellEditors] [DBCS] canna input mode
fires
// bogus
// event
// from Text Control
//
// An exception is made for Ctrl+Enter for multi-line
texts,
// since
// a default selection event is not sent in this case.
if (text != null && !text.isDisposed() && (text.getStyle()
& SWT.MULTI) != 0) {
if ((keyEvent.stateMask & SWT.CTRL) != 0) {
super.keyReleaseOccured(keyEvent);
}
}
return;
}
super.keyReleaseOccured(keyEvent);
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method copies the current selection to
the
* clipboard.
*/
@Override
public void performCopy() {
text.copy();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method cuts the current selection to the
* clipboard.
*/
@Override
public void performCut() {
text.cut();
checkSelection();
checkDeleteable();
checkSelectable();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method deletes the current selection
or, if
* there is no selection, the character next character from the
current
* position.
*/
@Override
public void performDelete() {
if (text.getSelectionCount() > 0)
// remove the contents of the current selection
text.insert(""); //$NON-NLS-1$
else {
// remove the next character
int pos = text.getCaretPosition();
if (pos < text.getCharCount()) {
text.setSelection(pos, pos + 1);
text.insert(""); //$NON-NLS-1$
}
}
checkSelection();
checkDeleteable();
checkSelectable();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method pastes the the clipboard
contents over
* the current selection.
*/
@Override
public void performPaste() {
text.paste();
checkSelection();
checkDeleteable();
checkSelectable();
}

/**
* The <code>TextCellEditor</code> implementation of this
* <code>CellEditor</code> method selects all of the current text.
*/
@Override
public void performSelectAll() {
text.selectAll();
checkSelection();
checkDeleteable();
}

}

public class ComboCellEditor extends DialogCellEditor {

public ComboCellEditor(final Composite parent, final int style) {
super(parent, style);
}

/**
* return text editor
*
* @return
*/
public Text getText() {
return super.text;
}

@Override
protected Object openDialogBox(final Control cellEditorWindow) {
MessageBox messageBox = new
MessageBox(cellEditorWindow.getShell());
messageBox.open();

return "test";
}
}

private class ColumnLableProvider extends ColumnLabelProvider {

private static final String dialog = "Dialog";

public ColumnLableProvider(ColumnViewer viewer) {
if (JFaceResources.getImageRegistry().get(dialog) == null) {
JFaceResources.getImageRegistry().put(dialog,
makeShot(viewer.getControl()));
} else {
JFaceResources.getImageRegistry().get(dialog).dispose();
JFaceResources.getImageRegistry().remove(dialog);
JFaceResources.getImageRegistry().put(dialog,
makeShot(viewer.getControl()));
}
}

private Image makeShot(Control control) {
Shell shell = new Shell(control.getShell(), SWT.NO_TRIM);

// otherwise we have a default gray color
Color backgroundColor = control.getBackground();
shell.setBackground(backgroundColor);

Button dialogButton = new Button(shell, SWT.DOWN);
dialogButton.setBackground(backgroundColor);
dialogButton.setText("...");
// otherwise an image is located in a corner
dialogButton.setLocation(1, 1);
Point bsize = dialogButton.computeSize(SWT.DEFAULT,
SWT.DEFAULT);
bsize.x = Math.max(bsize.x - 1, bsize.y - 1);
bsize.y = Math.max(bsize.x - 1, bsize.y - 1);

// otherwise an image is stretched by width
dialogButton.setSize(bsize);
shell.setSize(bsize);

shell.open();
GC gc = new GC(shell);
Image image = new Image(control.getDisplay(), bsize.x,
bsize.y);
gc.copyArea(image, 0, 0);
gc.dispose();
shell.close();

return image;
}

@Override
public Image getImage(final Object element) {
return JFaceResources.getImageRegistry().get(dialog);
}

@Override
public void update(final ViewerCell cell) {
Object element = cell.getElement();
cell.setText(element.toString());
Image image = getImage(element);
cell.setImage(image);
cell.setBackground(getBackground(element));
cell.setForeground(getForeground(element));
cell.setFont(getFont(element));
}

}
}
Previous Topic:Tables and Checkboxes
Next Topic:to Platform Debug experts
Goto Forum:
  


Current Time: Fri Sep 27 07:21:46 GMT 2024

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

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

Back to the top