Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1a4fcf04601f617cc3e019260c32884c06143ad7 (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
/*******************************************************************************
 * 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.framework.ui.skynet.explorer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.event.DefaultBasicGuidArtifact;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
import org.eclipse.osee.framework.skynet.core.event.EventUtil;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.IArtifactEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.AccessTopicEvent;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event.model.EventModType;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.ui.skynet.IArtifactExplorerEventHandler;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

/**
 * Common location for event handling for ArtifactExplorers in order to keep number of registrations and processing to a
 * minimum.
 *
 * @author Donald G. Dunne
 */
public class ArtifactExplorerEventManager implements IArtifactEventListener, EventHandler {

   static List<IArtifactExplorerEventHandler> handlers = new CopyOnWriteArrayList<>();
   static ArtifactExplorerEventManager instance;

   public static void add(IArtifactExplorerEventHandler iWorldEventHandler) {
      if (instance == null) {
         instance = new ArtifactExplorerEventManager();
         OseeEventManager.addListener(instance);
      }
      ArtifactExplorerEventManager.handlers.add(iWorldEventHandler);
   }

   public static void remove(IArtifactExplorerEventHandler iWorldEventHandler) {
      if (instance != null) {
         ArtifactExplorerEventManager.handlers.remove(iWorldEventHandler);
      }
   }

   @Override
   public List<? extends IEventFilter> getEventFilters() {
      // Can't filter cause this class handles all artifact explorers which can care about different branches
      return null;
   }

   /**
    * @return true if branch is not null, matches the branch for the event and is not deleted or purged
    */
   private boolean isArtifactExplorerValidForEvents(ArtifactExplorer artifactExplorer, BranchId brancFromEvent) {
      boolean toReturn = false;
      if (artifactExplorer != null) {
         Branch branch = artifactExplorer.getBranch();
         toReturn = branch != null && brancFromEvent.equals(branch) && !branch.isDeleted() && !branch.isPurged();
      }
      return toReturn;
   }

   @Override
   public void handleArtifactEvent(final ArtifactEvent artifactEvent, Sender sender) {
      IWorkbench workbench = PlatformUI.getWorkbench();
      if (workbench == null || workbench.isClosing() || workbench.isStarting()) {
         return;
      }

      // Do not process event if branch is null, deleted or purged.  But, don't want to remove as handler cause another branch may be selected
      final List<IArtifactExplorerEventHandler> handlersToProcess = new ArrayList<>();
      for (IArtifactExplorerEventHandler handler : handlers) {
         if (handler.isDisposed()) {
            handlers.remove(handler);
         } else if (isArtifactExplorerValidForEvents(handler.getArtifactExplorer(), artifactEvent.getBranch())) {
            handlersToProcess.add(handler);
         }
      }

      EventUtil.eventLog("ArtifacExplorer: handleArtifactEvent called [" + artifactEvent + "] - sender " + sender + "");
      final Collection<Artifact> modifiedArts =
         artifactEvent.getCacheArtifacts(EventModType.Modified, EventModType.Reloaded);
      artifactEvent.getRelCacheArtifacts();
      final Collection<EventBasicGuidArtifact> deletedPurgedArts =
         artifactEvent.get(EventModType.Deleted, EventModType.Purged);
      final Collection<DefaultBasicGuidArtifact> relOrderChangedArtifacts = artifactEvent.getRelOrderChangedArtifacts();

      Displays.ensureInDisplayThread(new Runnable() {
         @Override
         public void run() {
            if (!deletedPurgedArts.isEmpty()) {
               for (IArtifactExplorerEventHandler handler : handlersToProcess) {
                  try {
                     if (!handler.isDisposed()) {
                        handler.getArtifactExplorer().getTreeViewer().remove(
                           deletedPurgedArts.toArray(new Object[deletedPurgedArts.size()]));
                     }
                  } catch (Exception ex) {
                     OseeLog.log(Activator.class, Level.SEVERE,
                        "Error processing event handler for deleted - " + handler, ex);
                  }
               }
            }
            for (IArtifactExplorerEventHandler handler : handlersToProcess) {
               try {
                  if (!handler.isDisposed()) {
                     for (Artifact artifact : modifiedArts) {
                        // Don't refresh deleted artifacts
                        if (artifact.isDeleted()) {
                           continue;
                        }
                        handler.getArtifactExplorer().getTreeViewer().update(artifact, null);
                     }

                     // We do not need to refresh each artifact for each handler, just the handler itself
                     handler.getArtifactExplorer().getTreeViewer().refresh();

                     for (DefaultBasicGuidArtifact guidArt : relOrderChangedArtifacts) {
                        try {
                           Artifact artifact = ArtifactCache.getActive(guidArt);
                           if (artifact != null) {
                              handler.getArtifactExplorer().getTreeViewer().refresh(artifact);
                           }
                        } catch (Exception ex) {
                           OseeLog.log(Activator.class, Level.SEVERE, ex);
                        }
                     }
                  }
               } catch (Exception ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, "Error processing event handler for modified - " + handler,
                     ex);
               }
            }
         }
      });
   }

   @Override
   public void handleEvent(Event event) {
      try {
         if (AccessTopicEvent.ACCESS_ARTIFACT_LOCK_MODIFIED.matches(event)) {
            for (final IArtifactExplorerEventHandler handler : handlers) {
               if (!handler.isDisposed()) {
                  Displays.ensureInDisplayThread(new Runnable() {

                     @Override
                     public void run() {
                        handler.getArtifactExplorer().getTreeViewer().refresh();
                        handler.getArtifactExplorer().refreshBranchWarning();
                     }
                  });
               }
            }
         }
      } catch (Exception ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
   }

}

Back to the top