Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/org/editor/CanvasFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public class CanvasFrame extends JPanel implements MouseListener, MouseMotionLis
private static CanvasFrame _the = null;
private DockKey key = new DockKey("canvas");

/**
* Initializes the CanvasFrame with grid rendering, mouse interaction, and a periodic repaint timer.
*
* Sets up the layout, background color, mouse listeners, grid image, preferred size, and starts a timer to repaint the canvas and update frame timing.
*/
private CanvasFrame() {
super(new BorderLayout());
this.setBackground(new Color(18, 18, 18));
Expand Down Expand Up @@ -306,6 +311,14 @@ private void drawGrid() {
public void mouseClicked(MouseEvent e) {
}

/**
* Draws a red crosshair at the current mouse position on the canvas.
*
* If grid snapping is enabled, the crosshair aligns to the nearest grid intersection.
* The crosshair consists of intersecting vertical and horizontal lines and a small circle at the intersection point.
*
* @param g2 the graphics context used for drawing
*/
private void drawCrosshair(Graphics2D g2) {
if (mouseX >= 0 && mouseY >= 0) {

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/editor/CodeEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,23 @@ public class CodeEditor extends JPanel implements Dockable {
private DockKey key; // = new DockKey("textEditor");
public int tabIndex =0;

/**
* Constructs a new CodeEditor with no associated file, initializing a temporary editor instance.
*/
public CodeEditor() {
this(null);
}

/**
* Constructs a code editor panel with syntax highlighting, code completion, and docking support.
*
* <p>If a file path is provided, initializes the editor with that file; otherwise, creates a temporary file for editing.
* Sets up syntax highlighting for the "piccode" language, enables code folding, line numbers, bookmarks, and installs code completion.
* Configures docking integration with appropriate title, tooltip, and icon.
* Adds focus and caret listeners to update the selected editor and cursor position display.
*
* @param path the file path to open in the editor, or {@code null} to create a temporary file
*/
public CodeEditor(Path path) {
super(new BorderLayout());
textArea = new TextEditorPane();
Expand Down Expand Up @@ -131,6 +144,13 @@ public void focusLost(FocusEvent e) {
this.add(sp, BorderLayout.CENTER);
}

/**
* Saves the current file, writing the editor's content to disk.
*
* If the file is marked as temporary or an error occurs during saving, prompts the user to choose a save location. Updates the editor window's status and persists the file after saving.
*
* @return {@code true} if the file was saved successfully; {@code false} otherwise
*/
public boolean saveFile() {
if (isTmp) {
return saveFileAs();
Expand All @@ -155,6 +175,13 @@ public void setIsTmp(boolean isTmp) {
this.isTmp = isTmp;
}

/**
* Opens a file chooser dialog to save the current editor content to a user-selected file.
*
* Applies file filters for markdown and piccode files. If the user approves, saves the content to the chosen file, reloads it into the editor, updates the file path, persists the file, updates UI elements, and marks the file as non-temporary. Returns {@code true} if the save is successful, or {@code false} if the operation is canceled or an error occurs.
*
* @return {@code true} if the file was saved successfully; {@code false} otherwise
*/
public boolean saveFileAs() {
var fileChooser = new JFileChooser(".");
fileChooser.setFileFilter(FileFilter.mdFilter);
Expand Down Expand Up @@ -237,6 +264,11 @@ private CompletionProvider createCompletionProvider() {

}

/**
* Adds predefined code templates for common drawing operations to the global code template manager.
*
* These templates provide shorthand insertions for frequently used drawing functions in the editor.
*/
public static void createTemplateManager() {
CodeTemplateManager ctm = RSyntaxTextArea.getCodeTemplateManager();
String[][] templates = {
Expand All @@ -254,6 +286,12 @@ public static void createTemplateManager() {
}
}

/**
* Loads the specified file into the editor, applies appropriate syntax highlighting based on file extension, and persists the file.
*
* @param fp the file to load into the editor
* @return true if the file was loaded successfully; false if an I/O error occurred
*/
public boolean load(File fp) {
setIsTmp(false);
var loc = FileLocation.create(fp);
Expand All @@ -276,11 +314,21 @@ public boolean load(File fp) {
}
}

/**
* Returns the docking key associated with this editor for integration with the docking framework.
*
* @return the DockKey for this editor
*/
@Override
public DockKey getDockKey() {
return key;
}

/**
* Returns the editor component for docking integration.
*
* @return this editor panel as a Swing component
*/
@Override
public Component getComponent() {
return this;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/editor/DockablePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,56 @@
public class DockablePanel extends JPanel implements Dockable {
private DockKey key;

/**
* Constructs a DockablePanel with the specified layout and a unique DockKey ID.
*
* @param layout the layout manager to use for this panel
*/
public DockablePanel(LayoutManager layout) {
super(layout);
key = new DockKey("dock-" + System.nanoTime());
}

/**
* Constructs a DockablePanel with the specified layout manager and a DockKey identified by the given ID.
*
* @param layout the layout manager to use for this panel
* @param id the unique identifier for the DockKey associated with this panel
*/
public DockablePanel(LayoutManager layout, String id) {
super(layout);
key = new DockKey(id);
}

/**
* Constructs a DockablePanel with the specified layout, identifier, name, tooltip, and icon.
*
* @param layout the layout manager to use for this panel
* @param id the unique identifier for the dock key
* @param name the display name for the dock key
* @param tip the tooltip text for the dock key
* @param icon the icon identifier used to retrieve the panel's icon
*/
public DockablePanel(LayoutManager layout, String id, String name, String tip, String icon) {
super(layout);
key = new DockKey(id, name, tip, Icons.getIcon(icon));
}

/**
* Returns the {@link DockKey} associated with this dockable panel.
*
* @return the DockKey identifying and describing this panel for docking operations
*/
@Override
public DockKey getDockKey() {
return key;
}

/**
* Returns this panel as the dockable component.
*
* @return this panel instance
*/
@Override
public Component getComponent() {
return this;
Expand Down
Loading