| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * 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: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.vaaclipse.addons.common.resource; |
| |
| import java.io.IOException; |
| import java.util.HashSet; |
| import java.util.Set; |
| import java.util.Timer; |
| import java.util.TimerTask; |
| |
| import javax.annotation.PostConstruct; |
| import javax.inject.Inject; |
| |
| import org.eclipse.e4.core.services.events.IEventBroker; |
| import org.eclipse.e4.ui.model.application.ui.advanced.MPerspectiveStack; |
| import org.eclipse.e4.ui.workbench.IModelResourceHandler; |
| import org.eclipse.e4.ui.workbench.UIEvents; |
| import org.eclipse.e4.ui.workbench.UIEvents.EventTags; |
| import org.eclipse.osbp.vaaclipse.addons.common.event.EventTopicNormalizer; |
| import org.osgi.service.event.Event; |
| import org.osgi.service.event.EventHandler; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| /** |
| * Observes changes in the layout and forces a resource save. |
| */ |
| public class LayoutChangedObserver implements EventHandler { |
| |
| /** The Constant ELEMENT_CONTAINER_SELECTED_ELEMENT_SET. */ |
| private static final String ELEMENT_CONTAINER_SELECTED_ELEMENT_SET = "org/eclipse/e4/ui/model/ui/ElementContainer/selectedElement/SET"; |
| |
| /** The Constant LOGGER. */ |
| private static final Logger LOGGER = LoggerFactory |
| .getLogger(LayoutChangedObserver.class); |
| |
| /** The topics. */ |
| private static Set<String> TOPICS = new HashSet<String>(); |
| static { |
| TOPICS.add(UIEvents.UIElement.TOPIC_CONTAINERDATA); |
| TOPICS.add(UIEvents.ElementContainer.TOPIC_SELECTEDELEMENT); |
| TOPICS.add(UIEvents.ElementContainer.TOPIC_CHILDREN); |
| } |
| |
| /** The event broker. */ |
| @Inject |
| private IEventBroker eventBroker; |
| |
| /** The topic normalizer. */ |
| @Inject |
| private EventTopicNormalizer topicNormalizer; |
| |
| /** The resource handler. */ |
| @Inject |
| private IModelResourceHandler resourceHandler; |
| |
| /** The current timer. */ |
| private Timer currentTimer; |
| |
| /** |
| * This method is called when information about an LayoutChanged which was |
| * previously requested using an asynchronous interface becomes available. |
| */ |
| @PostConstruct |
| public void setup() { |
| // commented temporarily by ri - don't save perspectives |
| // for (String topic : TOPICS) { |
| // eventBroker.subscribe(topic, this); |
| // } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event) |
| */ |
| @Override |
| public void handleEvent(Event event) { |
| Object changedObj = event.getProperty(EventTags.ELEMENT); |
| |
| if (topicNormalizer.unwrapTopic(event.getTopic()).equals( |
| ELEMENT_CONTAINER_SELECTED_ELEMENT_SET)) { |
| if (!(changedObj instanceof MPerspectiveStack)) { |
| return; |
| } |
| } |
| |
| synchronized (this) { |
| // Wait for 1000ms before saving. Most probably several events will |
| // arrive in the next milli seconds |
| if (currentTimer == null) { |
| currentTimer = new Timer(); |
| currentTimer.schedule(new TimerTask() { |
| @Override |
| public void run() { |
| try { |
| resourceHandler.save(); |
| LOGGER.debug("ApplicationModel-Resource saved."); |
| resetTimer(); |
| } catch (IOException e) { |
| LOGGER.warn("{}", e); |
| } |
| } |
| }, 1000); |
| LOGGER.debug("Scheduled Model-Save-Timer."); |
| } else { |
| LOGGER.debug("Timer already active."); |
| } |
| } |
| |
| } |
| |
| /** |
| * This method is called when information about an LayoutChanged which was |
| * previously requested using an asynchronous interface becomes available. |
| */ |
| protected void resetTimer() { |
| synchronized (this) { |
| currentTimer = null; |
| } |
| } |
| |
| /** |
| * This method is called when information about an LayoutChanged which was |
| * previously requested using an asynchronous interface becomes available. |
| */ |
| public void dispose() { |
| eventBroker.unsubscribe(this); |
| } |
| |
| } |