/** * In the selectionChanged method not only the selection of this * diagram is set but also the selection in the projectbrowser. * * @param gse The event. */ public void selectionChanged(GraphSelectionEvent gse) { if (!updatingSelection) { updatingSelection = true; List<Fig> selections = gse.getSelections(); ActionCut.getInstance().setEnabled( selections != null && !selections.isEmpty()); // TODO: If ActionCopy is no longer a singleton, how shall // this work? ActionCopy.getInstance() .setEnabled(selections != null && !selections.isEmpty()); /* * ActionPaste.getInstance().setEnabled( Globals.clipBoard * != null && !Globals.clipBoard.isEmpty()); */ // the old selection List currentSelection = TargetManager.getInstance().getTargets(); List removedTargets = new ArrayList(currentSelection); List addedTargets = new ArrayList(); for (Object selection : selections) { Object owner = TargetManager.getInstance().getOwner(selection); if (currentSelection.contains(owner)) { removedTargets.remove(owner); // remains selected } else { // add to selection addedTargets.add(owner); } } if (addedTargets.size() == 1 && removedTargets.size() == currentSelection.size() && removedTargets.size() != 0) { // Optimize for the normal case to minimize target changes TargetManager.getInstance().setTarget(addedTargets.get(0)); } else { for (Object o : removedTargets) { TargetManager.getInstance().removeTarget(o); } for (Object o : addedTargets) { TargetManager.getInstance().addTarget(o); } } updatingSelection = false; } }
/** * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent) * * Change in explorer tree selection -> set target in target * manager. */ public void valueChanged(TreeSelectionEvent e) { if (!updatingSelectionViaTreeSelection) { updatingSelectionViaTreeSelection = true; // get the elements TreePath[] addedOrRemovedPaths = e.getPaths(); TreePath[] selectedPaths = getSelectionPaths(); List elementsAsList = new ArrayList(); for (int i = 0; selectedPaths != null && i < selectedPaths.length; i++) { Object element = ((DefaultMutableTreeNode) selectedPaths[i] .getLastPathComponent()).getUserObject(); elementsAsList.add(element); // scan the visible rows for duplicates of // this elem and select them int rows = getRowCount(); for (int row = 0; row < rows; row++) { Object rowItem = ((DefaultMutableTreeNode) getPathForRow( row).getLastPathComponent()).getUserObject(); if (rowItem == element && !(isRowSelected(row))) { addSelectionRow(row); } } } // check which targetmanager method to call boolean callSetTarget = true; List addedElements = new ArrayList(); for (int i = 0; i < addedOrRemovedPaths.length; i++) { Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i] .getLastPathComponent()).getUserObject(); if (!e.isAddedPath(i)) { callSetTarget = false; break; } addedElements.add(element); } if (callSetTarget && addedElements.size() == elementsAsList.size() && elementsAsList.containsAll(addedElements)) { TargetManager.getInstance().setTargets(elementsAsList); } else { // we must call the correct method on targetmanager // for each added or removed target List removedTargets = new ArrayList(); List addedTargets = new ArrayList(); for (int i = 0; i < addedOrRemovedPaths.length; i++) { Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i] .getLastPathComponent()).getUserObject(); if (e.isAddedPath(i)) { addedTargets.add(element); } else { removedTargets.add(element); } } // we can't remove the targets in one go, we have to // do it one by one. if (!removedTargets.isEmpty()) { Iterator it = removedTargets.iterator(); while (it.hasNext()) { TargetManager.getInstance().removeTarget(it.next()); } } if (!addedTargets.isEmpty()) { Iterator it = addedTargets.iterator(); while (it.hasNext()) { TargetManager.getInstance().addTarget(it.next()); } } } updatingSelectionViaTreeSelection = false; } }
Clone fragments detected by clone detection tool
File path: /ArgoUML-0.34-src/argouml/src/argouml-app/src/org/argouml/uml/diagram/ui/TabDiagram.java File path: /ArgoUML-0.34-src/argouml/src/argouml-app/src/org/argouml/ui/explorer/ExplorerTree.java
Method name: void selectionChanged(GraphSelectionEvent) Method name: void valueChanged(TreeSelectionEvent)
Number of AST nodes: 2 Number of AST nodes: 2
1
/**
1
/**
2
     * In the selectionChanged method not only the selection of this
2
         * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
3
     * diagram is set but also the selection in the projectbrowser.
3
         * 
4
     *
4
         *      Change in explorer tree selection -> set target in target
5
     * @param gse The event.
5
         *      manager.
6
     */
6
         */
7
    public void selectionChanged(GraphSelectionEvent gse) {
7
        public void valueChanged(TreeSelectionEvent e) {
8
        if (!updatingSelection) {
8
9
            updatingSelection = true;
9
            if (!updatingSelectionViaTreeSelection) {
10
            List<Fig> selections = gse.getSelections();
10
                updatingSelectionViaTreeSelection = true;
11
            ActionCut.getInstance().setEnabled(
11
12
                    selections != null && !selections.isEmpty());
12
                // get the elements
13
13
                TreePath[] addedOrRemovedPaths = e.getPaths();
14
            // TODO: If ActionCopy is no longer a singleton, how shall
14
                TreePath[] selectedPaths = getSelectionPaths();
15
            //       this work?
15
                List elementsAsList = new ArrayList();
16
            ActionCopy.getInstance()
16
                for (int i = 0; selectedPaths != null
17
                    .setEnabled(selections != null && !selections.isEmpty());
17
                        && i < selectedPaths.length; i++) {
18
            /*
18
                    Object element = ((DefaultMutableTreeNode) selectedPaths[i]
19
             * ActionPaste.getInstance().setEnabled( Globals.clipBoard
19
                            .getLastPathComponent()).getUserObject();
20
             * != null && !Globals.clipBoard.isEmpty());
20
                    elementsAsList.add(element);
21
             */
21
                    // scan the visible rows for duplicates of
22
            // the old selection
22
                    // this elem and select them
23
            List currentSelection =
23
                    int rows = getRowCount();
24
                TargetManager.getInstance().getTargets();
24
                    for (int row = 0; row < rows; row++) {
25
25
                        Object rowItem = ((DefaultMutableTreeNode) getPathForRow(
26
            List removedTargets = new ArrayList(currentSelection);
26
                                row).getLastPathComponent()).getUserObject();
27
            List addedTargets = new ArrayList();
27
                        if (rowItem == element && !(isRowSelected(row))) {
28
            for (Object selection : selections) {
28
                            addSelectionRow(row);
29
                Object owner = TargetManager.getInstance().getOwner(selection);
29
                        }
30
                if (currentSelection.contains(owner)) {
30
                    }
31
                    removedTargets.remove(owner); // remains selected
31
                }
32
                } else {
32
33
                    // add to selection
33
                // check which targetmanager method to call
34
                    addedTargets.add(owner);
34
                boolean callSetTarget = true;
35
                }
35
                List addedElements = new ArrayList();
36
            }
36
                for (int i = 0; i < addedOrRemovedPaths.length; i++) {
37
            if (addedTargets.size() == 1
37
                    Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i]
38
                    && removedTargets.size() == currentSelection.size()
38
                            .getLastPathComponent()).getUserObject();
39
                    && removedTargets.size() != 0) {
39
                    if (!e.isAddedPath(i)) {
40
                // Optimize for the normal case to minimize target changes
40
                        callSetTarget = false;
41
                TargetManager.getInstance().setTarget(addedTargets.get(0));
41
                        break;
42
            } else {
42
                    }
43
                for (Object o : removedTargets) {
43
                    addedElements.add(element);
44
                    TargetManager.getInstance().removeTarget(o);
44
                }
45
                }
45
46
                for (Object o : addedTargets) {
46
                if (callSetTarget
47
                    TargetManager.getInstance().addTarget(o);
47
                        && addedElements.size() == elementsAsList.size()
48
                }
48
                        && elementsAsList.containsAll(addedElements)) {
49
            }
49
                    TargetManager.getInstance().setTargets(elementsAsList);
50
            updatingSelection = false;
50
                } else {
51
        }
51
                    // we must call the correct method on targetmanager
52
52
                    // for each added or removed target
53
    }
53
                    List removedTargets = new ArrayList();
54
                    List addedTargets = new ArrayList();
55
                    for (int i = 0; i < addedOrRemovedPaths.length; i++) {
56
                        Object element = ((DefaultMutableTreeNode) addedOrRemovedPaths[i]
57
                                .getLastPathComponent()).getUserObject();
58
                        if (e.isAddedPath(i)) {
59
                            addedTargets.add(element);
60
                        } else {
61
                            removedTargets.add(element);
62
                        }
63
                    }
64
                    // we can't remove the targets in one go, we have to
65
                    // do it one by one.
66
                    if (!removedTargets.isEmpty()) {
67
                        Iterator it = removedTargets.iterator();
68
                        while (it.hasNext()) {
69
                            TargetManager.getInstance().removeTarget(it.next());
70
                        }
71
                    }
72
                    if (!addedTargets.isEmpty()) {
73
                        Iterator it = addedTargets.iterator();
74
                        while (it.hasNext()) {
75
                            TargetManager.getInstance().addTarget(it.next());
76
                        }
77
                    }
78
                }
79
80
                updatingSelectionViaTreeSelection = false;
81
            }
82
        }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements3
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment0
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.750
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    16
    for (Object o : removedTargets)
    33
    while (it.hasNext())
    17
    TargetManager.getInstance().removeTarget(o);
    17
    TargetManager.getInstance().removeTarget(o);
    34
    TargetManager.getInstance().removeTarget(it.next());
    Differences
    Expression1Expression2Difference
    oit.next()TYPE_COMPATIBLE_REPLACEMENT
    Preondition Violations
    Expression o cannot be parameterized, because it has dependencies to/from statements that will be extracted
    34
    TargetManager.getInstance().removeTarget(it.next());
    Precondition Violations (1)
    Row Violation
    1Expression o cannot be parameterized, because it has dependencies to/from statements that will be extracted