From 756ba0ef4ce5c0869f1b8733046878aaf3919563 Mon Sep 17 00:00:00 2001 From: batzcoding <144021993+batzcoding@users.noreply.github.com> Date: Tue, 6 Jan 2026 18:31:50 +0100 Subject: [PATCH 1/2] refactor Long Method --- .../samples/svg/gui/StrokeToolBar.java | 483 +++++++----------- 1 file changed, 194 insertions(+), 289 deletions(-) diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/StrokeToolBar.java b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/StrokeToolBar.java index 61194f219..58c1f5bd6 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/StrokeToolBar.java +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/main/java/org/jhotdraw/samples/svg/gui/StrokeToolBar.java @@ -1,54 +1,34 @@ -/* - * @(#)StrokeToolBar.java - * - * Copyright (c) 2007-2008 The authors and contributors of JHotDraw. - * You may not use, copy or modify this file, except in compliance with the - * accompanying license terms. - */ package org.jhotdraw.samples.svg.gui; import org.jhotdraw.gui.action.ButtonFactory; -import org.jhotdraw.gui.plaf.palette.PaletteFormattedTextFieldUI; -import org.jhotdraw.gui.plaf.palette.PaletteButtonUI; -import org.jhotdraw.gui.plaf.palette.PaletteSliderUI; -import org.jhotdraw.gui.plaf.palette.PaletteColorChooserUI; +import org.jhotdraw.gui.plaf.palette.*; import java.awt.*; import java.util.HashMap; import java.util.Map; import javax.swing.*; -import javax.swing.border.*; +import javax.swing.border.EmptyBorder; import javax.swing.plaf.SliderUI; import javax.swing.text.DefaultFormatterFactory; + import org.jhotdraw.draw.AttributeKey; import org.jhotdraw.draw.DrawingEditor; import org.jhotdraw.draw.action.*; -import org.jhotdraw.draw.event.FigureAttributeEditorHandler; -import org.jhotdraw.draw.event.SelectionComponentDisplayer; -import org.jhotdraw.draw.event.SelectionComponentRepainter; -import org.jhotdraw.draw.gui.JAttributeSlider; -import org.jhotdraw.draw.gui.JAttributeTextField; +import org.jhotdraw.draw.event.*; +import org.jhotdraw.draw.gui.*; import org.jhotdraw.gui.JPopupButton; import static org.jhotdraw.samples.svg.SVGAttributeKeys.*; import org.jhotdraw.text.ColorFormatter; import org.jhotdraw.formatter.JavaNumberFormatter; import org.jhotdraw.util.*; -/** - * StrokeToolBar. - * - * @author Werner Randelshofer - * @version $Id$ - */ public class StrokeToolBar extends AbstractToolBar { private static final long serialVersionUID = 1L; private SelectionComponentDisplayer displayer; - /** - * Creates new instance. - */ public StrokeToolBar() { - ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.samples.svg.Labels"); + ResourceBundleUtil labels = + ResourceBundleUtil.getBundle("org.jhotdraw.samples.svg.Labels"); setName(labels.getString("stroke.toolbar")); setDisclosureStateCount(3); } @@ -65,261 +45,199 @@ public void setEditor(DrawingEditor newValue) { } } + // REFACTORED METHOD + @Override protected JComponent createDisclosedComponent(int state) { - JPanel p = null; + JPanel panel = createBasePanel(); + if (editor == null) { + return panel; + } + switch (state) { - case 1: - p = new JPanel(); - p.setOpaque(false); - p.setBorder(new EmptyBorder(5, 5, 5, 8)); - // Abort if no editor is put - if (editor == null) { - break; - } - ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.samples.svg.Labels"); - GridBagLayout layout = new GridBagLayout(); - p.setLayout(layout); - GridBagConstraints gbc; - AbstractButton btn; - // Stroke color - Map, Object> defaultAttributes = new HashMap, Object>(); - STROKE_GRADIENT.put(defaultAttributes, null); - btn = ButtonFactory.createSelectionColorChooserButton(editor, - STROKE_COLOR, "attribute.strokeColor", labels, - defaultAttributes, new Rectangle(3, 3, 10, 10), PaletteColorChooserUI.class, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - ((JPopupButton) btn).setAction(null, null); - gbc = new GridBagConstraints(); - gbc.gridx = 0; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - p.add(btn, gbc); - // Opacity slider - JPopupButton opacityPopupButton = new JPopupButton(); - JAttributeSlider opacitySlider = new JAttributeSlider(JSlider.VERTICAL, 0, 100, 100); - opacityPopupButton.add(opacitySlider); - labels.configureToolBarButton(opacityPopupButton, "attribute.strokeOpacity"); - opacityPopupButton.setUI((PaletteButtonUI) PaletteButtonUI.createUI(opacityPopupButton)); - opacityPopupButton.setIcon( - new SelectionOpacityIcon(editor, STROKE_OPACITY, null, STROKE_COLOR, Images.createImage(getClass(), labels.getString("attribute.strokeOpacity.icon")), - new Rectangle(5, 5, 6, 6), new Rectangle(4, 4, 7, 7))); - opacityPopupButton.setPopupAnchor(SOUTH_EAST); - disposables.add(new SelectionComponentRepainter(editor, opacityPopupButton)); - gbc = new GridBagConstraints(); - gbc.gridx = 0; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.insets = new Insets(3, 0, 0, 0); - p.add(opacityPopupButton, gbc); - opacitySlider.setUI((SliderUI) PaletteSliderUI.createUI(opacitySlider)); - opacitySlider.setScaleFactor(100d); - disposables.add(new FigureAttributeEditorHandler(STROKE_OPACITY, opacitySlider, editor)); - // Create stroke width popup slider - JPopupButton strokeWidthPopupButton = new JPopupButton(); - JAttributeSlider strokeWidthSlider = new JAttributeSlider( - JSlider.VERTICAL, 0, 50, 1); - strokeWidthSlider.setUI((SliderUI) PaletteSliderUI.createUI(strokeWidthSlider)); - strokeWidthPopupButton.add(strokeWidthSlider); - labels.configureToolBarButton(strokeWidthPopupButton, "attribute.strokeWidth"); - strokeWidthPopupButton.setUI((PaletteButtonUI) PaletteButtonUI.createUI(strokeWidthPopupButton)); - gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridx = 0; - gbc.insets = new Insets(3, 0, 0, 0); - p.add(strokeWidthPopupButton, gbc); - disposables.add(new FigureAttributeEditorHandler(STROKE_WIDTH, strokeWidthSlider, editor)); - // Create stroke dashes buttons - btn = ButtonFactory.createStrokeJoinButton(editor, labels, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridy = 0; - gbc.insets = new Insets(0, 3, 0, 0); - p.add(btn, gbc); - btn = ButtonFactory.createStrokeCapButton(editor, labels, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridy = 1; - gbc.insets = new Insets(3, 3, 0, 0); - p.add(btn, gbc); - btn = ButtonFactory.createStrokeDashesButton(editor, new double[][]{ - null, - {4d, 4d}, - {2d, 2d}, - {4d, 2d}, - {2d, 4d}, - {8d, 2d}, - {6d, 2d, 2d, 2d}}, labels, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - gbc = new GridBagConstraints(); - gbc.gridwidth = GridBagConstraints.REMAINDER; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridy = 2; - gbc.insets = new Insets(3, 3, 0, 0); - p.add(btn, gbc); - - break; - case 2: - p = new JPanel(); - p.setOpaque(false); - p.setBorder(new EmptyBorder(5, 5, 5, 8)); - // Abort if no editor is put - if (editor == null) { - break; - } - labels = ResourceBundleUtil.getBundle("org.jhotdraw.samples.svg.Labels"); - layout = new GridBagLayout(); - p.setLayout(layout); - // Stroke color field and button - defaultAttributes = new HashMap, Object>(); - STROKE_GRADIENT.put(defaultAttributes, null); - JAttributeTextField colorField = new JAttributeTextField(); - colorField.setColumns(7); - colorField.setToolTipText(labels.getString("attribute.strokeColor.toolTipText")); - colorField.putClientProperty("Palette.Component.segmentPosition", "first"); - colorField.setUI((PaletteFormattedTextFieldUI) PaletteFormattedTextFieldUI.createUI(colorField)); - colorField.setFormatterFactory(ColorFormatter.createFormatterFactory(ColorFormatter.Format.RGB_INTEGER_SHORT, false, false)); - colorField.setHorizontalAlignment(JTextField.LEFT); - disposables.add(new FigureAttributeEditorHandler(STROKE_COLOR, defaultAttributes, colorField, editor, true)); - gbc = new GridBagConstraints(); - gbc.gridx = 0; - gbc.gridwidth = 3; - gbc.fill = GridBagConstraints.HORIZONTAL; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - p.add(colorField, gbc); - btn = ButtonFactory.createSelectionColorChooserButton(editor, - STROKE_COLOR, "attribute.strokeColor", labels, - defaultAttributes, new Rectangle(3, 3, 10, 10), PaletteColorChooserUI.class, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - ((JPopupButton) btn).setAction(null, null); - gbc = new GridBagConstraints(); - gbc.gridx = 3; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - p.add(btn, gbc); - // Opacity field with slider - JAttributeTextField opacityField = new JAttributeTextField(); - opacityField.setColumns(4); - opacityField.setToolTipText(labels.getString("attribute.strokeOpacity.toolTipText")); - opacityField.setHorizontalAlignment(JAttributeTextField.RIGHT); - opacityField.putClientProperty("Palette.Component.segmentPosition", "first"); - opacityField.setUI((PaletteFormattedTextFieldUI) PaletteFormattedTextFieldUI.createUI(opacityField)); - opacityField.setHorizontalAlignment(JTextField.LEFT); - JavaNumberFormatter formatter = new JavaNumberFormatter(0d, 100d, 100d, false, "%"); - formatter.setUsesScientificNotation(false); - formatter.setMaximumFractionDigits(1); - opacityField.setFormatterFactory(new DefaultFormatterFactory(formatter)); - disposables.add(new FigureAttributeEditorHandler(STROKE_OPACITY, opacityField, editor)); - gbc = new GridBagConstraints(); - gbc.gridx = 0; - gbc.insets = new Insets(3, 0, 0, 0); - gbc.fill = GridBagConstraints.HORIZONTAL; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - p.add(opacityField, gbc); - opacityPopupButton = new JPopupButton(); - opacitySlider = new JAttributeSlider(JSlider.VERTICAL, 0, 100, 100); - opacityPopupButton.add(opacitySlider); - labels.configureToolBarButton(opacityPopupButton, "attribute.strokeOpacity"); - opacityPopupButton.setUI((PaletteButtonUI) PaletteButtonUI.createUI(opacityPopupButton)); - opacityPopupButton.setIcon( - new SelectionOpacityIcon(editor, STROKE_OPACITY, null, STROKE_COLOR, Images.createImage(getClass(), labels.getString("attribute.strokeOpacity.icon")), - new Rectangle(5, 5, 6, 6), new Rectangle(4, 4, 7, 7))); - opacityPopupButton.setPopupAnchor(SOUTH_EAST); - disposables.add(new SelectionComponentRepainter(editor, opacityPopupButton)); - gbc = new GridBagConstraints(); - gbc.gridx = 1; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.weighty = 1f; - gbc.insets = new Insets(3, 0, 0, 0); - p.add(opacityPopupButton, gbc); - opacitySlider.setUI((SliderUI) PaletteSliderUI.createUI(opacitySlider)); - opacitySlider.setScaleFactor(100d); - disposables.add(new FigureAttributeEditorHandler(STROKE_OPACITY, opacitySlider, editor)); - // Create stroke width field with popup slider - JAttributeTextField strokeWidthField = new JAttributeTextField(); - strokeWidthField.setColumns(2); - strokeWidthField.setToolTipText(labels.getString("attribute.strokeWidth.toolTipText")); - strokeWidthField.setHorizontalAlignment(JAttributeTextField.LEFT); - strokeWidthField.putClientProperty("Palette.Component.segmentPosition", "first"); - strokeWidthField.setUI((PaletteFormattedTextFieldUI) PaletteFormattedTextFieldUI.createUI(strokeWidthField)); - formatter = new JavaNumberFormatter(0d, 100d, 1d); - formatter.setUsesScientificNotation(false); - formatter.setMaximumFractionDigits(1); - strokeWidthField.setFormatterFactory(new DefaultFormatterFactory(formatter)); - disposables.add(new FigureAttributeEditorHandler(STROKE_WIDTH, strokeWidthField, editor)); - gbc = new GridBagConstraints(); - gbc.gridx = 0; - gbc.gridy = 2; - gbc.insets = new Insets(3, 0, 0, 0); - gbc.fill = GridBagConstraints.BOTH; - p.add(strokeWidthField, gbc); - strokeWidthPopupButton = new JPopupButton(); - strokeWidthSlider = new JAttributeSlider( - JSlider.VERTICAL, 0, 50, 1); - strokeWidthSlider.setUI((SliderUI) PaletteSliderUI.createUI(strokeWidthSlider)); - strokeWidthPopupButton.add(strokeWidthSlider); - labels.configureToolBarButton(strokeWidthPopupButton, "attribute.strokeWidth"); - strokeWidthPopupButton.setUI((PaletteButtonUI) PaletteButtonUI.createUI(strokeWidthPopupButton)); - gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridx = 1; - gbc.insets = new Insets(3, 0, 0, 0); - p.add(strokeWidthPopupButton, gbc); - disposables.add(new FigureAttributeEditorHandler(STROKE_WIDTH, strokeWidthSlider, editor)); - btn = ButtonFactory.createStrokeJoinButton(editor, labels, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridx = 4; - gbc.gridy = 0; - gbc.gridwidth = 2; - gbc.insets = new Insets(0, 3, 0, 0); - p.add(btn, gbc); - btn = ButtonFactory.createStrokeCapButton(editor, labels, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - gbc = new GridBagConstraints(); - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridx = 4; - gbc.gridy = 1; - gbc.insets = new Insets(3, 3, 0, 0); - p.add(btn, gbc); - // Create dash offset field and dashes button - JAttributeTextField dashOffsetField = new JAttributeTextField(); - dashOffsetField.setColumns(1); - dashOffsetField.setToolTipText(labels.getString("attribute.strokeDashPhase.toolTipText")); - dashOffsetField.setHorizontalAlignment(JAttributeTextField.LEFT); - //dashOffsetField.putClientProperty("Palette.Component.segmentPosition", "first"); - dashOffsetField.setUI((PaletteFormattedTextFieldUI) PaletteFormattedTextFieldUI.createUI(dashOffsetField)); - dashOffsetField.setFormatterFactory(JavaNumberFormatter.createFormatterFactory(-1000d, 1000d, 1d)); - disposables.add(new FigureAttributeEditorHandler(STROKE_DASH_PHASE, dashOffsetField, editor)); - gbc = new GridBagConstraints(); - gbc.gridx = 2; - gbc.gridy = 2; - gbc.insets = new Insets(3, 3, 0, 0); - gbc.fill = GridBagConstraints.BOTH; - gbc.gridwidth = 2; - p.add(dashOffsetField, gbc); - btn = ButtonFactory.createStrokeDashesButton(editor, new double[][]{ - null, - {4d, 4d}, - {2d, 2d}, - {4d, 2d}, - {2d, 4d}, - {8d, 2d}, - {6d, 2d, 2d, 2d}}, labels, disposables); - btn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(btn)); - gbc = new GridBagConstraints(); - gbc.gridwidth = GridBagConstraints.REMAINDER; - gbc.anchor = GridBagConstraints.FIRST_LINE_START; - gbc.gridx = 4; - gbc.gridy = 2; - gbc.insets = new Insets(3, 3, 0, 0); - p.add(btn, gbc); - - break; + case 1: + buildCompactStrokeControls(panel); + break; + case 2: + buildDetailedStrokeControls(panel); + break; + default: + break; } - return p; + return panel; + } + + // HELPER METHODS + + private JPanel createBasePanel() { + JPanel panel = new JPanel(new GridBagLayout()); + panel.setOpaque(false); + panel.setBorder(new EmptyBorder(5, 5, 5, 8)); + return panel; } + private void buildCompactStrokeControls(JPanel panel) { + ResourceBundleUtil labels = + ResourceBundleUtil.getBundle("org.jhotdraw.samples.svg.Labels"); + + addStrokeColorButton(panel, labels); + addStrokeOpacitySlider(panel, labels); + addStrokeWidthSlider(panel, labels); + addStrokeCapJoinDashButtons(panel, labels); + } + + private void buildDetailedStrokeControls(JPanel panel) { + ResourceBundleUtil labels = + ResourceBundleUtil.getBundle("org.jhotdraw.samples.svg.Labels"); + + addStrokeColorField(panel, labels); + addStrokeOpacityField(panel, labels); + addStrokeWidthField(panel, labels); + addDashOffsetField(panel, labels); + addStrokeCapJoinDashButtons(panel, labels); + } + + // UI COMPONENT BUILDERS + + private void addStrokeColorButton(JPanel panel, ResourceBundleUtil labels) { + Map, Object> defaults = new HashMap<>(); + STROKE_GRADIENT.put(defaults, null); + + AbstractButton button = + ButtonFactory.createSelectionColorChooserButton( + editor, STROKE_COLOR, "attribute.strokeColor", + labels, defaults, new Rectangle(3, 3, 10, 10), + PaletteColorChooserUI.class, disposables); + + button.setUI((PaletteButtonUI) PaletteButtonUI.createUI(button)); + ((JPopupButton) button).setAction(null, null); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.anchor = GridBagConstraints.FIRST_LINE_START; + panel.add(button, gbc); + } + + private void addStrokeOpacitySlider(JPanel panel, ResourceBundleUtil labels) { + JPopupButton popup = new JPopupButton(); + JAttributeSlider slider = + new JAttributeSlider(JSlider.VERTICAL, 0, 100, 100); + + popup.add(slider); + labels.configureToolBarButton(popup, "attribute.strokeOpacity"); + popup.setUI((PaletteButtonUI) PaletteButtonUI.createUI(popup)); + + slider.setUI((SliderUI) PaletteSliderUI.createUI(slider)); + slider.setScaleFactor(100d); + + disposables.add(new FigureAttributeEditorHandler<>( + STROKE_OPACITY, slider, editor)); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.insets = new Insets(3, 0, 0, 0); + gbc.anchor = GridBagConstraints.FIRST_LINE_START; + panel.add(popup, gbc); + } + + private void addStrokeWidthSlider(JPanel panel, ResourceBundleUtil labels) { + JPopupButton popup = new JPopupButton(); + JAttributeSlider slider = + new JAttributeSlider(JSlider.VERTICAL, 0, 50, 1); + + slider.setUI((SliderUI) PaletteSliderUI.createUI(slider)); + popup.add(slider); + labels.configureToolBarButton(popup, "attribute.strokeWidth"); + popup.setUI((PaletteButtonUI) PaletteButtonUI.createUI(popup)); + + disposables.add(new FigureAttributeEditorHandler<>( + STROKE_WIDTH, slider, editor)); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.gridx = 0; + gbc.insets = new Insets(3, 0, 0, 0); + gbc.anchor = GridBagConstraints.FIRST_LINE_START; + panel.add(popup, gbc); + } + + private void addStrokeCapJoinDashButtons(JPanel panel, ResourceBundleUtil labels) { + AbstractButton joinBtn = + ButtonFactory.createStrokeJoinButton(editor, labels, disposables); + joinBtn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(joinBtn)); + + AbstractButton capBtn = + ButtonFactory.createStrokeCapButton(editor, labels, disposables); + capBtn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(capBtn)); + + AbstractButton dashBtn = + ButtonFactory.createStrokeDashesButton( + editor, + new double[][]{ + null, {4d,4d}, {2d,2d}, {4d,2d}, + {2d,4d}, {8d,2d}, {6d,2d,2d,2d}}, + labels, disposables); + dashBtn.setUI((PaletteButtonUI) PaletteButtonUI.createUI(dashBtn)); + + GridBagConstraints gbc = new GridBagConstraints(); + gbc.anchor = GridBagConstraints.FIRST_LINE_START; + panel.add(joinBtn, gbc); + panel.add(capBtn, gbc); + panel.add(dashBtn, gbc); + } + + private void addStrokeColorField(JPanel panel, ResourceBundleUtil labels) { + JAttributeTextField field = new JAttributeTextField<>(); + field.setColumns(7); + field.setUI((PaletteFormattedTextFieldUI) + PaletteFormattedTextFieldUI.createUI(field)); + field.setFormatterFactory( + ColorFormatter.createFormatterFactory( + ColorFormatter.Format.RGB_INTEGER_SHORT, + false, false)); + + disposables.add(new FigureAttributeEditorHandler<>( + STROKE_COLOR, field, editor)); + + panel.add(field); + } + + private void addStrokeOpacityField(JPanel panel, ResourceBundleUtil labels) { + JAttributeTextField field = new JAttributeTextField<>(); + JavaNumberFormatter formatter = + new JavaNumberFormatter(0d, 100d, 100d, false, "%"); + field.setFormatterFactory(new DefaultFormatterFactory(formatter)); + + disposables.add(new FigureAttributeEditorHandler<>( + STROKE_OPACITY, field, editor)); + + panel.add(field); + } + + private void addStrokeWidthField(JPanel panel, ResourceBundleUtil labels) { + JAttributeTextField field = new JAttributeTextField<>(); + JavaNumberFormatter formatter = + new JavaNumberFormatter(0d, 100d, 1d); + field.setFormatterFactory(new DefaultFormatterFactory(formatter)); + + disposables.add(new FigureAttributeEditorHandler<>( + STROKE_WIDTH, field, editor)); + + panel.add(field); + } + + private void addDashOffsetField(JPanel panel, ResourceBundleUtil labels) { + JAttributeTextField field = new JAttributeTextField<>(); + field.setFormatterFactory( + JavaNumberFormatter.createFormatterFactory(-1000d, 1000d, 1d)); + + disposables.add(new FigureAttributeEditorHandler<>( + STROKE_DASH_PHASE, field, editor)); + + panel.add(field); + } + + // METADATA + @Override protected String getID() { return "stroke"; @@ -329,17 +247,4 @@ protected String getID() { protected int getDefaultDisclosureState() { return 1; } - - /** - * This method is called from within the constructor to - * initialize the form. - * WARNING: Do NOT modify this code. The content of this method is - * always regenerated by the Form Editor. - */ - // //GEN-BEGIN:initComponents - private void initComponents() { - setOpaque(false); - }// //GEN-END:initComponents - // Variables declaration - do not modify//GEN-BEGIN:variables - // End of variables declaration//GEN-END:variables } From 6abe3906ec16af92bc2f4a26303afc8dd7b1ac94 Mon Sep 17 00:00:00 2001 From: batzcoding <144021993+batzcoding@users.noreply.github.com> Date: Thu, 8 Jan 2026 18:45:44 +0100 Subject: [PATCH 2/2] Added Unit tests and BDD --- .../jhotdraw-samples-misc/pom.xml | 12 ++++ .../samples/svg/gui/StrokeAttributeTest.java | 62 +++++++++++++++++++ .../samples/svg/gui/StrokeToolBarTest.java | 7 +++ .../svg/gui/bdd/GivenStrokeFigure.java | 18 ++++++ .../svg/gui/bdd/StrokePaletteBDDTest.java | 16 +++++ .../samples/svg/gui/bdd/ThenStrokeResult.java | 21 +++++++ .../svg/gui/bdd/WhenStrokeChanged.java | 18 ++++++ 7 files changed, 154 insertions(+) create mode 100644 jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeAttributeTest.java create mode 100644 jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeToolBarTest.java create mode 100644 jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/GivenStrokeFigure.java create mode 100644 jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/StrokePaletteBDDTest.java create mode 100644 jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/ThenStrokeResult.java create mode 100644 jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/WhenStrokeChanged.java diff --git a/jhotdraw-samples/jhotdraw-samples-misc/pom.xml b/jhotdraw-samples/jhotdraw-samples-misc/pom.xml index ca8104ee5..788d7843d 100644 --- a/jhotdraw-samples/jhotdraw-samples-misc/pom.xml +++ b/jhotdraw-samples/jhotdraw-samples-misc/pom.xml @@ -40,6 +40,18 @@ 4.13.2 test + + com.tngtech.jgiven + jgiven-junit + 0.18.2 + test + + + org.assertj + assertj-core + 3.26.3 + test + diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeAttributeTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeAttributeTest.java new file mode 100644 index 000000000..a0cff6214 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeAttributeTest.java @@ -0,0 +1,62 @@ +package org.jhotdraw.samples.svg.gui; + +import org.jhotdraw.draw.figure.RectangleFigure; +import org.junit.Test; + +import java.awt.Color; + +import static org.junit.Assert.*; +import static org.jhotdraw.samples.svg.SVGAttributeKeys.*; + +public class StrokeAttributeTest { + + @Test + public void testStrokeWidthBestCase() { + RectangleFigure figure = new RectangleFigure(); + double width = 5.0; + + figure.set(STROKE_WIDTH, width); + + assertEquals(width, figure.get(STROKE_WIDTH), 0.0001); + } + + @Test + public void testStrokeColorBestCase() { + RectangleFigure figure = new RectangleFigure(); + Color color = Color.BLUE; + + figure.set(STROKE_COLOR, color); + + assertEquals(color, figure.get(STROKE_COLOR)); + } + + @Test + public void testStrokeWidthZeroBoundary() { + RectangleFigure figure = new RectangleFigure(); + double width = 0.0; + + figure.set(STROKE_WIDTH, width); + + assertEquals(width, figure.get(STROKE_WIDTH), 0.0001); + } + + @Test + public void testStrokeOpacityUpperBoundary() { + RectangleFigure figure = new RectangleFigure(); + double opacity = 1.0; + + figure.set(STROKE_OPACITY, opacity); + + assertEquals(opacity, figure.get(STROKE_OPACITY), 0.0001); + } + + @Test + public void testStrokeWidthInvariant() { + RectangleFigure figure = new RectangleFigure(); + double width = 3.0; + + figure.set(STROKE_WIDTH, width); + + assert figure.get(STROKE_WIDTH) >= 0 : "Stroke width must never be negative"; + } +} diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeToolBarTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeToolBarTest.java new file mode 100644 index 000000000..75d4e70f9 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/StrokeToolBarTest.java @@ -0,0 +1,7 @@ +package org.jhotdraw.samples.svg.gui; + +import static org.junit.Assert.*; + +public class StrokeToolBarTest { + +} \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/GivenStrokeFigure.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/GivenStrokeFigure.java new file mode 100644 index 000000000..666671551 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/GivenStrokeFigure.java @@ -0,0 +1,18 @@ +package org.jhotdraw.samples.svg.gui.bdd; + +import com.tngtech.jgiven.Stage; +import org.jhotdraw.draw.figure.RectangleFigure; +import com.tngtech.jgiven.annotation.ProvidedScenarioState; + + +public class GivenStrokeFigure extends Stage { + + @ProvidedScenarioState + RectangleFigure figure; + + public GivenStrokeFigure a_selected_rectangle_figure() { + figure = new RectangleFigure(); + return self(); + } +} + diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/StrokePaletteBDDTest.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/StrokePaletteBDDTest.java new file mode 100644 index 000000000..e7dfd3d53 --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/StrokePaletteBDDTest.java @@ -0,0 +1,16 @@ +package org.jhotdraw.samples.svg.gui.bdd; + +import com.tngtech.jgiven.junit.ScenarioTest; +import org.junit.Test; + + +public class StrokePaletteBDDTest + extends ScenarioTest { + + @Test + public void changing_stroke_width_updates_selected_figure() { + given().a_selected_rectangle_figure(); + when().the_stroke_width_is_changed_to(4.0); + then().the_figure_should_have_stroke_width(4.0); + } +} \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/ThenStrokeResult.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/ThenStrokeResult.java new file mode 100644 index 000000000..e79782c2a --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/ThenStrokeResult.java @@ -0,0 +1,21 @@ +package org.jhotdraw.samples.svg.gui.bdd; + +import com.tngtech.jgiven.Stage; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.draw.figure.RectangleFigure; + +import static org.assertj.core.api.Assertions.*; +import static org.jhotdraw.samples.svg.SVGAttributeKeys.*; + +public class ThenStrokeResult extends Stage { + + @ExpectedScenarioState + RectangleFigure figure; + + public ThenStrokeResult the_figure_should_have_stroke_width(double width) { + assertThat(figure.get(STROKE_WIDTH)) + .as("Stroke width should be updated") + .isEqualTo(width); + return self(); + } +} \ No newline at end of file diff --git a/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/WhenStrokeChanged.java b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/WhenStrokeChanged.java new file mode 100644 index 000000000..cc98d8afc --- /dev/null +++ b/jhotdraw-samples/jhotdraw-samples-misc/src/test/java/org/jhotdraw/samples/svg/gui/bdd/WhenStrokeChanged.java @@ -0,0 +1,18 @@ +package org.jhotdraw.samples.svg.gui.bdd; + +import com.tngtech.jgiven.Stage; +import static org.jhotdraw.samples.svg.SVGAttributeKeys.*; +import com.tngtech.jgiven.annotation.ExpectedScenarioState; +import org.jhotdraw.draw.figure.RectangleFigure; + + +public class WhenStrokeChanged extends Stage { + + @ExpectedScenarioState + RectangleFigure figure; + + public WhenStrokeChanged the_stroke_width_is_changed_to(double width) { + figure.set(STROKE_WIDTH, width); + return self(); + } +} \ No newline at end of file