/* Draws a feature called "SELECTED" when the user highlights DNA */
  void paintSelectedFeature(int start, int end) {
    // first see if a selected feature already exists
    // if it does, remove it
    removeSelectedFeature();

    // paint the new one
    Feature f = new Feature(cgview.getFeatureSlots().get(0), "SELECTED");
    f.setColor(Color.green);
    new FeatureRange(f, start, end);

    repaint();
  }
  /* Set up the CGView to display the plasmid */
  private void initCgview() {
    int length = plasmid.getSequence().length();
    cgview = new Cgview(length);

    // some optional settings
    cgview.setWidth(getWidth());
    cgview.setHeight(getHeight());
    cgview.setTitle(plasmid.getName());
    cgview.setLabelPlacementQuality(100);
    cgview.setShowWarning(true);
    cgview.setLabelLineLength(8.0d);
    cgview.setLabelLineThickness(0.5f);
    cgview.setLabelShuffle(false);

    // create a FeatureSlot to hold sequence features
    FeatureSlot featureSlot = new FeatureSlot(cgview, CgviewConstants.DIRECT_STRAND);

    // create random sequence features
    List<org.autogene.core.bio.entities.Annotation> annotations = plasmid.getAnnotations();
    HashMap<String, Integer> counts = new HashMap<String, Integer>();

    // create the feature annotations to view in the plasmid
    for (int i = 0; i < annotations.size(); i++) {

      // update the label names such as Aapl (1) and Aapl (2) in the case that
      // there is more than one of the same annotation in two different locations
      Annotation a = annotations.get(i);
      String name = a.getFeature().getName();
      int cur = 1;
      if (counts.containsKey(name)) {
        cur = counts.get(name);
        cur++;
        counts.put(name, cur);

        //   Log.addText("plasmidpanel: " + name + "," + cur);
      } else {
        counts.put(name, cur);
        //   Log.addText("plasmidpanel: " + name + "," + cur);

      }

      // simply create the feature
      System.out.println("ABCD creating feature with " + name + " " + cur);
      Feature feature = new Feature(featureSlot, (name + " (" + cur + ")"));
      feature.setColor(
          a.getScore() == 1.0
              ? ColorConstants.NEUTRAL_COLOR_PERFECT_MATCH
              : ColorConstants.NEUTRAL_COLOR_IMPERFECT_MATCH);
      FeatureRange featureRange = new FeatureRange(feature, a.getStart(), a.getEnd());
    }
  }
 private Annotation findAnnotationOfFeature(Feature f) {
   List<Annotation> annotations = plasmid.getAnnotations();
   for (int i = 0; i < annotations.size(); i++) {
     org.autogene.core.bio.entities.Feature o = annotations.get(i).getFeature();
     if (f.getLabel().equals(o.getDisplayName())) return annotations.get(i);
   }
   return null;
 }
  /* Searches for the "SELECTED" feature and removes it if it exists */
  void removeSelectedFeature() {
    boolean found = true;
    ArrayList<FeatureSlot> slots = cgview.getFeatureSlots();
    for (int i = 0; i < slots.size(); i++) {
      FeatureSlot fs = slots.get(i);
      ArrayList<Feature> features = fs.getFeatures();
      for (int j = 0; j < features.size(); j++) {
        Feature f = features.get(j);
        if (f.getLabel().equals("SELECTED")) {
          found = true;
          features.remove(j);
          break;
        }
      }

      if (found) break;
    }

    repaint();
  }
  @Override
  public void mousePressed(MouseEvent me) {

    /* Check to see if we clicked a label */

    int x = me.getX(), y = me.getY();

    Feature selectedFeature = null;
    ArrayList<OuterLabel> outerLabels = cgview.getOuterLabels();
    OuterLabel ol = null;
    for (int o = 0; o < outerLabels.size(); o++) {

      ol = outerLabels.get(o);
      Rectangle2D rect = ol.getBounds();

      double rectX = rect.getX(),
          rectY = rect.getY(),
          rectW = rect.getWidth(),
          rectH = rect.getHeight();

      if (x > rectX && x < (rectX + rectW) && y > rectY && y < (rectY + rectH)) {
        ArrayList<FeatureSlot> featureSlots = cgview.getFeatureSlots();

        for (int i = 0; i < featureSlots.size(); i++) {
          FeatureSlot fs = featureSlots.get(i);
          ArrayList<Feature> features = fs.getFeatures();
          for (int j = 0; j < features.size(); j++) {
            Feature f = features.get(j);
            String featureLabel = f.getLabel();

            if (featureLabel.equals(ol.getLabelText())) {
              selectedFeature = f;
              break;
            }

            if (selectedFeature != null) break;
          }

          if (selectedFeature != null) break;
        }
      }

      if (selectedFeature != null) break;
    }

    /* If we have selected a feature, either make it appear selected or
     * deselected. */
    if (selectedFeature != null) {

      // paint the feature on the cgview
      Annotation a = findAnnotationOfFeature(selectedFeature);

      Color toUse =
          (a.getScore() == 1.0)
              ? ColorConstants.NEUTRAL_COLOR_PERFECT_MATCH
              : ColorConstants.NEUTRAL_COLOR_IMPERFECT_MATCH;
      Color fColor = selectedFeature.getColor();
      Color blue =
          (a.getScore() == 1.0)
              ? ColorConstants.NEUTRAL_COLOR_PERFECT_MATCH
              : ColorConstants.NEUTRAL_COLOR_IMPERFECT_MATCH;
      Color orange = ColorConstants.ROW_SELECTED_COLOR;
      boolean isBlue =
          fColor.getRed() == blue.getRed()
              && fColor.getGreen() == blue.getGreen()
              && fColor.getBlue() == blue.getBlue();
      boolean isOrange =
          fColor.getRed() == orange.getRed()
              && fColor.getGreen() == orange.getGreen()
              && fColor.getBlue() == orange.getBlue();
      Color turq = ColorConstants.CUSTOM_ANNOTATION_COLOR;
      boolean isTurquoise =
          fColor.getRed() == turq.getRed()
              && fColor.getGreen() == turq.getGreen()
              && fColor.getBlue() == turq.getBlue();
      Color ye = ColorConstants.VIEW_FEATURE_FROM_PRIVATE_REGISTRY_COLOR;
      boolean isYellow =
          fColor.getRed() == ye.getRed()
              && fColor.getGreen() == ye.getGreen()
              && fColor.getBlue() == ye.getBlue();
      if (isBlue || isOrange || isTurquoise || isYellow) {
        toUse = ColorConstants.SELECTED_COLOR;
        selectedFeatures.add(selectedFeature.getLabel());
      } else if (((MyTableModel) home.getTable().getModel())
          .isFeatureSelected(selectedFeature.getLabel())) {
        toUse = ColorConstants.ROW_SELECTED_COLOR;
        selectedFeatures.remove(selectedFeature.getLabel());
      } else {
        toUse =
            (a.getScore() == 1.0)
                ? ColorConstants.NEUTRAL_COLOR_PERFECT_MATCH
                : ColorConstants.NEUTRAL_COLOR_IMPERFECT_MATCH;
        selectedFeatures.remove(selectedFeature.getLabel());
      }

      selectedFeature.setColor(toUse);

      ArrayList<FeatureRange> f = selectedFeature.getRanges();
      for (int n = 0; n < f.size(); n++) {
        f.get(n).setColor(toUse);
      }

      // ol.setColor(Color.red);

      repaint();

      // highlight text in the textpane
      FeatureRange fr = (FeatureRange) selectedFeature.getRanges().get(0);

      if (home != null) home.highlightDNA(fr);

    } else {
      System.out.println("selected feature is null");
    }
  }