Skip to main content
summaryrefslogtreecommitdiffstats
blob: ba3cd210d33b6a7a74274688ebf0a49fdd7a765d (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
/*******************************************************************************
 * 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.artifact.editor.parts;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Result;
import org.eclipse.osee.framework.help.ui.OseeHelpContext;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.AccessPolicy;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.ui.plugin.util.HelpUtil;
import org.eclipse.osee.framework.ui.skynet.artifact.editor.ArtifactEditor;
import org.eclipse.osee.framework.ui.skynet.artifact.editor.sections.AttributeTypeUtil;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.internal.DslGrammarManager;
import org.eclipse.osee.framework.ui.skynet.internal.ServiceUtil;
import org.eclipse.osee.framework.ui.skynet.widgets.IArtifactStoredWidget;
import org.eclipse.osee.framework.ui.skynet.widgets.XModifiedListener;
import org.eclipse.osee.framework.ui.skynet.widgets.XOption;
import org.eclipse.osee.framework.ui.skynet.widgets.XWidget;
import org.eclipse.osee.framework.ui.skynet.widgets.XWidgetAccessDecorationProvider;
import org.eclipse.osee.framework.ui.skynet.widgets.XWidgetDecorator;
import org.eclipse.osee.framework.ui.skynet.widgets.XWidgetUtility;
import org.eclipse.osee.framework.ui.skynet.widgets.util.AttributeXWidgetManager;
import org.eclipse.osee.framework.ui.skynet.widgets.util.DefaultAttributeXWidgetProvider;
import org.eclipse.osee.framework.ui.skynet.widgets.util.DefaultXWidgetOptionResolver;
import org.eclipse.osee.framework.ui.skynet.widgets.util.IAttributeXWidgetProvider;
import org.eclipse.osee.framework.ui.skynet.widgets.util.SwtXWidgetRenderer;
import org.eclipse.osee.framework.ui.skynet.widgets.util.XWidgetPage;
import org.eclipse.osee.framework.ui.skynet.widgets.util.XWidgetRendererItem;
import org.eclipse.osee.framework.ui.swt.ALayout;
import org.eclipse.osee.framework.ui.swt.FontManager;
import org.eclipse.osee.framework.ui.swt.Widgets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.events.IExpansionListener;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * @author Roberto E. Escobar
 */
public class AttributeFormPart extends AbstractFormPart {

   private final ArtifactEditor editor;
   private Composite composite;
   private final XWidgetDecorator decorator = new XWidgetDecorator();
   private final Map<IAttributeType, Composite> xWidgetsMap = new HashMap<IAttributeType, Composite>();

   private final XModifiedListener widgetModifiedListener = new XModifiedListener() {

      @Override
      public void widgetModified(XWidget widget) {
         editor.onDirtied();
      }
   };

   public AttributeFormPart(ArtifactEditor editor) {
      this.editor = editor;
      try {
         AccessPolicy policy = ServiceUtil.getAccessPolicy();
         decorator.addProvider(new XWidgetAccessDecorationProvider(policy));
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex.toString(), ex);
      }
   }

   @Override
   public void refresh() {
      super.refresh();//<--This method resets the dirty bits on all widgets, but does not implicitly revert their values. (see below)
      decorator.refresh();
      getManagedForm().getForm().getBody().layout(true);

      //Revert any unsaved changes in the widgets.
      List<XWidget> widgets = XWidgetUtility.findXWidgetsInControl(composite);
      for (XWidget xWidget : widgets) {
         if (xWidget.isEditable()) {
            if (xWidget instanceof IArtifactStoredWidget) {
               IArtifactStoredWidget aWidget = (IArtifactStoredWidget) xWidget;
               try {
                  aWidget.revert();
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex);
               }
            }
         }
      }
   }

   public void createContents(Composite composite) {
      this.composite = composite;
      decorator.dispose();

      composite.setLayout(ALayout.getZeroMarginLayout(1, false));
      composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
      composite.setVisible(false);

      try {
         Artifact artifact = editor.getEditorInput().getArtifact();

         List<IAttributeType> types = AttributeTypeUtil.getTypesWithData(artifact);
         addWidgetForAttributeType(types);

         layoutControls(composite);
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, "Unable to access attribute types", ex);
      } finally {
         composite.setVisible(true);
      }
   }

   public void addWidgetForAttributeType(Collection<IAttributeType> attributeTypes) throws OseeCoreException {
      Artifact artifact = editor.getEditorInput().getArtifact();
      boolean isEditable = !artifact.isReadOnly();

      for (IAttributeType attributeType : attributeTypes) {
         Composite internalComposite;
         if (DefaultAttributeXWidgetProvider.useMultiLineWidget(attributeType) || DslGrammarManager.isDslAttributeType(attributeType)) {
            internalComposite = createAttributeTypeControlsInSection(composite, attributeType, isEditable, 15);
         } else {
            internalComposite = createAttributeTypeControls(composite, artifact, attributeType, isEditable, false, 20);
         }
         setLabelFonts(internalComposite, FontManager.getDefaultLabelFont());
         HelpUtil.setHelp(internalComposite, OseeHelpContext.ARTIFACT_EDITOR__ATTRIBUTES);
         xWidgetsMap.put(attributeType, internalComposite);
      }
      //refresh(); <-- This call reverts unsaved changes to all widgets.  Not the behavior we want here.
      decorator.refresh();
      getManagedForm().getForm().getBody().layout(true, true);
   }

   @Override
   public void dispose() {
      Widgets.disposeWidget(composite);
      super.dispose();
   }

   public static void setLabelFonts(Control parent, Font font) {
      if (parent instanceof Label) {
         Label label = (Label) parent;
         label.setFont(font);
      }
      if (parent instanceof Composite) {
         Composite container = (Composite) parent;
         for (Control child : container.getChildren()) {
            setLabelFonts(child, font);
         }
         container.layout();
      }
   }

   private void layoutControls(Control control) {
      if (control instanceof Label || control instanceof Button) {
         control.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, false));
      }

      if (control instanceof Composite) {
         Composite container = (Composite) control;
         for (Control child : container.getChildren()) {
            layoutControls(child);
         }
      }
   }

   private Composite createAttributeTypeControls(Composite parent, Artifact artifact, IAttributeType attributeType, boolean isEditable, boolean isExpandable, int leftMargin) {
      FormToolkit toolkit = getManagedForm().getToolkit();
      Composite internalComposite = toolkit.createComposite(parent, SWT.WRAP);

      GridLayout layout = ALayout.getZeroMarginLayout(1, false);
      layout.marginLeft = leftMargin;
      internalComposite.setLayout(layout);
      internalComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

      try {
         IAttributeXWidgetProvider xWidgetProvider = AttributeXWidgetManager.getAttributeXWidgetProvider(attributeType);
         List<XWidgetRendererItem> concreteWidgets = xWidgetProvider.getDynamicXWidgetLayoutData(attributeType);
         if (isExpandable) {
            for (XWidgetRendererItem data : concreteWidgets) {
               data.getXOptionHandler().add(XOption.NO_LABEL);
            }
         }
         XWidgetPage workPage = new XWidgetPage(concreteWidgets, new DefaultXWidgetOptionResolver());

         SwtXWidgetRenderer xWidgetLayout =
            workPage.createBody(getManagedForm(), internalComposite, artifact, widgetModifiedListener, isEditable);
         Collection<XWidget> xWidgets = xWidgetLayout.getXWidgets();

         for (XWidget xWidget : xWidgets) {
            xWidget.addXModifiedListener(new XWidgetValidationListener());
            decorator.addWidget(xWidget);
         }
      } catch (OseeCoreException ex) {
         toolkit.createLabel(parent, String.format("Error creating controls for: [%s]", attributeType.getName()));
      }
      return internalComposite;
   }

   private Composite createAttributeTypeControlsInSection(Composite parent, IAttributeType attributeType, boolean isEditable, int leftMargin) {
      FormToolkit toolkit = getManagedForm().getToolkit();

      int style = ExpandableComposite.SHORT_TITLE_BAR | ExpandableComposite.TREE_NODE;
      ExpandableComposite expandable = toolkit.createExpandableComposite(parent, style);
      expandable.setText(attributeType.getName());

      GridLayout layout = ALayout.getZeroMarginLayout(1, false);
      expandable.setLayout(layout);
      expandable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

      Artifact artifact = editor.getEditorInput().getArtifact();

      Composite composite =
         createAttributeTypeControls(expandable, artifact, attributeType, isEditable, true, leftMargin);
      expandable.setClient(composite);

      expandable.addExpansionListener(new IExpansionListener() {

         @Override
         public void expansionStateChanged(ExpansionEvent e) {
            getManagedForm().getForm().reflow(true);
         }

         @Override
         public void expansionStateChanging(ExpansionEvent e) {
            getManagedForm().getForm().reflow(false);
         }

      });
      toolkit.paintBordersFor(expandable);

      return expandable;
   }

   @Override
   public void commit(boolean onSave) {
      int saveCount = 0;
      List<XWidget> widgets = XWidgetUtility.findXWidgetsInControl(composite);
      for (XWidget xWidget : widgets) {
         if (xWidget.isEditable()) {
            if (xWidget instanceof IArtifactStoredWidget) {
               IArtifactStoredWidget aWidget = (IArtifactStoredWidget) xWidget;
               try {
                  if (aWidget.isDirty().isTrue()) {
                     aWidget.saveToArtifact();
                     xWidget.removeControlCausedMessage("attribute.dirty");
                     saveCount++;
                  } else {
                     saveCount++;
                  }
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex.toString(), ex);
               }
            }
         } else {
            saveCount++;
         }
      }

      // Ensure all changes saved
      if (saveCount == widgets.size()) {
         super.commit(onSave);
      }
      refresh();
   }

   public void removeWidgetForAttributeType(Collection<? extends IAttributeType> attributeTypes) {
      for (IAttributeType attributeType : attributeTypes) {
         xWidgetsMap.remove(attributeType).dispose();
         //decorator.addWidget(xWidget)
      }
      if (attributeTypes.size() > 0) {
         markDirty();
      }
      //refresh(); <-- This call reverts unsaved changes to all widgets.  Not the behavior we want here.
      decorator.refresh();
      getManagedForm().getForm().getBody().layout(true, true);
   }

   private final class XWidgetValidationListener implements XModifiedListener {

      @Override
      public void widgetModified(XWidget xWidget) {
         if (xWidget != null && xWidget instanceof IArtifactStoredWidget) {
            IArtifactStoredWidget aWidget = (IArtifactStoredWidget) xWidget;
            try {
               Result result = aWidget.isDirty();
               if (result.isTrue()) {
                  xWidget.setControlCausedMessage("attribute.dirty", "Dirty", IMessageProvider.INFORMATION);
                  if (!isDirty()) {
                     markDirty();
                  }
               } else {
                  xWidget.removeControlCausedMessage("attribute.dirty");
               }
            } catch (Exception ex) {
               xWidget.setControlCausedMessage("attribute.dirty", "Unable to compute isDirty", IMessageProvider.ERROR);
            }
         }
      }
   }
}

Back to the top