package com.karateca.jstoolbox.tobrowser;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.psi.PsiFile;
import com.karateca.jstoolbox.MyAction;
import com.karateca.jstoolbox.config.JsToolboxSettings;

/**
 * @author Andres Dominguez
 */
public class ToBrowserAction extends MyAction {
  @Override
  public void actionPerformed(AnActionEvent actionEvent) {
    if (!canEnableAction(actionEvent)) {
      return;
    }

    JsToolboxSettings settings = new JsToolboxSettings();

    PsiFile file = actionEvent.getData(LangDataKeys.PSI_FILE);
    String fileName = getCurrentFileName(actionEvent);

    if (settings.getUseFilePath()) {
      // Get the directory.
      String directory = file.getContainingDirectory().toString();
      String fromPath = settings.getFromPath();
      fileName = directory.replaceFirst("^.*" + fromPath, fromPath) +
          System.getProperty("file.separator") + fileName;
    }

    String searchUrl = settings.getSearchUrl();

    String url = searchUrl.replace(JsToolboxSettings.FILE_NAME_TOKEN, fileName);
    openBrowser(url);
  }

  private void openBrowser(String url) {
    try {
      BrowserUtil.browse(url);
    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
    }
  }
}

@@ -184,16 +184,54 @@
 taKeys;%0A
+import com.intellij.psi.PsiDirectory;%0A
 import c
@@ -573,67 +573,8 @@
  %7D%0A%0A
-    JsToolboxSettings settings = new JsToolboxSettings();%0A%0A
     
@@ -632,16 +632,120 @@
 _FILE);%0A
+    if (file == null) %7B%0A      return;%0A    %7D%0A%0A    JsToolboxSettings settings = new JsToolboxSettings();%0A%0A
     Stri
@@ -867,16 +867,32 @@
     
-String d
+PsiDirectory containingD
 irec
@@ -931,20 +931,75 @@
 ry()
-.toString();
+;%0A      if (containingDirectory == null) %7B%0A        return;%0A      %7D%0A
 %0A   
@@ -1060,25 +1060,46 @@
 eName = 
-directory
+containingDirectory.toString()
 .replace

remove some warnings