58edcb083b2226b12d18cb23651f8ef0f3eeadf8
[public/netxms.git] / webui / webapp / NXSL / src / org / netxms / ui / eclipse / nxsl / widgets / ScriptEditor.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 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.widgets;
20
21 import java.util.Collection;
22 import java.util.Set;
23 import org.eclipse.rap.rwt.RWT;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.CLabel;
26 import org.eclipse.swt.events.MouseAdapter;
27 import org.eclipse.swt.events.MouseEvent;
28 import org.eclipse.swt.events.MouseListener;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.graphics.Point;
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.swt.widgets.Text;
36 import org.netxms.ui.eclipse.console.resources.SharedColors;
37 import org.netxms.ui.eclipse.console.resources.SharedIcons;
38
39 /**
40 * NXSL script editor
41 *
42 */
43 public class ScriptEditor extends Composite
44 {
45 private Text editor;
46 private String hintText;
47 private Composite hintArea;
48 private Text hintTextControl = null;
49 private Label hintsExpandButton = null;
50
51 /**
52 * @param parent
53 * @param style
54 * @param editorStyle
55 */
56 public ScriptEditor(Composite parent, int style, int editorStyle)
57 {
58 this(parent, style, editorStyle, true, null);
59 }
60
61 /**
62 * @param parent
63 * @param style
64 * @param editorStyle
65 * @param showLineNumbers
66 */
67 public ScriptEditor(Composite parent, int style, int editorStyle, boolean showLineNumbers)
68 {
69 this(parent, style, editorStyle, showLineNumbers, null);
70 }
71
72 /**
73 * @param parent
74 * @param style
75 * @param editorStyle
76 * @param showLineNumbers
77 * @param hints
78 */
79 public ScriptEditor(Composite parent, int style, int editorStyle, boolean showLineNumbers, String hints)
80 {
81 super(parent, style);
82
83 hintText = hints;
84
85 GridLayout layout = new GridLayout();
86 layout.marginWidth = 0;
87 layout.marginHeight = 0;
88 layout.verticalSpacing = 0;
89 setLayout(layout);
90
91 if (hints != null)
92 {
93 createHintsArea();
94 }
95
96 editor = new Text(this, editorStyle);
97 editor.setData(RWT.CUSTOM_VARIANT, "monospace");
98 editor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
99 }
100
101 /**
102 * Create hints area
103 */
104 private void createHintsArea()
105 {
106 hintArea = new Composite(this, SWT.NONE);
107 GridLayout layout = new GridLayout();
108 layout.marginWidth = 0;
109 layout.marginHeight = 0;
110 layout.verticalSpacing = 0;
111 layout.numColumns = 2;
112 hintArea.setLayout(layout);
113 hintArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
114 hintArea.setBackground(SharedColors.getColor(SharedColors.MESSAGE_BAR_BACKGROUND, getDisplay()));
115
116 CLabel hintsTitle = new CLabel(hintArea, SWT.NONE);
117 hintsTitle.setBackground(SharedColors.getColor(SharedColors.MESSAGE_BAR_BACKGROUND, getDisplay()));
118 hintsTitle.setForeground(SharedColors.getColor(SharedColors.MESSAGE_BAR_TEXT, getDisplay()));
119 hintsTitle.setImage(SharedIcons.IMG_INFORMATION);
120 hintsTitle.setText("Hints");
121 hintsTitle.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
122 hintsTitle.addMouseListener(new MouseAdapter() {
123 @Override
124 public void mouseDoubleClick(MouseEvent e)
125 {
126 if (e.button == 1)
127 toggleHints();
128 }
129 });
130
131 hintsExpandButton = new Label(hintArea, SWT.NONE);
132 hintsExpandButton.setBackground(hintArea.getBackground());
133 hintsExpandButton.setCursor(getDisplay().getSystemCursor(SWT.CURSOR_HAND));
134 hintsExpandButton.setImage(SharedIcons.IMG_EXPAND);
135 hintsExpandButton.setToolTipText("Hide message");
136 GridData gd = new GridData();
137 gd.verticalAlignment = SWT.CENTER;
138 hintsExpandButton.setLayoutData(gd);
139 hintsExpandButton.addMouseListener(new MouseListener() {
140 private boolean doAction = false;
141
142 @Override
143 public void mouseDoubleClick(MouseEvent e)
144 {
145 if (e.button == 1)
146 doAction = false;
147 }
148
149 @Override
150 public void mouseDown(MouseEvent e)
151 {
152 if (e.button == 1)
153 doAction = true;
154 }
155
156 @Override
157 public void mouseUp(MouseEvent e)
158 {
159 if ((e.button == 1) && doAction)
160 toggleHints();
161 }
162 });
163
164 Label separator = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
165 separator.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
166 }
167
168 /**
169 * Get underlying text widget
170 * @return text widget
171 */
172 public Text getTextWidget()
173 {
174 return editor;
175 }
176
177 /**
178 * Set text for editing
179 * @param text
180 */
181 public void setText(String text)
182 {
183 editor.setText((text != null) ? text : "");
184 }
185
186 /**
187 * Get editor's content
188 * @return
189 */
190 public String getText()
191 {
192 return editor.getText();
193 }
194
195 /**
196 * @param functions the functions to set
197 */
198 public void setFunctions(Set<String> functions)
199 {
200 }
201
202 /**
203 * Add functions
204 *
205 * @param fc
206 */
207 public void addFunctions(Collection<String> fc)
208 {
209 }
210
211 /**
212 * @param variables the variables to set
213 */
214 public void setVariables(Set<String> variables)
215 {
216 }
217
218 /**
219 * Add variables
220 *
221 * @param vc
222 */
223 public void addVariables(Collection<String> vc)
224 {
225 }
226
227 /**
228 * @return the functionsCache
229 */
230 public String[] getFunctions()
231 {
232 return new String[0];
233 }
234
235 /**
236 * @return the variablesCache
237 */
238 public String[] getVariables()
239 {
240 return new String[0];
241 }
242
243 /**
244 * Get icon for given autocompletion proposal type. Proposal types defined in NXSLProposalProcessor.
245 *
246 * @param type proposal type
247 * @return image for given proposal type or null
248 */
249 public Image getProposalIcon(int type)
250 {
251 return null;
252 }
253
254 /* (non-Javadoc)
255 * @see org.eclipse.swt.widgets.Composite#computeSize(int, int, boolean)
256 */
257 @Override
258 public Point computeSize(int wHint, int hHint, boolean changed)
259 {
260 Point p = editor.computeSize(wHint, hHint, changed);
261 p.y += 4;
262 return p;
263 }
264
265 /**
266 * Toggle hints area
267 */
268 private void toggleHints()
269 {
270 if (hintTextControl != null)
271 {
272 hintTextControl.dispose();
273 hintTextControl = null;
274 hintsExpandButton.setImage(SharedIcons.IMG_EXPAND);
275 }
276 else
277 {
278 hintTextControl = new Text(hintArea, SWT.MULTI | SWT.WRAP);
279 hintTextControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
280 hintTextControl.setEditable(false);
281 hintTextControl.setText(hintText);
282 hintTextControl.setBackground(SharedColors.getColor(SharedColors.MESSAGE_BAR_BACKGROUND, getDisplay()));
283 hintTextControl.setForeground(SharedColors.getColor(SharedColors.MESSAGE_BAR_TEXT, getDisplay()));
284 hintsExpandButton.setImage(SharedIcons.IMG_COLLAPSE);
285 }
286 layout(true, true);
287 }
288 }