From ce4714dc6b158e219ccd3cd6004c9560524d366b Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Sat, 8 Jun 2024 07:55:42 +0400 Subject: [PATCH] sonarlint refactor --- .../creme332/controller/CanvasController.java | 48 ++++++++----------- .../creme332/controller/Controller.java | 19 +++----- .../creme332/controller/FrameController.java | 1 - .../controller/MenuBarController.java | 5 +- .../java/com/github/creme332/model/Mode.java | 6 +-- .../java/com/github/creme332/view/Frame.java | 13 ++--- .../github/creme332/view/SideMenuPanel.java | 2 - 7 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/github/creme332/controller/CanvasController.java b/src/main/java/com/github/creme332/controller/CanvasController.java index b3b46ea5..18d75fd0 100644 --- a/src/main/java/com/github/creme332/controller/CanvasController.java +++ b/src/main/java/com/github/creme332/controller/CanvasController.java @@ -12,43 +12,49 @@ public class CanvasController { private Canvas canvas; private Point initialClick; - public final int MAX_CELL_SIZE = 500; - public final int MIN_CELL_SIZE = 30; + public static final int MAX_CELL_SIZE = 500; + public static final int DEFAULT_CELL_SIZE = 100; + public static final int MIN_CELL_SIZE = 30; + public static final int ZOOM_INCREMENT = 10; public CanvasController(Canvas canvas) { this.canvas = canvas; - + canvas.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { handleCanvasResize(); } }); - + canvas.addMouseListener(new MouseAdapter() { + @Override public void mousePressed(MouseEvent e) { handleMousePressed(e); } }); - + canvas.addMouseMotionListener(new MouseAdapter() { + @Override public void mouseMoved(MouseEvent e) { - // System.out.format("Mouse moved: %d, %d\n", e.getX(), e.getY()); + // TODO document why this method is empty } }); - + canvas.addMouseMotionListener(new MouseAdapter() { + @Override public void mouseDragged(MouseEvent e) { handleMouseDragged(e); } }); - + canvas.addMouseWheelListener(new MouseAdapter() { + @Override public void mouseWheelMoved(MouseWheelEvent e) { handleCanvasZoom(e); } }); - + // Add action listeners for the zoom panel buttons canvas.getHomeButton().addActionListener(e -> handleHomeButton()); canvas.getZoomInButton().addActionListener(e -> handleZoomInButton()); @@ -56,24 +62,19 @@ public void mouseWheelMoved(MouseWheelEvent e) { } private void handleCanvasZoom(MouseWheelEvent e) { - // System.out.println("Mouse wheel moved " + e.getScrollAmount() + " " + - // e.getWheelRotation()); - if (e.getWheelRotation() == 1) { // zoom out - canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - 10)); + canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - ZOOM_INCREMENT)); } else { // zoom in - canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + 10)); + canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + ZOOM_INCREMENT)); } canvas.repaint(); } private void handleMouseDragged(MouseEvent e) { - // System.out.format("Mouse dragged: %d, %d\n", e.getX(), e.getY()); - if (initialClick == null) { initialClick = e.getPoint(); return; @@ -82,7 +83,6 @@ private void handleMouseDragged(MouseEvent e) { Point currentDrag = e.getPoint(); int deltaX = currentDrag.x - initialClick.x; int deltaY = currentDrag.y - initialClick.y; - // System.out.format("Mouse dragged by: %d, %d\n", deltaX, deltaY); canvas.setYZero(canvas.getYZero() + deltaY); canvas.setXZero(canvas.getXZero() + deltaX); @@ -103,31 +103,25 @@ private void handleCanvasResize() { canvas.positionZoomPanel(); canvas.positionToolbar(); - System.out.println("Canvas size: " + width + " x " + height); - canvas.repaint(); } private void handleMousePressed(MouseEvent e) { initialClick = e.getPoint(); - - System.out.format("Mouse pressed: %d, %d\n", e.getX(), e.getY()); } - // Logic for Home button + private void handleHomeButton() { - canvas.setCellSize(100); // Set the zoom level to 1 + canvas.setCellSize(DEFAULT_CELL_SIZE); canvas.repaint(); } - // Logic for Zoom In button private void handleZoomInButton() { - canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + 10)); + canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + ZOOM_INCREMENT)); canvas.repaint(); } - // Logic for Zoom Out button private void handleZoomOutButton() { - canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - 10)); + canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - ZOOM_INCREMENT)); canvas.repaint(); } } diff --git a/src/main/java/com/github/creme332/controller/Controller.java b/src/main/java/com/github/creme332/controller/Controller.java index 1ab29695..2cb52247 100644 --- a/src/main/java/com/github/creme332/controller/Controller.java +++ b/src/main/java/com/github/creme332/controller/Controller.java @@ -17,33 +17,26 @@ public class Controller { private Toolbar toolbar; private FrameController frameController; - private CanvasController canvasController; - private MenuBarController menuController; - private SideMenuController sideController; - private ToolBarController toolbarController; public Controller() { try { toolbar = new Toolbar(); - toolbarController = new ToolBarController(toolbar); + new ToolBarController(toolbar); canvas = new Canvas(toolbar); - canvasController = new CanvasController(canvas); + new CanvasController(canvas); frame = new Frame(canvas); frameController = new FrameController(app, frame); menuBar = frame.getMyMenuBar(); - menuController = new MenuBarController(app, menuBar); + new MenuBarController(app, menuBar); - sideController = new SideMenuController(app, frame.getSideMenuPanel()); - } catch (InvalidIconSizeException e) { + new SideMenuController(app, frame.getSideMenuPanel()); + } catch (InvalidIconSizeException | InvalidPathException e) { System.err.println("Error: " + e.getMessage()); System.exit(1); - } catch (InvalidPathException e) { - System.err.println("Error: " + e.getMessage()); - System.exit(1); - } catch (Exception e) { + } catch (Exception e) { System.err.println("Unexpected error: " + e.getMessage()); e.printStackTrace(); System.exit(1); diff --git a/src/main/java/com/github/creme332/controller/FrameController.java b/src/main/java/com/github/creme332/controller/FrameController.java index e70c9584..389073e7 100644 --- a/src/main/java/com/github/creme332/controller/FrameController.java +++ b/src/main/java/com/github/creme332/controller/FrameController.java @@ -29,7 +29,6 @@ public void componentResized(ComponentEvent e) { int menuHeight = frame.getMyMenuBar().getHeight(); int sideWidth = Math.min(400, width / 3); - System.out.println("Frame size: " + width + " x " + height); frame.getMainPanel().setPreferredSize(new Dimension(menuWidth, height - menuHeight)); frame.setPreferredSize(new Dimension(sideWidth, height - menuHeight)); } diff --git a/src/main/java/com/github/creme332/controller/MenuBarController.java b/src/main/java/com/github/creme332/controller/MenuBarController.java index 2fb0c730..dc95ac59 100644 --- a/src/main/java/com/github/creme332/controller/MenuBarController.java +++ b/src/main/java/com/github/creme332/controller/MenuBarController.java @@ -20,7 +20,7 @@ public class MenuBarController { private int activeMenuIndex = 0; - final private MatteBorder VISIBLE_BORDER = BorderFactory.createMatteBorder( + private static final MatteBorder VISIBLE_BORDER = BorderFactory.createMatteBorder( 2, 2, 2, 2, new Color(97, 97, 255)); Mode[][] modes = { @@ -60,6 +60,7 @@ public MenuBarController(AppState app, MenuBar menu) { // when user clicks on a menu jMenu.addMouseListener(new MouseAdapter() { + @Override public void mousePressed(MouseEvent e) { // remove border from previously active menu @@ -82,6 +83,7 @@ public void mousePressed(MouseEvent e) { final int menuItemIndex = j; // add mouse listener to menu item jMenuItem.addMouseListener(new MouseAdapter() { + @Override public void mousePressed(MouseEvent e) { // change menu icon to icon of menu item jMenu.setIcon(jMenuItem.getIcon()); @@ -93,6 +95,7 @@ public void mousePressed(MouseEvent e) { } menu.getSideBarButton().addMouseListener(new MouseAdapter() { + @Override public void mousePressed(MouseEvent e) { app.setSideBarVisibility(!app.getSideBarVisibility()); } diff --git a/src/main/java/com/github/creme332/model/Mode.java b/src/main/java/com/github/creme332/model/Mode.java index 7dd25a33..d173d181 100644 --- a/src/main/java/com/github/creme332/model/Mode.java +++ b/src/main/java/com/github/creme332/model/Mode.java @@ -25,9 +25,9 @@ public enum Mode { DELETE("DELETE"), MOVE_GRAPHICS_VIEW("MOVE_GRAPHICS_VIEW"); - private final String Mode; + private final String mode; - Mode(String Mode) { - this.Mode = Mode; + Mode(String mode) { + this.mode = mode; } } diff --git a/src/main/java/com/github/creme332/view/Frame.java b/src/main/java/com/github/creme332/view/Frame.java index 83f194e0..848e3cd5 100644 --- a/src/main/java/com/github/creme332/view/Frame.java +++ b/src/main/java/com/github/creme332/view/Frame.java @@ -2,8 +2,6 @@ import java.awt.BorderLayout; import java.awt.CardLayout; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; import javax.swing.JFrame; import javax.swing.JPanel; @@ -15,9 +13,8 @@ * Frame of the GUI application. */ public class Frame extends JFrame { - // initial frame properties - private final int frameWidth = 1600; // initial width - private final int frameHeight = 1000; // initial height + private static final int INITIAL_FRAME_WIDTH = 1600; + private static final int INITIAL_FRAME_HEIGHT = 1000; /** * Layout used for screenContainer for swapping screens @@ -47,19 +44,19 @@ public Frame(Canvas canvas) throws InvalidPathException { this.setTitle("polydraw"); // set frame size - this.setSize(frameWidth, frameHeight); + this.setSize(INITIAL_FRAME_WIDTH, INITIAL_FRAME_HEIGHT); // make frame resizable this.setResizable(true); // add close button to frame - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); // set application icon this.setIconImage(new IconLoader().loadIcon("/icons/icosahedron.png").getImage()); // center frame on startup if frame is not maximized - if (this.getExtendedState() != JFrame.MAXIMIZED_BOTH) { + if (this.getExtendedState() != MAXIMIZED_BOTH) { this.setLocationRelativeTo(null); } diff --git a/src/main/java/com/github/creme332/view/SideMenuPanel.java b/src/main/java/com/github/creme332/view/SideMenuPanel.java index 7e562cb3..8eae1352 100644 --- a/src/main/java/com/github/creme332/view/SideMenuPanel.java +++ b/src/main/java/com/github/creme332/view/SideMenuPanel.java @@ -1,8 +1,6 @@ package com.github.creme332.view; import java.awt.Color; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JLabel;