70c40da08006668cdcca8e06d45430776eb9e119
[public/netxms.git] / src / java / netxms-eclipse / PolicyManager / src / org / netxms / ui / eclipse / policymanager / propertypages / ConfigFile.java
1 /**
2 *
3 */
4 package org.netxms.ui.eclipse.policymanager.propertypages;
5
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.core.runtime.IStatus;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.core.runtime.jobs.Job;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Text;
17 import org.eclipse.ui.dialogs.PropertyPage;
18 import org.eclipse.ui.progress.UIJob;
19 import org.netxms.client.NXCException;
20 import org.netxms.client.NXCObjectModificationData;
21 import org.netxms.client.NXCSession;
22 import org.netxms.client.objects.AgentPolicyConfig;
23 import org.netxms.ui.eclipse.policymanager.Activator;
24 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
25 import org.netxms.ui.eclipse.tools.WidgetHelper;
26
27 /**
28 * @author Victor
29 *
30 */
31 public class ConfigFile extends PropertyPage
32 {
33 private Text textName;
34 private Text textContent;
35 private String initialName;
36 private String initialContent;
37 private AgentPolicyConfig object;
38
39
40 /* (non-Javadoc)
41 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
42 */
43 @Override
44 protected Control createContents(Composite parent)
45 {
46 Composite dialogArea = new Composite(parent, SWT.NONE);
47
48 object = (AgentPolicyConfig)getElement().getAdapter(AgentPolicyConfig.class);
49 if (object == null) // Paranoid check
50 return dialogArea;
51
52 GridLayout layout = new GridLayout();
53 layout.verticalSpacing = WidgetHelper.OUTER_SPACING;
54 layout.marginWidth = 0;
55 layout.marginHeight = 0;
56 //FormLayout layout = new FormLayout();
57 dialogArea.setLayout(layout);
58
59 // File name
60 initialName = new String(object.getFileName());
61 textName = WidgetHelper.createLabeledText(dialogArea, SWT.SINGLE | SWT.BORDER, SWT.DEFAULT, "File name",
62 initialName, WidgetHelper.DEFAULT_LAYOUT_DATA);
63
64 // File content
65 Label label = new Label(dialogArea, SWT.NONE);
66 label.setText("File");
67
68 initialContent = new String(object.getFileContent());
69 GridData gd = new GridData();
70 gd.grabExcessHorizontalSpace = true;
71 gd.grabExcessVerticalSpace = true;
72 gd.horizontalAlignment = GridData.FILL;
73 gd.verticalAlignment = GridData.FILL;
74 gd.heightHint = 0;
75 textContent = new Text(dialogArea, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
76 textContent.setText(object.getFileContent());
77 textContent.setLayoutData(gd);
78
79 return dialogArea;
80 }
81
82
83 /**
84 * Apply changes
85 *
86 * @param isApply true if update operation caused by "Apply" button
87 */
88 protected void applyChanges(final boolean isApply)
89 {
90 if (textName.getText().equals(initialName) &&
91 textContent.getText().equals(initialContent))
92 return; // Nothing to apply
93
94 if (isApply)
95 setValid(false);
96
97 final String newName = new String(textName.getText());
98 final String newContent = new String(textContent.getText());
99 new Job("Change policy") {
100 @Override
101 protected IStatus run(IProgressMonitor monitor)
102 {
103 IStatus status;
104
105 try
106 {
107 if (object != null)
108 {
109 NXCObjectModificationData md = new NXCObjectModificationData(object.getObjectId());
110 md.setConfigFileName(newName);
111 md.setConfigFileContent(newContent);
112 ((NXCSession)ConsoleSharedData.getSession()).modifyObject(md);
113 }
114 initialName = newName;
115 initialContent = newContent;
116 status = Status.OK_STATUS;
117 }
118 catch(Exception e)
119 {
120 status = new Status(Status.ERROR, Activator.PLUGIN_ID,
121 (e instanceof NXCException) ? ((NXCException)e).getErrorCode() : 0,
122 "Cannot change agent policy: " + e.getMessage(), e);
123 }
124
125 if (isApply)
126 {
127 new UIJob("Update \"Configuration File\" property page") {
128 @Override
129 public IStatus runInUIThread(IProgressMonitor monitor)
130 {
131 ConfigFile.this.setValid(true);
132 return Status.OK_STATUS;
133 }
134 }.schedule();
135 }
136
137 return status;
138 }
139 }.schedule();
140 }
141
142 /* (non-Javadoc)
143 * @see org.eclipse.jface.preference.PreferencePage#performOk()
144 */
145 @Override
146 public boolean performOk()
147 {
148 applyChanges(false);
149 return true;
150 }
151
152 /* (non-Javadoc)
153 * @see org.eclipse.jface.preference.PreferencePage#performApply()
154 */
155 @Override
156 protected void performApply()
157 {
158 applyChanges(true);
159 }
160 }