Search in sources :

Example 96 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class Document method recalcArrows.

// isValidModel
/**
 * remove all arrows, then add based on the beastObject inputs *
 */
void recalcArrows() {
    // remove all arrows
    for (int i = m_objects.size() - 1; i >= 0; i--) {
        Shape shape = m_objects.get(i);
        if (shape instanceof Arrow) {
            m_objects.remove(i);
        }
    }
    // build map for quick resolution of PluginShapes
    HashMap<BEASTInterface, BEASTObjectShape> map = new HashMap<>();
    for (Shape shape : m_objects) {
        if (shape instanceof BEASTObjectShape) {
            map.put(((BEASTObjectShape) shape).m_beastObject, (BEASTObjectShape) shape);
        }
    }
    // re-insert arrows, if any
    for (int i = m_objects.size() - 1; i >= 0; i--) {
        Shape shape = m_objects.get(i);
        if (shape instanceof BEASTObjectShape) {
            BEASTObjectShape headShape = ((BEASTObjectShape) shape);
            BEASTInterface beastObject = headShape.m_beastObject;
            try {
                List<Input<?>> inputs = beastObject.listInputs();
                for (Input<?> input : inputs) {
                    if (input.get() != null) {
                        if (input.get() instanceof BEASTInterface) {
                            BEASTObjectShape tailShape = map.get(input.get());
                            try {
                                Arrow arrow = new Arrow(tailShape, headShape, input.getName());
                                arrow.setID(getNewID(null));
                                m_objects.add(arrow);
                            } catch (Exception e) {
                            // ignore, can happen when not all inputs are to be shown
                            }
                        }
                        if (input.get() instanceof List<?>) {
                            for (Object o : (List<?>) input.get()) {
                                if (o != null && o instanceof BEASTInterface) {
                                    BEASTObjectShape tailShape = map.get(o);
                                    try {
                                        Arrow arrow = new Arrow(tailShape, headShape, input.getName());
                                        arrow.setID(getNewID(null));
                                        m_objects.add(arrow);
                                    } catch (Exception e) {
                                    // ignore, can happen when not all inputs are to be shown
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
// moveArrowsToBack();
}
Also used : HashMap(java.util.HashMap) Input(beast.core.Input) BEASTInterface(beast.core.BEASTInterface) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 97 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class InputEditorFactory method getAvailablePlugins.

// createInputEditor
/**
 * find beastObjects that could fit the input
 * @param input
 * @param parent beastObject containing the input
 * @param tabuList list of ids that are not allowed
 * @param doc
 * @return
 */
public List<String> getAvailablePlugins(Input<?> input, BEASTInterface parent, List<String> tabuList, BeautiDoc doc) {
    // List<String> beastObjectNames = BeautiConfig.getInputCandidates(parent, input);
    List<String> beastObjectNames = new ArrayList<>();
    if (beastObjectNames != null) {
        return beastObjectNames;
    }
    /* add ascendants to tabu list */
    if (tabuList == null) {
        tabuList = new ArrayList<>();
    }
    if (!doc.isExpertMode()) {
        for (BEASTInterface beastObject : BEASTObjectPanel.listAscendants(parent, doc.pluginmap.values())) {
            tabuList.add(beastObject.getID());
        }
    }
    // System.err.println(tabuList);
    /* collect all beastObjects in the system, that are not in the tabu list*/
    beastObjectNames = new ArrayList<>();
    for (BEASTInterface beastObject : doc.pluginmap.values()) {
        if (input.getType().isAssignableFrom(beastObject.getClass())) {
            boolean isTabu = false;
            if (tabuList != null) {
                for (String tabu : tabuList) {
                    if (tabu.equals(beastObject.getID())) {
                        isTabu = true;
                    }
                }
            }
            if (!isTabu) {
                try {
                    if (input.canSetValue(beastObject, parent)) {
                        beastObjectNames.add(beastObject.getID());
                    }
                } catch (Exception e) {
                // ignore
                }
            }
        }
    }
    /* add all beastObject-classes of type assignable to the input */
    if (doc.isExpertMode()) {
        List<String> classes = PackageManager.find(input.getType(), "beast");
        for (String className : classes) {
            try {
                Object o = Class.forName(className).newInstance();
                if (input.canSetValue(o, parent)) {
                    beastObjectNames.add("new " + className);
                }
            } catch (Exception e) {
            // ignore
            }
        }
    }
    return beastObjectNames;
}
Also used : ArrayList(java.util.ArrayList) BEASTInterface(beast.core.BEASTInterface) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 98 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class InputEditorFactory method createInputEditor.

public InputEditor createInputEditor(Input<?> input, int listItemNr, BEASTInterface beastObject, boolean addButtons, ExpandOption forceExpansion, ButtonStatus buttonStatus, InputEditor editor, BeautiDoc doc) throws NoSuchMethodException, SecurityException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    if (input.getType() == null) {
        input.determineClass(beastObject);
    }
    // Class<?> inputClass = input.get() != null ? input.get().getClass(): input.getType();
    Class<?> inputClass = input.getType();
    if (inputClass == null) {
        return null;
    }
    if (listItemNr >= 0) {
        inputClass = ((List<?>) input.get()).get(listItemNr).getClass();
    } else {
        if (input.get() != null && !input.get().getClass().equals(inputClass) && !(input.get() instanceof ArrayList)) {
            Log.trace.println(input.get().getClass() + " != " + inputClass);
            inputClass = input.get().getClass();
        }
    }
    // Log.trace.print(inputClass.getName() + " => ");
    InputEditor inputEditor;
    // check whether the super.editor has a custom method for creating an Editor
    if (editor != null) {
        try {
            String name = input.getName();
            name = new String(name.charAt(0) + "").toUpperCase() + name.substring(1);
            name = "create" + name + "Editor";
            Class<?> _class = editor.getClass();
            Method method = _class.getMethod(name);
            inputEditor = (InputEditor) method.invoke(editor);
            // Log.trace.println(inputEditor.getClass().getName() + " (CUSTOM EDITOR)");
            return inputEditor;
        } catch (Exception e) {
        // ignore
        }
    }
    if (listItemNr < 0 && (List.class.isAssignableFrom(inputClass) || (input.get() != null && input.get() instanceof List<?>))) {
        // handle list inputs
        if (listInputEditorMap.containsKey(inputClass)) {
            // use custom list input editor
            String inputEditorName = listInputEditorMap.get(inputClass);
            Constructor<?> con = Class.forName(inputEditorName).getConstructor(BeautiDoc.class);
            inputEditor = (InputEditor) con.newInstance(doc);
        // inputEditor = (InputEditor) Class.forName(inputEditor).newInstance();
        } else {
            // otherwise, use generic list editor
            inputEditor = new ListInputEditor(doc);
        }
        ((ListInputEditor) inputEditor).setButtonStatus(buttonStatus);
    } else if (input.possibleValues != null) {
        // handle enumeration inputs
        inputEditor = new EnumInputEditor(doc);
    } else {
        Class<?> inputClass2 = inputClass;
        while (inputClass2 != null && !inputEditorMap.containsKey(inputClass2)) {
            inputClass2 = inputClass2.getSuperclass();
        }
        if (inputClass2 == null) {
            inputEditor = new BEASTObjectInputEditor(doc);
        } else {
            // handle BEASTObject-input with custom input editors
            String inputEditorName = inputEditorMap.get(inputClass2);
            Constructor<?> con = Class.forName(inputEditorName).getConstructor(BeautiDoc.class);
            inputEditor = (InputEditor) con.newInstance(doc);
        }
    }
    // } else if (inputEditorMap.containsKey(inputClass)) {
    // // handle BEASTObject-input with custom input editors
    // String inputEditor = inputEditorMap.get(inputClass);
    // 
    // Constructor<?> con = Class.forName(inputEditor).getConstructor(BeautiDoc.class);
    // inputEditor = (InputEditor) con.newInstance(doc);
    // //inputEditor = (InputEditor) Class.forName(inputEditor).newInstance(doc);
    // //} else if (inputClass.isEnum()) {
    // //    inputEditor = new EnumInputEditor();
    // } else {
    // // assume it is a general BEASTObject, so create a default BEASTObject input editor
    // inputEditor = new PluginInputEditor(doc);
    // }
    String fullInputName = beastObject.getClass().getName() + "." + input.getName();
    // System.err.println(fullInputName);
    ExpandOption expandOption = forceExpansion;
    if (doc.beautiConfig.inlineBEASTObject.contains(fullInputName) || forceExpansion == ExpandOption.TRUE_START_COLLAPSED) {
        expandOption = ExpandOption.TRUE;
        // deal with initially collapsed beastObjects
        if (doc.beautiConfig.collapsedBEASTObjects.contains(fullInputName) || forceExpansion == ExpandOption.TRUE_START_COLLAPSED) {
            if (input.get() != null) {
                Object o = input.get();
                if (o instanceof ArrayList) {
                    for (Object o2 : (ArrayList<?>) o) {
                        if (o2 instanceof BEASTInterface) {
                            String id = ((BEASTInterface) o2).getID();
                            if (!ListInputEditor.g_initiallyCollapsedIDs.contains(id)) {
                                ListInputEditor.g_initiallyCollapsedIDs.add(id);
                                ListInputEditor.g_collapsedIDs.add(id);
                            }
                        }
                    }
                } else if (o instanceof BEASTInterface) {
                    String id = ((BEASTInterface) o).getID();
                    if (!ListInputEditor.g_initiallyCollapsedIDs.contains(id)) {
                        ListInputEditor.g_initiallyCollapsedIDs.add(id);
                        ListInputEditor.g_collapsedIDs.add(id);
                    }
                }
            }
        }
    }
    inputEditor.setDoc(doc);
    inputEditor.init(input, beastObject, listItemNr, expandOption, addButtons);
    ((JComponent) inputEditor).setBorder(BorderFactory.createEmptyBorder());
    inputEditor.getComponent().setVisible(true);
    // Log.trace.println(inputEditor.getClass().getName());
    return inputEditor;
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) JComponent(javax.swing.JComponent) Method(java.lang.reflect.Method) ExpandOption(beast.app.draw.InputEditor.ExpandOption) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) BEASTInterface(beast.core.BEASTInterface) BeautiDoc(beast.app.beauti.BeautiDoc)

Example 99 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class ListInputEditor method updateState.

// pluginSelector
protected void updateState() {
    for (int i = 0; i < ((List<?>) m_input.get()).size(); i++) {
        try {
            BEASTInterface beastObject = (BEASTInterface) ((List<?>) m_input.get()).get(i);
            beastObject.validateInputs();
            m_validateLabels.get(i).setVisible(false);
        } catch (IndexOutOfBoundsException e) {
        // happens when m_validateLabels is not large enough, so there is nothing to show
        } catch (Exception e) {
            // something went wrong, so show label if available
            if (m_validateLabels.size() > i) {
                m_validateLabels.get(i).setToolTipText(e.getMessage());
                m_validateLabels.get(i).setVisible(true);
            }
        }
    }
    validateInput();
    // this triggers properly re-layouting after an edit action
    setVisible(false);
    setVisible(true);
}
Also used : BEASTInterface(beast.core.BEASTInterface)

Example 100 with BEASTInterface

use of beast.core.BEASTInterface in project beast2 by CompEvol.

the class ListInputEditor method init.

/**
 * construct an editor consisting of
 * o a label
 * o a button for selecting another plug-in
 * o a set of buttons for adding, deleting, editing items in the list
 */
@Override
public void init(Input<?> input, BEASTInterface beastObject, int itemNr, ExpandOption isExpandOption, boolean addButtons) {
    m_bAddButtons = addButtons;
    m_bExpandOption = isExpandOption;
    m_input = input;
    m_beastObject = beastObject;
    this.itemNr = -1;
    addInputLabel();
    if (m_inputLabel != null) {
        m_inputLabel.setMaximumSize(new Dimension(m_inputLabel.getSize().width, 1000));
        m_inputLabel.setAlignmentY(1.0f);
        m_inputLabel.setVerticalAlignment(SwingConstants.TOP);
        m_inputLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    }
    m_listBox = Box.createVerticalBox();
    // list of inputs
    for (Object o : (List<?>) input.get()) {
        if (o instanceof BEASTInterface) {
            BEASTInterface beastObject2 = (BEASTInterface) o;
            addSingleItem(beastObject2);
        }
    }
    setLayout(new BorderLayout());
    add(m_listBox, BorderLayout.NORTH);
    buttonBox = Box.createHorizontalBox();
    if (m_buttonStatus == ButtonStatus.ALL || m_buttonStatus == ButtonStatus.ADD_ONLY) {
        addButton = new SmallButton("+", true);
        addButton.setName("+");
        addButton.setToolTipText("Add item to the list");
        addButton.addActionListener(e -> addItem());
        buttonBox.add(addButton);
        if (!doc.isExpertMode()) {
            // if nothing can be added, make add button invisible
            List<String> tabuList = new ArrayList<>();
            for (int i = 0; i < m_entries.size(); i++) {
                tabuList.add(m_entries.get(i).getText());
            }
            List<String> beastObjectNames = doc.getInputEditorFactory().getAvailablePlugins(m_input, m_beastObject, tabuList, doc);
            if (beastObjectNames.size() == 0) {
                addButton.setVisible(false);
            }
        }
    }
    // add validation label at the end of a list
    m_validateLabel = new SmallLabel("x", new Color(200, 0, 0));
    if (m_bAddButtons) {
        buttonBox.add(m_validateLabel);
        m_validateLabel.setVisible(true);
        validateInput();
    }
    buttonBox.add(Box.createHorizontalGlue());
    m_listBox.add(buttonBox);
    updateState();
// // RRB: is there a better way to ensure lists are not spaced out across all available space?
// JFrame frame = doc.getFrame();
// if (frame != null) {
// m_listBox.add(Box.createVerticalStrut(frame.getHeight() - 150));
// }
}
Also used : BorderLayout(java.awt.BorderLayout) Color(java.awt.Color) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BEASTInterface(beast.core.BEASTInterface) Dimension(java.awt.Dimension)

Aggregations

BEASTInterface (beast.core.BEASTInterface)111 ArrayList (java.util.ArrayList)43 List (java.util.List)27 IOException (java.io.IOException)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)18 SAXException (org.xml.sax.SAXException)18 NodeList (org.w3c.dom.NodeList)13 Input (beast.core.Input)12 MRCAPrior (beast.math.distributions.MRCAPrior)12 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 XMLParser (beast.util.XMLParser)11 TransformerException (javax.xml.transform.TransformerException)11 Alignment (beast.evolution.alignment.Alignment)10 XMLParserException (beast.util.XMLParserException)10 BEASTObject (beast.core.BEASTObject)9 Distribution (beast.core.Distribution)9 XMLProducer (beast.util.XMLProducer)9 CompoundDistribution (beast.core.util.CompoundDistribution)8 BranchRateModel (beast.evolution.branchratemodel.BranchRateModel)8