Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Display value different than data value in editing cells
Display value different than data value in editing cells [message #988963] Tue, 04 December 2012 00:35 Go to next message
Mauricio Bustamante is currently offline Mauricio Bustamante
Messages: 3
Registered: December 2012
Junior Member
hi!

(i'm not a native english speaker so please forgive my grammar mistakes)

I want to display derived data in a cell but when i'll try to edit the cell, i want to show me the original data value instead of the derived data. For example, let's say i'm selling a tablet at $300 and a costumer buys me 5. I want a column "Value" shows $1500 but when i'll edit it, it will show me 5.

From the example EditableGridExample.java i tried something like this:

private static IDisplayConverter getMillionsDisplayConverter() {
		return new DisplayConverter(){
			NumberFormat numberFormatter = new DecimalFormat("###,###,###");
                        int price = 300;

			public Object canonicalToDisplayValue(Object canonicalValue) {
				if (canonicalValue == null) {
					return null;
				}
				return numberFormatter.format(Integer.valueOf(canonicalValue.toString())*price);
			}

			public Object displayToCanonicalValue(Object displayValue) {
				return (numberFormatter.parse(displayValue.toString(), new ParsePosition(0))).intValue();
			}
		};
	}


This partially works. The thing is when a try to edit it for the second time, takes the value that it was displayed ($1500) instead of the value that i entered before (5).

could someone help me ?

thanks

Re: Display value different than data value in editing cells [message #989064 is a reply to message #988963] Tue, 04 December 2012 08:48 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1225
Registered: July 2012
Senior Member
in displayToCanonicalValue(Object displayValue) you are returning the value that is entered without calculating it back with .intValue() / price

At least this is what you should check. Set a breakpoint there or in the IDataProvider that is writing the values back to your model.
Re: Display value different than data value in editing cells [message #989134 is a reply to message #989064] Tue, 04 December 2012 12:59 Go to previous messageGo to next message
Mauricio Bustamante is currently offline Mauricio Bustamante
Messages: 3
Registered: December 2012
Junior Member
thanks for the answer

i put a breakpoint in "displayToCanonicalValue" but it didn't fire. It appears that the method never gets called. When this method is called ?

I don't know how to modify the IDataProvider in the default example.
Re: Display value different than data value in editing cells [message #989266 is a reply to message #988963] Wed, 05 December 2012 07:26 Go to previous messageGo to next message
Thomas  Mäder is currently offline Thomas Mäder
Messages: 46
Registered: July 2009
Member
Try a breakpoint in TextCellEditor.getCanonicalValue(). It might be a good idea to debug through what happens there.
Re: Display value different than data value in editing cells [message #989405 is a reply to message #989266] Thu, 06 December 2012 01:14 Go to previous message
Mauricio Bustamante is currently offline Mauricio Bustamante
Messages: 3
Registered: December 2012
Junior Member
i started playing with the "CalculatingGridExample" because is a lot more simpler than EditableGridExample and i learned how to do it.

What i did was to make my own DisplayConverter class and register it as "DisplayMode.NORMAL", that was the solution Smile

Here is the code

thanks

class CalulatingEditConfiguration extends AbstractRegistryConfiguration  {

	public void configureRegistry(IConfigRegistry configRegistry) {
		
		configRegistry.registerConfigAttribute(
				EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.NEVER_EDITABLE, 
				DisplayMode.EDIT, CalculatingGridExample.COLUMN_FOUR_LABEL);
		configRegistry.registerConfigAttribute(
				EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.NEVER_EDITABLE, 
				DisplayMode.EDIT, CalculatingGridExample.COLUMN_FIVE_LABEL);
		//configure the summary row to be not editable
		configRegistry.registerConfigAttribute(
				EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.NEVER_EDITABLE, 
				DisplayMode.EDIT, SummaryRowLayer.DEFAULT_SUMMARY_ROW_CONFIG_LABEL);
		configRegistry.registerConfigAttribute(
				EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE);

		configRegistry.registerConfigAttribute(
				CellConfigAttributes.DISPLAY_CONVERTER, new PriceConverter(), DisplayMode.NORMAL);
		configRegistry.registerConfigAttribute(
				CellConfigAttributes.DISPLAY_CONVERTER, new DefaultIntegerDisplayConverter(), DisplayMode.EDIT);
		
		configRegistry.registerConfigAttribute(
				CellConfigAttributes.DISPLAY_CONVERTER, new PercentageDisplayConverter(), 
				DisplayMode.NORMAL, CalculatingGridExample.COLUMN_FIVE_LABEL);

		configRegistry.registerConfigAttribute(
				CellConfigAttributes.DISPLAY_CONVERTER, new PercentageDisplayConverter(), 
				DisplayMode.NORMAL, SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 4);
	}
	
	class PriceConverter extends DisplayConverter {

		public Object canonicalToDisplayValue(Object canonicalValue) {
			try {
				if (isNotNull(canonicalValue)) {
					int num = Integer.valueOf(canonicalValue.toString());
					num = num*300;					
					return num;
				}
				return null;
			} catch (Exception e) {
				return canonicalValue;
			}
		}

		public Object displayToCanonicalValue(Object displayValue) {
			try {
				if (isNotNull(displayValue) && isNotEmpty(displayValue.toString())) {
					return Integer.valueOf(displayValue.toString())/300;
				}
				return null;
			} catch (Exception e) {
				throw new ConversionFailedException(
						Messages.getString("NumericDisplayConverter.failure", //$NON-NLS-1$
								new Object[] {displayValue}), e);
			}
		}
	}
}

Previous Topic:Using FilterList for tree in NatTable
Next Topic:How to change the font?
Goto Forum:
  


Current Time: Tue Jul 29 17:00:16 EDT 2014

Powered by FUDForum. Page generated in 0.01528 seconds