Search in sources :

Example 1 with ObjectContext

use of org.netxms.ui.eclipse.objects.ObjectContext in project netxms by netxms.

the class ObjectToolsDynamicMenu method fill.

/* (non-Javadoc)
	 * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
	 */
@Override
public void fill(Menu menu, int index) {
    Object selection = evalService.getCurrentState().getVariable(ISources.ACTIVE_MENU_SELECTION_NAME);
    if ((selection == null) || !(selection instanceof IStructuredSelection))
        return;
    final Set<ObjectContext> nodes = buildNodeSet((IStructuredSelection) selection);
    final Menu toolsMenu = new Menu(menu);
    final ImageCache imageCache = new ImageCache();
    toolsMenu.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            imageCache.dispose();
        }
    });
    ObjectTool[] tools = ObjectToolsCache.getInstance().getTools();
    Arrays.sort(tools, new Comparator<ObjectTool>() {

        @Override
        public int compare(ObjectTool arg0, ObjectTool arg1) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            return arg0.getName().replace("&", "").compareToIgnoreCase(arg1.getName().replace("&", ""));
        }
    });
    Map<String, Menu> menus = new HashMap<String, Menu>();
    int added = 0;
    for (int i = 0; i < tools.length; i++) {
        boolean enabled = (tools[i].getFlags() & ObjectTool.DISABLED) == 0;
        if (enabled && ObjectToolExecutor.isToolAllowed(tools[i], nodes) && ObjectToolExecutor.isToolApplicable(tools[i], nodes)) {
            // $NON-NLS-1$
            String[] path = tools[i].getName().split("\\-\\>");
            Menu rootMenu = toolsMenu;
            for (int j = 0; j < path.length - 1; j++) {
                // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                final String key = rootMenu.hashCode() + "@" + path[j].replace("&", "");
                Menu currMenu = menus.get(key);
                if (currMenu == null) {
                    currMenu = new Menu(rootMenu);
                    MenuItem item = new MenuItem(rootMenu, SWT.CASCADE);
                    item.setText(path[j]);
                    item.setMenu(currMenu);
                    menus.put(key, currMenu);
                }
                rootMenu = currMenu;
            }
            final MenuItem item = new MenuItem(rootMenu, SWT.PUSH);
            item.setText(path[path.length - 1]);
            ImageDescriptor icon = ObjectToolsCache.getInstance().findIcon(tools[i].getId());
            if (icon != null)
                item.setImage(imageCache.add(icon));
            item.setData(tools[i]);
            item.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    ObjectToolExecutor.execute(nodes, (ObjectTool) item.getData());
                }
            });
            added++;
        }
    }
    if (added > 0) {
        MenuItem toolsMenuItem = new MenuItem(menu, SWT.CASCADE, index);
        toolsMenuItem.setText(Messages.get().ObjectToolsDynamicMenu_TopLevelLabel);
        toolsMenuItem.setMenu(toolsMenu);
    } else {
        toolsMenu.dispose();
    }
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) HashMap(java.util.HashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DisposeEvent(org.eclipse.swt.events.DisposeEvent) ImageCache(org.netxms.ui.eclipse.tools.ImageCache) SelectionEvent(org.eclipse.swt.events.SelectionEvent) AbstractObject(org.netxms.client.objects.AbstractObject) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) ObjectContext(org.netxms.ui.eclipse.objects.ObjectContext) Menu(org.eclipse.swt.widgets.Menu) ObjectTool(org.netxms.client.objecttools.ObjectTool)

Example 2 with ObjectContext

use of org.netxms.ui.eclipse.objects.ObjectContext in project netxms by netxms.

the class ObjectToolsDynamicMenu method buildNodeSet.

/**
 * Build node set from selection
 *
 * @param selection
 * @return
 */
private Set<ObjectContext> buildNodeSet(IStructuredSelection selection) {
    final Set<ObjectContext> nodes = new HashSet<ObjectContext>();
    final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
    for (Object o : selection.toList()) {
        if (o instanceof AbstractNode) {
            nodes.add(new ObjectContext((AbstractNode) o, null));
        } else if ((o instanceof Container) || (o instanceof ServiceRoot) || (o instanceof Subnet) || (o instanceof Cluster)) {
            for (AbstractObject n : ((AbstractObject) o).getAllChilds(AbstractObject.OBJECT_NODE)) nodes.add(new ObjectContext((AbstractNode) n, null));
        } else if (o instanceof Alarm) {
            AbstractNode n = (AbstractNode) session.findObjectById(((Alarm) o).getSourceObjectId(), AbstractNode.class);
            if (n != null)
                nodes.add(new ObjectContext(n, (Alarm) o));
        } else if (o instanceof ObjectWrapper) {
            AbstractObject n = ((ObjectWrapper) o).getObject();
            if ((n != null) && (n instanceof AbstractNode))
                nodes.add(new ObjectContext((AbstractNode) n, null));
        }
    }
    return nodes;
}
Also used : NXCSession(org.netxms.client.NXCSession) AbstractNode(org.netxms.client.objects.AbstractNode) Cluster(org.netxms.client.objects.Cluster) ServiceRoot(org.netxms.client.objects.ServiceRoot) Container(org.netxms.client.objects.Container) AbstractObject(org.netxms.client.objects.AbstractObject) Alarm(org.netxms.client.events.Alarm) ObjectWrapper(org.netxms.ui.eclipse.objects.ObjectWrapper) AbstractObject(org.netxms.client.objects.AbstractObject) ObjectContext(org.netxms.ui.eclipse.objects.ObjectContext) Subnet(org.netxms.client.objects.Subnet) HashSet(java.util.HashSet)

