Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions ganttproject/src/net/sourceforge/ganttproject/GPTreeTableBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ of the License, or (at your option) any later version.
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
Expand All @@ -70,15 +71,10 @@ of the License, or (at your option) any later version.
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EventObject;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -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());
Copy link
Copy Markdown
Contributor

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


public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
value = myFormatter.format((Number)value);
Copy link
Copy Markdown
Contributor

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.

return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}

private GPAction myEditCellAction = new GPAction("tree.edit") {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down Expand Up @@ -905,12 +911,20 @@ protected List<ColumnList.Column> getDefaultColumns() {
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();
Copy link
Copy Markdown
Contributor

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

TableCellRenderer renderer;
Boolean costColumn = false;
Copy link
Copy Markdown
Contributor

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.

if (Double.class.equals(columnClass) && columnName.equals("cost")) {
Copy link
Copy Markdown
Contributor

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]

renderer = new DecimalRenderer();
costColumn = true;
} else {
renderer = createCellRenderer(columnClass);
}
if (renderer != null) {
result.setCellRenderer(renderer);
}
TableCellEditor editor = createCellEditor(columnClass);
if (editor != null) {
if (editor != null && !costColumn) {
result.setCellEditor(editor);
} else {
System.err.println("no editor for column=" + modelIndex + " class=" + columnClass);
Expand All @@ -925,7 +939,7 @@ private TableCellRenderer createCellRenderer(Class<?> columnClass) {
// {
// renderer = TableCellRenderers.getNewDefaultRenderer(columnClass);
//
// }
//}
return getTreeTable().getDefaultRenderer(columnClass);
}

Expand Down