Results 1 to 5 of 5
  1. #1
    deepa8400 is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default complexity issue!!

    this method has an overtly high cyclomatic complexity
    could you suggest an alternative way of writng dis method?

    Java Code:
    public void actionPerformed(ActionEvent actionEvent) {
    
    
            if ("Refresh".equals(actionEvent.getActionCommand())) {
    
    
            }
    
            if ("ZoomIn".equals(actionEvent.getActionCommand())) {
                //new oom(1.8f);
                double zoomInFactor = 1.2f;
                zoom(zoomInFactor);
            }
            if ("ZoomOut".equals(actionEvent.getActionCommand())) {
                double zoomOutFactor = 0.8f;
                zoom(zoomOutFactor);
            }
            if ("ZoomFit".equals(actionEvent.getActionCommand())) {
                view.fitContent(true);
                view.updateView();
    
            }
            if ("Zoom100".equals(actionEvent.getActionCommand())) {
                // view.focusView(0.1f, new Point(0, 0), true);
                double zoomFactor = 1.0f;
                view.setZoom(zoomFactor);
                view.updateView();
            }
    
            if ("Print".equals(actionEvent.getActionCommand())) {
                //view.print();
                printGraph();
            }
            if ("Save".equals(actionEvent.getActionCommand())) {
                saveGraph();
    
            }
            if ("Expand".equals(actionEvent.getActionCommand())) {
    
                isExpanded = true;
                isCollapsed = false;
                alteringGraphSize();
    
            }
            if ("Collapse".equals(actionEvent.getActionCommand())) {
                isCollapsed = true;
                isExpanded = false;
                alteringGraphSize();
    
            }
    
            if ("FullScreen".equals(actionEvent.getActionCommand())) {
    
    
                graphOnFullScreen();
            }
            if ("Restore".equals(actionEvent.getActionCommand())) {
                restoreScreen();
            }
            if ("Magnify".equals(actionEvent.getActionCommand())) {
                magnifyGraph();
    
            }
    
            if ("Find".equals(actionEvent.getActionCommand())) {
    
                if (searchToolBar.isVisible()) {
                    searchToolBar.setVisible(false);
                } else {
                    searchToolBar.setVisible(true);
                }
            }
    
            if ("Advanced Layout".equals(actionEvent.getActionCommand())) {
    
                if (popupAdvancedLayout == null) {
                    popupAdvancedLayout = new JPopupMenu();
                    YPackage yPackage = new YPackage(ResourceResolver.getFileResource("layout-modules.xml"));
                    System.out.println("yPackage = " + yPackage.getName());
                    createAdvancedLayoutMenu(yPackage, null, popupAdvancedLayout);
    
                    // popAdvancedLayout.add(createAdvancedLayoutMenu(yPackage, null, popAdvancedLayout));
                }
    
                popupAdvancedLayout.show(view, btnAdvancedLayout.getLocation().x, btnAdvancedLayout.getLocation().y);
            }
    
    
        }
    Last edited by Fubarable; 08-22-2009 at 04:24 PM. Reason: code tags added for readability

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Just looking at your code, seems to me you can use switch-case to in this case. No need to lookup actionEvent.getActionCommand() multiple times. At the same time, I didn't see any common task doing in each event.

    So the best thing I can recommended for you is take a piece of paper and re-design your logic here. Try to keep it as much as simple and common goals in the same level.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This is one reason many recommend that you don't add the same ActionListener to multiple buttons -- to avoid a "switch-board" type actionPerformed method. Better perhaps is to create different ActionListener objects each tailored to its own unique group of actions. I've also seen the Mediator design pattern used here.
    Last edited by Fubarable; 08-22-2009 at 04:23 PM.

  4. #4
    deepa8400 is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default

    ok....wud try.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by deepa8400 View Post
    ok....wud try.
    Ya give a try and if you can show it here. We can comment on that.

    At the same time, please not that Fubarable edit your code, actually formated, in very first post. Formatted codes are easy to read actually. So please try to use code tags next time.

Similar Threads

  1. Time complexity - foor loop
    By hawaiifiver in forum New To Java
    Replies: 5
    Last Post: 02-05-2011, 05:06 PM
  2. PDF Box issue
    By jazz2k8 in forum Advanced Java
    Replies: 0
    Last Post: 03-20-2009, 12:04 PM
  3. Replies: 2
    Last Post: 11-04-2008, 03:48 AM
  4. Singleton considered stupid, Java and complexity
    By fishtoprecords in forum Forum Lobby
    Replies: 11
    Last Post: 07-06-2008, 04:38 AM
  5. Issue
    By chaitu444 in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •