Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6af429b1ac465707f0da83ec218c0e0069225f26 (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
/*********************************************************************
 * Copyright (c) 2004, 2007 Boeing
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Boeing - initial API and implementation
 **********************************************************************/

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

import java.lang.ref.WeakReference;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.framework.core.data.AttributeTypeToken;
import org.eclipse.osee.framework.core.util.Result;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.validation.IOseeValidator;
import org.eclipse.osee.framework.skynet.core.validation.OseeValidator;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.widgets.Composite;

/**
 * @author Donald G. Dunne
 */
public class XTextDam extends XText implements AttributeWidget, EditorWidget {

   public static final String WIDGET_ID = XTextDam.class.getSimpleName();
   private Artifact artifactStrongRef;
   private AttributeTypeToken attributeType;
   private final boolean isWeakReference;
   private WeakReference<Artifact> artifactRef;
   private EditorData editorData;

   public XTextDam(String displayLabel) {
      this(displayLabel, false);
   }

   public XTextDam(String displayLabel, boolean isWeakReference) {
      super(displayLabel);
      this.isWeakReference = isWeakReference;
   }

   @Override
   public Artifact getArtifact() {
      Artifact toReturn = null;
      if (isWeakReference) {
         if (artifactRef.get() == null) {
            throw new OseeStateException("Artifact has been garbage collected");
         }
         toReturn = artifactRef.get();
      } else {
         toReturn = artifactStrongRef;
      }
      return toReturn;
   }

   @Override
   public AttributeTypeToken getAttributeType() {
      return attributeType;
   }

   private void setArtifact(Artifact artifact) {
      if (isWeakReference) {
         this.artifactRef = new WeakReference<>(artifact);
      } else {
         artifactStrongRef = artifact;
      }
   }

   @Override
   public void setAttributeType(Artifact artifact, AttributeTypeToken attributeType) {
      this.attributeType = attributeType;
      setArtifact(artifact);
      refresh();
   }

   @Override
   public void refresh() {
      super.set(getArtifact().getSoleAttributeValue(getAttributeType(), ""));
   }

   @Override
   public void saveToArtifact() {
      String value = get();
      if (!Strings.isValid(value)) {
         getArtifact().deleteSoleAttribute(getAttributeType());
      } else if (!value.equals(getArtifact().getSoleAttributeValue(getAttributeType(), ""))) {
         getArtifact().setSoleAttributeValue(getAttributeType(), value);
      }
   }

   @Override
   public Result isDirty() {
      if (isEditable()) {
         String enteredValue = get();
         String storedValue = getArtifact().getSoleAttributeValue(getAttributeType(), "");
         if (!enteredValue.equals(storedValue)) {
            return new Result(true, attributeType + " is dirty");
         }
      }
      return Result.FalseResult;
   }

   @Override
   public IStatus isValid() {
      IStatus status = super.isValid();
      if (status.isOK()) {
         try {
            if (getArtifact() != null && getAttributeType() != null) {
               status =
                  OseeValidator.getInstance().validate(IOseeValidator.SHORT, getArtifact(), getAttributeType(), get());
            }
         } catch (OseeCoreException ex) {
            status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error getting Artifact", ex);
         }
      }
      return status;
   }

   @Override
   public void revert() {
      setAttributeType(getArtifact(), getAttributeType());
   }

   @Override
   protected void createControls(Composite parent, int horizontalSpan) {
      super.createControls(parent, horizontalSpan);
      if (isAutoSave()) {
         getStyledText().addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(FocusEvent e) {
               if (getArtifact() != null && getArtifact().isValid()) {
                  saveToArtifact();
                  if (getArtifact().isDirty()) {
                     String comment = null;
                     if (editorData != null && Strings.isValid(editorData.getEditorName())) {
                        comment = editorData.getEditorName() + " Auto-Save";
                     } else {
                        comment = "XTextDam Auto-Save";
                     }
                     getArtifact().persist(comment);
                  }
               }
            }

         });
      }
   }

   @Override
   public void setEditorData(EditorData editorData) {
      this.editorData = editorData;
   }

}

Back to the top