Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / actioninfo / impl / DefaultActionInfoManager.java @ 38564

History | View | Annotate | Download (3.44 KB)

1
package org.gvsig.andami.actioninfo.impl;
2

    
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.Comparator;
6
import java.util.HashMap;
7
import java.util.Iterator;
8
import java.util.List;
9
import java.util.Map;
10

    
11
import org.gvsig.andami.actioninfo.ActionInfo;
12
import org.gvsig.andami.actioninfo.ActionInfoManager;
13
import org.gvsig.andami.actioninfo.ActionInfoStatusCache;
14
import org.gvsig.andami.plugins.IExtension;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

    
18
public class DefaultActionInfoManager implements  ActionInfoManager {
19
        private static Logger logger = LoggerFactory.getLogger(DefaultActionInfoManager.class);
20
        
21
        private Map<String, ActionInfo>actions = new HashMap<String, ActionInfo>();
22
        private int anonymousCounter = 1;
23
        
24
    public ActionInfo createAction(Class<? extends IExtension> extension, String name, String text, String command, String icon, String enableText, long position, String tip) {
25
            name = emptyToNull(name);
26
            String actionName = name;
27
            if( actionName==null ){
28
                    actionName = String.format("anonymous__%04d",this.anonymousCounter++);
29
            }
30
            ActionInfo action = new DefaultActionInfo(extension, actionName, text, command, icon, enableText, position, tip);
31
            ActionInfo previous = this.getAction(action.getName());
32
            if( previous != null ) {
33
                    action.merge(previous);
34
            }
35
            if( name == null  ){
36
                    logger.info("createAction: name of action is null/empty, rename to '"+actionName+"' ("+action.toString()+").");
37
            }
38
            if( action.getLabel()==null && action.getIconName()==null ) {
39
                    logger.info("createAction(name='"+name+"'): text and icon of action is null");
40
            }
41
            return action;
42
    }
43
        
44
        private String emptyToNull(String s) {
45
                if( s==null ) {
46
                        return null;
47
                }
48
                return "".equals(s.trim())? null:s;
49
        }
50
        
51
    public ActionInfo registerAction(ActionInfo action) {
52
            if( action == null ) {
53
                    // Avisamos en el log de que se intenta registrar una accion null, intentado
54
                    // sacar el stack para que se pueda ver quien lo esta haciendo, pero no
55
                    // provocamos un error, solo retornamos null.
56
                        try {
57
                                throw new IllegalArgumentException();
58
                        } catch (IllegalArgumentException e) {
59
                                logger.info("registerAction(null).", e);
60
                        }
61
                        return null;
62
            }
63
            ActionInfo previous = this.getAction(action.getName());
64
            if( previous != null ) {
65
                    previous.merge(action);
66
                return previous;
67
            } else {
68
                    this.actions.put(action.getName(), action);
69
                return action;
70
            }
71
    }
72
    
73
    public ActionInfo getAction(String name) {
74
            if( name == null || "".equals(name) ) {
75
                        try {
76
                                throw new IllegalArgumentException();
77
                        } catch (IllegalArgumentException e) {
78
                                logger.info("getAction(null/empty) return null.", e);
79
                                return null;
80
                        }
81
            }
82
            return this.actions.get(name);
83
    }
84
    
85
    public Iterator<ActionInfo> getActions() {
86
            List<ActionInfo> actions = new ArrayList<ActionInfo>();
87
            actions.addAll(this.actions.values());
88
            Collections.sort(actions, new Comparator<ActionInfo>() {
89
                    public int compare(ActionInfo arg0, ActionInfo arg1) {
90
                            String s0 = String.format("%s/%012d", arg0.getPluginName(), arg0.getPosition());
91
                            String s1 = String.format("%s/%012d", arg1.getPluginName(), arg1.getPosition());
92
                            return s0.compareTo(s1);
93
                    };
94
                });
95
            return actions.iterator();
96
    }
97
    
98
    public ActionInfoStatusCache createActionStatusCache() {
99
            return new DefaultActionInfoStatusCache();
100
    }
101

    
102
}