/*
 * Copyright 2012-2013 JetBrains s.r.o
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.jetpad.model.event;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompositeRegistration implements Registration {
  private List<Registration> myRegistrations;
  private boolean myRemoved;
  private boolean myRemoving;

  public CompositeRegistration(Registration... regs) {
    myRegistrations = new ArrayList<Registration>(regs.length);
    myRegistrations.addAll(Arrays.asList(regs));
  }

  public CompositeRegistration add(Registration r) {
    myRegistrations.add(r);
    return this;
  }

  @Override
  public void remove() {
    if (myRemoved || myRemoving) throw new IllegalStateException();

    myRemoving = true;
    try {
      for (Registration r : myRegistrations) {
        r.remove();
      }
    } finally {
      myRemoving = false;
    }
    myRemoved = true;
  }
}

@@ -1303,32 +1303,122 @@
     
-for (Registration r : my
+Callbacks.call(myRegistrations, new Callbacks.Caller%3CRegistration%3E() %7B%0A        @Override%0A        public void call(
 Regi
@@ -1425,17 +1425,18 @@
 stration
-s
+ r
 ) %7B%0A    
@@ -1439,16 +1439,18 @@
         
+  
 r.remove
@@ -1455,25 +1455,37 @@
 ve();%0A      
-%7D
+  %7D%0A      %7D);
 %0A    %7D final

improve code robustness