Skip to main content
summaryrefslogtreecommitdiffstats
blob: 21a415a2e9ac66d82b72cc857226f6eb7bb5301d (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
/*******************************************************************************
 * 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;

import java.util.Arrays;
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.viewers.IContentProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeSideSorter;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;

/**
 * The basis for the comments in this class can be found at
 * http://www.eclipse.org/articles/treeviewer-cg/TreeViewerArticle.htm
 * 
 * @author Ryan D. Brooks
 */
public class RelationContentProvider implements ITreeContentProvider {
   private static Object[] EMPTY_ARRAY = new Object[0];
   private Artifact artifactRoot;
   private final Map<Object, Object> childToParentMap = new HashMap<Object, Object>();

   @Override
   public void dispose() {
      // do nothing
   }

   /**
    * Notifies this content provider that the given viewer's input has been switched to a different element.
    * <p>
    * A typical use for this method is registering the content provider as a listener to changes on the new input (using
    * model-specific means), and deregistering the viewer from the old input. In response to these change notifications,
    * the content provider propagates the changes to the viewer.
    * </p>
    * 
    * @param viewer the viewer
    * @param oldInput the old input element, or <code>null</code> if the viewer did not previously have an input
    * @param newInput the new input element, or <code>null</code> if the viewer does not have an input
    * @see IContentProvider#inputChanged(Viewer, Object, Object)
    */
   @Override
   public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
      this.artifactRoot = (Artifact) newInput;
   }

   /**
    * The tree viewer calls its content provider&#8217;s getChildren method when it needs to create or display the child
    * elements of the domain object, <b>parent </b>. This method should answer an array of domain objects that represent
    * the unfiltered children of <b>parent </b>
    * 
    * @see ITreeContentProvider#getChildren(Object)
    */
   @Override
   public Object[] getChildren(Object parentElement) {
      try {
         if (parentElement instanceof Artifact) {
            Collection<RelationType> relationTypes = artifactRoot.getValidRelationTypes();
            for (RelationType type : relationTypes) {
               childToParentMap.put(type, parentElement);
            }
            Object[] ret = relationTypes.toArray();
            Arrays.sort(ret);
            return ret;
         } else if (parentElement instanceof RelationType) {
            RelationType relationType = (RelationType) parentElement;
            int sideAMax =
               RelationTypeManager.getRelationSideMax(relationType, artifactRoot.getArtifactType(), RelationSide.SIDE_A);
            int sideBMax =
               RelationTypeManager.getRelationSideMax(relationType, artifactRoot.getArtifactType(), RelationSide.SIDE_B);

            RelationTypeSideSorter sideA =
               RelationManager.createTypeSideSorter(artifactRoot, relationType, RelationSide.SIDE_A);
            RelationTypeSideSorter sideB =
               RelationManager.createTypeSideSorter(artifactRoot, relationType, RelationSide.SIDE_B);
            boolean onSideA = sideBMax > 0;
            boolean onSideB = sideAMax > 0;

            childToParentMap.put(sideA, parentElement);
            childToParentMap.put(sideB, parentElement);

            if (onSideA && onSideB) {
               return new Object[] {sideA, sideB};
            } else if (onSideA) {
               return new Object[] {sideA};
            } else if (onSideB) {
               return new Object[] {sideB};
            }
         } else if (parentElement instanceof RelationTypeSideSorter) {
            RelationTypeSideSorter relationSorter = (RelationTypeSideSorter) parentElement;
            List<? extends IArtifact> artifacts = artifactRoot.getRelatedArtifacts(relationSorter);
            WrapperForRelationLink[] wrapper = new WrapperForRelationLink[artifacts.size()];
            for (int i = 0; i < artifacts.size(); i++) {
               Artifact sideArtifact = artifacts.get(i).getFullArtifact();
               if (relationSorter.getSide().isSideA()) {
                  wrapper[i] =
                     new WrapperForRelationLink(relationSorter.getRelationType(), sideArtifact, sideArtifact,
                        relationSorter.getArtifact());
               } else {
                  wrapper[i] =
                     new WrapperForRelationLink(relationSorter.getRelationType(), sideArtifact,
                        relationSorter.getArtifact(), sideArtifact);
               }
               childToParentMap.put(wrapper[i], parentElement);
            }
            return wrapper;
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex);
      }

      return EMPTY_ARRAY;
   }

   @Override
   public Object getParent(Object element) {
      return childToParentMap.get(element);
   }

   /**
    * The tree viewer asks its content provider if the domain object represented by <b>element </b> has any children.
    * This method is used by the tree viewer to determine whether or not a plus or minus should appear on the tree
    * widget.
    * 
    * @see ITreeContentProvider#hasChildren(Object)
    */
   @Override
   public boolean hasChildren(Object element) {
      if (element instanceof RelationTypeSideSorter) {
         try {
            RelationTypeSideSorter sorter = (RelationTypeSideSorter) element;
            return RelationManager.getRelatedArtifactsCount(sorter.getArtifact(), sorter.getRelationType(),
               sorter.getSide()) > 0;
         } catch (OseeCoreException ex) {
            OseeLog.log(SkynetGuiPlugin.class, Level.SEVERE, ex);
            return false;
         }
      } else if (element instanceof RelationType) {
         return true;
      }

      return getChildren(element).length > 0;
   }

   /**
    * This is the method invoked by calling the <b>setInput </b> method on the tree viewer. In fact, the <b>getElements
    * </b> method is called only in response to the tree viewer's <b>setInput </b> method and should answer with the
    * appropriate domain objects of the inputElement. The <b>getElements </b> and <b>getChildren </b> methods operate in
    * a similar way. Depending on your domain objects, you may have the <b>getElements </b> simply return the result of
    * calling <b>getChildren </b>. The two methods are kept distinct because it provides a clean way to differentiate
    * between the root domain object and all other domain objects.
    * 
    * @see IStructuredContentProvider#getElements(Object)
    */
   @Override
   public Object[] getElements(Object inputElement) {
      return getChildren(inputElement);
   }
}

Back to the top