All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pepsoft.worldpainter.util.BetterAction Maven / Gradle / Ivy

There is a newer version: 2.23.2
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.pepsoft.worldpainter.util;

import org.jetbrains.annotations.NonNls;
import org.pepsoft.worldpainter.App;
import org.pepsoft.worldpainter.EventLogger;
import org.pepsoft.worldpainter.vo.EventVO;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author pepijn
 */
public abstract class BetterAction extends org.pepsoft.util.swing.BetterAction {
    public BetterAction(@NonNls String id, String name, Icon icon) {
        this(id, name, icon, true);
    }

    public BetterAction(@NonNls String id, String name) {
        this(id, name, true);
    }

    public BetterAction(@NonNls String id, String name, Icon icon, boolean logEvent) {
        super(name, icon);
        statisticsKey = createStatisticsKey(id);
        this.logEvent = logEvent;
        putValue(App.KEY_HELP_KEY, "Action/" + id);
    }

    public BetterAction(String id, String name, boolean logEvent) {
        super(name);
        statisticsKey = createStatisticsKey(id);
        this.logEvent = logEvent;
        putValue(App.KEY_HELP_KEY, "Action/" + id);
    }

    public final String getStatisticsKey() {
        return statisticsKey;
    }

    public final boolean isLogEvent() {
        return logEvent;
    }
    
    @Override
    public final void actionPerformed(ActionEvent e) {
        performAction(e);
        if (logEvent) {
            synchronized (actionCounts) {
                if (actionCounts.containsKey(statisticsKey)) {
                    actionCounts.put(statisticsKey, actionCounts.get(statisticsKey) + 1);
                } else {
                    actionCounts.put(statisticsKey, 1L);
                }
            }
        }
    }
    
    protected abstract void performAction(ActionEvent e);
    
    public static void flushEvents(EventLogger eventLogger) {
        synchronized (actionCounts) {
            for (Map.Entry entry: actionCounts.entrySet()) {
                eventLogger.logEvent(new EventVO(entry.getKey()).count(entry.getValue()));
            }
            actionCounts.clear();
        }
    }
    
    private static String createStatisticsKey(String id) {
        return "action." + id.replaceAll("[ \\t\\n\\x0B\\f\\r.]", "");
    }
    
    private final String statisticsKey;
    private final boolean logEvent;

    private static final Map actionCounts = new HashMap<>();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy