Password Style TextCellEditor showing original characters in TableViewer - eclipse

TextCellEditor passEdit has SWT.PASSWORD style. But when Add button was clicked original characters were shown instead of default * echo characters. Also focus listeners on passEdit were not working. i.e When user double clicks it should show original characters and on focus lost it should show password echo characters.
How to fix this?
public class UserAddSolution {
public static boolean flag = true;
private TextCellEditor userNameEdit;
private TextCellEditor passEdit;
class UserNamePassword {
private final String name;
private final String password;
public UserNamePassword(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
}
public UserAddSolution(final Shell shell) {
shell.setLayout(new GridLayout(2, false));
GridData layoutData = new GridData();
layoutData.heightHint = 300;
shell.setLayoutData(layoutData);
final TableViewer viewer = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION | SWT.MULTI);
viewer.setContentProvider(ArrayContentProvider.getInstance());
Table table = viewer.getTable();
table.setLayout(new GridLayout());
table.setLayoutData(layoutData);
userNameEdit = new TextCellEditor(table);
passEdit = new TextCellEditor(table, SWT.PASSWORD);
final Text tex = (Text) passEdit.getControl();
final char echar = tex.getEchoChar();
tex.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
tex.setEchoChar(echar);
}
#Override
public void focusGained(FocusEvent e) {
tex.setEchoChar('\0');
}
});
viewer.setCellEditors(new CellEditor[] { userNameEdit, passEdit });
viewer.setCellModifier(new ICellModifier() {
#Override
public boolean canModify(Object element, String property) {
return true;
}
#Override
public Object getValue(Object element, String property) {
UserNamePassword ele = (UserNamePassword) element;
if (property.equals("1")) {
return ele.getName();
} else if (property.equals("2")) {
return ele.getPassword();
}
return "";
}
#Override
public void modify(Object element, String property, Object value) {
}
});
viewer.setColumnProperties(new String[] { "1", "2" });
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
viewer) {
#Override
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};
int feature = ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
| ColumnViewerEditor.TABBING_VERTICAL
| ColumnViewerEditor.KEYBOARD_ACTIVATION;
TableViewerEditor.create(viewer, actSupport, feature);
TableViewerColumn userNameColumn;
userNameColumn = new TableViewerColumn(viewer, SWT.NONE);
userNameColumn.getColumn().setWidth(200);
userNameColumn.getColumn().setMoveable(true);
userNameColumn.getColumn().setText("Name");
userNameColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public Image getImage(Object element) {
return super.getImage(element);
}
#Override
public String getText(Object element) {
UserNamePassword fdf = (UserNamePassword) element;
return super.getText(fdf.getName());
}
});
TableViewerColumn passwordColumn;
passwordColumn = new TableViewerColumn(viewer, SWT.NONE);
passwordColumn.getColumn().setWidth(200);
passwordColumn.getColumn().setMoveable(true);
passwordColumn.getColumn().setText("Password");
passwordColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public Image getImage(Object element) {
return super.getImage(element);
}
#Override
public String getText(Object element) {
UserNamePassword fdf = (UserNamePassword) element;
return super.getText(fdf.getPassword());
}
#Override
public void update(ViewerCell cell) {
super.update(cell);
}
});
viewer.setInput(new ArrayList<UserNamePassword>());
table.setLinesVisible(true);
table.setHeaderVisible(true);
viewer.refresh();
Button add = new Button(shell, SWT.PUSH);
add.setText("Add");
add.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
viewer.add(new UserNamePassword("abc", "xyz"));
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new UserAddSolution(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

The ColumnLabelProvider is responsible for displaying the data in the table. The text widget appears only when you edit a cell. That's why you need to tell the label provider to mask characters and not to display the plain text.
As a consequence you don't need to set SWT.PASSWORD style to the passEdit since you want to display the password in the editor as plain text:
passEdit = new TextCellEditor(table, SWT.NONE);
You also don't need the FocusListener and you can set the echo char as a constant:
private static final String ECHARSTR = Character.toString((char)9679);
So you can remove 15 lines below the initialization of passEdit
I don't know if there is a solution for this in JFace (I think not) but you can easily solve this by modifying your existing label provider. My suggestion is to modify the getText() method of ColumnLabelProvider for passwordColumn as follows:
#Override
public String getText(Object element)
{
UserNamePassword fdf = (UserNamePassword)element;
return fdf.getPassword().replaceAll(".", ECHARSTR);
}

Related

how can we use combos in jface tableviewer columns

I am reading excel data and taking headers(only first row) as my columns and added to list and this list is added as input to my tableviewer to display data in my table.I am creating tablecolumns and added the label provider. I am able to see the data in the table. Now i want to display the all textboxes as combos in each column . How can i can add combos for each column? my code is like
my tableviewer class
public class TableViewerExample {
public TableViewerExample(Shell shell) {
TableViewer viewer = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
TableViewerColumn upDownColumn = new TableViewerColumn(viewer, SWT.NONE);
upDownColumn.getColumn().setText("Display Order");
upDownColumn.getColumn().setWidth(200);
upDownColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getDisplayOrder();
}
});
TableViewerColumn checkboxColumn = new TableViewerColumn(viewer, SWT.NONE);
checkboxColumn.getColumn().setText("Checkbox Selection");
checkboxColumn.getColumn().setWidth(200);
checkboxColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getCheckboxSelection();
}
});
TableViewerColumn uniqueFieldsColumn = new TableViewerColumn(viewer, SWT.NONE);
uniqueFieldsColumn.getColumn().setText("Unique Fields");
uniqueFieldsColumn.getColumn().setWidth(200);
uniqueFieldsColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getUniqueFields();
}
});
TableViewerColumn inputFieldNameColumn = new TableViewerColumn(viewer, SWT.NONE);
inputFieldNameColumn.getColumn().setText("Input Field Name");
inputFieldNameColumn.getColumn().setWidth(200);
inputFieldNameColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getInputFieldName();
}
});
TableViewerColumn dataTypeColumn = new TableViewerColumn(viewer, SWT.NONE);
dataTypeColumn.getColumn().setText("Data Type");
dataTypeColumn.getColumn().setWidth(200);
dataTypeColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getDataType();
}
});
TableViewerColumn inputFieldFormatColumn = new TableViewerColumn(viewer, SWT.NONE);
inputFieldFormatColumn.getColumn().setText("Input Field Format");
inputFieldFormatColumn.getColumn().setWidth(200);
inputFieldFormatColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getInputFieldFormat();
}
});
TableViewerColumn displayNameColumn = new TableViewerColumn(viewer, SWT.NONE);
displayNameColumn.getColumn().setText("Display Name");
displayNameColumn.getColumn().setWidth(200);
displayNameColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getDisplayName();
}
});
TableViewerColumn displayFormatColumn = new TableViewerColumn(viewer, SWT.NONE);
displayFormatColumn.getColumn().setText("Display Format");
displayFormatColumn.getColumn().setWidth(200);
displayFormatColumn.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
return ((DataSourceModel) element).getDisplayFormat();
}
});
viewer.setContentProvider(ArrayContentProvider.getInstance());
System.out.println(RetrieveExcelData.getDataSourceFieldsList());
viewer.setInput(RetrieveExcelData.getDataSourceFieldsList());
viewer.getTable().setLinesVisible(true);
viewer.getTable().setHeaderVisible(true);
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new TableViewerExample(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
setinput class
public class RetrieveExcelData {
public static List getDataSourceFieldsList() {
String filename = "D:\\ServiceOrders_point_Org_morethan500.xlsx";
List<List<XSSFCell>> sheetData = new ArrayList<>();
try (FileInputStream fis = new FileInputStream(filename)) {
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet myExcelSheet = workbook.getSheetAt(0);
XSSFRow row = (XSSFRow )myExcelSheet.getRow(0);
Iterator cells=row.cellIterator();
List<XSSFCell> data = new ArrayList<>();
while (cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return showExcelData(sheetData);
}
private static List showExcelData(List<List<XSSFCell>> sheetData) {
List<DataSourceModel> resultList = new ArrayList();
// Iterates the data and print it out to the console.
for (List<XSSFCell> data : sheetData) {
for (XSSFCell cell : data) {
DataSourceModel dataSourceModel=new DataSourceModel(cell.getStringCellValue(), cell.getStringCellValue(), cell.getStringCellValue(), cell.getStringCellValue(), cell.getStringCellValue(), cell.getStringCellValue(), cell.getStringCellValue(), cell.getStringCellValue());
resultList.add(dataSourceModel);
}
}
return resultList;
}
my model class
public class DataSourceModel {
private String displayOrder;
private String checkboxSelection;
private String uniqueFields;
private String inputFieldName;
private String dataType;
private String inputFieldFormat;
private String displayName;
private String displayFormat;
public DataSourceModel(String displayOrder,String checkboxSelection,String uniqueFields,String inputFieldName,String dataType, String inputFieldFormat, String displayName,String displayFormat) {
this.displayOrder=displayOrder;
this.checkboxSelection=checkboxSelection;
this.uniqueFields=uniqueFields;
this.inputFieldName=inputFieldName;
this.dataType=dataType;
this.inputFieldFormat=inputFieldFormat;
this.displayName=displayName;
this.displayFormat=displayFormat;
}
public String getDisplayOrder() {
return displayOrder;
}
public void setDisplayOrder(String displayOrder) {
this.displayOrder = displayOrder;
}
public String getCheckboxSelection() {
return checkboxSelection;
}
public void setCheckboxSelection(String checkboxSelection) {
this.checkboxSelection = checkboxSelection;
}
public String getUniqueFields() {
return uniqueFields;
}
public void setUniqueFields(String uniqueFields) {
this.uniqueFields = uniqueFields;
}
public String getInputFieldName() {
return inputFieldName;
}
public void setInputFieldName(String inputFieldName) {
this.inputFieldName = inputFieldName;
}
public String getDataType() {
return dataType;
}
public void setDataType(String dataType) {
this.dataType = dataType;
}
public String getInputFieldFormat() {
return inputFieldFormat;
}
public void setInputFieldFormat(String inputFieldFormat) {
this.inputFieldFormat = inputFieldFormat;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDisplayFormat() {
return displayFormat;
}
public void setDisplayFormat(String displayFormat) {
this.displayFormat = displayFormat;
}
and the table looks like
Now i want to make combos instead of text in the columns . Colud you please help how can i change to combos instead of text boxes using my code for all

Java: How do I get the text of two JLabels when copying JLabel's text with TransferHandler?

How do I get the text of two JLabels when copying JLabel's text with TransferHandler?
Label1111111 How can I keep the text of both JLabels when copied to Label2222222.
For this reason, I will take control of two Jlabel's texts. This shape can only get the text of JLabel, which was first held. Thank you in advance for your help.
public class deneme2 extends JFrame {
private static final int COPY = 0;
private static final int NONE = 0;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
deneme2 frame = new deneme2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public deneme2() {
JPanel panel= new JPanel();
MouseListener listener = new DragMouseAdapter();
JLabel label1 = new JLabel("Label1111111", JLabel.CENTER);
handlerLabel(label1);
label1.addMouseListener(listener);
panel.add(label1);
JLabel label2 = new JLabel("Label2222222", JLabel.CENTER);
handlerLabel(label2);
label2.addMouseListener(listener);
panel.add(label2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.add(panel);
setContentPane(contentPane);
}
private void handlerLabel (JLabel lbl)
{
lbl.setTransferHandler(new TransferHandler("text") {
#Override
protected void exportDone(JComponent source, Transferable data, int action) {
if (action == COPY){
((JLabel)lbl.getDropTarget().getDropTargetContext().getComponent()).getText();
//((JLabel) source).setText("LabelEmpty");
}
}
});
}
private class DragMouseAdapter extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
JComponent comp = (JComponent)e.getSource();
TransferHandler handler = comp.getTransferHandler();
handler.exportAsDrag(comp, e, TransferHandler.COPY);
}
}
}
Maybe in this way. I added inner class MyLabel and console output in your handlerLabel. Ctrl+v this under your main. It will print to console the original String property of each MyLabels.
public class MyLabel extends JLabel {
String original;
public MyLabel (String text)
{
super(text);
this.original=text;
}
public String getOriginal() {
return original;
}
public void setOriginal(String original) {
this.original = original;
}
}
public deneme2() {
JPanel panel= new JPanel();
MouseListener listener = (MouseListener) new DragMouseAdapter();
MyLabel label1 = new MyLabel("Label1111111");
handlerLabel(label1);
label1.addMouseListener(listener);
panel.add(label1);
MyLabel label2 = new MyLabel("Label2222222");
handlerLabel(label2);
label2.addMouseListener(listener);
panel.add(label2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.add(panel);
setContentPane(contentPane);
}
private void handlerLabel(MyLabel lbl) {
lbl.setTransferHandler(new TransferHandler("text") {
#Override
protected void exportDone(JComponent source, Transferable data, int action) {
if (action == COPY) {
System.out.println(((MyLabel) lbl.getDropTarget().getDropTargetContext().getComponent()).getOriginal());
}
}
});
}
Edit: check this out. Ctrl+V this under your main. It will print to console text of source label and text of target label, also the original property. Class MyLabel implements DropTargetListener , which is then registered by new DropTarget(this, this). Then we define what will happen in overriden drop method.
public class MyLabel extends JLabel implements DropTargetListener {
String original;
public MyLabel(String text) {
super(text);
this.original = text;
new DropTarget(this, this);
this.setTransferHandler(new TransferHandler("text"));
final MouseListener listener = new MouseAdapter() {
#Override
public void mousePressed(final MouseEvent me) {
final MyLabel label = (MyLabel) me.getSource();
final TransferHandler handler = label.getTransferHandler();
handler.exportAsDrag(label, me, TransferHandler.COPY);
}
};
this.addMouseListener(listener);
}
public String getOriginal() {
return original;
}
public void setOriginal(String original) {
this.original = original;
}
#Override
public void dragEnter(DropTargetDragEvent dtde) {
}
#Override
public void dragOver(DropTargetDragEvent dtde) {
}
#Override
public void dropActionChanged(DropTargetDragEvent dtde) {
}
#Override
public void dragExit(DropTargetEvent dte) {
}
#Override
public void drop(DropTargetDropEvent dtde) {
try {
final String sourceString = (String) dtde.getTransferable().getTransferData(new DataFlavor("application/x-java-jvm-local-objectref; class=java.lang.String"));
System.out.println("Source: " + sourceString + " target: " + this.getText());
this.setText(sourceString);
System.out.println("Original target: "+this.getOriginal());
} catch (final UnsupportedFlavorException | IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public deneme2() {
JPanel panel = new JPanel();
MyLabel label1 = new MyLabel("Label1111111a");
panel.add(label1);
MyLabel label2 = new MyLabel("Label2222222b");
panel.add(label2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.add(panel);
setContentPane(contentPane);
}
}
EDIT2: Following code generates this result see result at the end of this answer:
or
It also prints original property to console. Those could be organised differently (MyLabel, MyLabelTransferable, MyLabelDropTargetListener, MyLabelTransferHandler classes, just to give an idea for future refactoring) but it works in this way too. Consider it a quickfix for your use case.
So main class is this:
public class Deneme2 extends JFrame {
private static final int COPY = 0;
private static final int NONE = 0;
public static void main(String[] args) {
Deneme2 mainFrame = new Deneme2();
SwingUtilities.invokeLater(() -> {//let's get that frame on EDT rollin lambda style:)
mainFrame.setVisible(true);
});
}
public Deneme2() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
MyLabel label1 = new MyLabel("Label1111111a");
panel.add(label1, BorderLayout.WEST);
MyLabel label2 = new MyLabel("Label2222222b");
panel.add(label2, BorderLayout.EAST);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 450, 300);
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
this.add(panel);
}
}
Then MyLabel.java :
public class MyLabel extends JLabel implements DropTargetListener, Transferable {
String original;
protected static final DataFlavor MYLABEL_DATA_FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + "; class=\"" + MyLabel.class.getCanonicalName() + "\"",
"MyLabel label");
protected static final DataFlavor[] SUPPORTED_FLAVORS = {MYLABEL_DATA_FLAVOR};
public MyLabel(String text) {
super(text);
this.original = text;
new DropTarget(this, this);
this.setTransferHandler(new MyLabelTransferHandler()); //here we use our custom TransferHandler
final MouseListener listener = new MouseAdapter() {
#Override
public void mousePressed(final MouseEvent me) {
final MyLabel label = (MyLabel) me.getSource();
final TransferHandler handler = label.getTransferHandler();
handler.exportAsDrag(label, me, TransferHandler.COPY);
}
};
this.addMouseListener(listener);
}
public String getOriginal() {
return original;
}
public void setOriginal(String original) {
this.original = original;
}
#Override
public void dragEnter(DropTargetDragEvent dtde) {
if (dtde.getTransferable().isDataFlavorSupported(MyLabel.MYLABEL_DATA_FLAVOR)) {
System.out.println("Drop accept - MyLabel");
dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
} else {
dtde.rejectDrag();
}
}
#Override
public void dragOver(DropTargetDragEvent dtde) {
//System.out.println("Drag over");
}
#Override
public void dropActionChanged(DropTargetDragEvent dtde) {
System.out.println("Action changed");
}
#Override
public void dragExit(DropTargetEvent dte) {
System.out.println("Exited");
}
#Override
public void drop(DropTargetDropEvent dtde) {
System.out.println("Drop detected");
if (dtde.getTransferable().isDataFlavorSupported(MyLabel.MYLABEL_DATA_FLAVOR)) {
Transferable t = dtde.getTransferable();
if (t.isDataFlavorSupported(MyLabel.MYLABEL_DATA_FLAVOR)) {
try {
Object transferData = t.getTransferData(MyLabel.MYLABEL_DATA_FLAVOR);
if (transferData instanceof MyLabel) {
MyLabel mySourceLabel = (MyLabel) transferData;
if (!(mySourceLabel.equals(this))) {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
this.setText(mySourceLabel.getText());
mySourceLabel.setText("Empty");
System.out.println(mySourceLabel.getOriginal() + " " + this.getOriginal());
} else {
dtde.rejectDrop();
System.out.println("Drop rejected - the same MyLabel");
}
} else {
dtde.rejectDrop();
}
} catch (UnsupportedFlavorException | IOException ex) {
dtde.rejectDrop();
}
} else {
dtde.rejectDrop();
}
}
}
#Override
public DataFlavor[] getTransferDataFlavors() {
return SUPPORTED_FLAVORS;
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(MYLABEL_DATA_FLAVOR) || flavor.equals(DataFlavor.stringFlavor);
}
#Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor.equals(MYLABEL_DATA_FLAVOR)) {
return this;
} else if (flavor.equals(DataFlavor.stringFlavor)) {
return this.getText();
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
And then MyLabelTransferHandler.java :
public class MyLabelTransferHandler extends TransferHandler {
#Override
public boolean canImport(TransferHandler.TransferSupport support) {
return (support.getComponent() instanceof MyLabel) && support.isDataFlavorSupported(MyLabel.MYLABEL_DATA_FLAVOR);
}
#Override
public boolean importData(JComponent src, Transferable transferable) {
return src instanceof MyLabel;
}
#Override
public int getSourceActions(JComponent c) {
return DnDConstants.ACTION_COPY;
}
#Override
protected Transferable createTransferable(JComponent c) {
Transferable t = (MyLabel)c;
return t;
}
#Override
protected void exportDone(JComponent source, Transferable data, int action) {
System.out.println("Export done.");
}
}
After final edit result looks like this:
Little clarification:
protected static final DataFlavor MYLABEL_DATA_FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + "; class=\"" + MyLabel.class.getCanonicalName() + "\"",
"MyLabel label");
in MyLabel. That's important line. This will make getTransferData() return the same instance of MyLabel. If you would do it like:
protected static final DataFlavor MYLABEL_DATA_FLAVOR = new DataFlavor(MyLabel.class, "MyLabel label");
you won't be able to change mySourceLabel text to "Empty" in overridden drop() method, since you would be given a copy of that object.
Also Why shouldn't you extend JFrame and other components? . And you can provide checking for "Empty" text (if getText() returns "Empty", then don't change text in target MyLabel).

assertion failed: Unknown column layout data

When I create combo box in particular only one 3rd column using table viewer in Eclipse SWT.
I think I've done everything ok until now, however when I compile the code then i get error:
Code:
public void createPartControl(Composite parent) {
Composite tableComposite = new Composite(parent, SWT.NONE);
tableColumnLayout = new TableColumnLayout();
tableComposite.setLayout(tableColumnLayout);
tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true));
tableViewer = new TableViewer(tableComposite, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
// TODO viewer.setLabelProvider(new ViewLabelProvider());
table = tableViewer.getTable();
// Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
String[] titles = { "Threat Name", "Category Name", "Status",
"Priority", "Description", "Justification" };
int[] bounds = { 100, 100, 100, 100 };
TableViewerColumn col = createTableViewerColumn(titles[2], bounds[2], 2);
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
Dummy p = (Dummy) element;
return p.getValue();
}
});
col.setEditingSupport(new FirstValueEditingSupport(tableViewer));
}
private SelectionAdapter getSelectionAdapter(final TableColumn column,
final int index) {
SelectionAdapter selectionAdapter = new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
tableViewer.refresh();
}
};
return selectionAdapter;
}
private static class Dummy {
public String value;
public Dummy(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static class FirstValueEditingSupport extends EditingSupport {
private final TableViewer viewer;
private final CellEditor editor;
private final String[] possibleValues = { "Mitigated",
"Not Applicable", "Not Started", "Needs Investigation" };
public FirstValueEditingSupport(TableViewer viewer) {
super(viewer);
this.viewer = viewer;
this.editor = new ComboBoxCellEditor(viewer.getTable(),
possibleValues);
}
#Override
protected CellEditor getCellEditor(Object element) {
return editor;
}
#Override
protected boolean canEdit(Object element) {
return true;
}
#Override
protected Object getValue(Object element) {
Dummy dummy = (Dummy) element;
int index = 0;
for (int i = 0; i < possibleValues.length; i++) {
if (Objects.equals(possibleValues[i], dummy.getValue())) {
index = i;
break;
}
}
return index;
}
#Override
protected void setValue(Object element, Object value) {
Dummy dummy = (Dummy) element;
int index = (Integer) value;
dummy.setValue(possibleValues[index]);
viewer.update(element, null);
}
}
private void fillRows(String shortdesc, String categ, String descp) {
System.out.println("fillRows call from above method.");
TableColumn status_Name_Col = tableViewer.getTable().getColumn(2);
System.out.println("**************** status_Name_Col ************ "
+ status_Name_Col);
tableViewer
.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(
SelectionChangedEvent selectionChangedEvent) {
StructuredSelection selection = (StructuredSelection) selectionChangedEvent
.getSelection();
System.out.println(((Dummy) selection.getFirstElement())
.getValue());
}
});
List<Dummy> elements = new ArrayList<>();
for (int i = 0; i < Connection.Number_Of_Connection; i++) {
elements.add(new Dummy("First option"));
}
tableViewer.setInput(elements);
tableColumnLayout.setColumnData(status_Name_Col, new ColumnWeightData(
1, true));
tableViewerColumn.setEditingSupport(new FirstValueEditingSupport(
tableViewer));
}
Questions:
how to display combo box in particular only one column?
You must not do the TableViewer setInput call until you have set up everything on the table. All the content providers, label providers, column layouts, editing support, .... must be set before setInput is called.
You must also call setColumnData for every column you create.

add wicket panel in #oninitilize

Hi i have been strugguling with this for a while. could you please suggest changes.
public class JobDetails extends Panel implements Serializable {
private static Logger LOGGER = Logger.getLogger(JobDetails.class);
public static final long serialVersionUID = 42L;
private List<Job> list;
#Override
protected void onInitialize() {
super.onInitialize();
}
public JobDetails(String id, final PageParameters params) {
super(id);
FeedbackPanel feedbackpanel = new FeedbackPanel("feedbackpanel");
add(feedbackpanel);
String JOBNUMBER = params.get("jobnumber").toString();
String OBJECTTYPE = params.get("objecttype").toString();
String OBJECTNUMBER = params.get("objectnumber").toString();
if (JOBNUMBER != null) {
LOGGER.info("JOBNUMBER != null");
list = Utils.retrieve(JOBNUMBER);
} else {
list = Utils.retrieve(OBJECTTYPE, OBJECTNUMBER);
}
DataView dataView = new DataView("jobs", new ListDataProvider(list)) {
#Override
public void onConfigure() {
super.onConfigure();
setVisible(getDataProvider().size() > 0);
}
#Override
protected void populateItem(final Item item) {
final Job job = (Job) item.getModelObject();
Link plink = new Link("parentJobLink") {
#Override
public void onClick() {
PageParameters p2 = new PageParameters();
p2.add("jobNumber", job.getParentJob());
JobDetails.this.replaceWith(new ParentJobDetails("innerpanel", p2));
}
};
plink.add(new Label("parentJobLabel", job.getParentJob()));
item.add(plink);
item.add(new Label("jobType", job.getJobType()));
item.add(new Label("whoSubmitted", job.getWhoSubmitted()));
item.add(new Label("objectType", job.getObjectType()));
item.add(new Label("objectNumber", job.getObjectNumber()));
item.add(new Label("objectRevision", job.getObjectRevision()));
item.add(new Label("jobStatus", job.getJobStatus()));
}
};
dataView.setItemsPerPage(20);
add(dataView);
add(new CustomPagingNavigator("navigator", dataView));
if (list.size() == 0) {
***"Replace the Current Panel with new(SearchInnerPanel("innerpanel", params)"***
}
}
}
Scenario: when i search for a job, if a job exists job is displayed in this panel and if the job does not exist it is redirected back to the search panel. i am unable to redirect back to the search panel.
Here is how the code should look really like:
public class JobDetails extends Panel {
private static final Logger LOGGER = Logger.getLogger(JobDetails.class);
public static final long serialVersionUID = 42L;
private List<Job> list;
public JobDetails(String id, final PageParameters params) {
super(id);
FeedbackPanel feedbackpanel = new FeedbackPanel("feedbackpanel");
add(feedbackpanel);
}
#Override
protected void onInitialize() {
super.onInitialize();
PageParameters params = getPage().getPageParameters();
String JOBNUMBER = params.get("jobnumber").toString();
String OBJECTTYPE = params.get("objecttype").toString();
String OBJECTNUMBER = params.get("objectnumber").toString();
if (JOBNUMBER != null) {
LOGGER.info("JOBNUMBER != null");
list = Utils.retrieve(JOBNUMBER);
} else {
list = Utils.retrieve(OBJECTTYPE, OBJECTNUMBER);
}
DataView dataView = new DataView("jobs", new ListDataProvider(list)) {
#Override
public void onConfigure() {
super.onConfigure();
setVisible(getDataProvider().size() > 0);
}
#Override
protected void populateItem(final Item item) {
final Job job = (Job) item.getModelObject();
Link plink = new Link("parentJobLink") {
#Override
public void onClick() {
PageParameters p2 = new PageParameters();
p2.add("jobNumber", job.getParentJob());
JobDetails.this.replaceWith(new ParentJobDetails("innerpanel", p2));
}
};
plink.add(new Label("parentJobLabel", job.getParentJob()));
item.add(plink);
item.add(new Label("jobType", job.getJobType()));
item.add(new Label("whoSubmitted", job.getWhoSubmitted()));
item.add(new Label("objectType", job.getObjectType()));
item.add(new Label("objectNumber", job.getObjectNumber()));
item.add(new Label("objectRevision", job.getObjectRevision()));
item.add(new Label("jobStatus", job.getJobStatus()));
}
};
dataView.setItemsPerPage(20);
add(dataView);
add(new CustomPagingNavigator("navigator", dataView));
if (list.size() == 0) {
replaceWith(new(SearchInnerPanel("innerpanel", params));
}
}
}
But it would be much better if you move the code that retrieves the list to the parent of this panel. If there are items in the list then use JobDetails panel, otherwise use SearchInnerPanel

CheckboxCell, MultiSelectionModel unwantonly reset DataGrid's data

Using GWT 2.4...
I am building upon a complex Composite dual view/edit mode implementation that is backed GWT's DataGrid and MultiSelectionModel. My goal is for a user to be able to click a checkbox in each row that they'd like to post updates for.
Here's a screenshot from a semi-functional interface:
Note the selected (highlighted) rows.
Now the problem is that when I type something in any of the cells (e.g., the first row's $ cell under the $/Mw 1 composite cell header), then click that row's checkbox (or any other row's checkbox for that matter) to select or de-select, the value gets reset to the original value when the screen's data was first requested. Not desired behavior by any stretch!
Let's take a look at my custom implementation for the grid. (Excuse the length).
public abstract class ToggleableGrid<T extends Identifiable<?>> extends Composite {
private static final int CHKBOX_COLUMN_WIDTH = App.INSTANCE.checkboxColumnWidth();
private static final DisplayMode DEFAULT_MODE = DisplayMode.VIEW;
private ProvidesKey<T> keyProvider;
private DataGrid<T> grid;
private MultiSelectionModel<T> selectionModel;
private ListDataProvider<T> dataProvider;
private int tabIndex = 0;
public ToggleableGrid() {
final DataGridConfiguration config = new DefaultDataGridConfiguration();
initGrid(config);
}
public ToggleableGrid(DataGridConfiguration config) {
initGrid(config);
}
private void initGrid(DataGridConfiguration config) {
keyProvider = new ProvidesKey<T>() {
#Override
public Object getKey(T item) {
return item == null ? null : item.getId();
}
};
grid = new DataGrid<T>(config.getPageSize(), config.getResources(), keyProvider);
// Set the message to display when the table is empty.
grid.setEmptyTableWidget(new Label(UiMessages.INSTANCE.no_results()));
initWidget(grid);
setVisible(true);
}
public void setInput(List<T> content) {
setInput(content, DEFAULT_MODE);
}
public void setInput(List<T> content, DisplayMode mode) {
resetTableColumns();
if (isInEditMode(mode)) {
// Add a selection model so we can select cells
selectionModel = new MultiSelectionModel<T>(keyProvider);
grid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<T> createCheckboxManager(0));
addRowSelector();
}
dataProvider = new ListDataProvider<T>(content);
final ListHandler<T> sortHandler = new ListHandler<T>(dataProvider.getList());
grid.addColumnSortHandler(sortHandler);
initializeStructure(constructMetadata(), sortHandler, mode);
dataProvider.addDataDisplay(grid);
}
// see https://stackoverflow.com/questions/3772480/remove-all-columns-from-a-celltable
// concrete classes are forced to maintain a handle on all columns added
private void resetTableColumns() {
for (final Column<T, ?> column: allColumns()) {
grid.removeColumn(column);
}
allColumns().clear();
}
protected boolean isInEditMode(DisplayMode currentDisplayMode) {
boolean result = false;
if (currentDisplayMode.equals(DisplayMode.EDIT)) {
result = true;
}
return result;
}
protected abstract Set<Column<T, ?>> allColumns();
protected abstract TableMetadata constructMetadata();
protected abstract void initializeStructure(TableMetadata metadata, ListHandler<T> sortHandler, DisplayMode mode);
protected void setColumnHorizontalAlignment(Column<T, ?> column, HorizontalAlignmentConstant alignment) {
column.setHorizontalAlignment(alignment);
}
// TODO figure out how to add a checkbox to column header that provides select/de-select all capability
// see https://stackoverflow.com/questions/6174689/gwt-celltable-programmatically-select-checkboxcell
protected void addRowSelector() {
final Column<T, Boolean> rowSelectColumn = new Column<T, Boolean>(new CheckboxCell(true, false)) {
#Override
public Boolean getValue(T value) {
Boolean result;
// check for null value and return null;
if(value == null || value.getId() == null) {
result = null;
} else { // get value from the selection model
result = selectionModel.isSelected(value);
}
return result;
}
};
addColumn(rowSelectColumn, UiMessages.INSTANCE.select());
setColumnWidth(rowSelectColumn, CHKBOX_COLUMN_WIDTH, Unit.PX);
setColumnHorizontalAlignment(rowSelectColumn, HasHorizontalAlignment.ALIGN_CENTER);
}
protected void setColumnWidth(Column<T, ?> column, int width, Unit unit) {
grid.setColumnWidth(column, width, unit);
}
protected void addColumn(Column<T, ?> column, String columnHeaderName) {
addColumn(column, columnHeaderName, HasHorizontalAlignment.ALIGN_RIGHT);
}
protected void addColumn(Column<T, ?> column, String columnHeaderName, HorizontalAlignmentConstant alignment) {
final SafeHtmlBuilder sb = new SafeHtmlBuilder();
final String divStart = "<div align=\""+ alignment.getTextAlignString() + "\" class=\"" +UiResources.INSTANCE.style().word_wrap() + "\">";
sb.appendHtmlConstant(divStart).appendEscaped(columnHeaderName).appendHtmlConstant("</div>");
final SafeHtml header = sb.toSafeHtml();
grid.addColumn(column, header);
allColumns().add(column);
}
protected CompositeCell<T> generateCompositeCell(final List<HasCell<T, ?>> hasCells) {
final CompositeCell<T> compositeCell = new CompositeCell<T>(hasCells) {
#Override
public void render(Context context, T value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<table><tbody><tr>");
super.render(context, value, sb);
sb.appendHtmlConstant("</tr></tbody></table>");
}
#Override
protected Element getContainerElement(Element parent) {
// Return the first TR element in the table.
return parent.getFirstChildElement().getFirstChildElement().getFirstChildElement();
}
#Override
protected <X> void render(Context context, T value,
SafeHtmlBuilder sb, HasCell<T, X> hasCell) {
final Cell<X> cell = hasCell.getCell();
sb.appendHtmlConstant("<td>");
cell.render(context, hasCell.getValue(value), sb);
sb.appendHtmlConstant("</td>");
}
};
return compositeCell;
}
// FIXME not working quite the way we'd expect, index incremented within column for each row, not each row by column
protected int nextTabIndex() {
tabIndex++;
return tabIndex;
}
protected AbstractCellTable<T> getGrid() {
return grid;
}
/**
* Gets the selected (row(s) of) data from grid (used in edit mode)
* #return the selected data (as per selection model)
*/
public List<T> getSelectedData() {
final List<T> data = new ArrayList<T>();
data.addAll(selectionModel.getSelectedSet());
return data;
}
/**
* Gets all (row(s) of) data in grid (used in edit mode)
* #return all data as list
*/
public List<T> getAllData() {
return dataProvider.getList();
}
/**
* Clears the currently selected (row(s) of) data (used in edit mode)
*/
public void clearSelectedData() {
selectionModel.clear();
grid.redraw();
}
}
So, the interesting methods to stare at above (I think) are setInput, generateCompositeCell and addRowSelector.
We initialize the grid with List data and set a display mode in setInput. It's here as well that the selection model is initialized. It uses GWT's DefaultSelectionEventManager createCheckboxManager().
I've been trying to grok the event model, but it eludes me. I've visited the following sources online, but have come up short on avenues to solving this problem.
-- https://groups.google.com/forum/?fromgroups#!topic/google-web-toolkit/k5sfURxDaVg
AbstractInputCell's getConsumedEventsImpl adds focus, blur and keydown, so this (I believe) is not a track I need to explore
-- GWT CellTable programmatically select CheckBoxCell
The various ways you can instantiate a CheckBoxCell got me curious, and I've tried many constructor argument permutations, but the one I settled on (true, false) is (I believe) the right one
Agreeing here and now (before being reprimanded) that there's perhaps some unnecessary complexity in my implementation, but I am looking for guidance nonetheless. Thanks!
Update
If it helps here's an impl of the aforementioned ToggleableGrid. If anything it gives you more detail on what goes into each CompositeCell. For details on AbstractValidatableColumn and ValidatableInputCell, see: In search of a GWT validation example... where art thou?.
public class EnergyOfferGrid extends ToggleableGrid<EnergyOfferDTO> {
public EnergyOfferGrid() {
super();
}
public EnergyOfferGrid(DataGridConfiguration config) {
super(config);
}
private static final int MAX_NUMBER_OF_MW_PRICE_POINTS = App.INSTANCE.maxNoOfMwPricePoints();
private Set<Column<EnergyOfferDTO, ?>> columns = new HashSet<Column<EnergyOfferDTO, ?>>();
#Override
protected Set<Column<EnergyOfferDTO, ?>> allColumns() {
return columns;
}
#Override
protected TableMetadata constructMetadata() {
final TableMetadata metadata = new TableMetadata();
// TODO Consider a predefined set of ReferenceData to be held in a common package
// Use Slope
metadata.addColumnMetadata(UiMessages.INSTANCE.use_slope(), new String[] {UiMessages.INSTANCE.yes(), UiMessages.INSTANCE.no()}, new String[] {"true", "false"});
return metadata;
}
#Override
protected void initializeStructure(TableMetadata metadata, ListHandler<EnergyOfferDTO> sortHandler, DisplayMode currentDisplayMode) {
addHourColumn(sortHandler);
addUseSlopeColumn(metadata, sortHandler, currentDisplayMode);
for (int i = 0; i < MAX_NUMBER_OF_MW_PRICE_POINTS; i++) { // zero-based indexing
addPriceMwColumn(i, currentDisplayMode);
}
}
protected void addHourColumn(ListHandler<EnergyOfferDTO> sortHandler) {
final Column<EnergyOfferDTO, String> hourColumn = new Column<EnergyOfferDTO, String>(new TextCell()) {
#Override
public String getValue(EnergyOfferDTO energyOffer) {
String result = "";
if (energyOffer.getId() != null) {
final String isoDateTime = energyOffer.getId().getOperatingHour();
if (isoDateTime != null && !isoDateTime.isEmpty()) {
final Date dateTime = CSTimeUtil.isoToDate(isoDateTime);
if (dateTime != null) {
result = CSTimeUtil.dateToHour(dateTime);
}
}
}
return result;
}
};
hourColumn.setSortable(true);
sortHandler.setComparator(hourColumn, new Comparator<EnergyOfferDTO>() {
#Override
public int compare(EnergyOfferDTO eo1, EnergyOfferDTO eo2) {
final String date1 = eo1.getId() != null ? eo1.getId().getOperatingHour() : "";
final String date2 = eo2.getId() != null ? eo2.getId().getOperatingHour() : "";
return date1.compareTo(date2);
}
});
// We know that the data is sorted by hour by default.
getGrid(). getColumnSortList().push(hourColumn);
addColumn(hourColumn, UiMessages.INSTANCE.hour());
setColumnWidth(hourColumn, 45, Unit.PX);
setColumnHorizontalAlignment(hourColumn, HasHorizontalAlignment.ALIGN_RIGHT);
}
protected void addUseSlopeColumn(TableMetadata metadata, ListHandler<EnergyOfferDTO> sortHandler, DisplayMode currentDisplayMode) {
final ReferenceData refData = metadata.allColumnMetadata().get(UiMessages.INSTANCE.use_slope());
Column<EnergyOfferDTO, String> useSlopeColumn;
Cell<String> cell;
if (isInEditMode(currentDisplayMode)) {
cell = new ReferenceDataBackedSelectionCell(refData);
} else {
cell = new TextCell();
}
useSlopeColumn = new Column<EnergyOfferDTO, String>(cell) {
#Override
public String getValue(EnergyOfferDTO energyOffer) {
return refData.getDisplayValueForSubmitValue(Boolean.toString(energyOffer.isSlopeUsed()));
}
};
useSlopeColumn.setSortable(true);
sortHandler.setComparator(useSlopeColumn, new Comparator<EnergyOfferDTO>() {
#Override
public int compare(EnergyOfferDTO eo1, EnergyOfferDTO eo2) {
final String slopeUsed1 = String.valueOf(eo1.isSlopeUsed());
final String slopeUsed2 = String.valueOf(eo1.isSlopeUsed());
return slopeUsed1.compareTo(slopeUsed2);
}
});
addColumn(useSlopeColumn, UiMessages.INSTANCE.use_slope());
setColumnWidth(useSlopeColumn, 75, Unit.PX);
setColumnHorizontalAlignment(useSlopeColumn, HasHorizontalAlignment.ALIGN_RIGHT);
}
protected void addPriceMwColumn(final int colIndex, DisplayMode currentDisplayMode) {
// Construct a composite cell for energy offers that includes a pair of text inputs
final List<HasCell<EnergyOfferDTO, ?>> columns = new ArrayList<
HasCell<EnergyOfferDTO, ?>>();
// this DTO is passed along so that price and mw values for new entries are kept together
final OfferPriceMwPair newOfferPriceMwPair = new OfferPriceMwPair();
// Price
final Column<EnergyOfferDTO, String> priceColumn = generatePriceColumn(colIndex, newOfferPriceMwPair, currentDisplayMode);
columns.add(priceColumn);
// MW
final Column<EnergyOfferDTO, String> mwColumn = generateMwColumn(colIndex, newOfferPriceMwPair, currentDisplayMode);
columns.add(mwColumn);
// Composite
final CompositeCell<EnergyOfferDTO> priceMwColumnInnards = generateCompositeCell(columns);
final IdentityColumn<EnergyOfferDTO> priceMwColumn = new IdentityColumn<EnergyOfferDTO>(priceMwColumnInnards);
final StringBuilder colHeader = new StringBuilder();
colHeader.append(UiMessages.INSTANCE.price_mw_header()).append(" ").append(String.valueOf(colIndex + 1));
addColumn(priceMwColumn, colHeader.toString());
setColumnWidth(priceMwColumn, 7, Unit.EM);
setColumnHorizontalAlignment(priceMwColumn, HasHorizontalAlignment.ALIGN_RIGHT);
}
protected Column<EnergyOfferDTO, String> generatePriceColumn(final int colIndex, final OfferPriceMwPair newOfferPriceMwPair, DisplayMode currentDisplayMode) {
Column<EnergyOfferDTO, String> priceColumn;
if (isInEditMode(currentDisplayMode)) {
priceColumn = new BigDecimalValidatableColumn<EnergyOfferDTO, OfferPriceMwPair>(nextTabIndex(), getGrid()) {
#Override
public String getValue(EnergyOfferDTO energyOffer) {
return obtainPriceValue(colIndex, energyOffer, false);
}
#Override
public void doUpdate(int index, EnergyOfferDTO energyOffer, String value) {
if (value != null && !value.isEmpty()) {
// number format exceptions should be caught and handled by event bus's handle method
final double valueAsDouble = NumberFormat.getDecimalFormat().parse(value);
final BigDecimal price = BigDecimal.valueOf(valueAsDouble);
final List<OfferPriceMwPair> offerPriceCurve = energyOffer.getCurve();
final OfferPriceMwPair offerPriceMwPair = offerPriceCurve.get(colIndex);
if (offerPriceMwPair == null) { // we have a new price value
newOfferPriceMwPair.setPrice(price);
offerPriceCurve.add(newOfferPriceMwPair);
} else {
offerPriceMwPair.setPrice(price);
}
}
}
#Override
protected String getPropertyName() {
return "price";
}
#Override
protected Class<OfferPriceMwPair> getPropertyOwner() {
return OfferPriceMwPair.class;
}
};
} else {
priceColumn = new Column<EnergyOfferDTO, String>(new TextCell()) {
#Override
public String getValue(EnergyOfferDTO energyOffer) {
final String result = obtainPriceValue(colIndex, energyOffer, true);
return result;
}
};
}
return priceColumn;
}
private String obtainPriceValue(final int colIndex, EnergyOfferDTO energyOffer, boolean withCurrency) {
String result = "";
if (energyOffer != null) {
final List<OfferPriceMwPair> offerPriceCurve = energyOffer.getCurve();
final int numberOfPairs = offerPriceCurve.size();
if (colIndex < numberOfPairs) {
final OfferPriceMwPair offerPriceMwPair = offerPriceCurve.get(colIndex);
if (offerPriceMwPair != null) {
final BigDecimal price = offerPriceMwPair.getPrice();
if (price != null) {
final double value = price.doubleValue();
if (withCurrency) {
result = NumberFormat.getCurrencyFormat().format(value);
} else {
result = NumberFormat.getDecimalFormat().format(value);
}
}
}
}
}
return result;
}
protected Column<EnergyOfferDTO, String> generateMwColumn(final int colIndex, final OfferPriceMwPair newOfferPriceMwPair, DisplayMode currentDisplayMode) {
Column<EnergyOfferDTO, String> mwColumn;
if (isInEditMode(currentDisplayMode)) {
mwColumn = new BigDecimalValidatableColumn<EnergyOfferDTO, PriceMwPair>(nextTabIndex(), getGrid()) {
#Override
public String getValue(EnergyOfferDTO energyOffer) {
return obtainMwValue(colIndex, energyOffer);
}
#Override
public void doUpdate(int index, EnergyOfferDTO energyOffer, String value) {
if (value != null && !value.isEmpty()) {
// number format exceptions should be caught and handled by event bus's handle method
final double valueAsDouble = NumberFormat.getDecimalFormat().parse(value);
final BigDecimal mw = BigDecimal.valueOf(valueAsDouble);
final List<OfferPriceMwPair> offerPriceCurve = energyOffer.getCurve();
final OfferPriceMwPair offerPriceMwPair = offerPriceCurve.get(colIndex);
if (offerPriceMwPair == null) { // we have a new price value
newOfferPriceMwPair.setMw(mw);
offerPriceCurve.add(newOfferPriceMwPair);
} else {
offerPriceMwPair.setMw(mw);
}
}
}
#Override
protected String getPropertyName() {
return "mw";
}
#Override
protected Class<PriceMwPair> getPropertyOwner() {
return PriceMwPair.class;
}
};
} else {
mwColumn = new Column<EnergyOfferDTO, String>(new TextCell()) {
#Override
public String getValue(EnergyOfferDTO energyOffer) {
final String result = obtainMwValue(colIndex, energyOffer);
return result;
}
};
}
return mwColumn;
}
private String obtainMwValue(final int colIndex, EnergyOfferDTO energyOffer) {
String result = "";
if (energyOffer != null) {
final List<OfferPriceMwPair> offerPriceCurve = energyOffer.getCurve();
final int numberOfPairs = offerPriceCurve.size();
if (colIndex < numberOfPairs) {
final PriceMwPair offerPriceMwPair = offerPriceCurve.get(colIndex);
if (offerPriceMwPair != null) {
final BigDecimal mw = offerPriceMwPair.getMw();
if (mw != null) {
result = NumberFormat.getDecimalFormat().format(mw);
}
}
}
}
return result;
}
}
All that custom work w.r.t. WrapperCell and CompositeValidatableColumn was unnecessary.
It turns out that there's a way you should not construct CompositeCells. See http://code.google.com/p/google-web-toolkit/issues/detail?id=5714. My CompositeCells were not receiving events. So, I changed the way I construct them in ToggleableGrid.
protected CompositeCell<T> generateCompositeCell(final List<HasCell<T, String>> hasCells) {
final CompositeCell<T> compositeCell = new CompositeCell<T>(hasCells) {
// to not run afoul of http://code.google.com/p/google-web-toolkit/issues/detail?id=5714
#Override
public void render(Context context, T value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<div style=\"display: inline\">");
super.render(context, value, sb);
sb.appendHtmlConstant("</div>");
}
#Override
protected Element getContainerElement(Element parent) {
// Return the first element in the DIV.
return parent.getFirstChildElement();
}
};
return compositeCell;
}
After that change and incorporating my other validation-oriented classes: ValidatableFieldUpdater, AbstractValidatableColumn (and derivatives), ValidatableInputField and ConversionResult, life couldn't be more grand!