Skip to main content
summaryrefslogtreecommitdiffstats
blob: af23181f559e9daa8956fd4158b33190bb8fe1f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

package org.eclipse.osee.framework.ui.skynet.widgets;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osee.framework.jdk.core.type.MutableBoolean;
import org.eclipse.osee.framework.jdk.core.util.AXml;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.skynet.SkynetGuiPlugin;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.IMessageManager;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Abstract class for all widgets used in Wizards and Editors
 */
public abstract class XWidget {
   public final static String XWIDGET_DATA_KEY = "xWidget";

   private IManagedForm managedForm;

   protected Label labelWidget = null;
   private String label = "";
   private String xmlRoot = "";
   private String xmlSubRoot = "";
   private String toolTip = null;
   private boolean requiredEntry = false;
   private boolean editable = true;
   private final MutableBoolean isNotificationAllowed = new MutableBoolean(true);

   protected boolean verticalLabel = false;
   protected boolean fillVertically = false;
   protected boolean fillHorizontally = false;

   public boolean isFillHorizontally() {
      return fillHorizontally;
   }

   private boolean displayLabel = true;
   private final Set<XModifiedListener> modifiedListeners = new LinkedHashSet<XModifiedListener>();
   private MouseListener mouseLabelListener;
   protected FormToolkit toolkit;

   public XWidget(String label) {
      this.label = label;
   }

   public XWidget(String label, String xmlRoot) {
      this.label = label;
      this.xmlRoot = xmlRoot;
   }

   public XWidget(String label, String xmlRoot, String xmlSubRoot) {
      this.label = label;
      this.xmlRoot = xmlRoot;
      this.xmlSubRoot = xmlSubRoot;
   }

   public void setToolTip(String toolTip) {
      this.toolTip = toolTip;
      if (this.labelWidget != null && !labelWidget.isDisposed()) {
         this.labelWidget.setToolTipText(toolTip);
      }
   }

   public void addXModifiedListener(XModifiedListener listener) {
      modifiedListeners.add(listener);
   }

   public void notifyXModifiedListeners() {
      if (areNotificationsAllowed()) {
         for (XModifiedListener listener : modifiedListeners) {
            listener.widgetModified(this);
         }
      }
   }

   public boolean areNotificationsAllowed() {
      return isNotificationAllowed.getValue();
   }

   protected IManagedForm getManagedForm() {
      return managedForm;
   }

   public boolean isInForm() {
      return getManagedForm() != null;
   }

   protected IMessageManager getMessageManager() {
      return getManagedForm() != null ? managedForm.getMessageManager() : null;
   }

   public void setMessage(String messageId, String messageText, int type) {
      IMessageManager messageManager = getMessageManager();
      if (messageManager != null && isFormReady()) {
         messageManager.addMessage(messageId, messageText, null, type);
      }
   }

   public boolean isFormReady() {
      // Set to true if outside of a form;
      boolean result = managedForm == null;
      if (managedForm != null) {
         result = !managedForm.getForm().isDisposed();
      }
      return result;
   }

   public void setControlCausedMessage(String messageId, String messageText, int type) {
      IMessageManager messageManager = getMessageManager();
      if (messageManager != null && isFormReady()) {
         messageManager.addMessage(messageId, messageText, null, type, getErrorMessageControl());
      }
   }

   public void setControlCausedMessageByObject(String messageText, int type) {
      IMessageManager messageManager = getMessageManager();
      if (messageManager != null && isFormReady()) {
         try {
            messageManager.addMessage(this, messageText, null, type, getErrorMessageControl());
         } catch (SWTException ex) {
            //Do nothing
         }
      }
   }

   public void removeControlCausedMessageByObject() {
      IMessageManager messageManager = getMessageManager();
      if (messageManager != null && isFormReady()) {
         messageManager.removeMessage(this, getErrorMessageControl());
      }
   }

   public void removeControlCausedMessage(String messageId) {
      IMessageManager messageManager = getMessageManager();
      if (messageManager != null && isFormReady()) {
         messageManager.removeMessage(messageId, getErrorMessageControl());
      }
   }

   public void validate() {
      if (Widgets.isAccessible(getControl()) && isFormReady() && areNotificationsAllowed()) {
         IStatus status = isValid();
         if (isInForm()) {
            XWidgetValidateUtility.setStatus(status, this);
         } else {
            if (Widgets.isAccessible(labelWidget)) {
               labelWidget.setForeground(status.isOK() ? null : Displays.getSystemColor(SWT.COLOR_RED));
               if (mouseLabelListener == null) {
                  mouseLabelListener = new MouseListener() {
                     @Override
                     public void mouseDoubleClick(MouseEvent e) {
                        openHelp();
                     }

                     @Override
                     public void mouseDown(MouseEvent e) {
                        // do nothing
                     }

                     @Override
                     public void mouseUp(MouseEvent e) {
                        // do nothing
                     }
                  };
                  labelWidget.addMouseListener(mouseLabelListener);
               }
            }
         }
      }
   }

   /**
    * Return the control that the error message is to be placed. By default the getControl() will be used. Override to
    * change.
    */
   public Control getErrorMessageControl() {
      return getControl();
   }

   public abstract Control getControl();

   public void openHelp() {
      try {
         if (toolTip != null && label != null) {
            MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
               label + " Tool Tip", toolTip);
         }
      } catch (Exception ex) {
         OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex);
      }
   }

   protected void setNotificationsAllowed(boolean areAllowed) {
      isNotificationAllowed.setValue(areAllowed);
   }

   protected void createControls(Composite parent, int horizontalSpan) {
      // provided for subclass implementation
   }

   public final void createWidgets(Composite parent, int horizontalSpan) {
      setNotificationsAllowed(false);
      try {
         createControls(parent, horizontalSpan);
      } finally {
         setNotificationsAllowed(true);
      }
   }

   public final void createWidgets(IManagedForm managedForm, Composite parent, int horizontalSpan) {
      if (managedForm != null) {
         this.toolkit = managedForm.getToolkit();
         this.managedForm = managedForm;
      }
      createWidgets(parent, horizontalSpan);
      adaptControls(toolkit);

      // Added to be able to operate on XWidget who create the control
      Control internalControl = getControl();
      if (internalControl != null) {
         internalControl.setData(XWIDGET_DATA_KEY, this);
      }
   }

   public void adaptControls(FormToolkit toolkit) {
      if (toolkit != null) {
         if (getControl() != null) {
            toolkit.adapt(getControl(), true, false);
         }
         if (labelWidget != null) {
            toolkit.adapt(labelWidget, true, true);
            toolkit.adapt(labelWidget.getParent(), true, true);
         }
      }
   }

   /**
    * Create Widgets used to display label and entry for wizards and editors
    */
   public void dispose() {
      if (Widgets.isAccessible(managedForm.getForm())) {
         removeControlCausedMessageByObject();
      }
   }

   public abstract void setFocus();

   public abstract void refresh();

   public abstract IStatus isValid();

   /**
    * Called with string found between xml tags Used by setFromXml() String will be sent through AXml.xmlToText() before
    * being sent to setXmlData implementation. Used by: setFromXml
    * 
    * @param str - value to set
    */
   public abstract void setXmlData(String str);

   /**
    * Return string to save off between xml tags Used by call to toXml() String returned will be sent through
    * AXml.textToXml() before being saved Used by: toXml
    * 
    * @return Return Xml data string.
    */
   protected abstract String getXmlData();

   public abstract String toHTML(String labelFont);

   protected String toXml() throws Exception {
      if (xmlSubRoot.equals("")) {
         return toXml(xmlRoot);
      } else {
         return toXml(xmlRoot, xmlSubRoot);
      }
   }

   protected String toXml(String xmlRoot) throws Exception {
      String s = "<" + xmlRoot + ">" + AXml.textToXml(getXmlData()) + "</" + xmlRoot + ">\n";
      return s;
   }

   public String toXml(String xmlRoot, String xmlSubRoot) throws Exception {
      String s =
         "<" + xmlRoot + ">" + "<" + xmlSubRoot + ">" + AXml.textToXml(getXmlData()) + "</" + xmlSubRoot + ">" + "</" + xmlRoot + ">\n";
      return s;
   }

   public void setFromXml(String xml) throws IllegalStateException {
      Matcher m;
      m = Pattern.compile("<" + xmlRoot + ">(.*?)</" + xmlRoot + ">", Pattern.MULTILINE | Pattern.DOTALL).matcher(xml);
      if (m.find()) {
         setXmlData(AXml.xmlToText(m.group(1)));
      }
   }

   public Vector<String> getDisplayLabels() {
      Vector<String> l = new Vector<String>();
      l.add(label);
      return l;
   }

   public void setDisplayLabel(String displayLabel) {
      label = displayLabel;
   }

   public boolean isEditable() {
      return editable;
   }

   public void setEditable(boolean editable) {
      this.editable = editable;
   }

   public boolean isVerticalLabel() {
      return verticalLabel;
   }

   public void setVerticalLabel(boolean verticalLabel) {
      this.verticalLabel = verticalLabel;
   }

   public String getXmlRoot() {
      return xmlRoot;
   }

   public void setXmlRoot(String xmlRoot) {
      this.xmlRoot = xmlRoot;
   }

   public String getXmlSubRoot() {
      return xmlSubRoot;
   }

   public void setXmlSubRoot(String xmlSubRoot) {
      this.xmlSubRoot = xmlSubRoot;
   }

   public String getToolTip() {
      return toolTip;
   }

   public boolean isFillVertically() {
      return fillVertically;
   }

   public void setFillVertically(boolean fillVertically) {
      this.fillVertically = fillVertically;
   }

   public String getLabel() {
      return label;
   }

   public void setLabel(String label) {
      this.label = label;
      if (labelWidget != null && !labelWidget.isDisposed()) {
         labelWidget.setText(label);
      }
   }

   public Label getLabelWidget() {
      return labelWidget;
   }

   protected void setLabelWidget(Label labelWidget) {
      this.labelWidget = labelWidget;
   }

   public boolean isRequiredEntry() {
      return requiredEntry;
   }

   public void setRequiredEntry(boolean requiredEntry) {
      this.requiredEntry = requiredEntry;
   }

   protected abstract String getReportData();

   @Override
   public String toString() {
      return String.format("%s: %s\n\n", getLabel(), getReportData());
   }

   public void setDisplayLabel(boolean displayLabel) {
      this.displayLabel = displayLabel;
   }

   public void setFillHorizontally(boolean fillHorizontally) {
      this.fillHorizontally = fillHorizontally;
   }

   public abstract Object getData();

   public boolean isDisplayLabel() {
      return displayLabel;
   }

   public Collection<? extends XWidget> getChildrenXWidgets() {
      return Collections.emptyList();
   }
}

Back to the top