2f5d69ba4e7dc394de4e8e0f2adf8884ea81a20f
[public/netxms.git] / src / java / netxms-eclipse / ObjectManager / src / org / netxms / ui / eclipse / objectmanager / propertypages / CustomAttributes.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 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.objectmanager.propertypages;
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.jface.viewers.ArrayContentProvider;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IDoubleClickListener;
30 import org.eclipse.jface.viewers.ISelectionChangedListener;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.jface.viewers.Viewer;
34 import org.eclipse.jface.viewers.ViewerFilter;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionEvent;
38 import org.eclipse.swt.events.SelectionListener;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.layout.RowData;
42 import org.eclipse.swt.layout.RowLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Event;
47 import org.eclipse.ui.dialogs.PropertyPage;
48 import org.netxms.client.NXCObjectModificationData;
49 import org.netxms.client.NXCSession;
50 import org.netxms.client.objects.AbstractObject;
51 import org.netxms.ui.eclipse.jobs.ConsoleJob;
52 import org.netxms.ui.eclipse.objectmanager.Activator;
53 import org.netxms.ui.eclipse.objectmanager.Messages;
54 import org.netxms.ui.eclipse.objectmanager.dialogs.AttributeEditDialog;
55 import org.netxms.ui.eclipse.objectmanager.propertypages.helpers.AttrListLabelProvider;
56 import org.netxms.ui.eclipse.objectmanager.propertypages.helpers.AttrViewerComparator;
57 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
58 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
59 import org.netxms.ui.eclipse.tools.WidgetHelper;
60 import org.netxms.ui.eclipse.widgets.SortableTableViewer;
61
62 /**
63 * "Custom Attributes" property page
64 */
65 public class CustomAttributes extends PropertyPage
66 {
67 public static final int COLUMN_NAME = 0;
68 public static final int COLUMN_VALUE = 1;
69
70 private AbstractObject object = null;
71 private SortableTableViewer viewer;
72 private Button addButton;
73 private Button editButton;
74 private Button deleteButton;
75 private Map<String, String> attributes = null;
76 private boolean isModified = false;
77
78 /* (non-Javadoc)
79 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
80 */
81 @Override
82 protected Control createContents(Composite parent)
83 {
84 Composite dialogArea = new Composite(parent, SWT.NONE);
85
86 object = (AbstractObject)getElement().getAdapter(AbstractObject.class);
87 if (object == null) // Paranoid check
88 return dialogArea;
89
90 GridLayout layout = new GridLayout();
91 layout.verticalSpacing = WidgetHelper.OUTER_SPACING;
92 layout.marginWidth = 0;
93 layout.marginHeight = 0;
94 dialogArea.setLayout(layout);
95
96 final String[] columnNames = { Messages.get().CustomAttributes_Name, Messages.get().CustomAttributes_Value };
97 final int[] columnWidths = { 150, 250 };
98 viewer = new SortableTableViewer(dialogArea, columnNames, columnWidths, 0, SWT.UP,
99 SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
100 viewer.setContentProvider(new ArrayContentProvider());
101 viewer.setLabelProvider(new AttrListLabelProvider());
102 viewer.setComparator(new AttrViewerComparator());
103
104 attributes = new HashMap<String, String>(object.getCustomAttributes());
105 viewer.setInput(attributes.entrySet());
106
107 if (!Platform.getPreferencesService().getBoolean("org.netxms.ui.eclipse.console", "SHOW_HIDDEN_ATTRIBUTES", false, null)) //$NON-NLS-1$ //$NON-NLS-2$
108 {
109 viewer.addFilter(new ViewerFilter() {
110 @SuppressWarnings("unchecked")
111 @Override
112 public boolean select(Viewer viewer, Object parentElement, Object element)
113 {
114 return !((Entry<String, String>)element).getKey().startsWith("."); //$NON-NLS-1$
115 }
116 });
117 }
118
119 GridData gridData = new GridData();
120 gridData.verticalAlignment = GridData.FILL;
121 gridData.grabExcessVerticalSpace = true;
122 gridData.horizontalAlignment = GridData.FILL;
123 gridData.grabExcessHorizontalSpace = true;
124 gridData.heightHint = 0;
125 viewer.getControl().setLayoutData(gridData);
126
127 Composite buttons = new Composite(dialogArea, SWT.NONE);
128 RowLayout buttonLayout = new RowLayout();
129 buttonLayout.type = SWT.HORIZONTAL;
130 buttonLayout.pack = false;
131 buttonLayout.marginWidth = 0;
132 buttonLayout.marginRight = 0;
133 buttons.setLayout(buttonLayout);
134 gridData = new GridData();
135 gridData.horizontalAlignment = SWT.RIGHT;
136 buttons.setLayoutData(gridData);
137
138 addButton = new Button(buttons, SWT.PUSH);
139 addButton.setText(Messages.get().CustomAttributes_Add);
140 RowData rd = new RowData();
141 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
142 addButton.setLayoutData(rd);
143 addButton.addSelectionListener(new SelectionListener() {
144 @Override
145 public void widgetDefaultSelected(SelectionEvent e)
146 {
147 widgetSelected(e);
148 }
149
150 @Override
151 public void widgetSelected(SelectionEvent e)
152 {
153 final AttributeEditDialog dlg = new AttributeEditDialog(CustomAttributes.this.getShell(), null, null);
154 if (dlg.open() == Window.OK)
155 {
156 if (attributes.containsKey(dlg.getAttrName()))
157 {
158 MessageDialogHelper.openWarning(CustomAttributes.this.getShell(), Messages.get().CustomAttributes_Warning,
159 String.format(Messages.get().CustomAttributes_WarningAlreadyExist, dlg.getAttrName()));
160 }
161 else
162 {
163 attributes.put(dlg.getAttrName(), dlg.getAttrValue());
164 viewer.setInput(attributes.entrySet());
165 CustomAttributes.this.isModified = true;
166 }
167 }
168 }
169 });
170
171 editButton = new Button(buttons, SWT.PUSH);
172 editButton.setText(Messages.get().CustomAttributes_Modify);
173 rd = new RowData();
174 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
175 editButton.setLayoutData(rd);
176 editButton.addSelectionListener(new SelectionListener() {
177 @Override
178 public void widgetDefaultSelected(SelectionEvent e)
179 {
180 widgetSelected(e);
181 }
182
183 @SuppressWarnings("unchecked")
184 @Override
185 public void widgetSelected(SelectionEvent e)
186 {
187 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
188 if (selection.size() == 1)
189 {
190 Entry<String, String> element = (Entry<String, String>)selection.getFirstElement();
191 final AttributeEditDialog dlg = new AttributeEditDialog(CustomAttributes.this.getShell(), element.getKey(), element.getValue());
192 if (dlg.open() == Window.OK)
193 {
194 attributes.put(dlg.getAttrName(), dlg.getAttrValue());
195 viewer.setInput(attributes.entrySet());
196 CustomAttributes.this.isModified = true;
197 }
198 }
199 }
200 });
201
202 deleteButton = new Button(buttons, SWT.PUSH);
203 deleteButton.setText(Messages.get().CustomAttributes_Delete);
204 rd = new RowData();
205 rd.width = WidgetHelper.BUTTON_WIDTH_HINT;
206 deleteButton.setLayoutData(rd);
207 deleteButton.addSelectionListener(new SelectionListener() {
208 @Override
209 public void widgetDefaultSelected(SelectionEvent e)
210 {
211 widgetSelected(e);
212 }
213
214 @SuppressWarnings("unchecked")
215 @Override
216 public void widgetSelected(SelectionEvent e)
217 {
218 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
219 Iterator<Entry<String, String>> it = selection.iterator();
220 if (it.hasNext())
221 {
222 while(it.hasNext())
223 {
224 Entry<String, String> element = it.next();
225 attributes.remove(element.getKey());
226 }
227 viewer.setInput(attributes.entrySet());
228 CustomAttributes.this.isModified = true;
229 }
230 }
231 });
232
233 viewer.addDoubleClickListener(new IDoubleClickListener() {
234 @Override
235 public void doubleClick(DoubleClickEvent event)
236 {
237 editButton.notifyListeners(SWT.Selection, new Event());
238 }
239 });
240
241 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
242 @Override
243 public void selectionChanged(SelectionChangedEvent event)
244 {
245 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
246 editButton.setEnabled(selection.size() == 1);
247 deleteButton.setEnabled(selection.size() > 0);
248 }
249 });
250
251 return dialogArea;
252 }
253
254 /**
255 * Apply changes
256 *
257 * @param isApply true if update operation caused by "Apply" button
258 */
259 protected void applyChanges(final boolean isApply)
260 {
261 if (!isModified)
262 return; // Nothing to apply
263
264 if (isApply)
265 setValid(false);
266
267 final NXCObjectModificationData md = new NXCObjectModificationData(object.getObjectId());
268 md.setCustomAttributes(attributes);
269 final NXCSession session = (NXCSession)ConsoleSharedData.getSession();
270 new ConsoleJob(Messages.get().CustomAttributes_JobName, null, Activator.PLUGIN_ID, null) {
271 @Override
272 protected void runInternal(IProgressMonitor monitor) throws Exception
273 {
274 session.modifyObject(md);
275 isModified = false;
276 }
277
278 @Override
279 protected String getErrorMessage()
280 {
281 return Messages.get().CustomAttributes_JobError;
282 }
283
284 @Override
285 protected void jobFinalize()
286 {
287 if (isApply)
288 {
289 runInUIThread(new Runnable() {
290 @Override
291 public void run()
292 {
293 CustomAttributes.this.setValid(true);
294 }
295 });
296 }
297 }
298 }.start();
299 }
300
301 /* (non-Javadoc)
302 * @see org.eclipse.jface.preference.PreferencePage#performApply()
303 */
304 @Override
305 protected void performApply()
306 {
307 applyChanges(true);
308 }
309
310 /* (non-Javadoc)
311 * @see org.eclipse.jface.preference.PreferencePage#performOk()
312 */
313 @Override
314 public boolean performOk()
315 {
316 applyChanges(false);
317 return true;
318 }
319 }