blob: aaaa7f4d1eeb75a1f6f47d875bd505708722c014 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 Rushan R. Gilmullin and others.
* 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:
* Rushan R. Gilmullin - initial API and implementation
*******************************************************************************/
package org.eclipse.osbp.vaaclipse.behaviour.idgenerator;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
public class ElementIdGeneratorAddon {
@Inject
IEventBroker eventBroker;
@Inject
MApplication app;
private EventHandler childrenHandler = new EventHandler() {
public void handleEvent(Event event) {
Object changedObj = event.getProperty(UIEvents.EventTags.ELEMENT);
if (!(changedObj instanceof MElementContainer<?>))
return;
String eventType = (String) event
.getProperty(UIEvents.EventTags.TYPE);
if (UIEvents.EventTypes.ADD.equals(eventType)) {
Object newValue = event
.getProperty(UIEvents.EventTags.NEW_VALUE);
if (!(newValue instanceof MUIElement))
return;
MUIElement element = (MUIElement) newValue;
if (element.getElementId() == null
|| element.getElementId().trim().isEmpty()) {
element.setElementId(UUID.randomUUID().toString());
}
}
}
};
@PostConstruct
void init(IEclipseContext context) {
eventBroker.subscribe(UIEvents.ElementContainer.TOPIC_CHILDREN,
childrenHandler);
}
@PreDestroy
void removeListeners() {
eventBroker.unsubscribe(childrenHandler);
}
}