/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.uiDesigner.make;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;
import com.intellij.uiDesigner.editor.UIFormEditor;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author yole
 */
public class FormElementNavigatable implements Navigatable {
  private final Project myProject;
  private VirtualFile myVirtualFile;
  private @Nullable String myComponentId;

  public FormElementNavigatable(final Project project, final VirtualFile virtualFile, @Nullable final String componentId) {
    myProject = project;
    myVirtualFile = virtualFile;
    myComponentId = componentId;
  }

  public void navigate(boolean requestFocus) {
    OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, myVirtualFile);
    final List<FileEditor> fileEditors = FileEditorManager.getInstance(myProject).openEditor(descriptor, requestFocus);
    if (myComponentId != null) {
      for(FileEditor editor: fileEditors) {
        if (editor instanceof UIFormEditor) {
          ((UIFormEditor) editor).selectComponentById(myComponentId);
          break;
        }
      }
    }
  }

  public boolean canNavigate() {
    return true;
  }

  public boolean canNavigateToSource() {
    return true;
  }
}

@@ -983,24 +983,66 @@
 estFocus) %7B%0A
+    if (!myVirtualFile.isValid()) return;%0A
     OpenFile
@@ -1514,28 +1514,47 @@
 %0A    return 
-true
+myVirtualFile.isValid()
 ;%0A  %7D%0A%0A  pub
@@ -1604,12 +1604,31 @@
 urn 
-true
+myVirtualFile.isValid()
 ;%0A  

check for valid VirtualFile (IDEADEV-11081)