Example 3 with ObjectContext

use of org.netxms.ui.eclipse.objects.ObjectContext 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 4 with ObjectContext

use of org.netxms.ui.eclipse.objects.ObjectContext in project netxms by netxms.

the class GraphTemplateCache method execute.

public static void execute(final AbstractNode node, final GraphSettings data, final DciValue[] values, final Display disp) throws IOException, NXCException {
    final NXCSession session = ConsoleSharedData.getSession();
    List<String> textsToExpand = new ArrayList<String>();
    textsToExpand.add(data.getTitle());
    String name = session.substitureMacross(new ObjectContext(node, null), textsToExpand, new HashMap<String, String>()).get(0);
    final GraphSettings result = new GraphSettings(data, name);
    ChartDciConfig[] conf = result.getDciList();
    final HashSet<ChartDciConfig> newList = new HashSet<ChartDciConfig>();
    int foundByDescription = -1;
    int foundDCICount = 0;
    // parse config and compare name as regexp and then compare description
    for (int i = 0; i < conf.length; i++) {
        Pattern namePattern = Pattern.compile(conf[i].dciName);
        Pattern descriptionPattern = Pattern.compile(conf[i].dciDescription);
        int j;
        for (j = 0; j < values.length; j++) {
            if (!conf[i].dciName.isEmpty() && namePattern.matcher(values[j].getName()).find()) {
                newList.add(new ChartDciConfig(values[j]));
                foundDCICount++;
                if (!conf[i].multiMatch)
                    break;
            }
            if (!conf[i].dciDescription.isEmpty() && descriptionPattern.matcher(values[j].getDescription()).find()) {
                foundByDescription = j;
                if (conf[i].multiMatch) {
                    newList.add(new ChartDciConfig(values[j]));
                    foundDCICount++;
                }
            }
        }
        if (!conf[i].multiMatch && j == values.length && foundByDescription >= 0) {
            foundDCICount++;
            newList.add(new ChartDciConfig(values[foundByDescription]));
        }
    }
    if (foundDCICount > 0) {
        disp.syncExec(new Runnable() {

            @Override
            public void run() {
                result.setDciList(newList.toArray(new ChartDciConfig[newList.size()]));
                showPredefinedGraph(result, node.getObjectId());
            }
        });
    } else {
        disp.syncExec(new Runnable() {

            @Override
            public void run() {
                MessageDialogHelper.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Graph creation from template error", "None of template graphs DCI were found on a node.");
            }
        });
    }
}
Also used : Pattern(java.util.regex.Pattern) NXCSession(org.netxms.client.NXCSession) HashMap(java.util.HashMap) ChartDciConfig(org.netxms.client.datacollection.ChartDciConfig) ArrayList(java.util.ArrayList) GraphSettings(org.netxms.client.datacollection.GraphSettings) ObjectContext(org.netxms.ui.eclipse.objects.ObjectContext) HashSet(java.util.HashSet)

Example 5 with ObjectContext

use of org.netxms.ui.eclipse.objects.ObjectContext in project netxms by netxms.

the class Commands method onObjectChange.

/* (non-Javadoc)
	 * @see org.netxms.ui.eclipse.objectview.objecttabs.elements.OverviewPageElement#onObjectChange()
	 */
@Override
protected void onObjectChange() {
    commandBox.deleteAll(false);
    if (getObject() instanceof AbstractNode) {
        ObjectTool[] tools = ObjectToolsCache.getInstance().getTools();
        for (final ObjectTool tool : tools) {
            if (!tool.isVisibleInCommands() || !tool.isEnabled() || !tool.isApplicableForNode((AbstractNode) getObject()))
                continue;
            final Set<ObjectContext> nodes = new HashSet<ObjectContext>(1);
            nodes.add(new ObjectContext((AbstractNode) getObject(), null));
            if (!ObjectToolExecutor.isToolAllowed(tool, nodes))
                continue;
            final Action action = new Action(tool.getCommandDisplayName()) {

                @Override
                public void run() {
                    ObjectToolExecutor.execute(nodes, tool);
                }
            };
            ImageDescriptor icon = ObjectToolsCache.getInstance().findIcon(tool.getId());
            if (icon != null)
                action.setImageDescriptor(icon);
            commandBox.add(action, false);
        }
    } else if (getObject() instanceof Interface) {
        commandBox.add(actionWakeup, false);
    }
    commandBox.rebuild();
}
Also used : Action(org.eclipse.jface.action.Action) AbstractNode(org.netxms.client.objects.AbstractNode) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) ObjectContext(org.netxms.ui.eclipse.objects.ObjectContext) Interface(org.netxms.client.objects.Interface) ObjectTool(org.netxms.client.objecttools.ObjectTool) HashSet(java.util.HashSet)

Aggregations

ObjectContext (org.netxms.ui.eclipse.objects.ObjectContext)5 HashSet (java.util.HashSet)3 NXCSession (org.netxms.client.NXCSession)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)2 AbstractNode (org.netxms.client.objects.AbstractNode)2 AbstractObject (org.netxms.client.objects.AbstractObject)2 ObjectTool (org.netxms.client.objecttools.ObjectTool)2 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Action (org.eclipse.jface.action.Action)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 DisposeEvent (org.eclipse.swt.events.DisposeEvent)1 DisposeListener (org.eclipse.swt.events.DisposeListener)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 Menu (org.eclipse.swt.widgets.Menu)1 MenuItem (org.eclipse.swt.widgets.MenuItem)1