0 Replies - 1691 Views - Last Post: 23 June 2016 - 09:09 AM Rate Topic: -----

#1 jeffindenver  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 117
  • Joined: 07-August 15

Refactoring my nightmare

Posted 23 June 2016 - 09:09 AM

I'm nearly done refactoring an application that I use at work everyday for the last five or six months. It was a nightmare for sure filled with hardcoding and inflexibility.

A glimpse of The Nightmare
    private class BackgroundPopupMenu extends JPopupMenu {

        String label = "Choose a background";
        public BackgroundPopupMenu() {
            super.setLabel(label);
            JMenuItem mBlueOne = new JMenuItem("BlueOne");
            JMenuItem mBlueTwo = new JMenuItem("BlueTwo");
            JMenuItem mPurpleOne = new JMenuItem("PurpleOne");
            JMenuItem mPurpleTwo = new JMenuItem("PurpleTwo");
            JMenuItem mBeigeOne = new JMenuItem("BeigeOne");
            JMenuItem mBeigeTwo = new JMenuItem("BeigeTwo");
            JMenuItem mBeigeThree = new JMenuItem("BeigeThree");
            
            add(mBlueOne);
            add(mBlueTwo);
            add(mPurpleOne);
            add(mPurpleTwo);
            add(mBeigeOne);
            add(mBeigeTwo);
            add(mBeigeThree);
            mBlueOne.addActionListener(new RightMenuListener());
            mBlueTwo.addActionListener(new RightMenuListener());
            mPurpleOne.addActionListener(new RightMenuListener());
            mPurpleTwo.addActionListener(new RightMenuListener());
            mBeigeOne.addActionListener(new RightMenuListener());
            mBeigeTwo.addActionListener(new RightMenuListener());
            mBeigeThree.addActionListener(new RightMenuListener());
        }

        private class RightMenuListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent ae) {
              //change background here
                if (ae.getActionCommand().equals("BlueOne")) {
                    bg.setBg("blueOne");
                    bg.repaint();
                }
                if (ae.getActionCommand().equals("BlueTwo")) {
                    bg.setBg("blueTwo");
                    bg.repaint();
                }
                if (ae.getActionCommand().equals("PurpleOne")) {
                    bg.setBg("purpleOne");
                    bg.repaint();
                }
                if (ae.getActionCommand().equals("PurpleTwo")) {
                    bg.setBg("purpleTwo");
                    bg.repaint();
                }
                if (ae.getActionCommand().equals("BeigeOne")) {
                    bg.setBg("beigeOne");
                    bg.repaint();
                }
                if (ae.getActionCommand().equals("BeigeTwo")) {
                    bg.setBg("beigeTwo");
                    bg.repaint();
                }
                if (ae.getActionCommand().equals("BeigeThree")) {
                    bg.setBg("beigeThree");
                    bg.repaint();
                }
            }
        }
    }



The refactoring
Spoiler


More Nightmare
	    public void setBg(String str) {
        switch (str) {
            case "beigeOne":
                this.bg = new ImageIcon("backgrounds/beigeOne.png").getImage();
                break;
            case "beigeTwo":
                this.bg = new ImageIcon("backgrounds/beigeTwo.jpg").getImage();
                break;
            case "beigeThree":
                this.bg = new ImageIcon("backgrounds/beigeThree.jpg").getImage();
                break;
            case "purpleOne":
                this.bg = new ImageIcon("backgrounds/purpleOne.jpg").getImage();
                break;
            case "purpleTwo":
                this.bg = new ImageIcon("backgrounds/purpleTwo.png").getImage();
                break;
            case "blueOne":
                this.bg = new ImageIcon("backgrounds/blueOne.jpg").getImage();
                break;
            case "blueTwo":
                this.bg = new ImageIcon("backgrounds/blueTwo.jpg").getImage();
                break;
            default:
                this.bg = new ImageIcon("backgrounds/blueOne.jpg").getImage();
        }
    }



Another refactoring
Spoiler


Is This A Good Question/Topic? 0
  • +

Page 1 of 1