JFace DialogCellEditor: how to make buttons always appear? - java

I use JFace DialogCellEditor to show a button in a cell of a row of my JFace TableViewer which triggers a dialog when activated. This behaviour works well with the following code but the button only appears when the cell of the table hosting the button is explicitly selected.
public class CompareDialogCellEditor extends DialogCellEditor {
public CompareDialogCellEditor(Composite parent) {
super(parent);
}
#Override
protected Button createButton(Composite parent) {
Button button = super.createButton(parent);
button.setText("");
button.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKeys.COMPARE_ICON).createImage());
return button;
}
#Override
protected Object openDialogBox(Control cellEditorWindow) {
MessageDialog.openInformation(cellEditorWindow.getShell(), "Test", "It works");
return null;
}
}
Is there a way to force the button to always appear in the table and not only when the cell is selected? (the same behaviour goes for a label set by the overridden method setContents(...) )
Thanks

You can only edit one Viewer cell at a time. Viewer won't support editing multiple cells at a time unless you do some customization.
I can think of following solutions.
Paint widget ( button, text, combo..etc) like image on table cell and invoke
CellEditor when user activates it.
You can find some examples here about how to paint on Table Cell.
http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
I posted an answer about how to show button in table cell here. you can following the same concept with CellEditor
SWT - Tableviewer adding a remove button to a column in the table

Related

Lose Focus and Commit Edit on a JSpinner when clicking outside the spinner

I have a JTable, whose cell editors are JSpinners. I'm using a custom cell editor class to accomplish this. I have a few other components in my JPanel. My problem arises when the user is editing one of the cells (i.e. a JSpinner has focus) and then interacts with one of the other components without first pressing enter or losing focus. I want the JSpinner to immediately lose focus and commit the changes before running the code associated with the other components, but instead the JSpinner just retains its focus.
Ideally, I would like the JSpinner to lose focus immediately whenever the user clicks anywhere but inside the JSpinner itself. Here's my custom editor class:
public class PhyMappingTableCellEditor extends AbstractCellEditor implements TableCellEditor {
final JSpinner spinner = new JSpinner();
public PhyMappingTableCellEditor(ArrayList<String> phys) {
spinner.setModel(new SpinnerListModel(phys));
spinner.setBorder(new EmptyBorder(0,0,0,0));
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int row, int column) {
spinner.setValue(value);
return spinner;
}
#Override
public Object getCellEditorValue() {
return spinner.getValue();
}
}
Thanks in advance!
Assuming your custom editor works correctly when you tab from cell to cell then the problem is that you need to manually stop editing on the cell when the table loses focus.
Check out Table Stop Editing. It shows how to stop editing on a cell when the table loses focus. The solution will work for any cell that is being edited, not just your custom editor.
I had a similar problem. If the spinner value was edited using the keyboard and then you clicked another table cell with out first pressing enter, the edits were lost. I solved it by overriding AbstractCellEditor.stopCellEditing() like this:
#Override
public boolean stopCellEditing() {
try {
spinner.commitEdit();
}
catch (ParseException ex) {
// Do nothing
}
return super.stopCellEditing();
}

Showing Edit / Delete buttons for each row in a GWT CellTable?

I have a cell table showing some data. For each row, I want to have two columns which contain edit / delete buttons. When each button is clicked, it should be able to notify a listener which button was clicked (and preferably also be able to pass in the object that row is associated with).
How can I do this? Specifically, I know how to render a button, but how can I process the on-click event and pass in the object which the user clicked to edit or delete?
This is the standard approach:
myTable.addCellPreviewHandler(new Handler<MyObject>() {
#Override
public void onCellPreview(CellPreviewEvent<MyObject> event) {
if ("click".equals(event.getNativeEvent().getType())) {
if (event.getColumn() == 0 || event.getColumn() == 1) {
MyObject object = event.getValue();
Window.alert("Column clicked: " + event.getColumn());
}
}
}
});
This is a more efficient solution, because you only have one handler attached to a table, instead of trying to attach a handler to each button in each row.
I think you can make a foreach through all the rows in the celltable (I never worked with celltables)
And then you can add your own ClickHandler to the Button.
Something like that (not tested):
final int row = myrow; // add the row value or a object identifier or similar
Button delete_button = new Button("delete");
delete_button.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// Insert your delete funciton
delete(row);
}
});
You mentioned a Listener, Listener are depreciated, use Handler instead.

