Skip to content

Commit

Permalink
demo: possibility to toggle between normal and hindu-arabic line numb…
Browse files Browse the repository at this point in the history
…er format
  • Loading branch information
Mino260806 authored and bobbylight committed Aug 12, 2023
1 parent c76c937 commit cb60644
Showing 1 changed file with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 21,7 @@
import javax.swing.event.HyperlinkListener;

import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rtextarea.FoldIndicatorStyle;
import org.fife.ui.rtextarea.Gutter;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.fife.ui.rtextarea.*;


/**
Expand Down Expand Up @@ -139,6 137,18 @@ private JMenuBar createMenuBar() {
foldStyleSubMenu.add(classicStyleItem);
foldStyleSubMenu.add(modernStyleItem);
menu.add(foldStyleSubMenu);
JMenu lineNumberFormatSubMenu = new JMenu("Line Number Format");
JRadioButtonMenuItem normalStyleItem = new JRadioButtonMenuItem(
new LineNumberFormatAction("Normal", LineNumberList.DEFAULT_LINE_NUMBER_FORMATTER));
JRadioButtonMenuItem hinduArabicStyleItem = new JRadioButtonMenuItem(
new LineNumberFormatAction("Hindu-Arabic", new HinduArabicLineNumberFormatter()));
normalStyleItem.setSelected(true);
bg = new ButtonGroup();
bg.add(normalStyleItem);
bg.add(hinduArabicStyleItem);
lineNumberFormatSubMenu.add(normalStyleItem);
lineNumberFormatSubMenu.add(hinduArabicStyleItem);
menu.add(lineNumberFormatSubMenu);
JCheckBoxMenuItem cbItem = new JCheckBoxMenuItem(new CodeFoldingAction());
cbItem.setSelected(true);
menu.add(cbItem);
Expand Down Expand Up @@ -408,6 418,25 @@ public void actionPerformed(ActionEvent e) {

}

/**
* Changes how line numbers are displayed.
*/
private class LineNumberFormatAction extends AbstractAction {

private final LineNumberFormatter formatter;

LineNumberFormatAction(String name, LineNumberFormatter formatter) {
this.formatter = formatter;
putValue(NAME, name);
}

@Override
public void actionPerformed(ActionEvent e) {
scrollPane.getGutter().setLineNumberFormatter(formatter);
}

}

/**
* Changes the look and feel of the demo application.
*/
Expand Down Expand Up @@ -560,5 589,35 @@ public void actionPerformed(ActionEvent e) {

}

/**
* Formats line numbers into Hindu-Arabic numerals.
*/
private static class HinduArabicLineNumberFormatter implements LineNumberFormatter {
private static final String[] NUMERALS = {
"٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"
};

@Override
public String format(int lineNumber) {
if (lineNumber == 0) {
return NUMERALS[0];
}

StringBuilder sb = new StringBuilder();
while (lineNumber > 0) {
int digit = lineNumber % 10;
sb.insert(0, NUMERALS[digit]);
lineNumber /= 10;
}

return sb.toString();
}

@Override
public int getMaxLength(int maxLineNumber) {
return String.valueOf(maxLineNumber).length();
}
}


}

0 comments on commit cb60644

Please sign in to comment.