package sourcelookup;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainerTypeDelegate;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.launching.LaunchingMessages;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * // TODO (michael) document me
 * 
 * @author Björn Michael
 * @since 1.0
 */
public class MyMvnSourceContainerTypeDelegate extends AbstractSourceContainerTypeDelegate {

  static final String TYPE_ID = "sourcelookup.myMvnSourceContainerType";

  private static final String MVNCONTAINER = "mvncontainer";
  private static final String PROJECT_NAME = "name";

  @Override
  public ISourceContainer createSourceContainer(final String memento) throws CoreException {
    final Node node = parseDocument(memento);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
      final Element element = (Element) node;
      if (MVNCONTAINER.equals(element.getNodeName())) {
        final String string = element.getAttribute(PROJECT_NAME);
        if (string == null || string.length() == 0) {
          abort(LaunchingMessages.JavaProjectSourceContainerTypeDelegate_5, null);
        }

        final IWorkspace workspace = ResourcesPlugin.getWorkspace();
        final IProject project = workspace.getRoot().getProject(string);
        final IJavaProject javaProject = JavaCore.create(project);

        return new MyMvnSourceContainer(javaProject);
      }
      abort(LaunchingMessages.JavaProjectSourceContainerTypeDelegate_6, null);
    }
    abort(LaunchingMessages.JavaProjectSourceContainerTypeDelegate_7, null);

    return null;
  }

  @Override
  public String getMemento(final ISourceContainer container) throws CoreException {
    final MyMvnSourceContainer sourceContainer = (MyMvnSourceContainer) container;
    final Document document = newDocument();
    final Element element = document.createElement(MVNCONTAINER);
    element.setAttribute(PROJECT_NAME, sourceContainer.getProjectName());
    document.appendChild(element);

    return serializeDocument(document);
  }

}

@@ -440,69 +440,8 @@
 re;%0A
-import org.eclipse.jdt.internal.launching.LaunchingMessages;%0A
 impo
@@ -523,42 +523,151 @@
 e;%0A%0A
-/**%0A * // TODO (michael) document 
+import sourcelookup.internal.SourceLookupMessages;%0A%0A/**%0A * // TODO (michael) document me%0A * %0A * TODO(michael) chose a more appropriate class na
 me%0A 
@@ -940,16 +940,17 @@
 ainer%22;%0A
+%0A
   privat
@@ -987,17 +987,24 @@
 NAME = %22
-n
+projectN
 ame%22;%0A%0A 
@@ -1153,16 +1153,17 @@
 mento);%0A
+%0A
     if (
@@ -1251,16 +1251,17 @@
 ) node;%0A
+%0A
       if
@@ -1448,37 +1448,34 @@
 ort(
-LaunchingMessages.JavaProject
+SourceLookupMessages.MyMvn
 Sour
@@ -1502,9 +1502,21 @@
 ate_
-5
+NameIsMissing
 , nu
@@ -1799,24 +1799,25 @@
 t);%0A      %7D%0A
+%0A
       abort(
@@ -1820,37 +1820,34 @@
 ort(
-LaunchingMessages.JavaProject
+SourceLookupMessages.MyMvn
 Sour
@@ -1874,9 +1874,28 @@
 ate_
-6
+ProjectNameIsMissing
 , nu
@@ -1905,16 +1905,17 @@
 ;%0A    %7D%0A
+%0A
     abor
@@ -1920,37 +1920,34 @@
 ort(
-LaunchingMessages.JavaProject
+SourceLookupMessages.MyMvn
 Sour
@@ -1974,9 +1974,21 @@
 ate_
-7
+InvalidFormat
 , nu

Use own messages