Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c389d7b43fe9d32e562de69f1e6db1627c2d831d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.editor.pom;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.jface.text.IUndoManager;

/**
 * @author Anton Kraev
 */
public class NotificationCommandStack extends BasicCommandStack {

  private MavenPomEditor editor;
  private List<MavenPomEditorPage> pages;
  private boolean isDirty = false;

  public NotificationCommandStack(MavenPomEditor editor) {
    this.editor = editor;
    this.pages = editor.getPages();
  }
  
  @Override
  public void execute(Command command) {
    processCommand(command, false);
    
    IUndoManager undoManager = editor.getSourcePage().getTextViewer().getUndoManager();
    undoManager.beginCompoundChange();
    try {
      super.execute(command);
    } finally {
      undoManager.endCompoundChange();
    }
    
    processCommand(command, true);
    fireDirty();
  }

  private void processCommand(Command command, boolean add) {
    if (command instanceof CompoundCommand) {
      CompoundCommand compoundCommand = (CompoundCommand) command;
      Iterator<Command> commands = compoundCommand.getCommandList().iterator();
      while (commands.hasNext()) {
        processCommand(commands.next(), add);
      }
    }
    
    if (command instanceof AddCommand) {
      AddCommand addCommand = (AddCommand) command;
      Iterator<?> it = addCommand.getCollection().iterator();
      while (it.hasNext()) {
        processListeners(it.next(), add);
      }
    }

    if (command instanceof SetCommand) {
      SetCommand setCommand = (SetCommand) command;
      processListeners(setCommand.getValue(), add);
    }

    if (command instanceof RemoveCommand) {
      RemoveCommand removeCommand = (RemoveCommand) command;
      Collection<?> collection = removeCommand.getCollection();
      if(collection!=null) {
        Iterator<?> it = collection.iterator();
        while (it.hasNext()) {
          Object next = it.next();
          if (next instanceof EObject) {
            EObject object = (EObject) next;
            for (int i=0; i<pages.size(); i++) {
              object.eAdapters().remove(pages.get(i));
            }
          }
        }
      }
    }
  }

  private void processListeners(Object next, boolean add) {
    if (next instanceof EObject) {
      EObject object = (EObject) next;
      for (int i=0; i<pages.size(); i++) {
        if (add) {
          if (!object.eAdapters().contains(pages.get(i)))
            object.eAdapters().add(pages.get(i));
        } else {
          object.eAdapters().remove(pages.get(i));
        }
      }
    }
  }
  
  @Override
  public void redo() {
    super.redo();
    fireDirty();
  }

  private void fireDirty() {
    if (isDirty != isSaveNeeded()) {
      editor.editorDirtyStateChanged();
    }
    isDirty = isSaveNeeded();
  }

  @Override
  public void undo() {
    super.undo();
    fireDirty();
  }

}

Back to the top