| /** |
| * Copyright (c) 2011, 2014 - Lunifera GmbH (Gross Enzersdorf) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| |
| package org.eclipse.osbp.vaaclipse.addons.common.status; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.Set; |
| |
| import org.eclipse.e4.core.services.events.IEventBroker; |
| import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| import org.eclipse.osbp.runtime.common.validation.IStatus; |
| import org.eclipse.osbp.vaaclipse.addons.common.api.IE4Topics; |
| import org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope; |
| |
| /** |
| * The Class StatusScope. |
| */ |
| public class StatusScope implements IStatusScope { |
| |
| /** The status list. */ |
| private final List<IStatus> statusList = new ArrayList<IStatus>(10); |
| |
| /** The status hash. */ |
| private final Set<String> statusHash = new HashSet<String>(); |
| |
| /** The part. */ |
| private final MPart part; |
| |
| /** The event broker. */ |
| private final IEventBroker eventBroker; |
| |
| /** |
| * Instantiates a new status scope. |
| * |
| * @param part |
| * the part |
| * @param eventBroker |
| * the event broker |
| */ |
| public StatusScope(MPart part, IEventBroker eventBroker) { |
| this.part = part; |
| this.eventBroker = eventBroker; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#getMPart() |
| */ |
| @Override |
| public MPart getMPart() { |
| return part; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#addStatus(org.eclipse.osbp.runtime.common.validation.IStatus) |
| */ |
| @Override |
| public void addStatus(IStatus status) { |
| |
| String hash = toHash(status); |
| if (statusHash.contains(hash)) { |
| return; |
| } |
| |
| if (statusList.contains(status)) { |
| return; |
| } |
| |
| statusHash.add(hash); |
| statusList.add(status); |
| |
| sendEvent(Collections.singletonList(status)); |
| } |
| |
| /** |
| * To hash. |
| * |
| * @param status |
| * the status |
| * @return the string |
| */ |
| private String toHash(IStatus status) { |
| String hash = String.format("%s-%s-%s", status.getCode(), |
| status.getProperty(IStatus.PROP_FIELD_ID), |
| status.getProperty(IStatus.PROP_JAVAX_PROPERTY_PATH)); |
| return hash; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#addAllStatus(java.util.Collection) |
| */ |
| @Override |
| public void addAllStatus(Collection<IStatus> status) { |
| if (status != null) { |
| |
| for (IStatus s : status) { |
| if (!statusList.contains(s)) { |
| String hash = toHash(s); |
| if (statusHash.contains(hash)) { |
| continue; |
| } |
| statusHash.add(hash); |
| statusList.add(s); |
| } |
| } |
| |
| sendEvent(status); |
| } |
| } |
| |
| /** |
| * Send event. |
| * |
| * @param status |
| * the status |
| */ |
| private void sendEvent(Collection<IStatus> status) { |
| Map<String, Object> props = new HashMap<String, Object>(); |
| props.put(IE4Topics.StatusManagerEvents.PROP_SCOPE, this); |
| props.put(IE4Topics.StatusManagerEvents.PROP_STATUS, status); |
| eventBroker.post( |
| IE4Topics.StatusManagerEvents.VALIDATIONS_CHANGED_TOPIC, props); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#clearStatus() |
| */ |
| @Override |
| public void clearStatus() { |
| statusList.clear(); |
| statusHash.clear(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#getAllStatus() |
| */ |
| @Override |
| public List<IStatus> getAllStatus() { |
| return new ArrayList<IStatus>(statusList); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#removeStatus(org.eclipse.osbp.runtime.common.validation.IStatus) |
| */ |
| @Override |
| public void removeStatus(IStatus status) { |
| statusList.remove(status); |
| statusHash.remove(toHash(status)); |
| |
| sendEvent(Collections.singletonList(status)); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#removeAllStatus(java.util.Collection) |
| */ |
| @Override |
| public void removeAllStatus(Collection<IStatus> status) { |
| for (IStatus s : status) { |
| String hash = toHash(s); |
| statusHash.remove(hash); |
| statusList.remove(s); |
| } |
| |
| sendEvent(status); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.status.IStatusScope#modifyStatus(java.util.Collection, java.util.Collection) |
| */ |
| @Override |
| public void modifyStatus(Collection<IStatus> oldStatus, |
| Collection<IStatus> newStatus) { |
| |
| for (IStatus s : oldStatus) { |
| String hash = toHash(s); |
| statusHash.remove(hash); |
| statusList.remove(s); |
| } |
| |
| if (newStatus != null) { |
| for (IStatus s : newStatus) { |
| // avoid duplicate add |
| if (!statusList.contains(s)) { |
| String hash = toHash(s); |
| if (statusHash.contains(hash)) { |
| continue; |
| } |
| |
| statusHash.add(hash); |
| statusList.add(s); |
| } |
| } |
| } |
| |
| sendEvent(newStatus); |
| } |
| } |