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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand All @@ -46,28 +45,21 @@
* business logic.
*/
public class MainFrame extends JFrame {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you plan to serialize the frame instance, this declaration isn't strictly necessary.

private static final long serialVersionUID = 2628641249775639871L;

// --- Subsystems ---
private final RenderingEngine renderingEngine;
private final Timer debounceTimer;
private final RecentFilesManager recentFilesManager; // NEW: Manager instance

// --- File Handling State ---
private final JFileChooser fileChooser;
private Path currentFilePath = null;
// --- UI Components (Injected by Sierra) ---
// Stores the path of the currently loaded file

@Outlet
private JMenuBar menuBar;

@Outlet
private JMenuItem openItem;
// Stores the path of the currently loaded file
private Path currentFilePath = null;

@Outlet
private JMenuItem saveItem;
// --- UI Components (Injected by Sierra) ---
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current convention is to declare outlets as follows, although obviously either will work.

Initializing the value to null avoids a warning in Intellij.

private @Outlet JMenuBar menuBar = null;
private @Outlet JMenuItem openItem = null;
private @Outlet JMenuItem saveItem = null;

@Outlet
private JMenu recentMenu;
Expand All @@ -81,9 +73,6 @@ public class MainFrame extends JFrame {
@Outlet
private JSplitPane splitPane;

@Outlet
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This outlet is not needed when the editor pane is declared in markup - see below.

private JScrollPane editorScrollPane;

@Outlet
private JPanel previewPanel;

Expand All @@ -93,33 +82,31 @@ public class MainFrame extends JFrame {
@Outlet
private JLabel filePathLabel; // The <label> for the file path

// --- Manually Created Components ---
private RSyntaxTextArea editorPane;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to markup.

@Outlet
private RSyntaxTextArea editorPane;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing Sierra code uses spaces rather than tabs.


public MainFrame() {
super("Sierra UI Previewer");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor - this is not needed.

this.renderingEngine = new RenderingEngine();
renderingEngine = new RenderingEngine();

this.recentFilesManager = new RecentFilesManager(MainFrame.class);
recentFilesManager = new RecentFilesManager(MainFrame.class);

setContentPane(UILoader.load(this, "MainFrame.xml"));
// @todo see why it wasn't working to set in xml

// This is an overloaded setter; BeanAdapter only recognizes the version
// with the same type as the getter
splitPane.setDividerLocation(0.5);
splitPane.setResizeWeight(0.5);

this.fileChooser = new JFileChooser();
FileNameExtensionFilter xmlFilter = new FileNameExtensionFilter("XML Files (*.xml)", "xml");
fileChooser = new JFileChooser();
var xmlFilter = new FileNameExtensionFilter("XML Files (*.xml)", "xml");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current Sierra code uses the var keyword introduced in Java 10.

fileChooser.setFileFilter(xmlFilter);
fileChooser.setAcceptAllFileFilterUsed(false);

setupMenuBar();

setupCustomEditor();

// 5. Set layout for previewPanel
// previewPanel.setLayout(new BorderLayout());

this.debounceTimer = setupDebounceTimer();
debounceTimer = setupDebounceTimer();

// Wire editor events
editorPane.getDocument().addDocumentListener(new DocumentListener() {
Expand All @@ -141,8 +128,8 @@ public void changedUpdate(DocumentEvent e) {
}
});

URL iconURL = getClass().getResource("/sierra.png");
Image icon = new ImageIcon(iconURL).getImage();
var iconURL = getClass().getResource("/sierra.png");
var icon = new ImageIcon(iconURL).getImage();
this.setIconImage(icon);

// 8. Trigger an initial render
Expand Down Expand Up @@ -228,11 +215,7 @@ private CompletionProvider createCompletionProvider() {
* placeholder that Sierra injected.
*/
private void setupCustomEditor() {
editorPane = new RSyntaxTextArea(25, 80);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to markup.

editorPane.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_XML);
editorPane.setCodeFoldingEnabled(true);
editorPane.setAntiAliasingEnabled(true);
editorPane.setEditable(true); // Ensure it's editable

CompletionProvider provider = createCompletionProvider();

Expand All @@ -245,8 +228,6 @@ private void setupCustomEditor() {
if (provider != null) {
ac.install(editorPane);
}

editorScrollPane.setViewportView(editorPane);
}

// --- Rendering/Control Logic ---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ private void loadRecentFiles() {
// Split the string by the separator, convert to Path objects, and collect.
recentFiles.addAll(Arrays.stream(savedList.split(PATH_SEPARATOR))
.filter(s -> !s.trim().isEmpty())
.map(Paths::get)
.collect(Collectors.toList()));
.map(Paths::get).toList());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream#toList() was introduced in Java 16.

}
}

Expand All @@ -77,7 +76,7 @@ public void addFile(Path path) {
recentFiles.remove(path);

// 2. Add to the front (most recent)
recentFiles.add(0, path);
recentFiles.addFirst(path);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List#addFirst() was introduced in Java 21.


// 3. Trim the list if it exceeds the max size
if (recentFiles.size() > MAX_RECENT_FILES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
package org.httprpc.sierra.previewer;

import com.formdev.flatlaf.FlatLightLaf;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.httprpc.sierra.UILoader;

import javax.swing.*;

public class SierraPreviewerApp {

public static void main(String[] args) {
FlatLightLaf.setup();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the recommended way to initialize FlatLaf.


UILoader.bind("syntax-text-area", RSyntaxTextArea.class, RSyntaxTextArea::new);

// Run all UI code on the Event Dispatch Thread (EDT)
SwingUtilities.invokeLater(() -> {
try {
// Set a modern Look and Feel if available
UIManager.setLookAndFeel(new FlatLightLaf());
} catch (Exception e) {
System.err.println("Could not set System Look and Feel.");
}

MainFrame frame = new MainFrame();
var frame = new MainFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 800);
frame.setLocationRelativeTo(null); // Center on screen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,15 @@ public SierraXMLCompletionProvider() {
}

private void loadTags() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor simplification that avoids unnecessary local declarations.

Iterable<String> tagNames = UILoader.getTags();
for (String tagName : tagNames) {
tagCompletions.add(new BasicCompletion(this, tagName, "Sierra UI Element"));
Class<?> type = UILoader.getType(tagName);
Map<String, String> attributes = getAttributesForClass(type);
elementAttributeDefinitions.put(tagName, attributes);
for (var tag : UILoader.getTags()) {
tagCompletions.add(new BasicCompletion(this, tag, "Sierra UI Element"));
elementAttributeDefinitions.put(tag, getAttributesForClass(UILoader.getType(tag)));
}

tagCompletions.sort(Comparator.comparing(Completion::getInputText));


baseClassAttributeDefinitions.put(JComponent.class.getName(), getAttributesForClass(JComponent.class));
addCommonAttributes();

}

private void addCommonAttributes() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE column-panel SYSTEM "sierra.dtd">

<column-panel>
<menu-bar name="menuBar">
<menu text="File">
Expand All @@ -12,26 +12,18 @@
</menu>
<menu text="About">
<menu-item name="aboutItem" text="About Previewer"/>
</menu>
</menu>
</menu-bar>

<split-pane name="splitPane" weight="1">
<scroll-pane name="editorScrollPane" weight="1">
<label text="XML will appear here."
horizontalAlignment="center"
verticalAlignment="center"
foreground="gray"
weight="1"/>
</scroll-pane>
<column-panel name="previewPanel" padding="5, 5, 5, 5" weight="1">
<label text="Preview will appear here."
horizontalAlignment="center"
verticalAlignment="center"
foreground="gray"
weight="1"/>
</column-panel>
</split-pane>

<label name="filePathLabel" text="" padding="4, 6, 4, 6"/>
<split-pane name="splitPane" resizeWeight="0.5" weight="1">
<scroll-pane weight="1">
<syntax-text-area name="editorPane" columns="80" rows="25"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare editor in markup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that this will make it seem like "syntax-text-area" is part of the standard distribution, which it is not. So maybe this is not a good idea after all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agree - only valid widgets should be in the xml - even if they are placeholders to be replaced at runtime

codeFoldingEnabled="true"
antiAliasingEnabled="true"/>
</scroll-pane>
<column-panel name="previewPanel" padding="5, 5, 5, 5" weight="1"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop placeholder label that didn't seem to be visible in UI.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah at one point I had it showing and then somehow messed it up

</split-pane>

<label name="filePathLabel" padding="4, 6, 4, 6"/>
<label name="statusBar" text="Ready." padding="2, 5, 2, 5"/>
</column-panel>