org.eclipse.mylyn.tasks.ui_3.7.1.v20120425-0100

getExtensionRegistry();
      
      initConnectorCores(taskListExternalizer, repositoryManager, registry);
      
      IExtensionPoint templatesExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.core.templates");
      IExtension[] templateExtensions = templatesExtensionPoint.getExtensions();
      IExtension[] arrayOfIExtension1;
      int j = (arrayOfIExtension1 = templateExtensions).length;
      for (int i = 0; i < j; i++)
      {
        IExtension templateExtension = arrayOfIExtension1[i];
        elements = templateExtension.getConfigurationElements();
        IConfigurationElement[] arrayOfIConfigurationElement2;
        int m = (arrayOfIConfigurationElement2 = elements).length;
        for (int k = 0; k < m; k++)
        {
          element = arrayOfIConfigurationElement2[k];
          if ((!isDisabled(element)) && 
            (element.getName().equals("repository"))) {
            readRepositoryTemplate(element);
          }
        }
      }
      IExtensionPoint presentationsExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.presentations");
      IExtension[] presentations = presentationsExtensionPoint.getExtensions();
      IConfigurationElement[] elements = (element = presentations).length;
      IConfigurationElement[] elements;
      Object element;
      for (IConfigurationElement[] arrayOfIConfigurationElement1 = 0; arrayOfIConfigurationElement1 < elements; arrayOfIConfigurationElement1++)
      {
        IExtension presentation = element[arrayOfIConfigurationElement1];
        elements = presentation.getConfigurationElements();
        IConfigurationElement[] arrayOfIConfigurationElement3;
        int i1 = (arrayOfIConfigurationElement3 = elements).length;
        for (int n = 0; n < i1; n++)
        {
          element = arrayOfIConfigurationElement3[n];
          if (!isDisabled((IConfigurationElement)element)) {
            readPresentation((IConfigurationElement)element);
          }
        }
      }
      IExtensionPoint editorsExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.editors");
      IExtension[] editors = editorsExtensionPoint.getExtensions();
      IConfigurationElement localIConfigurationElement1 = (element = editors).length;
      for (IConfigurationElement element = 0; element < localIConfigurationElement1; element++)
      {
        IExtension editor = element[element];
        IConfigurationElement[] elements = editor.getConfigurationElements();
        IConfigurationElement[] arrayOfIConfigurationElement4;
        int i3 = (arrayOfIConfigurationElement4 = elements).length;
        for (int i2 = 0; i2 < i3; i2++)
        {
          IConfigurationElement element = arrayOfIConfigurationElement4[i2];
          if ((!isDisabled(element)) && 
            (element.getName().equals("pageFactory"))) {
            readTaskEditorPageFactory(element);
          }
        }
      }
      coreExtensionsRead = true;
    }
  }
  
  private static void initConnectorCores(TaskListExternalizer taskListExternalizer, TaskRepositoryManager repositoryManager, IExtensionRegistry registry)
  {
    List<ConnectorDescriptor> descriptors = new ArrayList();
    MultiStatus result = new MultiStatus("org.eclipse.mylyn.tasks.ui", 0, "Repository connectors failed to load.", null);
    
    Map<String, List<ConnectorDescriptor>> descriptorById = new LinkedHashMap();
    IExtensionPoint repositoriesExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.repositories");
    IExtension[] repositoryExtensions = repositoriesExtensionPoint.getExtensions();
    IExtension[] arrayOfIExtension1;
    int j = (arrayOfIExtension1 = repositoryExtensions).length;
    IConfigurationElement[] elements;
    for (int i = 0; i < j; i++)
    {
      IExtension repositoryExtension = arrayOfIExtension1[i];
      elements = repositoryExtension.getConfigurationElements();
      ConnectorDescriptor descriptor = null;
      IConfigurationElement tasklistMigratorElement = null;
      IConfigurationElement repositoryMigratorElement = null;
      IConfigurationElement[] arrayOfIConfigurationElement1;
      int m = (arrayOfIConfigurationElement1 = elements).length;
      for (int k = 0; k < m; k++)
      {
        IConfigurationElement element = arrayOfIConfigurationElement1[k];
        if (element.getName().equals("connectorCore")) {
          descriptor = new ConnectorDescriptor(element);
        } else if (element.getName().equals("taskListMigrator")) {
          tasklistMigratorElement = element;
        } else if (element.getName().equals("repositoryMigrator")) {
          repositoryMigratorElement = element;
        }
      }
      if (descriptor != null)
      {
        taskListMigratorElement = tasklistMigratorElement;
        repositoryMigratorElement = repositoryMigratorElement;
        descriptors.add(descriptor);
        if (descriptor.getId() != null) {
          add(descriptorById, descriptor.getId(), descriptor);
        }
      }
    }
    checkForConflicts(descriptors, result, descriptorById);
    
    Map<String, List<ConnectorDescriptor>> descriptorByConnectorKind = new LinkedHashMap();
    for (ConnectorDescriptor descriptor : descriptors)
    {
      IStatus status = descriptor.createConnector();
      if ((status.isOK()) && (repositoryConnector != null)) {
        add(descriptorByConnectorKind, descriptor.getConnectorKind(), descriptor);
      } else {
        result.add(status);
      }
    }
    checkForConflicts(descriptors, result, descriptorByConnectorKind);
    
    Object taskListmigrators = new ArrayList();
    Object repositoryMigrators = new ArrayList();
    for (ConnectorDescriptor descriptor : descriptors) {
      if (repositoryConnector != null)
      {
        repositoryManager.addRepositoryConnector(repositoryConnector);
        if (taskListMigratorElement != null)
        {
          IStatus status = descriptor.createTaskListMigrator();
          if ((status.isOK()) && (taskListMigrator != null)) {
            ((List)taskListmigrators).add(taskListMigrator);
          } else {
            result.add(status);
          }
        }
        if (repositoryMigratorElement != null)
        {
          IStatus status = descriptor.createRepositoryMigrator();
          if ((status.isOK()) && (repositoryMigrator != null)) {
            ((List)repositoryMigrators).add(repositoryMigrator);
          } else {
            result.add(status);
          }
        }
      }
    }
    if (!result.isOK()) {
      StatusHandler.log(result);
    }
    repositoryManager.initialize((List)repositoryMigrators);
    taskListExternalizer.initialize((List)taskListmigrators);
  }
  
  private static boolean isDisabled(IConfigurationElement element)
  {
    return disabledContributors.contains(element.getContributor().getName());
  }
  
  private static void checkForConflicts(List<ConnectorDescriptor> descriptors, MultiStatus result, Map<String, List<ConnectorDescriptor>> descriptorById)
  {
    for (Map.Entry<String, List<ConnectorDescriptor>> entry : descriptorById.entrySet()) {
      if (((List)entry.getValue()).size() > 1)
      {
        MultiStatus status = new MultiStatus("org.eclipse.mylyn.tasks.ui", 0, NLS.bind(
          "Connector ''{0}'' registered by multiple extensions.", entry.getKey()), null);
        for (ConnectorDescriptor descriptor : (List)entry.getValue())
        {
          status.add(new Status(
            4, 
            "org.eclipse.mylyn.tasks.ui", 
            NLS.bind(
            "All extensions contributed by ''{0}'' have been disabled.", descriptor.getPluginId()), null));
          disabledContributors.add(descriptor.getPluginId());
          descriptors.remove(descriptor);
        }
        result.add(status);
      }
    }
  }
  
  private static void add(Map<String, List<ConnectorDescriptor>> descriptorById, String id, ConnectorDescriptor descriptor)
  {
    List<ConnectorDescriptor> list = (List)descriptorById.get(id);
    if (list == null)
    {
      list = new ArrayList();
      descriptorById.put(id, list);
    }
    list.add(descriptor);
  }
  
  public static void initWorkbenchUiExtensions()
  {
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    
    IExtensionPoint repositoriesExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.repositories");
    IExtension[] repositoryExtensions = repositoriesExtensionPoint.getExtensions();
    IExtension[] arrayOfIExtension1;
    int j = (arrayOfIExtension1 = repositoryExtensions).length;
    for (int i = 0; i < j; i++)
    {
      IExtension repositoryExtension = arrayOfIExtension1[i];
      elements = repositoryExtension.getConfigurationElements();
      IConfigurationElement[] arrayOfIConfigurationElement2;
      int m = (arrayOfIConfigurationElement2 = elements).length;
      for (int k = 0; k < m; k++)
      {
        element = arrayOfIConfigurationElement2[k];
        if ((!isDisabled(element)) && 
          (element.getName().equals("connectorUi"))) {
          readRepositoryConnectorUi(element);
        }
      }
    }
    IExtensionPoint linkProvidersExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.projectLinkProviders");
    IExtension[] linkProvidersExtensions = linkProvidersExtensionPoint.getExtensions();
    IConfigurationElement[] elements = (element = linkProvidersExtensions).length;
    IConfigurationElement[] elements;
    Object element;
    for (IConfigurationElement[] arrayOfIConfigurationElement1 = 0; arrayOfIConfigurationElement1 < elements; arrayOfIConfigurationElement1++)
    {
      IExtension linkProvidersExtension = element[arrayOfIConfigurationElement1];
      elements = linkProvidersExtension.getConfigurationElements();
      IConfigurationElement[] arrayOfIConfigurationElement3;
      int i1 = (arrayOfIConfigurationElement3 = elements).length;
      for (int n = 0; n < i1; n++)
      {
        element = arrayOfIConfigurationElement3[n];
        if ((!isDisabled((IConfigurationElement)element)) && 
          (((IConfigurationElement)element).getName().equals("linkProvider"))) {
          readLinkProvider((IConfigurationElement)element);
        }
      }
    }
    IExtensionPoint duplicateDetectorsExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.duplicateDetectors");
    IExtension[] dulicateDetectorsExtensions = duplicateDetectorsExtensionPoint.getExtensions();
    IConfigurationElement localIConfigurationElement1 = (element = dulicateDetectorsExtensions).length;
    IConfigurationElement[] elements;
    Object element;
    for (IConfigurationElement element = 0; element < localIConfigurationElement1; element++)
    {
      IExtension dulicateDetectorsExtension = element[element];
      elements = dulicateDetectorsExtension.getConfigurationElements();
      IConfigurationElement[] arrayOfIConfigurationElement4;
      int i3 = (arrayOfIConfigurationElement4 = elements).length;
      for (int i2 = 0; i2 < i3; i2++)
      {
        element = arrayOfIConfigurationElement4[i2];
        if ((!isDisabled((IConfigurationElement)element)) && 
          (((IConfigurationElement)element).getName().equals("detector"))) {
          readDuplicateDetector((IConfigurationElement)element);
        }
      }
    }
    IExtensionPoint extensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.ui.actions");
    IExtension[] extensions = extensionPoint.getExtensions();
    IConfigurationElement localIConfigurationElement3 = (element = extensions).length;
    for (IConfigurationElement localIConfigurationElement2 = 0; localIConfigurationElement2 < localIConfigurationElement3; localIConfigurationElement2++)
    {
      IExtension extension = element[localIConfigurationElement2];
      IConfigurationElement[] elements = extension.getConfigurationElements();
      IConfigurationElement[] arrayOfIConfigurationElement5;
      int i5 = (arrayOfIConfigurationElement5 = elements).length;
      for (int i4 = 0; i4 < i5; i4++)
      {
        IConfigurationElement element = arrayOfIConfigurationElement5[i4];
        if ((!isDisabled(element)) && 
          (element.getName().equals("dynamicPopupMenu"))) {
          readDynamicPopupContributor(element);
        }
      }
    }
  }
  
  private static void readPresentation(IConfigurationElement element)
  {
    try
    {
      String name = element.getAttribute("name");
      
      String iconPath = element.getAttribute("icon");
      ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
        element.getContributor().getName(), iconPath);
      AbstractTaskListPresentation presentation = (AbstractTaskListPresentation)element.createExecutableExtension("class");
      presentation.setPluginId(element.getNamespaceIdentifier());
      presentation.setImageDescriptor(imageDescriptor);
      presentation.setName(name);
      
      String primary = element.getAttribute("primary");
      if ((primary != null) && (primary.equals("true"))) {
        presentation.setPrimary(true);
      }
      TaskListView.addPresentation(presentation);
    }
    catch (Throwable e)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
        "Could not load presentation extension", e));
    }
  }
  
  private static void readDuplicateDetector(IConfigurationElement element)
  {
    try
    {
      Object obj = element.createExecutableExtension("class");
      if ((obj instanceof AbstractDuplicateDetector))
      {
        AbstractDuplicateDetector duplicateDetector = (AbstractDuplicateDetector)obj;
        duplicateDetector.setName(element.getAttribute("name"));
        duplicateDetector.setConnectorKind(element.getAttribute("kind"));
        TasksUiPlugin.getDefault().addDuplicateDetector(duplicateDetector);
      }
      else
      {
        StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
          "Could not load duplicate detector " + obj.getClass().getCanonicalName()));
      }
    }
    catch (Throwable e)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", "Could not load duplicate detector", e));
    }
  }
  
  private static void readLinkProvider(IConfigurationElement element)
  {
    try
    {
      Object repositoryLinkProvider = element.createExecutableExtension("class");
      if ((repositoryLinkProvider instanceof AbstractTaskRepositoryLinkProvider)) {
        TasksUiPlugin.getDefault().addRepositoryLinkProvider(
          (AbstractTaskRepositoryLinkProvider)repositoryLinkProvider);
      } else {
        StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
          "Could not load repository link provider " + 
          repositoryLinkProvider.getClass().getCanonicalName()));
      }
    }
    catch (Throwable e)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
        "Could not load repository link provider", e));
    }
  }
  
  private static void readTaskEditorPageFactory(IConfigurationElement element)
  {
    String id = element.getAttribute("id");
    if (id == null)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", "Editor page factory must specify id"));
      return;
    }
    try
    {
      Object item = element.createExecutableExtension("class");
      if ((item instanceof AbstractTaskEditorPageFactory))
      {
        AbstractTaskEditorPageFactory editorPageFactory = (AbstractTaskEditorPageFactory)item;
        editorPageFactory.setId(id);
        editorPageFactory.setPluginId(element.getNamespaceIdentifier());
        TasksUiPlugin.getDefault().addTaskEditorPageFactory(editorPageFactory);
      }
      else
      {
        StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
          "Could not load editor page factory " + item.getClass().getCanonicalName() + " must implement " + 
          AbstractTaskEditorPageFactory.class.getCanonicalName()));
      }
    }
    catch (Throwable e)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", "Could not load page editor factory", 
        e));
    }
  }
  
  private static void readRepositoryConnectorUi(IConfigurationElement element)
  {
    try
    {
      Object connectorUiObject = element.createExecutableExtension("class");
      if ((connectorUiObject instanceof AbstractRepositoryConnectorUi))
      {
        AbstractRepositoryConnectorUi connectorUi = (AbstractRepositoryConnectorUi)connectorUiObject;
        if (TasksUiPlugin.getConnector(connectorUi.getConnectorKind()) != null)
        {
          TasksUiPlugin.getDefault().addRepositoryConnectorUi(connectorUi);
          
          String iconPath = element.getAttribute("brandingIcon");
          if (iconPath != null)
          {
            ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
              element.getContributor().getName(), iconPath);
            if (descriptor != null) {
              TasksUiPlugin.getDefault().addBrandingIcon(connectorUi.getConnectorKind(), 
                CommonImages.getImage(descriptor));
            }
          }
          String overlayIconPath = element.getAttribute("overlayIcon");
          if (overlayIconPath != null)
          {
            ImageDescriptor descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
              element.getContributor().getName(), overlayIconPath);
            if (descriptor != null) {
              TasksUiPlugin.getDefault().addOverlayIcon(connectorUi.getConnectorKind(), descriptor);
            }
          }
        }
        else
        {
          StatusHandler.log(new Status(
            4, 
            "org.eclipse.mylyn.tasks.ui", 
            NLS.bind(
            "Ignoring connector ui for kind ''{0}'' without corresponding core contributed by ''{1}''.", connectorUi.getConnectorKind(), element.getContributor().getName())));
        }
      }
      else
      {
        StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", "Could not load connector ui " + 
          connectorUiObject.getClass().getCanonicalName()));
      }
    }
    catch (Throwable e)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", "Could not load connector ui", e));
    }
  }
  
  private static void readRepositoryTemplate(IConfigurationElement element)
  {
    boolean anonymous = false;
    boolean addAuto = false;
    
    String label = element.getAttribute("label");
    String serverUrl = element.getAttribute("urlRepository");
    String repKind = element.getAttribute("repositoryKind");
    String version = element.getAttribute("version");
    String newTaskUrl = element.getAttribute("urlNewTask");
    String taskPrefix = element.getAttribute("urlTask");
    String taskQueryUrl = element.getAttribute("urlTaskQuery");
    String newAccountUrl = element.getAttribute("urlNewAccount");
    String encoding = element.getAttribute("characterEncoding");
    addAuto = Boolean.parseBoolean(element.getAttribute("addAutomatically"));
    anonymous = Boolean.parseBoolean(element.getAttribute("anonymous"));
    if ((serverUrl != null) && (label != null) && (repKind != null) && 
      (TasksUi.getRepositoryManager().getRepositoryConnector(repKind) != null))
    {
      RepositoryTemplate template = new RepositoryTemplate(label, serverUrl, encoding, version, newTaskUrl, 
        taskPrefix, taskQueryUrl, newAccountUrl, anonymous, addAuto);
      TasksUiPlugin.getRepositoryTemplateManager().addTemplate(repKind, template);
      IConfigurationElement[] arrayOfIConfigurationElement;
      int j = (arrayOfIConfigurationElement = element.getChildren()).length;
      for (int i = 0; i < j; i++)
      {
        IConfigurationElement configElement = arrayOfIConfigurationElement[i];
        String name = configElement.getAttribute("name");
        String value = configElement.getAttribute("value");
        if ((name != null) && (!name.equals("")) && (value != null)) {
          template.addAttribute(name, value);
        }
      }
    }
    else
    {
      StatusHandler.log(new Status(
        4, 
        "org.eclipse.mylyn.tasks.ui", 
        "Could not load repository template extension contributed by " + element.getNamespaceIdentifier() + " with connectorKind " + repKind));
    }
  }
  
  private static void readDynamicPopupContributor(IConfigurationElement element)
  {
    try
    {
      Object dynamicPopupContributor = element.createExecutableExtension("class");
      String menuPath = element.getAttribute("menuPath");
      if ((dynamicPopupContributor instanceof IDynamicSubMenuContributor)) {
        TasksUiPlugin.getDefault().addDynamicPopupContributor(menuPath, 
          (IDynamicSubMenuContributor)dynamicPopupContributor);
      } else {
        StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
          "Could not load dynamic popup menu: " + dynamicPopupContributor.getClass().getCanonicalName() + 
          " must implement " + IDynamicSubMenuContributor.class.getCanonicalName()));
      }
    }
    catch (Throwable e)
    {
      StatusHandler.log(new Status(4, "org.eclipse.mylyn.tasks.ui", 
        "Could not load dynamic popup menu extension", e));
    }
  }
  
  public static AbstractTaskActivityMonitor loadTaskActivityMonitor()
  {
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint connectorsExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.core.activityMonitor");
    
    IExtension[] extensions = connectorsExtensionPoint.getExtensions();
    IExtension[] arrayOfIExtension1;
    int j = (arrayOfIExtension1 = extensions).length;
    for (int i = 0; i < j; i++)
    {
      IExtension extension = arrayOfIExtension1[i];
      IConfigurationElement[] elements = extension.getConfigurationElements();
      IConfigurationElement[] arrayOfIConfigurationElement1;
      int m = (arrayOfIConfigurationElement1 = elements).length;
      for (int k = 0; k < m; k++)
      {
        IConfigurationElement element = arrayOfIConfigurationElement1[k];
        if ("activityMonitor".equals(element.getName())) {
          try
          {
            Object object = element.createExecutableExtension("class");
            if ((object instanceof AbstractTaskActivityMonitor)) {
              return (AbstractTaskActivityMonitor)object;
            }
            StatusHandler.log(new Status(
              4, 
              "org.eclipse.mylyn.tasks.core", 
              NLS.bind(
              "Task activity monitor ''{0}'' does not extend expected class for extension contributed by {1}", 
              object.getClass().getCanonicalName(), element.getContributor().getName())));
          }
          catch (Throwable e)
          {
            StatusHandler.log(new Status(
              4, 
              "org.eclipse.mylyn.tasks.core", 
              NLS.bind(
              "Task activity monitor failed to load for extension contributed by {0}", element.getContributor().getName()), e));
          }
        }
      }
    }
    return new DefaultTaskActivityMonitor();
  }
  
  public static AbstractTaskContextStore loadTaskContextStore()
  {
    IExtensionRegistry registry = Platform.getExtensionRegistry();
    IExtensionPoint connectorsExtensionPoint = registry.getExtensionPoint("org.eclipse.mylyn.tasks.core.contextStore");
    
    IExtension[] extensions = connectorsExtensionPoint.getExtensions();
    IExtension[] arrayOfIExtension1;
    int j = (arrayOfIExtension1 = extensions).length;
    for (int i = 0; i < j; i++)
    {
      IExtension extension = arrayOfIExtension1[i];
      IConfigurationElement[] elements = extension.getConfigurationElements();
      IConfigurationElement[] arrayOfIConfigurationElement1;
      int m = (arrayOfIConfigurationElement1 = elements).length;
      for (int k = 0; k < m; k++)
      {
        IConfigurationElement element = arrayOfIConfigurationElement1[k];
        if ("contextStore".equals(element.getName())) {
          try
          {
            Object object = element.createExecutableExtension("class");
            if ((object instanceof AbstractTaskContextStore)) {
              return (AbstractTaskContextStore)object;
            }
            StatusHandler.log(new Status(
              4, 
              "org.eclipse.mylyn.tasks.core", 
              NLS.bind(
              "Task context store ''{0}'' does not extend expected class for extension contributed by {1}", 
              object.getClass().getCanonicalName(), element.getContributor().getName())));
          }
          catch (Throwable e)
          {
            StatusHandler.log(new Status(
              4, 
              "org.eclipse.mylyn.tasks.core", 
              NLS.bind(
              "Task context store failed to load for extension contributed by {0}", element.getContributor().getName()), e));
          }
        }
      }
    }
    return new DefaultTaskContextStore();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.mylyn.internal.tasks.ui.util.TasksUiExtensionReader
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.mylyn.internal.tasks.ui.util;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.mylyn.internal.tasks.core.ITaskJobFactory;
import org.eclipse.mylyn.internal.tasks.core.ITaskList;
import org.eclipse.mylyn.internal.tasks.core.TaskAttachment;
import org.eclipse.mylyn.internal.tasks.core.data.FileTaskAttachmentSource;
import org.eclipse.mylyn.internal.tasks.core.sync.SubmitTaskAttachmentJob;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IRepositoryManager;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITaskActivityManager;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.context.AbstractTaskContextStore;
import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler;
import org.eclipse.mylyn.tasks.core.data.ITaskDataManager;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.sync.SubmitJob;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.statushandlers.StatusManager;

public class AttachmentUtil
{
  protected static final int BUFFER_SIZE = 1024;
  public static final String CONTEXT_DESCRIPTION = "mylyn/context/zip";
  private static final String CONTEXT_DESCRIPTION_LEGACY = "mylar/context/zip";
  private static final String CONTEXT_FILENAME = "mylyn-context.zip";
  private static final String CONTEXT_CONTENT_TYPE = "application/octet-stream";
  private static final String ATTACHMENT_DEFAULT_NAME = "attachment";
  private static final String CTYPE_ZIP = "zip";
  private static final String CTYPE_OCTET_STREAM = "octet-stream";
  private static final String CTYPE_TEXT = "text";
  private static final String CTYPE_HTML = "html";
  
  public static boolean postContext(AbstractRepositoryConnector connector, TaskRepository repository, ITask task, String comment, TaskAttribute attribute, IProgressMonitor monitor)
    throws CoreException
  {
    AbstractTaskAttachmentHandler attachmentHandler = connector.getTaskAttachmentHandler();
    TasksUiPlugin.getContextStore().saveActiveContext();
    
    File file = TasksUiPlugin.getContextStore().getFileForContext(task);
    if ((attachmentHandler != null) && (file != null) && (file.exists()))
    {
      FileTaskAttachmentSource attachment = new FileTaskAttachmentSource(file);
      attachment.setDescription("mylyn/context/zip");
      attachment.setName("mylyn-context.zip");
      attachmentHandler.postContent(repository, task, attachment, comment, attribute, monitor);
      return true;
    }
    return false;
  }
  
  public static List<ITaskAttachment> getContextAttachments(TaskRepository repository, ITask task)
  {
    List<ITaskAttachment> contextAttachments = new ArrayList();
    try
    {
      taskData = TasksUi.getTaskDataManager().getTaskData(task);
    }
    catch (CoreException localCoreException)
    {
      TaskData taskData;
      return contextAttachments;
    }
    TaskData taskData;
    if (taskData != null)
    {
      List<TaskAttribute> taskAttachments = taskData.getAttributeMapper().getAttributesByType(taskData, 
        "attachment");
      for (TaskAttribute attribute : taskAttachments)
      {
        TaskAttachment taskAttachment = new TaskAttachment(repository, task, attribute);
        taskData.getAttributeMapper().updateTaskAttachment(taskAttachment, attribute);
        if (isContext(taskAttachment)) {
          contextAttachments.add(taskAttachment);
        }
      }
    }
    return contextAttachments;
  }
  
  public static boolean hasContextAttachment(ITask task)
  {
    Assert.isNotNull(task);
    TaskRepository repository = TasksUi.getRepositoryManager().getRepository(task.getConnectorKind(), 
      task.getRepositoryUrl());
    List<ITaskAttachment> contextAttachments = getContextAttachments(repository, task);
    return contextAttachments.size() > 0;
  }
  
  public static boolean downloadContext(ITask task, final ITaskAttachment attachment, IRunnableContext context)
  {
    if (task.isActive()) {
      TasksUi.getTaskActivityManager().deactivateTask(task);
    }
    try
    {
      context.run(true, true, new IRunnableWithProgress()
      {
        /* Error */
        public void run(IProgressMonitor monitor)
          throws InvocationTargetException, InterruptedException
        {
          // Byte code:
          //   0: invokestatic 99	org/eclipse/mylyn/internal/tasks/ui/TasksUiPlugin:getContextStore	()Lorg/eclipse/mylyn/tasks/core/context/AbstractTaskContextStore;
          //   3: aload_0
          //   4: getfield 89	org/eclipse/mylyn/internal/tasks/ui/util/AttachmentUtil$1:val$task	Lorg/eclipse/mylyn/tasks/core/ITask;
          //   7: invokevirtual 102	org/eclipse/mylyn/tasks/core/context/AbstractTaskContextStore:getFileForContext	(Lorg/eclipse/mylyn/tasks/core/ITask;)Ljava/io/File;
          //   10: astore_2
          //   11: iconst_1
          //   12: istore_3
          //   13: new 40	java/io/BufferedOutputStream
          //   16: dup
          //   17: new 42	java/io/FileOutputStream
          //   20: dup
          //   21: aload_2
          //   22: invokespecial 93	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
          //   25: invokespecial 91	java/io/BufferedOutputStream:<init>	(Ljava/io/OutputStream;)V
          //   28: astore 4
          //   30: aload_0
          //   31: getfield 90	org/eclipse/mylyn/internal/tasks/ui/util/AttachmentUtil$1:val$attachment	Lorg/eclipse/mylyn/tasks/core/ITaskAttachment;
          //   34: aload 4
          //   36: aload_1
          //   37: invokestatic 100	org/eclipse/mylyn/internal/tasks/ui/util/AttachmentUtil:downloadAttachment	(Lorg/eclipse/mylyn/tasks/core/ITaskAttachment;Ljava/io/OutputStream;Lorg/eclipse/core/runtime/IProgressMonitor;)V
          //   40: iconst_0
          //   41: istore_3
          //   42: goto +34 -> 76
          //   45: astore 5
          //   47: new 47	java/lang/reflect/InvocationTargetException
          //   50: dup
          //   51: aload 5
          //   53: invokespecial 97	java/lang/reflect/InvocationTargetException:<init>	(Ljava/lang/Throwable;)V
          //   56: athrow
          //   57: astore 6
          //   59: aload 4
          //   61: invokevirtual 94	java/io/OutputStream:close	()V
          //   64: iload_3
          //   65: ifeq +8 -> 73
          //   68: aload_2
          //   69: invokevirtual 92	java/io/File:delete	()Z
          //   72: pop
          //   73: aload 6
          //   75: athrow
          //   76: aload 4
          //   78: invokevirtual 94	java/io/OutputStream:close	()V
          //   81: iload_3
          //   82: ifeq +50 -> 132
          //   85: aload_2
          //   86: invokevirtual 92	java/io/File:delete	()Z
          //   89: pop
          //   90: goto +42 -> 132
          //   93: pop
          //   94: new 45	java/lang/InterruptedException
          //   97: dup
          //   98: invokespecial 95	java/lang/InterruptedException:<init>	()V
          //   101: athrow
          //   102: astore_3
          //   103: new 47	java/lang/reflect/InvocationTargetException
          //   106: dup
          //   107: new 48	org/eclipse/core/runtime/CoreException
          //   110: dup
          //   111: new 54	org/eclipse/mylyn/tasks/core/RepositoryStatus
          //   114: dup
          //   115: iconst_4
          //   116: ldc 2
          //   118: iconst_5
          //   119: ldc 1
          //   121: aload_3
          //   122: invokespecial 101	org/eclipse/mylyn/tasks/core/RepositoryStatus:<init>	(ILjava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
          //   125: invokespecial 98	org/eclipse/core/runtime/CoreException:<init>	(Lorg/eclipse/core/runtime/IStatus;)V
          //   128: invokespecial 97	java/lang/reflect/InvocationTargetException:<init>	(Ljava/lang/Throwable;)V
          //   131: athrow
          //   132: return
          // Line number table:
          //   Java source line #140	-> byte code offset #0
          //   Java source line #142	-> byte code offset #11
          //   Java source line #143	-> byte code offset #13
          //   Java source line #145	-> byte code offset #30
          //   Java source line #146	-> byte code offset #40
          //   Java source line #147	-> byte code offset #45
          //   Java source line #148	-> byte code offset #47
          //   Java source line #149	-> byte code offset #57
          //   Java source line #150	-> byte code offset #59
          //   Java source line #151	-> byte code offset #64
          //   Java source line #152	-> byte code offset #68
          //   Java source line #154	-> byte code offset #73
          //   Java source line #150	-> byte code offset #76
          //   Java source line #151	-> byte code offset #81
          //   Java source line #152	-> byte code offset #85
          //   Java source line #154	-> byte code offset #90
          //   Java source line #155	-> byte code offset #93
          //   Java source line #156	-> byte code offset #94
          //   Java source line #157	-> byte code offset #102
          //   Java source line #158	-> byte code offset #103
          //   Java source line #159	-> byte code offset #107
          //   Java source line #160	-> byte code offset #118
          //   Java source line #159	-> byte code offset #125
          //   Java source line #158	-> byte code offset #128
          //   Java source line #162	-> byte code offset #132
          // Local variable table:
          //   start	length	slot	name	signature
          //   0	133	0	this	1
          //   0	133	1	monitor	IProgressMonitor
          //   10	76	2	targetFile	File
          //   12	70	3	exceptionThrown	boolean
          //   102	20	3	e	java.io.IOException
          //   28	49	4	out	java.io.OutputStream
          //   45	7	5	e	CoreException
          //   57	17	6	localObject	Object
          //   93	1	8	localOperationCanceledException	org.eclipse.core.runtime.OperationCanceledException
          // Exception table:
          //   from	to	target	type
          //   30	42	45	org/eclipse/core/runtime/CoreException
          //   30	57	57	finally
          //   11	90	93	org/eclipse/core/runtime/OperationCanceledException
          //   11	90	102	java/io/IOException
        }
      });
    }
    catch (InvocationTargetException e)
    {
      if ((e.getCause() instanceof CoreException)) {
        TasksUiInternal.displayStatus(Messages.AttachmentUtil_Mylyn_Information, 
          ((CoreException)e.getCause()).getStatus());
      } else {
        StatusManager.getManager().handle(new Status(4, "org.eclipse.mylyn.tasks.ui", 
          "Unexpected error while retrieving context", e), 3);
      }
      return false;
    }
    catch (InterruptedException localInterruptedException)
    {
      return false;
    }
    TasksUiInternal.getTaskList().notifyElementChanged(task);
    TasksUiInternal.activateTaskThroughCommand(task);
    return true;
  }
  
  public static boolean uploadContext(TaskRepository repository, ITask task, String comment, IRunnableContext context)
  {
    TasksUiPlugin.getContextStore().saveActiveContext();
    File sourceContextFile = TasksUiPlugin.getContextStore().getFileForContext(task);
    if (!sourceContextFile.exists())
    {
      TasksUiInternal.displayStatus(Messages.AttachmentUtil_Mylyn_Information, new Status(2, 
        "org.eclipse.mylyn.tasks.ui", Messages.AttachmentUtil_The_context_is_empty));
      return false;
    }
    FileTaskAttachmentSource source = new FileTaskAttachmentSource(sourceContextFile);
    source.setName("mylyn-context.zip");
    source.setDescription("mylyn/context/zip");
    source.setContentType("application/octet-stream");
    AbstractRepositoryConnector connector = TasksUi.getRepositoryManager().getRepositoryConnector(
      repository.getConnectorKind());
    SubmitJob submitJob = TasksUiInternal.getJobFactory().createSubmitTaskAttachmentJob(connector, 
      repository, task, source, comment, null);
    try
    {
      context.run(true, true, new IRunnableWithProgress()
      {
        public void run(IProgressMonitor monitor)
          throws InvocationTargetException, InterruptedException
        {
          if (((SubmitTaskAttachmentJob)AttachmentUtil.this).run(monitor) == Status.CANCEL_STATUS) {
            throw new InterruptedException();
          }
        }
      });
    }
    catch (InvocationTargetException e)
    {
      StatusManager.getManager().handle(new Status(4, "org.eclipse.mylyn.tasks.ui", 
        "Unexpected error while attaching context", e), 3);
      return false;
    }
    catch (InterruptedException localInterruptedException)
    {
      return false;
    }
    IStatus status = submitJob.getStatus();
    if ((status != null) && (status.getSeverity() != 8))
    {
      TasksUiInternal.displayStatus(Messages.AttachmentUtil_Mylyn_Information, submitJob.getStatus());
      return false;
    }
    return true;
  }
  
  public static boolean hasLocalContext(ITask task)
  {
    Assert.isNotNull(task);
    return TasksUiPlugin.getContextStore().hasContext(task);
  }
  
  public static boolean isContext(ITaskAttachment attachment)
  {
    return ("mylyn/context/zip".equals(attachment.getDescription())) || ("mylar/context/zip".equals(attachment.getDescription()));
  }
  
  public static boolean canUploadAttachment(ITask task)
  {
    TaskRepository repository = TasksUi.getRepositoryManager().getRepository(task.getConnectorKind(), 
      task.getRepositoryUrl());
    if (repository != null)
    {
      AbstractRepositoryConnector connector = TasksUi.getRepositoryManager().getRepositoryConnector(
        repository.getConnectorKind());
      AbstractTaskAttachmentHandler attachmentHandler = connector.getTaskAttachmentHandler();
      if (attachmentHandler != null) {
        return attachmentHandler.canPostContent(repository,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd