Search in sources :

Example 1 with InputField

use of org.netxms.client.objecttools.InputField in project netxms by netxms.

the class ObjectToolExecutor method execute.

/**
 * Execute object tool on node set
 *
 * @param tool Object tool
 */
public static void execute(final Set<ObjectContext> nodes, final ObjectTool tool) {
    final Map<String, String> inputValues;
    final InputField[] fields = tool.getInputFields();
    if (fields.length > 0) {
        Arrays.sort(fields, new Comparator<InputField>() {

            @Override
            public int compare(InputField f1, InputField f2) {
                return f1.getSequence() - f2.getSequence();
            }
        });
        inputValues = readInputFields(fields);
        if (inputValues == null)
            // cancelled
            return;
    } else {
        inputValues = new HashMap<String, String>(0);
    }
    final NXCSession session = ConsoleSharedData.getSession();
    new ConsoleJob(Messages.get().ObjectToolExecutor_JobName, null, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(IProgressMonitor monitor) throws Exception {
            List<String> expandedText = null;
            if ((tool.getFlags() & ObjectTool.ASK_CONFIRMATION) != 0) {
                String message = tool.getConfirmationText();
                if (nodes.size() == 1) {
                    // Expand message and action for 1 node, otherwise expansion occurs after confirmation
                    List<String> textToExpand = new ArrayList<String>();
                    textToExpand.add(tool.getConfirmationText());
                    if (tool.getToolType() == ObjectTool.TYPE_URL || tool.getToolType() == ObjectTool.TYPE_LOCAL_COMMAND) {
                        textToExpand.add(tool.getData());
                    }
                    ObjectContext node = nodes.iterator().next();
                    expandedText = session.substitureMacross(node, textToExpand, inputValues);
                    message = expandedText.remove(0);
                } else {
                    ObjectContext node = nodes.iterator().next();
                    message = node.substituteMacrosForMultiNodes(message, inputValues);
                }
                ConfirmationRunnable runnable = new ConfirmationRunnable(message);
                getDisplay().syncExec(runnable);
                if (!runnable.isConfirmed())
                    return;
                if (tool.getToolType() == ObjectTool.TYPE_URL || tool.getToolType() == ObjectTool.TYPE_LOCAL_COMMAND) {
                    expandedText = session.substitureMacross(nodes.toArray(new ObjectContext[nodes.size()]), tool.getData(), inputValues);
                }
            } else {
                if (tool.getToolType() == ObjectTool.TYPE_URL || tool.getToolType() == ObjectTool.TYPE_LOCAL_COMMAND) {
                    expandedText = session.substitureMacross(nodes.toArray(new ObjectContext[nodes.size()]), tool.getData(), inputValues);
                }
            }
            // Check if password validation needed
            boolean validationNeeded = false;
            for (int i = 0; i < fields.length; i++) if (fields[i].getOptions().validatePassword) {
                validationNeeded = true;
                break;
            }
            if (validationNeeded) {
                for (int i = 0; i < fields.length; i++) {
                    if ((fields[i].getType() == InputFieldType.PASSWORD) && (fields[i].getOptions().validatePassword)) {
                        boolean valid = session.validateUserPassword(inputValues.get(fields[i].getName()));
                        if (!valid) {
                            final String fieldName = fields[i].getDisplayName();
                            getDisplay().syncExec(new Runnable() {

                                @Override
                                public void run() {
                                    MessageDialogHelper.openError(null, Messages.get().ObjectToolExecutor_ErrorTitle, String.format(Messages.get().ObjectToolExecutor_ErrorText, fieldName));
                                }
                            });
                            return;
                        }
                    }
                }
            }
            int i = 0;
            for (final ObjectContext n : nodes) {
                if (tool.getToolType() == ObjectTool.TYPE_URL || tool.getToolType() == ObjectTool.TYPE_LOCAL_COMMAND) {
                    final String tmp = expandedText.get(i++);
                    getDisplay().syncExec(new Runnable() {

                        @Override
                        public void run() {
                            executeOnNode(n, tool, inputValues, tmp);
                        }
                    });
                } else {
                    getDisplay().syncExec(new Runnable() {

                        @Override
                        public void run() {
                            executeOnNode(n, tool, inputValues, null);
                        }
                    });
                }
            }
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().ObjectToolExecutor_PasswordValidationFailed;
        }

        class ConfirmationRunnable implements Runnable {

            private boolean confirmed;

            private String message;

            public ConfirmationRunnable(String message) {
                this.message = message;
            }

            @Override
            public void run() {
                confirmed = MessageDialogHelper.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.get().ObjectToolsDynamicMenu_ConfirmExec, message);
            }

            boolean isConfirmed() {
                return confirmed;
            }
        }
    }.start();
}
Also used : InputField(org.netxms.client.objecttools.InputField) NXCSession(org.netxms.client.NXCSession) PartInitException(org.eclipse.ui.PartInitException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ArrayList(java.util.ArrayList) List(java.util.List) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) ObjectContext(org.netxms.ui.eclipse.objects.ObjectContext)

Example 2 with InputField

use of org.netxms.client.objecttools.InputField in project netxms by netxms.

the class InputFields method moveFieldUp.

/**
 * Move selected field up
 */
private void moveFieldUp() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.size() != 1)
        return;
    InputField f = (InputField) selection.getFirstElement();
    if (f.getSequence() > 0) {
        updateSequence(f.getSequence() - 1, 1);
        f.setSequence(f.getSequence() - 1);
        viewer.refresh();
    }
}
Also used : InputField(org.netxms.client.objecttools.InputField) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 3 with InputField

use of org.netxms.client.objecttools.InputField in project netxms by netxms.

the class InputFields method createContents.

/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
@Override
protected Control createContents(Composite parent) {
    objectTool = (ObjectToolDetails) getElement().getAdapter(ObjectToolDetails.class);
    for (InputField f : objectTool.getInputFields()) fields.add(new InputField(f));
    Composite dialogArea = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    layout.verticalSpacing = WidgetHelper.DIALOG_SPACING;
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    layout.numColumns = 2;
    dialogArea.setLayout(layout);
    viewer = new TableViewer(dialogArea, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
    GridData gd = new GridData();
    gd.horizontalAlignment = SWT.FILL;
    gd.grabExcessHorizontalSpace = true;
    gd.verticalAlignment = SWT.FILL;
    gd.grabExcessVerticalSpace = true;
    gd.horizontalSpan = 2;
    gd.widthHint = WidgetHelper.BUTTON_WIDTH_HINT * 6;
    viewer.getTable().setLayoutData(gd);
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(new InputFieldLabelProvider());
    viewer.setComparator(new ViewerComparator() {

        @Override
        public int compare(Viewer viewer, Object e1, Object e2) {
            return ((InputField) e1).getSequence() - ((InputField) e2).getSequence();
        }
    });
    setupTableColumns();
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            buttonEdit.setEnabled(selection.size() == 1);
            buttonRemove.setEnabled(selection.size() > 0);
            buttonUp.setEnabled(selection.size() == 1);
            buttonDown.setEnabled(selection.size() == 1);
        }
    });
    viewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent event) {
            editField();
        }
    });
    viewer.setInput(fields.toArray());
    Composite buttonsLeft = new Composite(dialogArea, SWT.NONE);
    RowLayout buttonLayout = new RowLayout();
    buttonLayout.type = SWT.HORIZONTAL;
    buttonLayout.pack = false;
    buttonLayout.marginWidth = 0;
    buttonsLeft.setLayout(buttonLayout);
    gd = new GridData();
    gd.horizontalAlignment = SWT.LEFT;
    gd.verticalIndent = WidgetHelper.OUTER_SPACING - WidgetHelper.INNER_SPACING;
    buttonsLeft.setLayoutData(gd);
    buttonUp = new Button(buttonsLeft, SWT.PUSH);
    buttonUp.setText(Messages.get().InputFields_Up);
    buttonUp.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            moveFieldUp();
        }
    });
    RowData rd = new RowData();
    rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
    buttonUp.setLayoutData(rd);
    buttonUp.setEnabled(false);
    buttonDown = new Button(buttonsLeft, SWT.PUSH);
    buttonDown.setText(Messages.get().InputFields_Down);
    buttonDown.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            moveFieldDown();
        }
    });
    rd = new RowData();
    rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
    buttonDown.setLayoutData(rd);
    buttonDown.setEnabled(false);
    Composite buttonsRight = new Composite(dialogArea, SWT.NONE);
    buttonLayout = new RowLayout();
    buttonLayout.type = SWT.HORIZONTAL;
    buttonLayout.pack = false;
    buttonLayout.marginWidth = 0;
    buttonsRight.setLayout(buttonLayout);
    gd = new GridData();
    gd.horizontalAlignment = SWT.RIGHT;
    gd.verticalIndent = WidgetHelper.OUTER_SPACING - WidgetHelper.INNER_SPACING;
    buttonsRight.setLayoutData(gd);
    buttonAdd = new Button(buttonsRight, SWT.PUSH);
    buttonAdd.setText(Messages.get().Columns_Add);
    buttonAdd.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            addField();
        }
    });
    rd = new RowData();
    rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
    buttonAdd.setLayoutData(rd);
    buttonEdit = new Button(buttonsRight, SWT.PUSH);
    buttonEdit.setText(Messages.get().Columns_Edit);
    buttonEdit.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            editField();
        }
    });
    rd = new RowData();
    rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
    buttonEdit.setLayoutData(rd);
    buttonEdit.setEnabled(false);
    buttonRemove = new Button(buttonsRight, SWT.PUSH);
    buttonRemove.setText(Messages.get().Columns_Delete);
    buttonRemove.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            removeField();
        }
    });
    rd = new RowData();
    rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
    buttonRemove.setLayoutData(rd);
    buttonRemove.setEnabled(false);
    return dialogArea;
}
Also used : InputField(org.netxms.client.objecttools.InputField) Composite(org.eclipse.swt.widgets.Composite) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) TableViewer(org.eclipse.jface.viewers.TableViewer) Viewer(org.eclipse.jface.viewers.Viewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) InputFieldLabelProvider(org.netxms.ui.eclipse.objecttools.propertypages.helpers.InputFieldLabelProvider) GridLayout(org.eclipse.swt.layout.GridLayout) RowData(org.eclipse.swt.layout.RowData) Button(org.eclipse.swt.widgets.Button) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) RowLayout(org.eclipse.swt.layout.RowLayout) GridData(org.eclipse.swt.layout.GridData) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) SelectionEvent(org.eclipse.swt.events.SelectionEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 4 with InputField

use of org.netxms.client.objecttools.InputField in project netxms by netxms.

the class InputFields method moveFieldDown.

/**
 * Move selected field down
 */
private void moveFieldDown() {
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    if (selection.size() != 1)
        return;
    InputField f = (InputField) selection.getFirstElement();
    if (f.getSequence() < fields.size() - 1) {
        updateSequence(f.getSequence() + 1, -1);
        f.setSequence(f.getSequence() + 1);
        viewer.refresh();
    }
}
Also used : InputField(org.netxms.client.objecttools.InputField) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 5 with InputField

use of org.netxms.client.objecttools.InputField in project netxms by netxms.

the class ObjectToolsTest method testCreateAndDelete.

public void testCreateAndDelete() throws Exception {
    final NXCSession session = connect();
    long id = session.generateObjectToolId();
    assertFalse(id == 0);
    System.out.println("Object tool ID generated: " + id);
    ObjectToolDetails td = new ObjectToolDetails(id, ObjectTool.TYPE_LOCAL_COMMAND, "UnitTest");
    td.setData("ping %{10%(not a field)} -t %(address) -s %(size)");
    td.addInputField(new InputField("size", InputFieldType.NUMBER, "Size (bytes)", null));
    td.addInputField(new InputField("unused", InputFieldType.PASSWORD, "Unused field", null));
    session.modifyObjectTool(td);
    td = session.getObjectToolDetails(id);
    for (InputField f : td.getInputFields()) System.out.println(f);
    session.deleteObjectTool(id);
    session.disconnect();
}
Also used : InputField(org.netxms.client.objecttools.InputField) ObjectToolDetails(org.netxms.client.objecttools.ObjectToolDetails)

Aggregations

InputField (org.netxms.client.objecttools.InputField)7 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)5 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ArrayContentProvider (org.eclipse.jface.viewers.ArrayContentProvider)1 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)1 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 Viewer (org.eclipse.jface.viewers.Viewer)1 ViewerComparator (org.eclipse.jface.viewers.ViewerComparator)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 SelectionListener (org.eclipse.swt.events.SelectionListener)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 RowData (org.eclipse.swt.layout.RowData)1 RowLayout (org.eclipse.swt.layout.RowLayout)1