1111import com .intellij .openapi .options .colors .ColorSettingsPage ;
1212import com .intellij .openapi .util .Key ;
1313import com .intellij .openapi .util .UserDataHolder ;
14+ import net .seesharpsoft .UnhandledSwitchCaseException ;
1415import net .seesharpsoft .intellij .plugins .csv .CsvIconProvider ;
1516import net .seesharpsoft .intellij .plugins .csv .highlighter .CsvSyntaxHighlighter ;
1617import org .jetbrains .annotations .NotNull ;
2526
2627public class CsvColorSettings implements ColorSettingsPage {
2728
28- private static final Integer MAX_COLUMN_HIGHLIGHT_COLORS = 10 ;
29+ private static final Integer MAX_COLUMN_COLORING_COLORS = 10 ;
2930 private static final AttributesDescriptor [] DESCRIPTORS ;
30- private static final List <TextAttributesKey > COLUMN_HIGHLIGHT_ATTRIBUTES ;
31- private static final Key <List <TextAttributes >> COLUMN_HIGHLIGHT_TEXT_ATTRIBUTES_KEY = Key .create ("CSV_PLUGIN_COLUMN_HIGHLIGHT_ATTRIBUTES " );
31+ private static final List <TextAttributesKey > COLUMN_COLORING_ATTRIBUTES ;
32+ private static final Key <List <TextAttributes >> COLUMN_COLORING_TEXT_ATTRIBUTES_KEY = Key .create ("CSV_PLUGIN_COLUMN_COLORING_ATTRIBUTES " );
3233
3334 static {
3435 List <AttributesDescriptor > attributesDescriptors = new ArrayList ();
@@ -38,35 +39,51 @@ public class CsvColorSettings implements ColorSettingsPage {
3839 attributesDescriptors .add (new AttributesDescriptor ("Escaped Text" , CsvSyntaxHighlighter .ESCAPED_TEXT ));
3940 attributesDescriptors .add (new AttributesDescriptor ("Comment" , CsvSyntaxHighlighter .COMMENT ));
4041
41- COLUMN_HIGHLIGHT_ATTRIBUTES = new ArrayList <>();
42- for (int i = 0 ; i < MAX_COLUMN_HIGHLIGHT_COLORS ; ++i ) {
43- TextAttributesKey textAttributesKey = createTextAttributesKey (String .format ("CSV_COLUMN_HIGHLIGHT_ATTRIBUTE_ %d" , i + 1 ), DefaultLanguageHighlighterColors . STRING );
44- COLUMN_HIGHLIGHT_ATTRIBUTES .add (textAttributesKey );
45- attributesDescriptors .add (new AttributesDescriptor (String .format ("Column Highlighting Color %d" , i + 1 ), textAttributesKey ));
42+ COLUMN_COLORING_ATTRIBUTES = new ArrayList <>();
43+ for (int i = 0 ; i < MAX_COLUMN_COLORING_COLORS ; ++i ) {
44+ TextAttributesKey textAttributesKey = createTextAttributesKey (String .format ("CSV_PLUGIN_COLUMN_COLORING_ATTRIBUTE_ %d" , i + 1 ), CsvSyntaxHighlighter . TEXT );
45+ COLUMN_COLORING_ATTRIBUTES .add (textAttributesKey );
46+ attributesDescriptors .add (new AttributesDescriptor (String .format ("Rainbow column color %d" , i + 1 ), textAttributesKey ));
4647 }
4748 DESCRIPTORS = attributesDescriptors .toArray (new AttributesDescriptor [attributesDescriptors .size ()]);
4849 }
4950
5051 public static TextAttributes getTextAttributesOfColumn (int columnIndex , UserDataHolder userDataHolder ) {
51- List <TextAttributes > textAttributeList = userDataHolder .getUserData (COLUMN_HIGHLIGHT_TEXT_ATTRIBUTES_KEY );
52+ List <TextAttributes > textAttributeList = userDataHolder .getUserData (COLUMN_COLORING_TEXT_ATTRIBUTES_KEY );
5253 if (textAttributeList == null ) {
5354 EditorColorsScheme editorColorsScheme = EditorColorsManager .getInstance ().getGlobalScheme ();
5455 textAttributeList = new ArrayList <>();
5556 int maxIndex = 0 ;
56- for (int colorDescriptorIndex = 0 ; colorDescriptorIndex < MAX_COLUMN_HIGHLIGHT_COLORS ; ++colorDescriptorIndex ) {
57- TextAttributesKey textAttributesKey = COLUMN_HIGHLIGHT_ATTRIBUTES .get (colorDescriptorIndex );
58- TextAttributes textAttributes = editorColorsScheme .getAttributes (textAttributesKey );
59- textAttributeList .add (textAttributes );
60- if (!textAttributesKey .getDefaultAttributes ().equals (textAttributes )) {
61- maxIndex = colorDescriptorIndex ;
62- }
57+ switch (CsvEditorSettings .getInstance ().getColumnColoring ()) {
58+ case RAINBOW :
59+ maxIndex = applyColumnTextAttributes (editorColorsScheme , textAttributeList );
60+ break ;
61+ case SIMPLE :
62+ textAttributeList .add (editorColorsScheme .getAttributes (CsvSyntaxHighlighter .TEXT ));
63+ break ;
64+ default :
65+ throw new UnhandledSwitchCaseException (CsvEditorSettings .getInstance ().getColumnColoring ());
6366 }
6467 textAttributeList = textAttributeList .subList (0 , maxIndex + 1 );
65- userDataHolder .putUserData (COLUMN_HIGHLIGHT_TEXT_ATTRIBUTES_KEY , textAttributeList );
68+ userDataHolder .putUserData (COLUMN_COLORING_TEXT_ATTRIBUTES_KEY , textAttributeList );
6669 }
6770 return textAttributeList .isEmpty () ? null : textAttributeList .get (columnIndex % textAttributeList .size ());
6871 }
6972
73+ private static int applyColumnTextAttributes (EditorColorsScheme editorColorsScheme , List <TextAttributes > textAttributeList ) {
74+ int maxIndex = 0 ;
75+ TextAttributes defaultTextAttributes = editorColorsScheme .getAttributes (CsvSyntaxHighlighter .TEXT );
76+ for (int colorDescriptorIndex = 0 ; colorDescriptorIndex < MAX_COLUMN_COLORING_COLORS ; ++colorDescriptorIndex ) {
77+ TextAttributesKey textAttributesKey = COLUMN_COLORING_ATTRIBUTES .get (colorDescriptorIndex );
78+ TextAttributes textAttributes = editorColorsScheme .getAttributes (textAttributesKey );
79+ textAttributeList .add (textAttributes );
80+ if (!textAttributes .equals (defaultTextAttributes )) {
81+ maxIndex = colorDescriptorIndex ;
82+ }
83+ }
84+ return maxIndex ;
85+ }
86+
7087 @ Nullable
7188 @ Override
7289 public Icon getIcon () {
@@ -85,7 +102,7 @@ public String getDemoText() {
85102 return "1,\" Eldon Base for stackable storage shelf, platinum\" ,Muhammed MacIntyre,3,-213.25,38.94,35,Nunavut,Storage & Organization,0.8\n " +
86103 "2,\" 1.7 Cubic Foot Compact \" \" Cube\" \" Office Refrigerators\" ,Barry French,293,457.81,208.16,68.02,Nunavut,Appliances,0.58\n " +
87104 "3,\" Cardinal Slant-D® Ring Binder, Heavy Gauge Vinyl\" ,Barry French,293,46.71,8.69,2.99,Nunavut,Binders and Binder Accessories,0.39\n " +
88- "4,R380,Clay Rozendal,483,1198.97,195.99,3.99,Nunavut,Telephones and Communication,0.58\n " +
105+ "# 4,R380,Clay Rozendal,483,1198.97,195.99,3.99,Nunavut,Telephones and Communication,0.58\n " +
89106 "5,Holmes HEPA Air Purifier,Carlos Soltero,515,30.94,21.78,5.94,Nunavut,Appliances,0.5" ;
90107 }
91108
0 commit comments