コード例 #1
0
ファイル: SamplesManager.java プロジェクト: flyingsky/studio3
  private void addSample(ProjectSampleElement sampleElement) {
    String categoryId = sampleElement.getCategory();
    SampleCategory category = categories.get(categoryId);
    if (category != null) {
      String id = sampleElement.getId();
      String name = sampleElement.getDisplayName();
      String location = sampleElement.getLocation();
      boolean isRemote = !location.toLowerCase().endsWith(".zip"); // $NON-NLS-1$
      if (!isRemote) {
        // retrieves the absolute location
        location = (new File(sampleElement.getDirectory(), location)).getAbsolutePath();
      }
      String description = sampleElement.getDescription();
      Map<String, URL> iconUrls = sampleElement.getIconUrls();
      SamplesReference sample =
          new SamplesReference(category, id, name, location, isRemote, description, iconUrls, null);
      sample.setNatures(sampleElement.getNatures());

      List<SamplesReference> samples = bundleSamplesByCategory.get(categoryId);
      if (samples == null) {
        samples = new ArrayList<SamplesReference>();
        bundleSamplesByCategory.put(categoryId, samples);
      }
      samples.add(sample);
      bundleSamplesById.put(id, sample);

      fireSampleAdded(sample);
    }
  }
コード例 #2
0
ファイル: SamplesManager.java プロジェクト: flyingsky/studio3
  private void readElement(IConfigurationElement element) {
    String elementName = element.getName();

    if (ELEMENT_CATEGORY.equals(elementName)) {
      String id = element.getAttribute(ATTR_ID);
      if (StringUtil.isEmpty(id)) {
        return;
      }
      String name = element.getAttribute(ATTR_NAME);
      if (StringUtil.isEmpty(name)) {
        return;
      }
      SampleCategory category = new SampleCategory(id, name, element);
      categories.put(id, category);

      String iconFile = element.getAttribute(ATTR_ICON);
      if (!StringUtil.isEmpty(iconFile)) {
        Bundle bundle = Platform.getBundle(element.getNamespaceIdentifier());
        URL url = bundle.getEntry(iconFile);
        category.setIconFile(ResourceUtil.resourcePathToString(url));
      }
    } else if (ELEMENT_SAMPLESINFO.equals(elementName)) {
      // either a local path or remote git url needs to be defined
      String path = null;
      boolean isRemote = false;

      Bundle bundle = Platform.getBundle(element.getNamespaceIdentifier());
      IConfigurationElement[] localPaths = element.getChildren(ELEMENT_LOCAL);
      if (localPaths.length > 0) {
        String location = localPaths[0].getAttribute(ATTR_LOCATION);
        URL url = bundle.getEntry(location);
        path = ResourceUtil.resourcePathToString(url);
      } else {
        IConfigurationElement[] remotePaths = element.getChildren(ELEMENT_REMOTE);
        if (remotePaths.length > 0) {
          path = remotePaths[0].getAttribute(ATTR_LOCATION);
          isRemote = true;
        }
      }
      if (path == null) {
        return;
      }

      String id = element.getAttribute(ATTR_ID);
      if (StringUtil.isEmpty(id)) {
        return;
      }
      String name = element.getAttribute(ATTR_NAME);
      if (StringUtil.isEmpty(name)) {
        return;
      }

      String categoryId = element.getAttribute(ATTR_CATEGORY);
      SampleCategory category = categories.get(categoryId);
      if (category == null) {
        categoryId = "default"; // $NON-NLS-1$
        category = categories.get(categoryId);
        if (category == null) {
          category =
              new SampleCategory(categoryId, Messages.SamplesManager_DefaultCategory_Name, element);
        }
      }

      List<SamplesReference> samples = sampleRefsByCategory.get(categoryId);
      if (samples == null) {
        samples = new ArrayList<SamplesReference>();
        sampleRefsByCategory.put(categoryId, samples);
      }

      String description = element.getAttribute(ATTR_DESCRIPTION);

      URL iconUrl = null;
      String iconPath = element.getAttribute(ATTR_ICON);
      if (!StringUtil.isEmpty(iconPath)) {
        URL url = bundle.getEntry(iconPath);
        try {
          iconUrl = FileLocator.toFileURL(url);
        } catch (IOException e) {
          IdeLog.logError(
              SamplesPlugin.getDefault(),
              MessageFormat.format(
                  "Unable to retrieve the icon at {0} for sample {1}",
                  iconPath, name), // $NON-NLS-1$
              e);
        }
      }
      Map<String, URL> iconUrls = new HashMap<String, URL>();
      iconUrls.put(SamplesReference.DEFAULT_ICON_KEY, iconUrl);

      SamplesReference samplesRef =
          new SamplesReference(category, id, name, path, isRemote, description, iconUrls, element);
      samples.add(samplesRef);
      samplesById.put(id, samplesRef);

      String infoFile = element.getAttribute(ATTR_INFOFILE);
      if (!StringUtil.isEmpty(infoFile)) {
        URL url = bundle.getEntry(infoFile);
        samplesRef.setInfoFile(ResourceUtil.resourcePathToString(url));
      }

      IConfigurationElement[] natures = element.getChildren(ELEMENT_NATURE);
      List<String> natureIds = new ArrayList<String>();
      String natureId;
      for (IConfigurationElement nature : natures) {
        natureId = nature.getAttribute(ATTR_ID);
        if (!StringUtil.isEmpty(natureId)) {
          natureIds.add(natureId);
        }
      }
      samplesRef.setNatures(natureIds.toArray(new String[natureIds.size()]));

      IConfigurationElement[] includes = element.getChildren(ELEMENT_INCLUDE);
      List<String> includePaths = new ArrayList<String>();
      String includePath;
      URL url;
      for (IConfigurationElement include : includes) {
        includePath = include.getAttribute(ATTR_PATH);
        if (!StringUtil.isEmpty(includePath)) {
          url = bundle.getEntry(includePath);
          path = ResourceUtil.resourcePathToString(url);
          if (path != null) {
            includePaths.add(path);
          }
        }
      }
      samplesRef.setIncludePaths(includePaths.toArray(new String[includePaths.size()]));
    }
  }