Example usage for javax.swing DefaultListModel addElement

List of usage examples for javax.swing DefaultListModel addElement

Introduction

In this page you can find the example usage for javax.swing DefaultListModel addElement.

Prototype

public void addElement(E element) 

Source Link

Document

Adds the specified component to the end of this list.

Usage

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

@Override
public void dataChangedEvent(String pGroup) {
    List<ManageableType> elements = TagManager.getSingleton().getAllElements();

    List<Tag> tags = new ArrayList<Tag>();
    for (ManageableType e : elements) {
        Tag t = (Tag) e;// w  w w  .j  av  a 2s  .c  o  m
        tags.add(t);
    }

    Collections.sort(tags, new Comparator<Tag>() {

        @Override
        public int compare(Tag o1, Tag o2) {
            try {
                return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
            } catch (Exception e) {
                return 0;
            }
        }
    });

    DefaultListModel tagModel = new DefaultListModel();
    tagModel.addElement(NoTag.getSingleton());
    for (Tag t : tags) {
        tagModel.addElement(t);
    }
    jVillageGroupList.setModel(tagModel);
    //select all groups and initialize lists
    jVillageGroupList.getSelectionModel().setSelectionInterval(0, (elements.size() > 0) ? elements.size() : 0);

}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

/**
 * Filter target lists by selected allies
 *///from  w w w.  j  ava  2s  .  co  m
private void fireFilterTargetByAllyEvent() {
    Ally a = (Ally) jTargetAllyList.getSelectedValue();
    Tribe[] tribes = AllyUtils.getTribes(a, Tribe.CASE_INSENSITIVE_ORDER);
    DefaultListModel model = new DefaultListModel();
    for (Tribe t : tribes) {
        model.addElement(t);
    }
    jTargetTribeList.setModel(model);
    jTargetTribeList.setSelectedIndex(0);
}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

private void fireTargetAllyFilterChangedEvent(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_fireTargetAllyFilterChangedEvent
    Ally[] allies = AllyUtils.getAlliesByFilter(jTargetAllyFilter.getText(), Ally.CASE_INSENSITIVE_ORDER);
    DefaultListModel targetAllyModel = new DefaultListModel();
    for (Ally a : allies) {
        targetAllyModel.addElement(a);
    }//from  w w w  .  j a  v  a2  s  .  c  o m
    jTargetAllyList.setModel(targetAllyModel);
}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

/**
 * Filter target lists by selected tribes
 *///from   ww  w .  ja  v  a  2s  . c  o  m
private void fireFilterTargetByTribeEvent() {
    Tribe t = (Tribe) jTargetTribeList.getSelectedValue();
    String[] continents = VillageUtils.getContinents(t);
    DefaultListModel contModel = new DefaultListModel();
    for (String cont : continents) {
        contModel.addElement(cont);
    }
    jTargetContinentList.setModel(contModel);
    if (continents.length > 0) {
        jTargetContinentList.getSelectionModel().setSelectionInterval(0, continents.length - 1);
    }
}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

/**
 * Filter source lists by selected groups
 *///from   ww  w  .  j a v  a  2  s . c  om
private void fireFilterSourceVillagesByGroupEvent() {
    List<Village> villageList = getGroupFilteredSourceVillages();
    String[] continents = VillageUtils.getContinents(villageList.toArray(new Village[villageList.size()]));
    DefaultListModel contModel = new DefaultListModel();
    for (String cont : continents) {
        contModel.addElement(cont);
    }
    //set continents list -> village list updates automatically via continent list listener
    jSourceContinentList.setModel(contModel);
    if (continents.length > 0) {
        jSourceContinentList.getSelectionModel().setSelectionInterval(0, continents.length - 1);
    }
}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

/**
 * Filter source list by selected continents
 *///from ww w  .  j a va 2  s  .c  o m
private void fireFilterSourceContinentEvent() {
    Object[] conts = jSourceContinentList.getSelectedValues();
    //build list of allowed continents
    List<Integer> allowedContinents = new LinkedList<Integer>();
    for (Object cont : conts) {
        int contId = Integer.parseInt(((String) cont).replaceAll("K", ""));
        allowedContinents.add(contId);
    }
    List<Village> villageList = getGroupFilteredSourceVillages();
    Village[] filtered = VillageUtils.getVillagesByContinent(
            villageList.toArray(new Village[villageList.size()]),
            allowedContinents.toArray(new Integer[allowedContinents.size()]), Village.CASE_INSENSITIVE_ORDER);

    DefaultListModel villageModel = new DefaultListModel();
    for (Village v : filtered) {
        villageModel.addElement(v);
    }
    jSourceVillageList.setModel(villageModel);
}

From source file:de.tor.tribes.ui.windows.TribeTribeAttackFrame.java

/**
 * Filter target lists by selected continents
 *//*from  ww w  .  ja v a 2s .  c  o m*/
private void fireFilterTargetByContinentEvent() {
    Object[] conts = jTargetContinentList.getSelectedValues();
    //build list of allowed continents
    List<Integer> allowedContinents = new LinkedList<Integer>();
    for (Object cont : conts) {
        int contId = Integer.parseInt(((String) cont).replaceAll("K", ""));
        allowedContinents.add(contId);
    }

    List<Tribe> tribes = new ArrayList<Tribe>();
    for (Object tribe : jTargetTribeList.getSelectedValues()) {
        tribes.add((Tribe) tribe);
    }

    Village[] filtered = VillageUtils.getVillagesByContinent(
            VillageUtils.getVillages(tribes.toArray(new Tribe[tribes.size()])),
            allowedContinents.toArray(new Integer[allowedContinents.size()]), Village.CASE_INSENSITIVE_ORDER);

    DefaultListModel villageModel = new DefaultListModel();
    for (Village v : filtered) {
        villageModel.addElement(v);
    }
    jTargetVillageList.setModel(villageModel);
}

From source file:dbseer.gui.actions.ExplainChartAction.java

public void printExplanations() {
    final StatisticalPackageRunner runner = DBSeerGUI.runner;
    int explanationColumnCount = 6;
    int predicateColumnCount = 5;

    int explanationRowCount = 0;
    int predicateRowCount = 0;
    double maxConfidence = Double.MIN_VALUE;

    this.explanations.clear();
    DefaultListModel explanationListModel = panel.getControlPanel().getExplanationListModel();
    explanationListModel.clear();//from  w  ww .j  a va  2 s  .  co  m

    JTextField confidenceThresholdTextField = panel.getControlPanel().getConfidenceThresholdTextField();
    if (!UserInputValidator.validateNumber(confidenceThresholdTextField.getText().trim(),
            "Confidence Threshold", true)) {
        return;
    }
    confidenceThreshold = Double.parseDouble(confidenceThresholdTextField.getText().trim());
    if (confidenceThreshold < 0 || confidenceThreshold > 100) {
        JOptionPane.showMessageDialog(null, "Confidence threshold must be between 1 and 100.", "Warning",
                JOptionPane.WARNING_MESSAGE);
        return;
    }

    try {
        Object[] explanations = (Object[]) runner.getVariableCell("explanations");
        explanationRowCount = explanations.length / explanationColumnCount;
        for (int r = 0; r < explanationRowCount; ++r) {
            String causeName = (String) explanations[r];
            double[] confidence = (double[]) explanations[r + explanationRowCount * 1];
            Object[] predicates = (Object[]) explanations[r + explanationRowCount * 5];

            if (runner instanceof OctaveRunner) {
                for (int i = 0; i < predicates.length; ++i) {
                    if (predicates[i] instanceof OctaveString) {
                        OctaveString str = (OctaveString) predicates[i];
                        predicates[i] = str.getString();
                    } else if (predicates[i] instanceof OctaveDouble) {
                        OctaveDouble val = (OctaveDouble) predicates[i];
                        predicates[i] = val.getData();
                    }
                }
            }

            DBSeerCausalModel explanation = new DBSeerCausalModel(causeName, confidence[0]);
            explanation.getPredicates();

            predicateRowCount = predicates.length / predicateColumnCount;
            for (int p = 0; p < predicateRowCount; ++p) {
                String predicateName = (String) predicates[p];
                double[] bounds = (double[]) predicates[p + predicateRowCount * 1];
                DBSeerPredicate predicate = new DBSeerPredicate(predicateName, bounds[0], bounds[1]);
                explanation.getPredicates().add(predicate);
            }

            this.explanations.add(explanation);

            if (maxConfidence < confidence[0]) {
                maxConfidence = confidence[0];
            }
        }
    } catch (Exception e) {
        DBSeerExceptionHandler.handleException(e);
    }

    //      double[] mockupConf = {82.74, 21.49, 14.23, 7.86};

    int rank = 1;
    for (DBSeerCausalModel explanation : explanations) {
        if (explanation.getConfidence() > confidenceThreshold) {
            //            String output = String.format("%d. %s\n", rank++, explanation.toString());
            // temp
            //            if (rank <= 4)
            //            {
            //               String output = String.format("%d. %s (%.2f%%)\n", rank, explanation.getCause(), mockupConf[rank - 1]);
            //               explanationListModel.addElement(output);
            //               rank++;
            //            }
            //            else
            //            {
            //               String output = String.format("%d. %s\n", rank++, explanation.toString());
            //               explanationListModel.addElement(output);
            //            }
            String output = String.format("%d. %s\n", rank++, explanation.toString());
            explanationListModel.addElement(output);
        }
    }

    if (maxConfidence < confidenceThreshold) {
        String output = String.format(
                "There are no possible causes with the confidence higher than threshold (%.2f%%).\n",
                confidenceThreshold);
        console.append(output);
        console.append("Showing the current predicates.\n");
    }

    printPredicates();
}

