Skip to main content
summaryrefslogtreecommitdiffstats
blob: a000eeedadce6af0740ceb693343c0c0d63ac4de (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
/*******************************************************************************
 * Copyright (c) 2010 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.ote.ui.message.tree;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.nebula.widgets.xviewer.XViewerColumn;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.framework.ui.swt.OverlayImage;
import org.eclipse.osee.framework.ui.swt.OverlayImage.Location;
import org.eclipse.osee.ote.ui.message.OteMessageImage;
import org.eclipse.osee.ote.ui.message.messageXViewer.MessageXViewerFactory;
import org.eclipse.osee.ote.ui.message.watch.ElementPath;
import org.eclipse.swt.graphics.Image;

/**
 * Defines a node in a {@link org.eclipse.swt.widgets.Tree} that maintains detials on
 * {@link org.eclipse.osee.ote.message.Message} for display
 * 
 * @author Ken J. Aguilar
 */
public class MessageNode extends AbstractTreeNode {

   private final Map<String, ElementNode> pathToElementNode = new HashMap<String, ElementNode>();

   protected static final Image normalImg = ImageManager.getImage(OteMessageImage.GEAR);
   @SuppressWarnings("unused")
   private static final Image wireAIUImg = ImageManager.getImage(OteMessageImage.WIRE_AIU);
   protected static final Image errorImg =
      new OverlayImage(normalImg, ImageDescriptor.createFromImage(ImageManager.getImage(OteMessageImage.ERROR_SM)),
         Location.BOT_RIGHT).createImage();
   private final String shortenedMessageName;
   private final String packageName;
   private final String type;

   public MessageNode(String msgClassName, Image image) {
      super(msgClassName, image);
      shortenedMessageName = msgClassName.substring(msgClassName.lastIndexOf('.') + 1);
      packageName = msgClassName.substring(0, msgClassName.lastIndexOf('.'));
      type = packageName.substring(packageName.lastIndexOf('.') + 1);
   }

   public MessageNode(String msgClassName) {
      this(msgClassName, normalImg);
   }

   public String getType() {
      return type;
   }

   public String getPackageName() {
      return packageName;
   }

   @Override
   public boolean canSetValue() {
      return false;
   }

   @Override
   public void setEnabled(boolean enabled) {
      super.setEnabled(enabled);
      if (!enabled) {
         setImage(errorImg);
      }
   }

   @Override
   String getLabel() {
      return getName();
   }

   @Override
   public <T> T visit(INodeVisitor<T> visitor) {
      return visitor.messageNode(this);
   }

   @Override
   public Image getImage(XViewerColumn columns) {
      if (columns == null) {
         return null;
      }
      if (columns.equals(MessageXViewerFactory.name)) {
         return getImage();
      }
      return null;
   }

   @Override
   public String getLabel(XViewerColumn columns) {
      if (columns == null) {
         return "";
      }
      if (columns.equals(MessageXViewerFactory.name)) {
         return getName();
      }
      return "";
   }

   /**
    * @param element
    */
   public ElementNode findChildElement(ElementPath element) {
      return pathToElementNode.get(element.asString());
   }

   public boolean hasDescendant(ElementPath element) {
      if (pathToElementNode.containsKey(element.asString())) {
         return true;
      }
      String path = element.asString();
      for (Map.Entry<String, ElementNode> entry : pathToElementNode.entrySet()) {
         if (entry.getKey().startsWith(path)) {
            if (entry.getValue().hasDescendant(element)) {
               return true;
            }
         }
      }
      return false;
   }

   public ElementNode findDescendant(ElementPath element) {
      String path = element.asString();
      ElementNode node = pathToElementNode.get(path);
      if (node == null) {
         for (Map.Entry<String, ElementNode> entry : pathToElementNode.entrySet()) {
            if (path.startsWith(entry.getKey())) {
               node = entry.getValue().findDescendant(element);
               if (node != null) {
                  return node;
               }
            }
         }
      }
      return node;
   }

   @Override
   public String getName() {
      return shortenedMessageName;
   }

   public String getMessageClassName() {
      return super.getName();
   }

   public void addChild(ElementNode node) {
      pathToElementNode.put(node.getElementPath().asString(), node);
      node.setParent(this);
   }

   @Override
   public Collection<ElementNode> getChildren() {
      return pathToElementNode.values();
   }

   @Override
   public boolean hasChildren() {
      return !pathToElementNode.isEmpty();
   }

   @Override
   public void removeAll() {
      for (AbstractTreeNode child : pathToElementNode.values()) {
         child.dispose();
      }
      pathToElementNode.clear();
   }

   @Override
   public void deleteChildren(Collection<AbstractTreeNode> children) {
      for (AbstractTreeNode child : children) {
         pathToElementNode.remove(((ElementNode) child).getElementPath().asString());
         child.dispose();
      }
   }

   public void collectDescendants(Collection<? super ElementNode> descendants) {
      for (ElementNode node : pathToElementNode.values()) {
         descendants.add(node);
         node.collectDescendants(descendants);
      }
   }
}

Back to the top