Skip to content

Commit

Permalink
Reworking action update - 4
Browse files Browse the repository at this point in the history
  • Loading branch information
hajdam committed Feb 15, 2024
1 parent 591570c commit 206187f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 49 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
.gradle

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

.nb-gradle
.nb-gradle-properties
/deps/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ public interface ActionModuleApi extends Module {
*
* @param targetMenu target menu
* @param menuId menu identificator
* @param activationUpdateService activation update service
*/
void buildMenu(JPopupMenu targetMenu, String menuId);
void buildMenu(JPopupMenu targetMenu, String menuId, ComponentActivationService activationUpdateService);

/**
* Returns menu using given identificator.
*
* @param targetMenuBar target menu bar
* @param menuId menu identificator
* @param activationUpdateService activation update service
*/
void buildMenu(JMenuBar targetMenuBar, String menuId);
void buildMenu(JMenuBar targetMenuBar, String menuId, ComponentActivationService activationUpdateService);

/**
* Registers menu associating it with given identificator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package org.exbin.framework.action.api;

import javax.annotation.ParametersAreNonnullByDefault;

/**
* Service for action update.
*
* @author ExBin Project (https://exbin.org)
*/
@ParametersAreNonnullByDefault
public interface ComponentActivationService {

/**
Expand All @@ -28,4 +31,9 @@ public interface ComponentActivationService {
* @param listener listener
*/
void registerListener(ComponentActivationListener listener);

/**
* Requests update of registered listeners.
*/
void requestUpdate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ActionModule implements ActionModuleApi {

private ClipboardActions clipboardActions = null;
private ClipboardTextActions clipboardTextActions = null;
private MenuHandler menuHandler = null;
private MenuManager menuManager = null;
private ToolBarManager toolBarManager = null;
private final ActionManager actionManager = new ActionManager();
private ResourceBundle resourceBundle;
Expand Down Expand Up @@ -159,12 +159,12 @@ public JMenuItem actionToMenuItem(Action action, @Nullable Map<String, ButtonGro
}

@Nonnull
private MenuHandler getMenuHandler() {
if (menuHandler == null) {
menuHandler = new MenuHandler();
private MenuManager getMenuManager() {
if (menuManager == null) {
menuManager = new MenuManager();
}

return menuHandler;
return menuManager;
}

@Nonnull
Expand All @@ -177,28 +177,28 @@ private ToolBarManager getToolBarManager() {
}

@Override
public void buildMenu(JPopupMenu targetMenu, String menuId) {
getMenuHandler().buildMenu(targetMenu, menuId);
public void buildMenu(JPopupMenu targetMenu, String menuId, ComponentActivationService activationUpdateService) {
getMenuManager().buildMenu(targetMenu, menuId, activationUpdateService);
}

@Override
public void buildMenu(JMenuBar targetMenuBar, String menuId) {
getMenuHandler().buildMenu(targetMenuBar, menuId);
public void buildMenu(JMenuBar targetMenuBar, String menuId, ComponentActivationService activationUpdateService) {
getMenuManager().buildMenu(targetMenuBar, menuId, activationUpdateService);
}

@Override
public void registerMenu(String menuId, String pluginId) {
getMenuHandler().registerMenu(menuId, pluginId);
getMenuManager().registerMenu(menuId, pluginId);
}

@Override
public void registerMenuGroup(String menuId, MenuGroup menuGroup) {
getMenuHandler().registerMenuGroup(menuId, menuGroup);
getMenuManager().registerMenuGroup(menuId, menuGroup);
}

@Override
public void registerMenuItem(String menuId, String pluginId, JMenu menu, MenuPosition position) {
getMenuHandler().registerMenuItem(menuId, pluginId, menu, position);
getMenuManager().registerMenuItem(menuId, pluginId, menu, position);
}

@Override
Expand All @@ -208,17 +208,17 @@ public void registerMenuItem(String menuId, String pluginId, JMenuItem item, Men

@Override
public void registerMenuItem(String menuId, String pluginId, Action action, MenuPosition position) {
getMenuHandler().registerMenuItem(menuId, pluginId, action, position);
getMenuManager().registerMenuItem(menuId, pluginId, action, position);
}

@Override
public void registerMenuItem(String menuId, String pluginId, String subMenuId, Action subMenuAction, MenuPosition position) {
getMenuHandler().registerMenuItem(menuId, pluginId, subMenuId, subMenuAction, position);
getMenuManager().registerMenuItem(menuId, pluginId, subMenuId, subMenuAction, position);
}

@Override
public void registerMenuItem(String menuId, String pluginId, String subMenuId, String subMenuName, MenuPosition position) {
getMenuHandler().registerMenuItem(menuId, pluginId, subMenuId, subMenuName, position);
getMenuManager().registerMenuItem(menuId, pluginId, subMenuId, subMenuName, position);
}

@Override
Expand Down Expand Up @@ -285,7 +285,7 @@ public void registerClipboardHandler(ClipboardActionsHandler clipboardHandler) {

@Override
public boolean menuGroupExists(String menuId, String groupId) {
return menuHandler.menuGroupExists(menuId, groupId);
return menuManager.menuGroupExists(menuId, groupId);
}

@Nonnull
Expand All @@ -299,7 +299,7 @@ public ClipboardActionsUpdater createClipboardActions(ClipboardActionsHandler cl

@Override
public void unregisterMenu(String menuId) {
getMenuHandler().unregisterMenu(menuId);
getMenuManager().unregisterMenu(menuId);
}

public void updateClipboardActionsState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.exbin.framework.action.api.ActionMenuContribution;
import org.exbin.framework.action.api.ActionMenuCreation;
import org.exbin.framework.action.api.ActionModuleApi;
import org.exbin.framework.action.api.ComponentActivationService;
import org.exbin.framework.action.api.DirectMenuContribution;
import org.exbin.framework.action.api.MenuContribution;
import org.exbin.framework.action.api.MenuGroup;
Expand All @@ -45,12 +46,12 @@
import org.exbin.framework.action.api.SubMenuContribution;

/**
* Menu handler.
* Menu manager.
*
* @author ExBin Project (https://exbin.org)
*/
@ParametersAreNonnullByDefault
public class MenuHandler {
public class MenuManager {

/**
* Menu records: menu id -> menu definition.
Expand All @@ -72,18 +73,18 @@ public class MenuHandler {
*/
private Map<String, String> pluginsUsage = new HashMap<>();

public MenuHandler() {
public MenuManager() {
}

public void buildMenu(JMenu targetMenu, String menuId) {
MenuHandler.this.buildMenu(new MenuWrapper(targetMenu), menuId);
public void buildMenu(JMenu targetMenu, String menuId, ComponentActivationService activationUpdateService) {
MenuManager.this.buildMenu(new MenuWrapper(targetMenu), menuId, activationUpdateService);
}

public void buildMenu(JPopupMenu targetMenu, String menuId) {
MenuHandler.this.buildMenu(new PopupMenuWrapper(targetMenu), menuId);
public void buildMenu(JPopupMenu targetMenu, String menuId, ComponentActivationService activationUpdateService) {
MenuManager.this.buildMenu(new PopupMenuWrapper(targetMenu), menuId, activationUpdateService);
}

private void buildMenu(MenuTarget targetMenu, String menuId) {
private void buildMenu(MenuTarget targetMenu, String menuId, ComponentActivationService activationUpdateService) {
MenuDefinition menuDef = menus.get(menuId);

if (menuDef == null) {
Expand Down Expand Up @@ -250,7 +251,7 @@ public void finish() {
public void process() {
SubMenuContribution subMenuContribution = (SubMenuContribution) next.contribution;
subMenu = new JMenu(subMenuContribution.getAction());
MenuHandler.this.buildMenu(new MenuWrapper(subMenu, targetMenu.isPopup()), subMenuContribution.getMenuId());
MenuManager.this.buildMenu(new MenuWrapper(subMenu, targetMenu.isPopup()), subMenuContribution.getMenuId(), null);
}

@Override
Expand Down Expand Up @@ -506,8 +507,8 @@ private enum OrderingMode {
BEFORE, ITEM, AFTER
};

public void buildMenu(JMenuBar targetMenuBar, String menuId) {
buildMenu(new MenuBarWrapper(targetMenuBar), menuId);
public void buildMenu(JMenuBar targetMenuBar, String menuId, ComponentActivationService activationUpdateService) {
buildMenu(new MenuBarWrapper(targetMenuBar), menuId, activationUpdateService);
}

public void registerMenu(String menuId, String pluginId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
public class ToolBarManager {

private final Map<Class<?>, List<ComponentActivationInstanceListener<?>>> activeComponentListeners = new HashMap<>();
private final Map<Class<?>, Object> activeComponentState = new HashMap<>();
private final ComponentActivationManager componentActivationManager = new ComponentActivationManager() {
@Override
public <T> void registerListener(Class<T> instanceClass, ComponentActivationInstanceListener<T> listener) {
Expand Down Expand Up @@ -157,7 +156,6 @@ public void buildToolBar(JToolBar targetToolBar, String toolBarId, ComponentActi
@Override
@SuppressWarnings("unchecked")
public <T> void updated(Class<T> instanceClass, T instance) {
activeComponentState.put(instanceClass, instance);
List<ComponentActivationInstanceListener<?>> instanceListeners = activeComponentListeners.get(instanceClass);
if (instanceListeners != null) {
for (ComponentActivationInstanceListener instanceListener : instanceListeners) {
Expand Down Expand Up @@ -261,7 +259,6 @@ private void processToolBarGroup(List<ToolBarGroupRecord> groups, JToolBar targe
processingPath.add(new ToolBarGroupRecordPathNode(groupRecord.subGroups));
}
}
initActiveComponent();
}

public void initToolbarAction(Action action) {
Expand All @@ -271,20 +268,6 @@ public void initToolbarAction(Action action) {
}
}

@SuppressWarnings("unchecked")
private void initActiveComponent() {
for (Map.Entry<Class<?>, List<ComponentActivationInstanceListener<?>>> entry : activeComponentListeners.entrySet()) {
Class<?> key = entry.getKey();
List<ComponentActivationInstanceListener<?>> listeners = entry.getValue();
if (listeners != null) {
Object instance = activeComponentState.get(key);
for (ComponentActivationInstanceListener listener : listeners) {
listener.update(instance);
}
}
}
}

@Nonnull
private JComponent createDefaultToolBarItem(Action action) {
JButton newItem = new JButton(action);
Expand Down Expand Up @@ -332,7 +315,6 @@ public void registerToolBarItem(String toolBarId, String pluginId, Action action

@SuppressWarnings("unchecked")
public <T> void updateActionsForComponent(Class<T> componentClass, @Nullable T componentInstance) {
activeComponentState.put(componentClass, componentInstance);
List<ComponentActivationInstanceListener<?>> componentListeners = activeComponentListeners.get(componentClass);
if (componentListeners != null) {
for (ComponentActivationInstanceListener componentListener : componentListeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void setMainPanel(Component component) {
@Override
public void loadMainMenu() {
ActionModuleApi actionModule = App.getModule(ActionModuleApi.class);
actionModule.buildMenu(menuBar, ActionConsts.MAIN_MENU_ID);
actionModule.buildMenu(menuBar, ActionConsts.MAIN_MENU_ID, frameComponentActivationService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package org.exbin.framework.frame;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.exbin.framework.action.api.ComponentActivationListener;
Expand All @@ -31,6 +34,7 @@
public class FrameComponentActivationService implements ComponentActivationService, ComponentActivationListener {

private final List<ComponentActivationListener> listeners = new ArrayList<>();
private final Map<Class<?>, Object> activeComponentState = new HashMap<>();

@Override
public void registerListener(ComponentActivationListener listener) {
Expand All @@ -39,8 +43,21 @@ public void registerListener(ComponentActivationListener listener) {

@Override
public <T> void updated(Class<T> instanceClass, @Nullable T instance) {
activeComponentState.put(instanceClass, instance);
for (ComponentActivationListener listener : listeners) {
listener.updated(instanceClass, instance);
}
}

@Override
public void requestUpdate() {
for (Map.Entry<Class<?>, Object> entry : activeComponentState.entrySet()) {
Class<?> key = entry.getKey();
Object instance = entry.getValue();
for (ComponentActivationListener listener : listeners) {
// TODO cast instance somehow
listener.updated(key, null);
}
}
}
}

0 comments on commit 206187f

Please sign in to comment.