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

org.marid.jfx.action.SpecialActions Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
/*-
 * #%L
 * marid-ide
 * %%
 * Copyright (C) 2012 - 2017 MARID software development group
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 * #L%
 */

package org.marid.jfx.action;

import javafx.beans.InvalidationListener;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableBooleanValue;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Control;

import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static javafx.scene.input.ContextMenuEvent.CONTEXT_MENU_REQUESTED;
import static org.marid.jfx.action.FxAction.grouped;
import static org.marid.jfx.action.SpecialActionType.*;

/**
 * @author Dmitry Ovchinnikov
 */
public class SpecialActions {

  private final EnumMap actionMap = new EnumMap<>(SpecialActionType.class);

  @Resource
  public void setAddAction(SpecialAction addAction) {
    actionMap.put(ADD, addAction);
  }

  @Resource
  public void setEditAction(SpecialAction editAction) {
    actionMap.put(EDIT, editAction);
  }

  @Resource
  public void setRemoveAction(SpecialAction removeAction) {
    actionMap.put(REMOVE, removeAction);
  }

  @Resource
  public void setClearAllAction(SpecialAction clearAllAction) {
    actionMap.put(CLEAR_ALL, clearAllAction);
  }

  @Resource
  public void setSelectAllAction(SpecialAction selectAllAction) {
    actionMap.put(SELECT_ALL, selectAllAction);
  }

  @Resource
  public void setCutAction(SpecialAction cutAction) {
    actionMap.put(CUT, cutAction);
  }

  @Resource
  public void setCopyAction(SpecialAction copyAction) {
    actionMap.put(COPY, copyAction);
  }

  @Resource
  public void setPasteAction(SpecialAction pasteAction) {
    actionMap.put(PASTE, pasteAction);
  }

  @Resource
  public void setMiscAction(SpecialAction miscAction) {
    actionMap.put(MISC, miscAction);
  }

  @Resource
  public void setRenameAction(SpecialAction renameAction) {
    actionMap.put(RENAME, renameAction);
  }

  @Resource
  public void setUpAction(SpecialAction upAction) {
    actionMap.put(UP, upAction);
  }

  @Resource
  public void setDownAction(SpecialAction downAction) {
    actionMap.put(DOWN, downAction);
  }

  public SpecialAction get(SpecialActionType type) {
    return actionMap.get(type);
  }

  public void reset() {
    actionMap.values().forEach(SpecialAction::reset);
  }

  public void assign(ObservableValue> actions) {
    final InvalidationListener listener = o -> actionMap.forEach((type, specialAction) -> {
      specialAction.reset();

      final List matched = actions.getValue().stream()
          .filter(a -> type == MISC && a.specialAction == null || a.specialAction == specialAction)
          .collect(Collectors.toList());

      switch (matched.size()) {
        case 0:
          break;
        case 1:
          specialAction.copy(matched.get(0));
          break;
        default:
          specialAction.bindChildren(new SimpleObjectProperty<>(FXCollections.observableArrayList(matched)));
          break;
      }
    });
    actions.addListener(listener);
    listener.invalidated(actions);
  }

  public void assign(ObservableBooleanValue focused, ObservableValue> actions) {
    focused.addListener((observable, oldValue, newValue) -> {
      if (newValue) {
        assign(actions);
      } else {
        reset();
      }
    });
  }

  public  T wrap(T control, Supplier> actions) {
    control.addEventFilter(CONTEXT_MENU_REQUESTED, event -> control.setContextMenu(grouped(actions.get())));
    return control;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy