c629eaffb5c5c9d9730b0634f88d039e0e0a1e38
[public/netxms.git] / src / java / netxms-eclipse / AlarmViewer / src / org / netxms / ui / eclipse / alarmviewer / views / AlarmDetails.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2012 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.alarmviewer.views;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.action.Action;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.custom.CLabel;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.ui.IActionBars;
36 import org.eclipse.ui.IViewSite;
37 import org.eclipse.ui.PartInitException;
38 import org.eclipse.ui.forms.events.HyperlinkAdapter;
39 import org.eclipse.ui.forms.events.HyperlinkEvent;
40 import org.eclipse.ui.forms.widgets.FormToolkit;
41 import org.eclipse.ui.forms.widgets.ImageHyperlink;
42 import org.eclipse.ui.forms.widgets.ScrolledForm;
43 import org.eclipse.ui.forms.widgets.Section;
44 import org.eclipse.ui.forms.widgets.TableWrapData;
45 import org.eclipse.ui.forms.widgets.TableWrapLayout;
46 import org.eclipse.ui.model.WorkbenchLabelProvider;
47 import org.eclipse.ui.part.ViewPart;
48 import org.netxms.client.NXCSession;
49 import org.netxms.client.events.Alarm;
50 import org.netxms.client.events.AlarmNote;
51 import org.netxms.client.objects.GenericObject;
52 import org.netxms.ui.eclipse.actions.RefreshAction;
53 import org.netxms.ui.eclipse.alarmviewer.Activator;
54 import org.netxms.ui.eclipse.alarmviewer.Messages;
55 import org.netxms.ui.eclipse.alarmviewer.dialogs.EditCommentDialog;
56 import org.netxms.ui.eclipse.alarmviewer.widgets.AlarmCommentsEditor;
57 import org.netxms.ui.eclipse.console.resources.StatusDisplayInfo;
58 import org.netxms.ui.eclipse.jobs.ConsoleJob;
59 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
60 import org.netxms.ui.eclipse.shared.SharedIcons;
61 import org.netxms.ui.eclipse.tools.ImageCache;
62
63 /**
64 * Alarm comments
65 */
66 public class AlarmDetails extends ViewPart
67 {
68 public static final String ID = "org.netxms.ui.eclipse.alarmviewer.views.AlarmDetails"; //$NON-NLS-1$
69
70 private static final String[] stateImage = { "icons/outstanding.png", "icons/acknowledged.png", "icons/terminated.png" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
71 private static final String[] stateText = { Messages.AlarmListLabelProvider_AlarmState_Outstanding, Messages.AlarmListLabelProvider_AlarmState_Acknowledged, Messages.AlarmListLabelProvider_AlarmState_Terminated };
72
73 private NXCSession session;
74 private long alarmId;
75 private ImageCache imageCache;
76 private WorkbenchLabelProvider wbLabelProvider;
77 private FormToolkit toolkit;
78 private ScrolledForm form;
79 private CLabel alarmSeverity;
80 private CLabel alarmState;
81 private CLabel alarmSource;
82 private Label alarmText;
83 private Composite editorsArea;
84 private ImageHyperlink linkAddComment;
85 private Map<Long, AlarmCommentsEditor> editors = new HashMap<Long, AlarmCommentsEditor>();
86 private Action actionRefresh;
87
88 /* (non-Javadoc)
89 * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite)
90 */
91 @Override
92 public void init(IViewSite site) throws PartInitException
93 {
94 super.init(site);
95 session = (NXCSession)ConsoleSharedData.getSession();
96 wbLabelProvider = new WorkbenchLabelProvider();
97
98 try
99 {
100 alarmId = Long.parseLong(site.getSecondaryId());
101 }
102 catch(NumberFormatException e)
103 {
104 throw new PartInitException(Messages.AlarmComments_InternalError, e);
105 }
106
107 setPartName(getPartName() + " [" + Long.toString(alarmId) + "]"); //$NON-NLS-1$ //$NON-NLS-2$
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
112 */
113 @Override
114 public void createPartControl(Composite parent)
115 {
116 imageCache = new ImageCache();
117
118 toolkit = new FormToolkit(parent.getDisplay());
119 form = toolkit.createScrolledForm(parent);
120
121 TableWrapLayout layout = new TableWrapLayout();
122 layout.numColumns = 1;
123 form.getBody().setLayout(layout);
124
125 createAlarmDetailsSection();
126 createEditorsSection();
127
128 createActions();
129 contributeToActionBars();
130
131 refresh();
132 }
133
134
135 /**
136 * Create actions
137 */
138 private void createActions()
139 {
140 actionRefresh = new RefreshAction() {
141 @Override
142 public void run()
143 {
144 refresh();
145 }
146 };
147 }
148
149 /**
150 * Contribute actions to action bar
151 */
152 private void contributeToActionBars()
153 {
154 IActionBars bars = getViewSite().getActionBars();
155 fillLocalPullDown(bars.getMenuManager());
156 fillLocalToolBar(bars.getToolBarManager());
157 }
158
159 /**
160 * Fill local pull-down menu
161 *
162 * @param manager
163 * Menu manager for pull-down menu
164 */
165 private void fillLocalPullDown(IMenuManager manager)
166 {
167 manager.add(actionRefresh);
168 }
169
170 /**
171 * Fill local tool bar
172 *
173 * @param manager
174 * Menu manager for local toolbar
175 */
176 private void fillLocalToolBar(IToolBarManager manager)
177 {
178 manager.add(actionRefresh);
179 }
180
181 /**
182 * Create alarm details section
183 */
184 private void createAlarmDetailsSection()
185 {
186 final Section details = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
187 details.setText(Messages.AlarmComments_Details);
188 TableWrapData twd = new TableWrapData();
189 twd.grabHorizontal = true;
190 twd.align = TableWrapData.FILL;
191 details.setLayoutData(twd);
192
193 final Composite clientArea = toolkit.createComposite(details);
194 GridLayout layout = new GridLayout();
195 layout.numColumns = 2;
196 layout.makeColumnsEqualWidth = true;
197 clientArea.setLayout(layout);
198 details.setClient(clientArea);
199
200 alarmSeverity = new CLabel(clientArea, SWT.NONE);
201 toolkit.adapt(alarmSeverity);
202 GridData gd = new GridData();
203 gd.horizontalAlignment = SWT.FILL;
204 gd.grabExcessHorizontalSpace = true;
205 alarmSeverity.setLayoutData(gd);
206
207 alarmState = new CLabel(clientArea, SWT.NONE);
208 toolkit.adapt(alarmState);
209 gd = new GridData();
210 gd.horizontalAlignment = SWT.FILL;
211 gd.grabExcessHorizontalSpace = true;
212 alarmState.setLayoutData(gd);
213
214 alarmSource = new CLabel(clientArea, SWT.NONE);
215 toolkit.adapt(alarmSource);
216 gd = new GridData();
217 gd.horizontalAlignment = SWT.FILL;
218 gd.grabExcessHorizontalSpace = true;
219 gd.horizontalSpan = 2;
220 alarmSource.setLayoutData(gd);
221
222 alarmText = toolkit.createLabel(clientArea, "", SWT.WRAP); //$NON-NLS-1$
223 gd = new GridData();
224 gd.horizontalAlignment = SWT.FILL;
225 gd.grabExcessHorizontalSpace = true;
226 gd.horizontalSpan = 2;
227 alarmText.setLayoutData(gd);
228 }
229
230 /**
231 * Create comment list
232 */
233 private void createEditorsSection()
234 {
235 final Section details = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
236 details.setText(Messages.AlarmComments_Comments);
237 TableWrapData twd = new TableWrapData();
238 twd.grabHorizontal = true;
239 twd.align = TableWrapData.FILL;
240 details.setLayoutData(twd);
241
242 editorsArea = toolkit.createComposite(details);
243 GridLayout layout = new GridLayout();
244 editorsArea.setLayout(layout);
245 details.setClient(editorsArea);
246
247 linkAddComment = toolkit.createImageHyperlink(editorsArea, SWT.NONE);
248 linkAddComment.setImage(imageCache.add(Activator.getImageDescriptor("icons/new_comment.png"))); //$NON-NLS-1$
249 linkAddComment.setText(Messages.AlarmComments_AddCommentLink);
250 linkAddComment.addHyperlinkListener(new HyperlinkAdapter() {
251 @Override
252 public void linkActivated(HyperlinkEvent e)
253 {
254 addComment();
255 }
256 });
257 }
258
259 /* (non-Javadoc)
260 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
261 */
262 @Override
263 public void setFocus()
264 {
265 form.setFocus();
266 }
267
268 /**
269 * Refresh view
270 */
271 private void refresh()
272 {
273 new ConsoleJob(Messages.AlarmComments_GetComments, this, Activator.PLUGIN_ID, null) {
274 @Override
275 protected void runInternal(IProgressMonitor monitor) throws Exception
276 {
277 final Alarm alarm = session.getAlarm(alarmId);
278 final List<AlarmNote> comments = session.getAlarmNotes(alarmId);
279 runInUIThread(new Runnable() {
280 @Override
281 public void run()
282 {
283 updateAlarmDetails(alarm);
284
285 for(AlarmCommentsEditor e : editors.values())
286 e.dispose();
287
288 for(AlarmNote n : comments)
289 editors.put(n.getId(), createEditor(n));
290
291 updateLayout();
292 }
293 });
294 }
295
296 @Override
297 protected String getErrorMessage()
298 {
299 return Messages.AlarmComments_GetError;
300 }
301 }.start();
302 }
303
304 /**
305 * Update layout after internal change
306 */
307 private void updateLayout()
308 {
309 form.reflow(true);
310 form.getParent().layout(true, true);
311 }
312
313 /**
314 * Create comment editor widget
315 *
316 * @param note alarm note associated with this widget
317 * @return
318 */
319 private AlarmCommentsEditor createEditor(AlarmNote note)
320 {
321 final AlarmCommentsEditor e = new AlarmCommentsEditor(editorsArea, toolkit, imageCache, note);
322 toolkit.adapt(e);
323 GridData gd = new GridData();
324 gd.horizontalAlignment = SWT.FILL;
325 gd.grabExcessHorizontalSpace = true;
326 e.setLayoutData(gd);
327 e.moveBelow(linkAddComment);
328 return e;
329 }
330
331 /**
332 * Add new comment
333 */
334 private void addComment()
335 {
336 final EditCommentDialog dlg = new EditCommentDialog(getSite().getShell());
337 if (dlg.open() != Window.OK)
338 return;
339
340 new ConsoleJob(Messages.AlarmComments_AddCommentJob, this, Activator.PLUGIN_ID, null) {
341 @Override
342 protected void runInternal(IProgressMonitor monitor) throws Exception
343 {
344 session.updateAlarmNote(alarmId, 0, dlg.getText());
345 runInUIThread(new Runnable() {
346 @Override
347 public void run()
348 {
349 refresh();
350 }
351 });
352 }
353
354 @Override
355 protected String getErrorMessage()
356 {
357 return Messages.AlarmComments_AddError;
358 }
359 }.start();
360 }
361
362 /**
363 * Update alarm details
364 *
365 * @param alarm
366 */
367 private void updateAlarmDetails(Alarm alarm)
368 {
369 alarmSeverity.setImage(StatusDisplayInfo.getStatusImage(alarm.getCurrentSeverity()));
370 alarmSeverity.setText(StatusDisplayInfo.getStatusText(alarm.getCurrentSeverity()));
371
372 alarmState.setImage(imageCache.add(Activator.getImageDescriptor(stateImage[alarm.getState()])));
373 alarmState.setText(stateText[alarm.getState()]);
374
375 GenericObject object = session.findObjectById(alarm.getSourceObjectId());
376 alarmSource.setImage((object != null) ? wbLabelProvider.getImage(object) : SharedIcons.IMG_UNKNOWN_OBJECT);
377 alarmSource.setText((object != null) ? object.getObjectName() : ("[" + Long.toString(alarm.getSourceObjectId()) + "]")); //$NON-NLS-1$ //$NON-NLS-2$
378
379 alarmText.setText(alarm.getMessage());
380 }
381
382 /* (non-Javadoc)
383 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
384 */
385 @Override
386 public void dispose()
387 {
388 imageCache.dispose();
389 wbLabelProvider.dispose();
390 super.dispose();
391 }
392 }