From source file:edu.ku.brc.af.ui.forms.FormViewObj.java

/**
 * Sets the appropriate index in the list box
 * @param list the list box/*from   w ww . j  a va  2  s. co m*/
 * @param data the data value
 */
protected static void setListValue(final JList list, final Object data) {

    Iterator<?> iter = null;
    if (data instanceof Set<?>) {
        iter = ((Set<?>) data).iterator();

    } else if (data instanceof org.hibernate.collection.PersistentSet) {
        iter = ((org.hibernate.collection.PersistentSet) data).iterator();
    }

    if (iter != null) {
        DefaultListModel defModel = new DefaultListModel();
        while (iter.hasNext()) {
            defModel.addElement(iter.next());
        }
        list.setModel(defModel);

    } else {
        ListModel model = list.getModel();
        for (int i = 0; i < model.getSize(); i++) {
            Object item = model.getElementAt(i);
            if (item instanceof String) {
                if (((String) item).equals(data)) {
                    list.setSelectedIndex(i);
                    return;
                }
            } else if (item.equals(data)) {
                list.setSelectedIndex(i);
                return;
            }
        }
    }
}

From source file:GUI.MainWindow.java

public void addReference(JTree tree, JList list, Reference current) {
    DefaultMutableTreeNode node = ((DefaultMutableTreeNode) tree.getLastSelectedPathComponent());
    if (node == null) {
        return;/*from w w w  .  j a  v a2 s  .  c om*/
    }

    Object obj = node.getUserObject();
    if (!(obj instanceof Vulnerability)) {
        return; // here be monsters, most likely in the merge tree
    }
    Vulnerability vuln = (Vulnerability) obj;
    DefaultListModel dlm = (DefaultListModel) list.getModel();
    // Build Input Field and display it
    JTextField description = new JTextField();
    JTextField url = new JTextField();

    // If current is not null then pre-set the description and risk
    if (current != null) {
        description.setText(current.getDescription());
        url.setText(current.getUrl());
    }

    JLabel error = new JLabel(
            "A valid URL needs to be supplied including the protocol i.e. http://www.github.com");
    error.setForeground(Color.red);
    Object[] message = { "Description:", description, "URL:", url };

    String url_string = null;
    Reference newref = null;
    while (url_string == null) {
        int option = JOptionPane.showConfirmDialog(null, message, "Add Reference",
                JOptionPane.OK_CANCEL_OPTION);
        if (option == JOptionPane.OK_OPTION) {
            System.out.println("User clicked ok, validating data");
            String ref_desc = description.getText();
            String ref_url = url.getText();
            if (!ref_desc.equals("") || !ref_url.equals("")) {
                // Both have values
                // Try to validate URL
                try {

                    URL u = new URL(url.getText());
                    u.toURI();
                    url_string = url.getText(); // Causes loop to end with a valid url

                } catch (MalformedURLException ex) {
                    url_string = null;
                    //ex.printStackTrace();
                } catch (URISyntaxException ex) {
                    url_string = null;
                    //ex.printStackTrace();
                }

            }

        } else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
            System.out.println("User clicked cancel/close");
            return; // ends the loop without making any chages
        }

        if (url_string == null) {
            // We need to show an error saying that the url failed to parse
            Object[] message2 = { error, "Description:", description, "URL:", url };
            message = message2;

        }
    }

    // If you get here there is a valid reference URL and description
    Reference ref = new Reference(description.getText(), url.getText());

    if (current == null) {
        // Add it to the vuln
        vuln.addReference(ref);
        // Add it to the GUI 
        dlm.addElement(ref);
        System.out.println("Valid reference added: " + ref);
    } else {
        // Modify it in the vuln
        vuln.modifyReference(current, ref);
        // Update the GUI
        dlm.removeElement(current);
        dlm.addElement(ref);
        System.out.println("Valid reference modified: " + ref);
    }

}