|
| 1 | +package com.github.minecraft_ta.totalDebugCompanion.model; |
| 2 | + |
| 3 | +import com.formdev.flatlaf.extras.FlatSVGIcon; |
| 4 | +import com.formdev.flatlaf.util.StringUtils; |
| 5 | +import com.github.minecraft_ta.totalDebugCompanion.ui.components.editors.CodeViewPanel; |
| 6 | +import com.github.minecraft_ta.totalDebugCompanion.ui.components.FontSizeSliderBar; |
| 7 | +import com.github.minecraft_ta.totalDebugCompanion.util.CodeUtils; |
| 8 | +import com.github.minecraft_ta.totalDebugCompanion.util.UIUtils; |
| 9 | + |
| 10 | +import javax.swing.*; |
| 11 | +import java.awt.*; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | + |
| 17 | +public class CodeView implements IEditorPanel { |
| 18 | + |
| 19 | + private static final Icon CLASS_ICON = new FlatSVGIcon("icons/class.svg"); |
| 20 | + |
| 21 | + private final Path path; |
| 22 | + private final CodeViewPanel codeViewPanel; |
| 23 | + |
| 24 | + public CodeView(Path path, int focusLine) { |
| 25 | + this.path = path; |
| 26 | + this.codeViewPanel = new CodeViewPanel(this); |
| 27 | + |
| 28 | + CompletableFuture.runAsync(() -> { |
| 29 | + try { |
| 30 | + String code = Files.readString(this.path); |
| 31 | + code = StringUtils.removeTrailing(code, "\n"); |
| 32 | + |
| 33 | + codeViewPanel.setCode(code); |
| 34 | + CodeUtils.highlightJavaCode(code, codeViewPanel.getEditorPane()); |
| 35 | + focusLine(focusLine); |
| 36 | + } catch (IOException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param line the line to scroll to, starting at index 1 |
| 44 | + */ |
| 45 | + public void focusLine(int line) { |
| 46 | + if (line < 1) |
| 47 | + throw new IllegalArgumentException(); |
| 48 | + |
| 49 | + this.codeViewPanel.focusLine(line); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getTitle() { |
| 54 | + String fullClassName = this.path.getFileName().toString().replace(".java", ""); |
| 55 | + return fullClassName.substring(fullClassName.lastIndexOf('.') + 1); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Icon getIcon() { |
| 60 | + return CLASS_ICON; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String getTooltip() { |
| 65 | + return this.path.getFileName().toString(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Component getComponent() { |
| 70 | + return UIUtils.topAndBottomStickyLayout(this.codeViewPanel, new FontSizeSliderBar()); |
| 71 | + } |
| 72 | + |
| 73 | + public Path getPath() { |
| 74 | + return this.path; |
| 75 | + } |
| 76 | +} |
0 commit comments