54ef375b48f6561f0a115a6dae84d481cf4f58d1
[public/netxms.git] / src / java / netxms-eclipse / NXSL / src / org / netxms / ui / eclipse / nxsl / views / ScriptEditorView.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2010 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 */
19 package org.netxms.ui.eclipse.nxsl.views;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.PropertyResourceBundle;
24 import java.util.ResourceBundle;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.jface.action.IMenuManager;
30 import org.eclipse.jface.action.IToolBarManager;
31 import org.eclipse.jface.action.Separator;
32 import org.eclipse.jface.text.IFindReplaceTarget;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.ModifyEvent;
35 import org.eclipse.swt.events.ModifyListener;
36 import org.eclipse.swt.layout.FillLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.ui.IActionBars;
39 import org.eclipse.ui.IViewSite;
40 import org.eclipse.ui.PartInitException;
41 import org.eclipse.ui.commands.ActionHandler;
42 import org.eclipse.ui.handlers.IHandlerService;
43 import org.eclipse.ui.part.ViewPart;
44 import org.eclipse.ui.progress.UIJob;
45 import org.eclipse.ui.texteditor.FindReplaceAction;
46 import org.netxms.api.client.scripts.Script;
47 import org.netxms.api.client.scripts.ScriptLibraryManager;
48 import org.netxms.ui.eclipse.actions.RefreshAction;
49 import org.netxms.ui.eclipse.jobs.ConsoleJob;
50 import org.netxms.ui.eclipse.nxsl.Activator;
51 import org.netxms.ui.eclipse.nxsl.widgets.ScriptEditor;
52 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
53
54 /**
55 * Script editor view
56 *
57 */
58 public class ScriptEditorView extends ViewPart
59 {
60 public static final String ID = "org.netxms.ui.eclipse.nxsl.views.ScriptEditorView";
61
62 private ScriptLibraryManager scriptLibraryManager;
63 private ScriptEditor editor;
64 private long scriptId;
65 private String scriptName;
66 private RefreshAction actionRefresh;
67 private Action actionSave;
68 private FindReplaceAction actionFindReplace;
69
70 /* (non-Javadoc)
71 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
72 */
73 @Override
74 public void init(IViewSite site) throws PartInitException
75 {
76 super.init(site);
77
78 scriptLibraryManager = (ScriptLibraryManager)ConsoleSharedData.getSession();
79 scriptId = Long.parseLong(site.getSecondaryId());
80 }
81
82 /* (non-Javadoc)
83 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
84 */
85 @Override
86 public void createPartControl(Composite parent)
87 {
88 parent.setLayout(new FillLayout());
89
90 editor = new ScriptEditor(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
91 editor.getTextWidget().addModifyListener(new ModifyListener() {
92 @Override
93 public void modifyText(ModifyEvent e)
94 {
95 actionSave.setEnabled(true);
96 actionFindReplace.update();
97 }
98 });
99
100 createActions();
101 contributeToActionBars();
102 //createPopupMenu();
103
104 reloadScript();
105 }
106
107 /* (non-Javadoc)
108 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
109 */
110 @Override
111 public void setFocus()
112 {
113 editor.setFocus();
114 }
115
116 /**
117 * Get resource bundle
118 * @return
119 * @throws IOException
120 */
121 private ResourceBundle getResourceBundle() throws IOException
122 {
123 InputStream in = null;
124 String resource = "resource.properties";
125 ClassLoader loader = this.getClass().getClassLoader();
126 if (loader != null)
127 {
128 in = loader.getResourceAsStream(resource);
129 }
130 else
131 {
132 in = ClassLoader.getSystemResourceAsStream(resource);
133 }
134
135 return new PropertyResourceBundle(in);
136 }
137
138 /**
139 * Create actions
140 */
141 private void createActions()
142 {
143 try
144 {
145 actionFindReplace = new FindReplaceAction(getResourceBundle(), "actions.find_and_replace.", this);
146 IHandlerService hs = (IHandlerService)getSite().getService(IHandlerService.class);
147 hs.activateHandler("org.eclipse.ui.edit.findReplace", new ActionHandler(actionFindReplace));
148 }
149 catch(IOException e)
150 {
151 e.printStackTrace();
152 }
153
154 actionRefresh = new RefreshAction()
155 {
156 /* (non-Javadoc)
157 * @see org.eclipse.jface.action.Action#run()
158 */
159 @Override
160 public void run()
161 {
162 reloadScript();
163 }
164 };
165
166 actionSave = new Action()
167 {
168 /* (non-Javadoc)
169 * @see org.eclipse.jface.action.Action#run()
170 */
171 @Override
172 public void run()
173 {
174 saveScript();
175 }
176 };
177 actionSave.setText("&Save");
178 actionSave.setImageDescriptor(Activator.getImageDescriptor("icons/save_edit.gif"));
179 }
180
181 /**
182 * Contribute actions to action bar
183 */
184 private void contributeToActionBars()
185 {
186 IActionBars bars = getViewSite().getActionBars();
187 fillLocalPullDown(bars.getMenuManager());
188 fillLocalToolBar(bars.getToolBarManager());
189 }
190
191 /**
192 * Fill local pull-down menu
193 *
194 * @param manager
195 * Menu manager for pull-down menu
196 */
197 private void fillLocalPullDown(IMenuManager manager)
198 {
199 manager.add(actionFindReplace);
200 manager.add(actionSave);
201 manager.add(new Separator());
202 manager.add(actionRefresh);
203 }
204
205 /**
206 * Fill local tool bar
207 *
208 * @param manager
209 * Menu manager for local toolbar
210 */
211 private void fillLocalToolBar(IToolBarManager manager)
212 {
213 manager.add(actionSave);
214 manager.add(new Separator());
215 manager.add(actionRefresh);
216 }
217
218 /**
219 * Reload script from server
220 */
221 private void reloadScript()
222 {
223 new ConsoleJob("Loading script [" + scriptId + "]", this, Activator.PLUGIN_ID, null) {
224 @Override
225 protected String getErrorMessage()
226 {
227 return "Cannot load script with ID " + scriptId + " from server";
228 }
229
230 @Override
231 protected void runInternal(IProgressMonitor monitor) throws Exception
232 {
233 final Script script = scriptLibraryManager.getScript(scriptId);
234 new UIJob("Update script editor") {
235 @Override
236 public IStatus runInUIThread(IProgressMonitor monitor)
237 {
238 scriptName = script.getName();
239 setPartName("Edit Script - " + scriptName);
240 editor.setText(script.getSource());
241 editor.setModified(false);
242 actionSave.setEnabled(false);
243 actionFindReplace.update();
244 return Status.OK_STATUS;
245 }
246 }.schedule();
247 }
248 }.start();
249 }
250
251 /**
252 * Save script
253 */
254 private void saveScript()
255 {
256 final String source = editor.getText();
257 editor.getTextWidget().setEditable(false);
258 new ConsoleJob("Saving script to library", this, Activator.PLUGIN_ID, null) {
259 @Override
260 protected String getErrorMessage()
261 {
262 return "Cannot save script to database";
263 }
264
265 @Override
266 protected void runInternal(IProgressMonitor monitor) throws Exception
267 {
268 scriptLibraryManager.modifyScript(scriptId, scriptName, source);
269 new UIJob("Update script editor") {
270 @Override
271 public IStatus runInUIThread(IProgressMonitor monitor)
272 {
273 editor.setModified(false);
274 editor.getTextWidget().setEditable(true);
275 actionSave.setEnabled(false);
276 return Status.OK_STATUS;
277 }
278 }.schedule();
279 }
280 }.start();
281 }
282
283 /* (non-Javadoc)
284 * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
285 */
286 @SuppressWarnings("rawtypes")
287 @Override
288 public Object getAdapter(Class adapter)
289 {
290 Object object = super.getAdapter(adapter);
291 if (object != null)
292 {
293 return object;
294 }
295 if (adapter.equals(IFindReplaceTarget.class))
296 {
297 return editor.getFindReplaceTarget();
298 }
299 return null;
300 }
301 }