/*
 * Copyright 2000-2009 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 com.intellij.openapi.actionSystem.impl;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.util.containers.HashMap;

import java.awt.*;
import java.lang.ref.WeakReference;
import java.util.Map;

public class SimpleDataContext implements DataContext {
  private final Map<String, Object> myDataId2Data;
  private final DataContext myParent;

  private WeakReference<Component> myContextComponent;
  
  private SimpleDataContext(String dataId, Object data, DataContext parent) {
    myDataId2Data = new HashMap<String, Object>(1);
    myDataId2Data.put(dataId, data);
    myParent = parent;
    myContextComponent = new WeakReference<Component>(IdeFocusManager.getGlobalInstance().getFocusOwner());
  }
  
  private SimpleDataContext(Map<String, Object> dataid2data, DataContext parent) {
    myDataId2Data = dataid2data;
    myParent = parent;
  }

  public Object getData(String dataId) {
    Object result =  myDataId2Data.containsKey(dataId) ? myDataId2Data.get(dataId) : 
           myParent == null ? null : myParent.getData(dataId);
    
    if (result == null && PlatformDataKeys.CONTEXT_COMPONENT.getName().equals(dataId)) {
      result = myContextComponent.get();  
    }
    
    return result;
  }

  public static DataContext getSimpleContext(String dataId, Object data, DataContext parent) {
    return new SimpleDataContext(dataId, data, parent);
  }
  
  public static DataContext getSimpleContext(Map<String,Object> dataId2data, DataContext parent) {
    return new SimpleDataContext(dataId2data, parent);
  }

  public static DataContext getSimpleContext(String dataId, Object data) {
    return getSimpleContext(dataId, data, null);
  }

  public static DataContext getProjectContext(Project project) {
    return getSimpleContext(PlatformDataKeys.PROJECT.getName(), project);
  }
}

@@ -1123,66 +1123,8 @@
 t;%0A%0A
-  private WeakReference%3CComponent%3E myContextComponent;%0A  %0A
   pr
@@ -1313,116 +1313,8 @@
 nt;%0A
-    myContextComponent = new WeakReference%3CComponent%3E(IdeFocusManager.getGlobalInstance().getFocusOwner());%0A
   %7D%0A
@@ -1763,46 +1763,67 @@
 t = 
-myContextComponent.get
+IdeFocusManager.getGlobalInstance().getFocusOwner
 ();
-  
 %0A    %7D%0A
-    
 %0A   

fix for EDT assertion in SimpleDataContext