You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionFactory.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.actions;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.GlobalWindow;
  25. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  26. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  27. import com.dmdirc.interfaces.ActionController;
  28. import com.dmdirc.interfaces.actions.ActionType;
  29. import com.dmdirc.interfaces.config.IdentityController;
  30. import java.nio.file.FileSystem;
  31. import java.nio.file.FileSystems;
  32. import java.util.List;
  33. import javax.inject.Inject;
  34. import javax.inject.Provider;
  35. import javax.inject.Singleton;
  36. /**
  37. * Factory for creating {@link Action}s.
  38. */
  39. @Singleton
  40. public class ActionFactory {
  41. /** The controller that will own actions. */
  42. private final Provider<ActionController> actionController;
  43. /** The controller to use to retrieve and update settings. */
  44. private final Provider<IdentityController> identityController;
  45. /** The global window to use for actions without windows. */
  46. private final Provider<GlobalWindow> globalWindowProvider;
  47. /** The factory to use to create substitutors. */
  48. private final ActionSubstitutorFactory substitutorFactory;
  49. /** The base directory to store actions in. */
  50. private final String actionsDirectory;
  51. /** Event bus to post events on. */
  52. private final DMDircMBassador eventBus;
  53. /** The file system to read/write actions to. */
  54. private final FileSystem filesystem;
  55. /**
  56. * Creates a new instance of {@link ActionFactory}.
  57. *
  58. * @param eventBus The event bus to post events on.
  59. * @param actionController The controller that will own actions.
  60. * @param identityController The controller to use to retrieve and update settings.
  61. * @param globalWindowProvider The global window to use for actions without windows.
  62. * @param substitutorFactory The factory to use to create substitutors.
  63. * @param actionsDirectory The base directory to store actions in.
  64. */
  65. @Inject
  66. public ActionFactory(final DMDircMBassador eventBus, final Provider<ActionController> actionController,
  67. final Provider<IdentityController> identityController,
  68. final Provider<GlobalWindow> globalWindowProvider,
  69. final ActionSubstitutorFactory substitutorFactory,
  70. @Directory(DirectoryType.ACTIONS) final String actionsDirectory) {
  71. this.filesystem = FileSystems.getDefault();
  72. this.eventBus = eventBus;
  73. this.actionController = actionController;
  74. this.identityController = identityController;
  75. this.globalWindowProvider = globalWindowProvider;
  76. this.substitutorFactory = substitutorFactory;
  77. this.actionsDirectory = actionsDirectory;
  78. }
  79. /**
  80. * Creates a new instance of Action. The group and name specified must be the group and name of
  81. * a valid action already saved to disk.
  82. *
  83. * @param group The group the action belongs to
  84. * @param name The name of the action
  85. *
  86. * @return A relevant action.
  87. */
  88. public Action getAction(final String group, final String name) {
  89. return new Action(
  90. filesystem,
  91. eventBus, globalWindowProvider,
  92. substitutorFactory,
  93. actionController.get(),
  94. identityController.get(),
  95. actionsDirectory,
  96. group,
  97. name);
  98. }
  99. /**
  100. * Creates a new instance of Action with the specified properties and saves it to disk.
  101. *
  102. * @param group The group the action belongs to
  103. * @param name The name of the action
  104. * @param triggers The triggers to use
  105. * @param response The response to use
  106. * @param conditions The conditions to use
  107. * @param conditionTree The condition tree to use
  108. * @param newFormat The new formatter to use
  109. *
  110. * @return A relevant action.
  111. */
  112. public Action getAction(final String group, final String name,
  113. final ActionType[] triggers, final String[] response,
  114. final List<ActionCondition> conditions,
  115. final ConditionTree conditionTree, final String newFormat) {
  116. return new Action(
  117. filesystem,
  118. eventBus, globalWindowProvider,
  119. substitutorFactory,
  120. actionController.get(),
  121. identityController.get(),
  122. actionsDirectory,
  123. group,
  124. name,
  125. triggers,
  126. response,
  127. conditions,
  128. conditionTree,
  129. newFormat);
  130. }
  131. }