1 /*
2  * Copyright (c) 2008-2019 Emmanuel Dupuy.
3  * This project is distributed under the GPLv3 license.
4  * This is a Copyleft license that gives the user the right to use,
5  * copy and modify the code freely for non-commercial purposes.
6  */
7 
8 package org.jd.gui.service.actions;
9 
10 import org.jd.gui.api.API;
11 import org.jd.gui.api.model.Container;
12 import org.jd.gui.service.extension.ExtensionService;
13 import org.jd.gui.spi.ContextualActionsFactory;
14 
15 import javax.swing.*;
16 import java.util.*;
17 
18 public class ContextualActionsFactoryService {
19     protected static final ContextualActionsFactoryService CONTEXTUAL_ACTIONS_FACTORY_SERVICE = new ContextualActionsFactoryService();
20 
getInstance()21     public static ContextualActionsFactoryService getInstance() { return CONTEXTUAL_ACTIONS_FACTORY_SERVICE; }
22 
23     protected static final ActionNameComparator COMPARATOR = new ActionNameComparator();
24 
25     protected final Collection<ContextualActionsFactory> providers = ExtensionService.getInstance().load(ContextualActionsFactory.class);
26 
get(API api, Container.Entry entry, String fragment)27     public Collection<Action> get(API api, Container.Entry entry, String fragment) {
28         HashMap<String, ArrayList<Action>> mapActions = new HashMap<>();
29 
30         for (ContextualActionsFactory provider : providers) {
31             Collection<Action> actions = provider.make(api, entry, fragment);
32 
33             for (Action action : actions) {
34                 String groupName = (String)action.getValue(ContextualActionsFactory.GROUP_NAME);
35                 ArrayList<Action> list = mapActions.get(groupName);
36 
37                 if (list == null) {
38                     mapActions.put(groupName, list=new ArrayList<>());
39                 }
40 
41                 list.add(action);
42             }
43         }
44 
45         if (!mapActions.isEmpty()) {
46             ArrayList<Action> result = new ArrayList<>();
47 
48             // Sort by group names
49             ArrayList<String> groupNames = new ArrayList<>(mapActions.keySet());
50             Collections.sort(groupNames);
51 
52             for (String groupName : groupNames) {
53                 if (! result.isEmpty()) {
54                     // Add 'null' to mark a separator
55                     result.add(null);
56                 }
57                 // Sort by names
58                 ArrayList<Action> actions = mapActions.get(groupName);
59                 Collections.sort(actions, COMPARATOR);
60                 result.addAll(actions);
61             }
62 
63             return result;
64         } else {
65             return Collections.emptyList();
66         }
67     }
68 
69     protected static class ActionNameComparator implements Comparator<Action> {
70         @Override
compare(Action a1, Action a2)71         public int compare(Action a1, Action a2) {
72             String n1 = (String)a1.getValue(Action.NAME);
73             if (n1 == null) {
74                 n1 = "";
75             }
76 
77             String n2 = (String)a2.getValue(Action.NAME);
78             if (n2 == null) {
79                 n2 = "";
80             }
81 
82             return n1.compareTo(n2);
83         }
84     }
85 }
86