Arranging the checkbox in checkboxtableviewer horizontally in eclipse e4

I am using eclipse e4 application. I am displaying a checkboxtableviewer in a Part. Now the checkbox are arranged vertically like the following pattern below.
checkbox MIN
checkbox MAX
checkbox AVG
checkbox COUNT
the following is the code snippet I have used.
public class StatisticsPart {
private CheckboxTableViewer tableViewer;
public Object[] statisticsSelected;
#Inject
public StatisticsPart() {
//TODO Your code here
}
#PostConstruct
public void postConstruct(Composite parent) {
tableViewer = new CheckboxTableViewer(parent, SWT.BORDER|SWT.HORIZONTAL);
tableViewer.add("MIN");
tableViewer.add("MAX");
tableViewer.add("AVG");
tableViewer.add("COUNT");
tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
tableViewer.addCheckStateListener(new ICheckStateListener() {
#Override
public void checkStateChanged(CheckStateChangedEvent event) {
statisticsSelected = tableViewer.getCheckedElements();
}
});
}
}
I want to display the checkbox name horizontally and all the corresponding checkboxes below it like the following pattern.
MIN MAX AVG COUNT
checkbox checkbox checkbox checkbox
Can anyone please help how to do this.
Thanks in advance
CheckboxTableViewer only supports one check box per row.
You could use a normal TableViewer and define four columns.
Use a ColumnLabelProvider for each column and override the getImage method to provide a checked or unchecked image.
You can change the value by using a CheckboxCellEditor in the editing support for the column.

GWT: Editing Text Of Tree Item

When a user clicks a 'Add Node' button above a tree and the program adds a tree item below the selected node, I would like to insert the new tree item with the text highlight and ready for editing by the user... like labels in GMail. Any ideas?
--Kirt
Are you using the GWT default TreeItem? If so, when you add the node, you could add the TreeItem with a Widget which you write which contains a TextBox and a Button to save.
When the save button is clicked, it calls setText() on the tree item with the text box's text, thus removing the widgets from the tree item.
It may be an even better idea to subclass TreeItem to encapsulate this logic and provide more functionality.
edit: Here, just because I'm feeling generous...
public class EditableTreeItem extends TreeItem {
public EditableTreeItem() {
super();
TextBox textBox = new TextBox();
Button saveButton = new Button("Save");
saveButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent e) {
if (!textBox.getText().isEmpty()) {
EditableTreeItem.this.setText(textBox.getText());
}
}
});
}
}

Show TableViewer not in line, but in matrix order

I use the TableViewer to show informations in a table. The user can select one of the shown options by selecting one line of the table.
I want to create a table in matrix form, in which the user can not only select the line. It should be possible to select every item of the table, like row 2 column 3. For every item selection an action is called to handle this item as it is in the TableViewer.
As far as i now, i can add CellModifier and CellEditors to the line of Columns of the table, but the reference for the action is always the line object and not the selected TableItem.
Does somebody have an example how to create such a matrix inside a Composite?
I can create it by setting a GridLayout and adding the components in a for-loop, but than i get issues, when i want to redraw the Composite with new childrens. The TableViewer does already have this handling, so i dont want to implement it again.
I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.
MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);
public class ColumnSelectionMouseListener implements MouseListener {
private TableColumn selectedColumn;
#Override
public void mouseDoubleClick(MouseEvent e) {
// Nothing to do here
}
#Override
public void mouseDown(MouseEvent e) {
table = (Table) e.widget;
TableItem item = table.getItem(new Point(e.x, e.y));
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumn(i);
Rectangle bounds = item.getBounds(i);
if (bounds.contains(e.x, e.y)) {
selectedColumn = column;
}
}
}
#Override
public void mouseUp(MouseEvent e) {
// Nothing to do here
}
public TableColumn getSelectedField() {
return selectedColumn;
}
}
Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.
Hope this can help.
Manu
Maybe the following JFace snippet will help:
Snippet058CellNavigationIn34
Ingo

Categories

Resources