Exemplo n.º 1
0
    /**
     * This is the only method necessary to overload. All we have to do is print the String value of
     * the model along with its index in the ModelList data structure.
     *
     * @param list the JList
     * @param value the value
     * @param index the index of the value
     * @param isSelected if true the item is selected
     * @param cellHasFocus whether it has the focus
     * @return the rendering component
     */
    public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

      Component modelComponent = null;

      if (value instanceof EnsembleLibraryModel) {

        EnsembleLibraryModel model = ((EnsembleLibraryModel) value);

        String modelString =
            index + ": " + model.getStringRepresentation().replaceAll("weka.classifiers.", "");

        modelComponent =
            super.getListCellRendererComponent(list, modelString, index, isSelected, cellHasFocus);

        if (!model.getOptionsWereValid()) {
          modelComponent.setBackground(Color.pink);
        }

        ((JComponent) modelComponent).setToolTipText(model.getDescriptionText());
      }

      return modelComponent;
    }
  /**
   * This will support the button triggered events for this panel.
   *
   * @param e the event
   */
  public void actionPerformed(ActionEvent e) {

    ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel());

    if (e.getSource() == m_GenerateButton) {

      // here we want to generate all permutations of the
      // options specified and then add each of them to the
      // model list panel

      Vector models = ((GenericObjectNode) m_TreeModel.getRoot()).getValues();

      int total = models.size();
      int invalid = 0;

      for (int i = 0; i < models.size(); i++) {
        Classifier classifier = (Classifier) models.get(i);

        // This method will invoke the classifier's setOptions
        // method to see if the current set of options was
        // valid.

        EnsembleLibraryModel model = m_ListModelsPanel.getLibrary().createModel(classifier);

        model.testOptions();

        if (!model.getOptionsWereValid()) invalid++;

        dataModel.add(model);
      }

      // update the message text with model generation info
      String generateString = new String("  " + total + " models generated");
      generateString += ", " + invalid + " had errors";
      m_GenerateLabel.setText(generateString);

    } else if (e.getSource() == m_RemoveSelectedButton) {

      // here we simply get the list of models that are
      // currently selected and ten remove them from the list

      Object[] currentModels = m_ModelList.getSelectedValues();

      for (int i = 0; i < currentModels.length; i++) {
        dataModel.removeElement(currentModels[i]);
      }

      // Shrink the selected range to the first index that was selected
      if (m_ModelList.getSelectedIndices().length > 0) {
        int selected[] = new int[1];
        selected[0] = m_ModelList.getSelectedIndices()[0];
        m_ModelList.setSelectedIndices(selected);
      }

    } else if (e.getSource() == m_RemoveInvalidButton) {

      // here we simply remove all the models that were not
      // valid

      Vector toRemove = new Vector();

      for (int i = 0; i < dataModel.getSize(); i++) {

        EnsembleLibraryModel currentModel = (EnsembleLibraryModel) dataModel.getElementAt(i);
        if (!currentModel.getOptionsWereValid()) {
          toRemove.add(currentModel);
        }
      }

      for (int i = 0; i < toRemove.size(); i++) dataModel.removeElement(toRemove.get(i));

    } else if (e.getSource() == m_AddAllButton) {

      // here we just need to add all of the models to the
      // ListModelsPanel object

      Iterator it = dataModel.iterator();

      while (it.hasNext()) {
        EnsembleLibraryModel currentModel = (EnsembleLibraryModel) it.next();
        if (currentModel.getOptionsWereValid()) {
          m_ListModelsPanel.addModel(currentModel);
        }
      }

      int size = dataModel.getSize();

      for (int i = 0; i < size; i++) {
        dataModel.removeElement(dataModel.getElementAt(0));
      }
    }
  }