Search in sources :

Example 1 with AbstractLayoutModel

use of org.csstudio.opibuilder.model.AbstractLayoutModel in project yamcs-studio by yamcs.

the class WidgetCreateCommand method redo.

@Override
public void redo() {
    if (newWidget instanceof AbstractLayoutModel && container.getLayoutWidget() != null) {
        MessageDialog.openError(null, "Creating widget failed", "There is already a layout widget in the container. " + "Please delete it before you can add a new layout widget.");
        return;
    }
    if (bounds != null) {
        newWidget.setLocation(bounds.x, bounds.y);
        if (bounds.width > 0 && bounds.height > 0)
            newWidget.setSize(bounds.width, bounds.height);
    }
    boolean autoName = false;
    for (AbstractWidgetModel child : container.getChildren()) {
        if (child.getName().equals(newWidget.getName()))
            autoName = true;
    }
    if (autoName) {
        Map<String, Integer> nameMap = new HashMap<String, Integer>();
        for (AbstractWidgetModel child : container.getChildren()) {
            String key = child.getName();
            int tailNo = 0;
            if (key.matches(".*_\\d+")) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                int i = key.lastIndexOf('_');
                tailNo = Integer.parseInt(key.substring(i + 1));
                key = key.substring(0, i);
            }
            if (nameMap.containsKey(key))
                nameMap.put(key, Math.max(nameMap.get(key) + 1, tailNo));
            else
                nameMap.put(key, 0);
        }
        String nameHead = newWidget.getName();
        if (nameHead.matches(".*_\\d+")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            nameHead = nameHead.substring(0, nameHead.lastIndexOf('_'));
        }
        newWidget.setName(nameHead + // $NON-NLS-1$
        "_" + (nameMap.get(nameHead) == null ? 0 : nameMap.get(nameHead) + 1));
    }
    container.addChild(index, newWidget);
    container.selectWidget(newWidget, append);
    if (newWidget instanceof AbstractContainerModel) {
        try {
            XMLUtil.fillLinkingContainers((AbstractContainerModel) newWidget);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : AbstractWidgetModel(org.csstudio.opibuilder.model.AbstractWidgetModel) AbstractContainerModel(org.csstudio.opibuilder.model.AbstractContainerModel) HashMap(java.util.HashMap) AbstractLayoutModel(org.csstudio.opibuilder.model.AbstractLayoutModel)

Aggregations

HashMap (java.util.HashMap)1 AbstractContainerModel (org.csstudio.opibuilder.model.AbstractContainerModel)1 AbstractLayoutModel (org.csstudio.opibuilder.model.AbstractLayoutModel)1 AbstractWidgetModel (org.csstudio.opibuilder.model.AbstractWidgetModel)1