-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show cost with 2 decimals #1677
base: master
Are you sure you want to change the base?
Show cost with 2 decimals #1677
Conversation
Thanks for the pull request. However, it needs to take all details into account. See my comments below. |
@@ -926,7 +937,11 @@ private TableCellRenderer createCellRenderer(Class<?> columnClass) { | |||
// renderer = TableCellRenderers.getNewDefaultRenderer(columnClass); | |||
// | |||
// } | |||
return getTreeTable().getDefaultRenderer(columnClass); | |||
if (Double.class.equals(columnClass)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way all columns with Double type will be formatted with two decimal digits. It is okay for cost (which is essentially money) but it may not be okay for other columns, including user-defined (imagine e.g. weight measured in tons).
I think that the best way to achieve the goal is to override method newTablecolumnext
in GanttTreeTable
and check if we are creating exactly COST column. We already use similar approach in ResourceTreeTable
.
Also, the same should be done with the editor, no? Otherwise the user will see values with 2 decimal digits, but once he starts editing, he will see something different.
@@ -96,6 +98,15 @@ public void applyComponentOrientation(ComponentOrientation o) { | |||
} | |||
}; | |||
|
|||
class DecimalRenderer extends DefaultTableCellRenderer { | |||
private final DecimalFormat myFormatter = new DecimalFormat("#0.00"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format of representing currency values is locale-specific. You want to use NumberFormat::getCurrencyInstance and pass the currently selected locale. GanttLanguage
is responsible for such things (and it stores the current locale)
TableCellRenderer renderer = createCellRenderer(columnClass); | ||
String columnName = getTreeTableModel().getColumnName(modelIndex).toLowerCase(); | ||
TableCellRenderer renderer; | ||
Boolean costColumn = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need to use Boolean
(capitalized) for a simple function-local boolean value. Atomic type is just fine.
@@ -96,6 +92,16 @@ public void applyComponentOrientation(ComponentOrientation o) { | |||
} | |||
}; | |||
|
|||
class DecimalRenderer extends DefaultTableCellRenderer { | |||
// private final DecimalFormat myFormatter = new DecimalFormat("#0.00"); | |||
private final DecimalFormat myFormatter = new DecimalFormat(NumberFormat.getNumberInstance(GanttLanguage.getInstance().getLocale()).toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pardon?
This fails because toString in format instances is not overridden and it is just a class name and memory address:
java.lang.IllegalArgumentException: Multiple decimal separators in pattern "java.text.DecimalFormat@674dc"
at java.base/java.text.DecimalFormat.applyPattern(DecimalFormat.java:3411)
at java.base/java.text.DecimalFormat.<init>(DecimalFormat.java:441)
at net.sourceforge.ganttproject.GPTreeTableBase$DecimalRenderer.<init>(GPTreeTableBase.java:97)
at net.sourceforge.ganttproject.GPTreeTableBase.newTableColumnExt(GPTreeTableBase.java:918)
Besides, you can just use NumberFormat directly without converting to Decimalformat.
This format instance will not update when user changes interface language in GanttProject settings. You need to listen to language change events
private final DecimalFormat myFormatter = new DecimalFormat(NumberFormat.getNumberInstance(GanttLanguage.getInstance().getLocale()).toString()); | ||
|
||
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { | ||
value = myFormatter.format((Number)value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cast is not necessary. format
argument is Object.
String columnName = getTreeTableModel().getColumnName(modelIndex).toLowerCase(); | ||
TableCellRenderer renderer; | ||
Boolean costColumn = false; | ||
if (Double.class.equals(columnClass) && columnName.equals("cost")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will work only if your system locale is English. Should it be some other locale, column name will not be "cost".
You can find TaskDefaultColumn instance by model index: TaskDefaultColumn.values()[modelIndex]
@@ -905,12 +911,20 @@ TableHeaderUiFacadeImpl getTableHeaderUiFacade() { | |||
protected TableColumnExt newTableColumnExt(int modelIndex) { | |||
TableColumnExt result = new TableColumnExt(modelIndex); | |||
Class<?> columnClass = getTreeTableModel().getColumnClass(modelIndex); | |||
TableCellRenderer renderer = createCellRenderer(columnClass); | |||
String columnName = getTreeTableModel().getColumnName(modelIndex).toLowerCase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is abstract and is shared between Gantt chart and Resource chart. Please move all checks which are related to tasks into GanttTreeTable
Fixes #1659