Skip to content

Commit fd2c41f

Browse files
committed
[FEATURE] calculate and adjust table column width to content
Fixes #112
1 parent e7756a4 commit fd2c41f

18 files changed

+394
-74
lines changed

src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvColumnInfo.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ public class CsvColumnInfo<T> {
99

1010
private int myColumnIndex;
1111
private int myMaxLength;
12+
private int myMaxLengthRowIndex;
1213
private Map<T, RowInfo> myElementInfos;
1314
private T myHeaderElement;
1415
private int mySize;
1516

16-
public CsvColumnInfo(int columnIndex, int maxLength) {
17+
public CsvColumnInfo(int columnIndex, int maxLength, int maxLengthRowIndex) {
1718
this.myColumnIndex = columnIndex;
18-
this.myMaxLength = maxLength;
19+
setMaxLength(maxLength, maxLengthRowIndex);
1920
this.myElementInfos = new HashMap<>();
2021
this.myHeaderElement = null;
2122
this.mySize = 0;
@@ -39,8 +40,13 @@ public int getMaxLength() {
3940
return myMaxLength;
4041
}
4142

42-
public void setMaxLength(int maxLength) {
43+
public void setMaxLength(int maxLength, int maxLengthRowIndex) {
4344
this.myMaxLength = maxLength;
45+
this.myMaxLengthRowIndex = maxLengthRowIndex;
46+
}
47+
48+
public int getMaxLengthRowIndex() {
49+
return this.myMaxLengthRowIndex;
4450
}
4551

4652
public int getSize() {

src/main/java/net/seesharpsoft/intellij/plugins/csv/CsvHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ public static CsvColumnInfoMap<PsiElement> createColumnInfoMap(CsvFile csvFile)
156156
for (CsvField field : record.getFieldList()) {
157157
Integer length = field.getTextLength();
158158
if (!columnInfoMap.containsKey(column)) {
159-
columnInfoMap.put(column, new CsvColumnInfo(column, length));
159+
columnInfoMap.put(column, new CsvColumnInfo(column, length, row));
160160
} else if (columnInfoMap.get(column).getMaxLength() < length) {
161-
columnInfoMap.get(column).setMaxLength(length);
161+
columnInfoMap.get(column).setMaxLength(length, row);
162162
}
163163
columnInfoMap.get(column).addElement(field, row, getFieldStartOffset(field), getFieldEndOffset(field));
164164
++column;

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/CsvEditorSettingsExternalizable.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
@SuppressWarnings("all")
2121
public class CsvEditorSettingsExternalizable implements PersistentStateComponent<CsvEditorSettingsExternalizable.OptionSet> {
2222

23-
public static final int TABLE_EDITOR_MIN_ROW_HEIGHT = 0;
24-
public static final int TABLE_EDITOR_MAX_ROW_HEIGHT = 10;
25-
public static final int TABLE_EDITOR_DEFAULT_ROW_HEIGHT = 3;
23+
public static final int TABLE_EDITOR_ROW_HEIGHT_MIN = 0;
24+
public static final int TABLE_EDITOR_ROW_HEIGHT_MAX = 10;
25+
public static final int TABLE_EDITOR_ROW_HEIGHT_DEFAULT = 3;
26+
public static final int TABLE_AUTO_MAX_COLUMN_WIDTH_DEFAULT = 300;
27+
public static final int TABLE_DEFAULT_COLUMN_WIDTH_DEFAULT = 100;
2628

2729
public enum EditorPrio {
2830
TEXT_FIRST,
@@ -39,6 +41,9 @@ public static final class OptionSet {
3941
public String TAB_HIGHLIGHT_COLOR;
4042
public EditorPrio EDITOR_PRIO;
4143
public int TABLE_EDITOR_ROW_HEIGHT;
44+
public int TABLE_AUTO_MAX_COLUMN_WIDTH;
45+
public int TABLE_DEFAULT_COLUMN_WIDTH;
46+
public boolean TABLE_AUTO_COLUMN_WIDTH_ON_OPEN;
4247
public boolean TABLE_COLUMN_HIGHTLIGHTING;
4348
public boolean ZERO_BASED_COLUMN_NUMBERING;
4449

@@ -56,7 +61,10 @@ public OptionSet() {
5661
TAB_HIGHLIGHT_COLOR = "-7984";
5762
EDITOR_PRIO = EditorPrio.TEXT_FIRST;
5863
SHOW_TABLE_EDITOR_INFO_PANEL = true;
59-
TABLE_EDITOR_ROW_HEIGHT = TABLE_EDITOR_DEFAULT_ROW_HEIGHT;
64+
TABLE_EDITOR_ROW_HEIGHT = TABLE_EDITOR_ROW_HEIGHT_DEFAULT;
65+
TABLE_AUTO_MAX_COLUMN_WIDTH = TABLE_AUTO_MAX_COLUMN_WIDTH_DEFAULT;
66+
TABLE_DEFAULT_COLUMN_WIDTH = TABLE_DEFAULT_COLUMN_WIDTH_DEFAULT;
67+
TABLE_AUTO_COLUMN_WIDTH_ON_OPEN = true;
6068
QUOTING_ENFORCED = false;
6169
TABLE_COLUMN_HIGHTLIGHTING = true;
6270
ZERO_BASED_COLUMN_NUMBERING = false;
@@ -160,8 +168,8 @@ public int getTableEditorRowHeight() {
160168
}
161169
public void setTableEditorRowHeight(int rowHeight) {
162170
int finalRowHeight = rowHeight;
163-
if (finalRowHeight > TABLE_EDITOR_MAX_ROW_HEIGHT) finalRowHeight = TABLE_EDITOR_MAX_ROW_HEIGHT;
164-
if (finalRowHeight < TABLE_EDITOR_MIN_ROW_HEIGHT) finalRowHeight = TABLE_EDITOR_MIN_ROW_HEIGHT;
171+
if (finalRowHeight > TABLE_EDITOR_ROW_HEIGHT_MAX) finalRowHeight = TABLE_EDITOR_ROW_HEIGHT_MAX;
172+
if (finalRowHeight < TABLE_EDITOR_ROW_HEIGHT_MIN) finalRowHeight = TABLE_EDITOR_ROW_HEIGHT_MIN;
165173
getState().TABLE_EDITOR_ROW_HEIGHT = finalRowHeight;
166174
}
167175

@@ -192,4 +200,25 @@ public boolean isFileEndLineBreak() {
192200
public void setFileEndLineBreak(boolean fileEndLineBreak) {
193201
getState().FILE_END_LINE_BREAK = fileEndLineBreak;
194202
}
203+
204+
public int getTableAutoMaxColumnWidth() {
205+
return getState().TABLE_AUTO_MAX_COLUMN_WIDTH;
206+
}
207+
public void setTableAutoMaxColumnWidth(int tableAutoMaxColumnWidth) {
208+
getState().TABLE_AUTO_MAX_COLUMN_WIDTH = tableAutoMaxColumnWidth;
209+
}
210+
211+
public int getTableDefaultColumnWidth() {
212+
return getState().TABLE_DEFAULT_COLUMN_WIDTH;
213+
}
214+
public void setTableDefaultColumnWidth(int tableDefaultColumnWidth) {
215+
getState().TABLE_DEFAULT_COLUMN_WIDTH = tableDefaultColumnWidth;
216+
}
217+
218+
public boolean isTableAutoColumnWidthOnOpen() {
219+
return getState().TABLE_AUTO_COLUMN_WIDTH_ON_OPEN;
220+
}
221+
public void setTableAutoColumnWidthOnOpen(boolean tableAutoColumnWidthOnOpen) {
222+
getState().TABLE_AUTO_COLUMN_WIDTH_ON_OPEN = tableAutoColumnWidthOnOpen;
223+
}
195224
}

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/CsvEditorSettingsProvider.form

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
6-
<xy x="20" y="20" width="1180" height="648"/>
6+
<xy x="20" y="20" width="1180" height="735"/>
77
</constraints>
88
<properties>
99
<enabled value="true"/>
@@ -91,84 +91,129 @@
9191
</grid>
9292
</children>
9393
</grid>
94-
<grid id="3e325" layout-manager="GridLayoutManager" row-count="7" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
94+
<grid id="3e325" layout-manager="GridLayoutManager" row-count="10" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
9595
<margin top="10" left="10" bottom="10" right="10"/>
9696
<constraints>
9797
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
9898
</constraints>
9999
<properties/>
100100
<border type="line" title="Table Editor"/>
101101
<children>
102-
<grid id="a07e0" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="0">
103-
<constraints>
104-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
105-
</constraints>
106-
<properties/>
107-
<border type="none"/>
108-
<children>
109-
<component id="bfa35" class="javax.swing.JLabel">
110-
<constraints/>
111-
<properties>
112-
<text value="Text-lines per row (default):"/>
113-
</properties>
114-
</component>
115-
<component id="d6ab5" class="javax.swing.JComboBox" binding="cbRowHeight">
116-
<constraints/>
117-
<properties>
118-
<model>
119-
<item value="Auto"/>
120-
<item value="1"/>
121-
<item value="2"/>
122-
<item value="3"/>
123-
<item value="4"/>
124-
<item value="5"/>
125-
<item value="6"/>
126-
<item value="7"/>
127-
<item value="8"/>
128-
<item value="9"/>
129-
<item value="10"/>
130-
</model>
131-
</properties>
132-
</component>
133-
</children>
134-
</grid>
135102
<component id="97488" class="javax.swing.JCheckBox" binding="cbShowInfoPanel">
136103
<constraints>
137-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
104+
<grid row="5" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
138105
</constraints>
139106
<properties>
140107
<text value="Show info panel"/>
141108
</properties>
142109
</component>
143110
<component id="d6a8e" class="javax.swing.JCheckBox" binding="cbQuotingEnforced">
144111
<constraints>
145-
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
112+
<grid row="6" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
146113
</constraints>
147114
<properties>
148115
<text value="Enforce value quoting"/>
149116
</properties>
150117
</component>
151118
<component id="f25dc" class="javax.swing.JCheckBox" binding="cbTableColumnHighlighting">
152119
<constraints>
153-
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
120+
<grid row="7" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
154121
</constraints>
155122
<properties>
156123
<text value="Enable column highlighting (Editor &gt; Color Scheme &gt; CSV &gt; Column Hightlighting Colors)"/>
157124
</properties>
158125
</component>
159126
<hspacer id="a028b">
160127
<constraints>
161-
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
128+
<grid row="8" column="0" row-span="1" col-span="6" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
162129
</constraints>
163130
</hspacer>
164131
<component id="e470c" class="javax.swing.JCheckBox" binding="cbFileEndLineBreak">
165132
<constraints>
166-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
133+
<grid row="4" column="0" row-span="1" col-span="6" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
167134
</constraints>
168135
<properties>
169136
<text value="Keep/ignore linebreak at file end"/>
170137
</properties>
171138
</component>
139+
<component id="77ade" class="javax.swing.JLabel">
140+
<constraints>
141+
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
142+
</constraints>
143+
<properties>
144+
<text value="Maximum column width (0 = no maximum):"/>
145+
</properties>
146+
</component>
147+
<hspacer id="d4de4">
148+
<constraints>
149+
<grid row="2" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
150+
</constraints>
151+
</hspacer>
152+
<component id="8554c" class="javax.swing.JFormattedTextField" binding="tfMaxColumnWidth" custom-create="true">
153+
<constraints>
154+
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
155+
<preferred-size width="150" height="-1"/>
156+
</grid>
157+
</constraints>
158+
<properties/>
159+
</component>
160+
<component id="b4509" class="javax.swing.JLabel">
161+
<constraints>
162+
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
163+
</constraints>
164+
<properties>
165+
<text value="Default column width (10 - 10000):"/>
166+
</properties>
167+
</component>
168+
<component id="15921" class="javax.swing.JFormattedTextField" binding="tfDefaultColumnWidth" custom-create="true">
169+
<constraints>
170+
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false">
171+
<preferred-size width="150" height="-1"/>
172+
</grid>
173+
</constraints>
174+
<properties/>
175+
</component>
176+
<hspacer id="9bde3">
177+
<constraints>
178+
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
179+
</constraints>
180+
</hspacer>
181+
<component id="5214d" class="javax.swing.JCheckBox" binding="cbAdjustColumnWidthOnOpen">
182+
<constraints>
183+
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
184+
</constraints>
185+
<properties>
186+
<text value="Adjust column width on open"/>
187+
</properties>
188+
</component>
189+
<component id="bfa35" class="javax.swing.JLabel">
190+
<constraints>
191+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
192+
</constraints>
193+
<properties>
194+
<text value="Text-lines per row (default):"/>
195+
</properties>
196+
</component>
197+
<component id="d6ab5" class="javax.swing.JComboBox" binding="cbRowHeight">
198+
<constraints>
199+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
200+
</constraints>
201+
<properties>
202+
<model>
203+
<item value="Auto"/>
204+
<item value="1"/>
205+
<item value="2"/>
206+
<item value="3"/>
207+
<item value="4"/>
208+
<item value="5"/>
209+
<item value="6"/>
210+
<item value="7"/>
211+
<item value="8"/>
212+
<item value="9"/>
213+
<item value="10"/>
214+
</model>
215+
</properties>
216+
</component>
172217
</children>
173218
</grid>
174219
<vspacer id="ff292">

src/main/java/net/seesharpsoft/intellij/plugins/csv/editor/CsvEditorSettingsProvider.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import org.jetbrains.annotations.Nullable;
88

99
import javax.swing.*;
10+
import javax.swing.text.NumberFormatter;
1011
import java.awt.*;
12+
import java.text.NumberFormat;
1113
import java.util.Objects;
1214

1315
public class CsvEditorSettingsProvider implements SearchableConfigurable {
@@ -27,6 +29,9 @@ public class CsvEditorSettingsProvider implements SearchableConfigurable {
2729
private JCheckBox cbTableColumnHighlighting;
2830
private JCheckBox cbZeroBasedColumnNumbering;
2931
private JCheckBox cbFileEndLineBreak;
32+
private JFormattedTextField tfMaxColumnWidth;
33+
private JFormattedTextField tfDefaultColumnWidth;
34+
private JCheckBox cbAdjustColumnWidthOnOpen;
3035

3136
@NotNull
3237
@Override
@@ -71,7 +76,10 @@ public boolean isModified() {
7176
!Objects.equals(cbEditorUsage.getSelectedIndex(), csvEditorSettingsExternalizable.getEditorPrio().ordinal()) ||
7277
isModified(cbTableColumnHighlighting, csvEditorSettingsExternalizable.isTableColumnHighlightingEnabled()) ||
7378
isModified(cbZeroBasedColumnNumbering, csvEditorSettingsExternalizable.isZeroBasedColumnNumbering()) ||
74-
isModified(cbFileEndLineBreak, csvEditorSettingsExternalizable.isFileEndLineBreak());
79+
isModified(cbFileEndLineBreak, csvEditorSettingsExternalizable.isFileEndLineBreak()) ||
80+
!tfMaxColumnWidth.getValue().equals(csvEditorSettingsExternalizable.getTableAutoMaxColumnWidth()) ||
81+
!tfDefaultColumnWidth.getValue().equals(csvEditorSettingsExternalizable.getTableDefaultColumnWidth()) ||
82+
isModified(cbAdjustColumnWidthOnOpen, csvEditorSettingsExternalizable.isTableAutoColumnWidthOnOpen());
7583
}
7684

7785
@Override
@@ -90,6 +98,9 @@ public void reset() {
9098
cbTableColumnHighlighting.setSelected(csvEditorSettingsExternalizable.isTableColumnHighlightingEnabled());
9199
cbZeroBasedColumnNumbering.setSelected(csvEditorSettingsExternalizable.isZeroBasedColumnNumbering());
92100
cbFileEndLineBreak.setSelected(csvEditorSettingsExternalizable.isFileEndLineBreak());
101+
tfMaxColumnWidth.setValue(csvEditorSettingsExternalizable.getTableAutoMaxColumnWidth());
102+
tfDefaultColumnWidth.setValue(csvEditorSettingsExternalizable.getTableDefaultColumnWidth());
103+
cbAdjustColumnWidthOnOpen.setSelected(csvEditorSettingsExternalizable.isTableAutoColumnWidthOnOpen());
93104
}
94105

95106
@Override
@@ -108,10 +119,28 @@ public void apply() throws ConfigurationException {
108119
csvEditorSettingsExternalizable.setTableColumnHighlightingEnabled(cbTableColumnHighlighting.isSelected());
109120
csvEditorSettingsExternalizable.setZeroBasedColumnNumbering(cbZeroBasedColumnNumbering.isSelected());
110121
csvEditorSettingsExternalizable.setFileEndLineBreak(cbFileEndLineBreak.isSelected());
122+
csvEditorSettingsExternalizable.setTableAutoMaxColumnWidth((int)tfMaxColumnWidth.getValue());
123+
csvEditorSettingsExternalizable.setTableDefaultColumnWidth((int)tfDefaultColumnWidth.getValue());
124+
csvEditorSettingsExternalizable.setTableAutoColumnWidthOnOpen(cbAdjustColumnWidthOnOpen.isSelected());
111125
}
112126

113127
protected void createUIComponents() {
114128
cbTabHighlightColor = new CheckBoxWithColorChooser("Highlight tab separator ");
115129
cbTabHighlightColor.setColor(Color.CYAN);
130+
131+
NumberFormat numberFormat = NumberFormat.getIntegerInstance();
132+
NumberFormatter numberFormatter = new NumberFormatter(numberFormat);
133+
numberFormatter.setValueClass(Integer.class);
134+
numberFormatter.setAllowsInvalid(false);
135+
numberFormatter.setMinimum(0);
136+
numberFormatter.setMaximum(Integer.MAX_VALUE);
137+
tfMaxColumnWidth = new JFormattedTextField(numberFormatter);
138+
139+
numberFormatter = new NumberFormatter(numberFormat);
140+
numberFormatter.setValueClass(Integer.class);
141+
numberFormatter.setAllowsInvalid(false);
142+
numberFormatter.setMinimum(10);
143+
numberFormatter.setMaximum(10000);
144+
tfDefaultColumnWidth = new JFormattedTextField(numberFormatter);
116145
}
117146
}

0 commit comments

Comments
 (0)