// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.debug.ui;

import org.chromium.debug.core.model.Value;
import org.chromium.debug.ui.editors.JsEditor;
import org.eclipse.core.resources.IFile;
import org.eclipse.debug.core.model.ILineBreakpoint;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.debug.ui.IValueDetailListener;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.part.FileEditorInput;

/**
 * An IDebugModelPresentation for the Chromium JavaScript debug model.
 */
public class JsDebugModelPresentation extends LabelProvider implements IDebugModelPresentation {

  public void setAttribute(String attribute, Object value) {
  }

  @Override
  public Image getImage(Object element) {
    // use default image
    return null;
  }

  @Override
  public String getText(Object element) {
    // use default label text
    return null;
  }

  public void computeDetail(IValue value, IValueDetailListener listener) {
    String detail = ""; //$NON-NLS-1$
    if (value instanceof Value) {
      // Avoid quoting string JavaScript values by getting the value string
      // from the underlying JsValue.
      detail = ((Value) value).getJsValue().getValueString();
    }

    listener.detailComputed(value, detail);
  }

  public IEditorInput getEditorInput(Object element) {
    return toEditorInput(element);
  }

  public static IEditorInput toEditorInput(Object element) {
    if (element instanceof IFile) {
      return new FileEditorInput((IFile) element);
    }

    if (element instanceof ILineBreakpoint) {
      return new FileEditorInput(
          (IFile) ((ILineBreakpoint) element).getMarker().getResource());
    }

    return null;
  }

  public String getEditorId(IEditorInput input, Object element) {
    if (element instanceof IFile || element instanceof ILineBreakpoint) {
      return JsEditor.EDITOR_ID;
    }

    return null;
  }
}

@@ -327,16 +327,110 @@
 .IFile;%0A
+import org.eclipse.core.resources.IResource;%0Aimport org.eclipse.debug.core.model.IBreakpoint;%0A
 import o
@@ -744,16 +744,88 @@
 rInput;%0A
+import org.eclipse.ui.PartInitException;%0Aimport org.eclipse.ui.ide.IDE;%0A
 import o
@@ -2181,32 +2181,48 @@
 ject element) %7B%0A
+    IFile file;%0A
     if (element 
@@ -2241,106 +2241,770 @@
 File
- %7C%7C element instanceof ILineBreakpoint) %7B%0A      return JsEditor.EDITOR_ID;%0A    %7D%0A%0A    return null;
+) %7B%0A      file = (IFile) element;%0A    %7D else if (element instanceof IBreakpoint) %7B%0A        IBreakpoint breakpoint = (IBreakpoint) element;%0A        IResource resource = breakpoint.getMarker().getResource();%0A        // Can the breakpoint resource be folder or project? Better check for it.%0A      if (resource instanceof IFile == false) %7B%0A        return null;%0A      %7D%0A      file = (IFile) resource;%0A    %7D else %7B%0A      return null;%0A    %7D%0A%0A    // Pick the editor based on the file extension, taking user preferences into account.%0A    try %7B%0A      return IDE.getEditorDescriptor(file).getId();%0A    %7D catch (PartInitException e) %7B%0A      // TODO(peter.rybin): should it really be the default case? There might be no virtual project.%0A      return JsEditor.EDITOR_ID;%0A    %7D
 %0A  %7D

Make editor not hardcoded