Zettelkasten-Team / Zettelkasten

Zettelkasten-Developer-Builds
http://zettelkasten.danielluedecke.de
GNU General Public License v3.0
716 stars 92 forks source link

Refactor `switchLayout` Method to Decouple from Specific Look and Feel #505

Open RalfBarkow opened 1 month ago

RalfBarkow commented 1 month ago

The switchLayout method in the SearchResultsFrame class is currently tightly coupled to specific Look and Feel (LaF) implementations, such as SeaGlass and Mac. This approach makes it difficult to manage and update the layout constraints for different LaFs and reduces the flexibility of the code.

Tasks:

  1. Define Layout Constraints:

    • Create a LayoutConstraints class that holds the border configurations for different LaFs.
    • Define a mapping from LaF names to their respective border configurations.
  2. Update Borders Declaratively:

    • Use the LayoutConstraints class to get the appropriate border configurations based on the current LaF.
    • Apply these configurations in the switchLayout method.
  3. Refactor switchLayout Method:

    • Remove direct checks for specific LaFs within the method.
    • Use the declarative constraints from LayoutConstraints to set the borders and other properties.
    • Ensure that the method still updates the JSplitPane orientation and handles additional styling for Mac if needed.

Example Implementation:

LayoutConstraints.java

public class LayoutConstraints {
    private Map<String, BorderConfig> lafBorders;

    public LayoutConstraints() {
        lafBorders = new HashMap<>();
        // Example configurations for different LaFs
        lafBorders.put("SeaGlass", new BorderConfig(
            BorderFactory.createMatteBorder(0, 0, 1, 0, ColorUtil.getBorderGray(settingsObj)),
            BorderFactory.createMatteBorder(1, 1, 0, 0, ColorUtil.getBorderGray(settingsObj)),
            BorderFactory.createMatteBorder(0, 0, 0, 1, ColorUtil.getBorderGray(settingsObj)),
            BorderFactory.createMatteBorder(0, 1, 0, 0, ColorUtil.getBorderGray(settingsObj))
        ));
        // Add other LaF configurations here
    }

    public BorderConfig getBorderConfig(String laf) {
        return lafBorders.getOrDefault(laf, new BorderConfig());
    }

    public static class BorderConfig {
        private Border verticalSplitBorder1;
        private Border verticalSplitBorder2;
        private Border horizontalSplitBorder1;
        private Border horizontalSplitBorder2;

        public BorderConfig() {
            // Default borders
            this(BorderFactory.createEmptyBorder(), BorderFactory.createEmptyBorder(), BorderFactory.createEmptyBorder(), BorderFactory.createEmptyBorder());
        }

        public BorderConfig(Border vSplitBorder1, Border vSplitBorder2, Border hSplitBorder1, Border hSplitBorder2) {
            this.verticalSplitBorder1 = vSplitBorder1;
            this.verticalSplitBorder2 = vSplitBorder2;
            this.horizontalSplitBorder1 = hSplitBorder1;
            this.horizontalSplitBorder2 = hSplitBorder2;
        }

        public Border getVerticalSplitBorder1() { return verticalSplitBorder1; }
        public Border getVerticalSplitBorder2() { return verticalSplitBorder2; }
        public Border getHorizontalSplitBorder1() { return horizontalSplitBorder1; }
        public Border getHorizontalSplitBorder2() { return horizontalSplitBorder2; }
    }
}

SearchResultsFrame.java

@Action
public void switchLayout() {
    int currentlayout = settingsObj.getSearchFrameSplitLayout();
    String currentLaf = settingsObj.getCurrentLookAndFeel(); // Assuming there's a method to get the current LaF

    LayoutConstraints layoutConstraints = new LayoutConstraints();
    LayoutConstraints.BorderConfig borderConfig = layoutConstraints.getBorderConfig(currentLaf);

    if (JSplitPane.HORIZONTAL_SPLIT == currentlayout) {
        currentlayout = JSplitPane.VERTICAL_SPLIT;
        jPanel1.setBorder(borderConfig.getVerticalSplitBorder1());
        jPanel2.setBorder(borderConfig.getVerticalSplitBorder2());
    } else {
        currentlayout = JSplitPane.HORIZONTAL_SPLIT;
        jPanel1.setBorder(borderConfig.getHorizontalSplitBorder1());
        jPanel2.setBorder(borderConfig.getHorizontalSplitBorder2());
    }

    settingsObj.setSearchFrameSplitLayout(currentlayout);
    jSplitPaneSearch1.setOrientation(currentlayout);

    // Apply additional styling if necessary (can be generalized or removed if no longer needed)
    if (settingsObj.isMacStyle()) {
        ZknMacWidgetFactory.updateSplitPane(jSplitPaneSearch1);
    }
}

Expected Outcome:

Labels:

Assignees:

Milestone:

github-actions[bot] commented 2 weeks ago

This issue is stale because it has been open for 30 days with no activity.