Skip to content

Commit 2541eb5

Browse files
committed
[INTERNAL] width calculation rework *WIP*
1 parent fd2c41f commit 2541eb5

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/CsvTableEditor.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,13 @@ public Font getFont() {
313313
return EditorColorsManager.getInstance().getGlobalScheme().getFont(EditorFontType.PLAIN);
314314
}
315315

316-
public int getStringWidth(String text) {
316+
public Dimension getPreferredCellSize(int row, int column) {
317+
Object[][] data = getDataHandler().getCurrentState();
318+
String text = data[row][column].toString();
317319
if (text == null) {
318-
return -1;
320+
return new Dimension();
319321
}
320-
return getFont().getSize() * text.length();
322+
return new Dimension(getFont().getSize() * text.length(), getPreferredRowHeight());
321323
}
322324

323325
public final void resetAllColumnWidths() {
@@ -339,26 +341,16 @@ protected int[] calculateDistributedColumnWidths() {
339341
Map<Integer, CsvColumnInfo<PsiElement>> columnInfos = this.getColumnInfoMap().getColumnInfos();
340342
Object[][] data = getDataHandler().getCurrentState();
341343
int[] widths = new int[columnInfos.size()];
342-
int maxWidth = 0;
343344
int tableAutoMaxColumnWidth = CsvEditorSettingsExternalizable.getInstance().getTableAutoMaxColumnWidth();
344345

345346
for (Map.Entry<Integer, CsvColumnInfo<PsiElement>> columnInfoEntry : columnInfos.entrySet()) {
346347
CsvColumnInfo<PsiElement> columnInfo = columnInfoEntry.getValue();
347-
int currentWidth = getStringWidth(data[columnInfo.getMaxLengthRowIndex()][columnInfo.getColumnIndex()].toString());
348-
widths[columnInfoEntry.getKey()] = currentWidth;
349-
if (currentWidth > maxWidth) {
350-
maxWidth = currentWidth;
348+
Dimension preferredDimension = getPreferredCellSize(columnInfo.getMaxLengthRowIndex(), columnInfo.getColumnIndex());
349+
int currentWidth = preferredDimension.width;
350+
if (tableAutoMaxColumnWidth != 0) {
351+
currentWidth = Math.min(tableAutoMaxColumnWidth, currentWidth);
351352
}
352-
}
353-
354-
if (tableAutoMaxColumnWidth == 0) {
355-
tableAutoMaxColumnWidth = maxWidth;
356-
} else {
357-
tableAutoMaxColumnWidth = Math.min(tableAutoMaxColumnWidth, maxWidth);
358-
}
359-
360-
for (int i = 0; i < widths.length; ++i) {
361-
widths[i] = (int) (tableAutoMaxColumnWidth * ((double) widths[i] / (double) maxWidth));
353+
widths[columnInfoEntry.getKey()] = currentWidth;
362354
}
363355

364356
return widths;

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableEditorMouseListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void mouseClicked(MouseEvent e) {
2525

2626
protected void addCommonActions(JBPopupMenu popupMenu) {
2727
popupMenu.add(new JSeparator());
28-
JMenuItem menuItem = new JMenuItem(csvTableEditor.lnkAdjustColumnWidth.getText(), csvTableEditor.lnkAdjustColumnWidth.getIcon());
28+
JMenuItem menuItem = new JMenuItem(csvTableEditor.lnkAdjustColumnWidth.getText(), IconLoader.getIcon("/media/icons/adjust-column-width.png"));
2929
menuItem.addActionListener(csvTableEditor.tableEditorActions.adjustColumnWidthAction);
3030
popupMenu.add(menuItem);
3131
menuItem = new JMenuItem("Reset to default width", IconLoader.getIcon("/media/icons/reset-column-width.png"));

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableEditorSwing.form

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
<component id="bdd04" class="com.intellij.ui.components.labels.LinkLabel" binding="lnkAdjustColumnWidth">
108108
<constraints/>
109109
<properties>
110-
<icon value="media/icons/adjust-column-width.png"/>
111110
<text value="Adjust column width"/>
112111
</properties>
113112
</component>

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/table/swing/CsvTableEditorSwing.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class CsvTableEditorSwing extends CsvTableEditor implements TableDataChangeEvent.Listener {
3636

3737
private static final int TOTAL_CELL_HEIGHT_SPACING = 3;
38-
private static final int TOTAL_CELL_WIDTH_SPACING = 6;
38+
private static final int TOTAL_CELL_WIDTH_SPACING = 10;
3939

4040
private JBTable tblEditor;
4141
private JPanel panelMain;
@@ -482,11 +482,13 @@ private int getFontHeight() {
482482
}
483483

484484
@Override
485-
public int getStringWidth(String text) {
486-
if (text == null) {
487-
return -1;
488-
}
489-
return getTable().getFontMetrics(getTable().getFont()).stringWidth(text) + TOTAL_CELL_WIDTH_SPACING;
485+
public Dimension getPreferredCellSize(int row, int column) {
486+
JBTable table = getTable();
487+
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
488+
// rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
489+
// getTable().getFont().getStringBounds()
490+
// return getTable().getFontMetrics(getTable().getFont()).stringWidth(text) + TOTAL_CELL_WIDTH_SPACING;
491+
return comp.getPreferredSize();
490492
}
491493

492494
public void changeFontSize(int changeAmount) {

0 commit comments

Comments
 (0)