d23670312f7b5372a4e90736dc9e13905f07ddc1
[public/netxms.git] / src / java / netxms-eclipse / FileManager / src / org / netxms / ui / eclipse / filemanager / actions / UploadFileToAgent.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2011 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */package org.netxms.ui.eclipse.filemanager.actions;
19
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.Set;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.IActionDelegate;
30 import org.eclipse.ui.IObjectActionDelegate;
31 import org.eclipse.ui.IWorkbenchPart;
32 import org.eclipse.ui.part.ViewPart;
33 import org.netxms.client.NXCSession;
34 import org.netxms.client.objects.AbstractObject;
35 import org.netxms.client.objects.Container;
36 import org.netxms.client.objects.EntireNetwork;
37 import org.netxms.client.objects.Node;
38 import org.netxms.client.objects.ServiceRoot;
39 import org.netxms.client.objects.Subnet;
40 import org.netxms.ui.eclipse.filemanager.Activator;
41 import org.netxms.ui.eclipse.filemanager.Messages;
42 import org.netxms.ui.eclipse.filemanager.dialogs.StartServerToAgentFileUploadDialog;
43 import org.netxms.ui.eclipse.jobs.ConsoleJob;
44 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
45
46 /**
47 * Action: upload file from server to agent(s)
48 *
49 */
50 public class UploadFileToAgent implements IObjectActionDelegate
51 {
52 private Shell shell;
53 private ViewPart viewPart;
54 private Set<Long> nodes = new HashSet<Long>();
55
56 /**
57 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
58 */
59 public void setActivePart(IAction action, IWorkbenchPart targetPart)
60 {
61 shell = targetPart.getSite().getShell();
62 viewPart = (targetPart instanceof ViewPart) ? (ViewPart)targetPart : null;
63 }
64
65 /**
66 * @see IActionDelegate#run(IAction)
67 */
68 public void run(IAction action)
69 {
70 final StartServerToAgentFileUploadDialog dlg = new StartServerToAgentFileUploadDialog(shell);
71 if (dlg.open() == Window.OK)
72 {
73 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
74 final Long[] nodeIdList = nodes.toArray(new Long[nodes.size()]);
75 new ConsoleJob(Messages.get().UploadFileToAgent_JobTitle, viewPart, Activator.PLUGIN_ID, null) {
76 @Override
77 protected String getErrorMessage()
78 {
79 return Messages.get().UploadFileToAgent_JobError;
80 }
81
82 @Override
83 protected void runInternal(IProgressMonitor monitor) throws Exception
84 {
85 String remoteFileName = dlg.getRemoteFileName();
86 if(!remoteFileName.isEmpty())
87 {
88 if(remoteFileName.endsWith("/") || remoteFileName.endsWith("\\"))
89 {
90 remoteFileName += dlg.getServerFile().getName();
91 }
92 }
93 else
94 {
95 remoteFileName = null;
96 }
97 for(int i = 0; i < nodeIdList.length; i++)
98 session.uploadFileToAgent(nodeIdList[i], dlg.getServerFile().getName(), remoteFileName, dlg.isCreateJobOnHold());
99 }
100 }.start();
101 }
102 }
103
104 /**
105 * @see IActionDelegate#selectionChanged(IAction, ISelection)
106 */
107 @SuppressWarnings("rawtypes")
108 public void selectionChanged(IAction action, ISelection selection)
109 {
110 nodes.clear();
111
112 if (!(selection instanceof IStructuredSelection))
113 {
114 action.setEnabled(false);
115 return;
116 }
117
118 Iterator it = ((IStructuredSelection)selection).iterator();
119 while(it.hasNext())
120 {
121 Object object = it.next();
122 if (object instanceof Node)
123 {
124 nodes.add(((Node)object).getObjectId());
125 }
126 else if ((object instanceof Container) || (object instanceof ServiceRoot) ||
127 (object instanceof Subnet) || (object instanceof EntireNetwork))
128 {
129 Set<AbstractObject> set = ((AbstractObject)object).getAllChilds(AbstractObject.OBJECT_NODE);
130 for(AbstractObject o : set)
131 nodes.add(o.getObjectId());
132 }
133 }
134
135 action.setEnabled(nodes.size() > 0);
136 }
137 }