File Source: AliasWrapper.java

         /* 
    P/P   *  Method: com.dmdirc.actions.wrappers.AliasWrapper__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     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  
    23  package com.dmdirc.actions.wrappers;
    24  
    25  import com.dmdirc.GlobalWindow;
    26  import com.dmdirc.Server;
    27  import com.dmdirc.ServerManager;
    28  import com.dmdirc.actions.Action;
    29  import com.dmdirc.actions.ActionCondition;
    30  import com.dmdirc.actions.ActionGroup;
    31  import com.dmdirc.actions.CoreActionType;
    32  import com.dmdirc.commandparser.CommandManager;
    33  import com.dmdirc.logger.ErrorLevel;
    34  import com.dmdirc.logger.Logger;
    35  import com.dmdirc.ui.input.TabCompletionType;
    36  
    37  import java.util.ArrayList;
    38  import java.util.List;
    39  
    40  /**
    41   * Encapsulates alias actions.
    42   *
    43   * @author chris
    44   */
    45  public final class AliasWrapper extends ActionGroup {
    46      
    47      /** Singleton instance of the alias wrapper. */
    48      private static AliasWrapper me;
    49      
    50      /** A list of registered alias names. */
    51      private final List<String> aliases = new ArrayList<String>();
    52      
    53      /**
    54       * Creates a new instance of AliasWrapper.
    55       */
    56      private AliasWrapper() {
                 /* 
    P/P           *  Method: void com.dmdirc.actions.wrappers.AliasWrapper()
                  * 
                  *  Postconditions:
                  *    this.actions == &amp;new ArrayList(ActionGroup#1)
                  *    this.aliases == &amp;new ArrayList(AliasWrapper#1)
                  *    this.author == null
                  *    this.description == null
                  *    this.component == -1
                  *    this.version == -1
                  *    this.name == &amp;"aliases"
                  *    this.settings == &amp;new HashMap(ActionGroup#2)
                  *    new ArrayList(ActionGroup#1) num objects == 1
                  *    new ArrayList(AliasWrapper#1) num objects == 1
                  *    ...
                  */
    57          super("aliases");
    58      }
    59      
    60      /**
    61       * Retrieves a singleton instance of this alias wrapper.
    62       *
    63       * @return A singleton instance of AliasWrapper
    64       */
    65      public static synchronized AliasWrapper getAliasWrapper() {
                 /* 
    P/P           *  Method: AliasWrapper getAliasWrapper()
                  * 
                  *  Preconditions:
                  *    init'ed(me)
                  * 
                  *  Postconditions:
                  *    me == One-of{old me, &amp;new AliasWrapper(getAliasWrapper#1)}
                  *    me != null
                  *    return_value == me
                  *    new AliasWrapper(getAliasWrapper#1) num objects <= 1
                  *    new AliasWrapper(getAliasWrapper#1).actions == &amp;new ArrayList(ActionGroup#1)
                  *    new AliasWrapper(getAliasWrapper#1).aliases == &amp;new ArrayList(AliasWrapper#1)
                  *    new AliasWrapper(getAliasWrapper#1).author == null
                  *    new AliasWrapper(getAliasWrapper#1).component == -1
                  *    new AliasWrapper(getAliasWrapper#1).description == null
                  *    new AliasWrapper(getAliasWrapper#1).name == &amp;"aliases"
                  *    ...
                  * 
                  *  Test Vectors:
                  *    me: Inverse{null}, Addr_Set{null}
                  */
    66          if (me == null) {
    67              me = new AliasWrapper();
    68          }
    69          
    70          return me;
    71      }
    72      
    73      /**
    74       * Retrieves a list of alias names registered with this wrapper.
    75       * 
    76       * @return A list of alias names
    77       */
    78      public List<String> getAliases() {
                 /* 
    P/P           *  Method: List getAliases()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getAliases#1)
                  *    new ArrayList(getAliases#1) num objects == 1
                  */
    79          return new ArrayList<String>(aliases);
    80      }
    81      
    82      /** {@inheritDoc} */
    83      @Override
    84      public void add(final Action action) {
                 /* 
    P/P           *  Method: void add(Action)
                  * 
                  *  Preconditions:
                  *    action != null
                  *    action.triggers != null
                  *    action.triggers.length >= 1
                  *    (soft) action.conditions != null
                  *    (soft) init'ed(action.name)
                  *    (soft) action.triggers[0] != null
                  *    (soft) action.triggers[...] != null
                  *    (soft) init'ed(com/dmdirc/GlobalWindow.globalWindow)
                  *    (soft) com/dmdirc/GlobalWindow.globalWindow.tabCompleter != null
                  *    (soft) init'ed(com/dmdirc/ServerManager.me)
                  *    ...
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
                  *    init'ed(com.dmdirc.ui.input.TabCompletionType.COMMAND)
                  *    java.util.Iterator:next(...)@98 != null
                  *    server.tabCompleter@98 != null
                  * 
                  *  Postconditions:
                  *    com/dmdirc/ServerManager.me == One-of{old com/dmdirc/ServerManager.me, &amp;new ServerManager(getServerManager#1)}
                  *    init'ed(com/dmdirc/ServerManager.me)
                  *    new ArrayList(ServerManager#1) num objects <= 1
                  *    new ServerManager(getServerManager#1) num objects <= 1
                  *    init'ed(new ServerManager(getServerManager#1).servers)
                  * 
                  *  Test Vectors:
                  *    com/dmdirc/GlobalWindow.globalWindow: Addr_Set{null}, Inverse{null}
                  *    java.lang.Object:equals(...)@85: {0}, {1}
                  */
    85          if (action.getTriggers()[0].equals(CoreActionType.UNKNOWN_COMMAND)) {
    86              
    87              final String commandName = getCommandName(action);
    88  
    89              if (commandName != null) {
    90                  super.add(action);
    91                  aliases.add(commandName);
    92  
    93                  if (GlobalWindow.getGlobalWindow() != null) {
    94                      GlobalWindow.getGlobalWindow().getTabCompleter()
    95                              .addEntry(TabCompletionType.COMMAND, commandName);
    96                  }
    97  
    98                  for (Server server : ServerManager.getServerManager().getServers()) {
    99                      server.getTabCompleter().addEntry(TabCompletionType.COMMAND, commandName);
   100                  }
   101              } else {
   102                  Logger.userError(ErrorLevel.MEDIUM, "Invalid alias action (no name): "
   103                          + action.getName());
   104              }
   105          } else {
   106              Logger.userError(ErrorLevel.MEDIUM, "Invalid alias action (wrong trigger): "
   107                      + action.getName());
   108          }
   109      }
   110      
   111      /** {@inheritDoc} */
   112      @Override
   113      public void remove(final Action action) {
                 /* 
    P/P           *  Method: void remove(Action)
                  * 
                  *  Preconditions:
                  *    action != null
                  *    action.triggers != null
                  *    action.triggers.length >= 1
                  *    (soft) action.conditions != null
                  *    (soft) action.triggers[0] != null
                  *    (soft) action.triggers[...] != null
                  *    (soft) init'ed(com/dmdirc/ServerManager.me)
                  *    (soft) this.actions != null
                  *    (soft) this.aliases != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.ui.input.TabCompletionType.COMMAND)
                  *    java.util.Iterator:next(...)@121 != null
                  *    server.tabCompleter@121 != null
                  * 
                  *  Postconditions:
                  *    com/dmdirc/ServerManager.me == One-of{old com/dmdirc/ServerManager.me, &amp;new ServerManager(getServerManager#1)}
                  *    init'ed(com/dmdirc/ServerManager.me)
                  *    new ArrayList(ServerManager#1) num objects <= 1
                  *    new ServerManager(getServerManager#1) num objects <= 1
                  *    init'ed(new ServerManager(getServerManager#1).servers)
                  * 
                  *  Test Vectors:
                  *    java.lang.Object:equals(...)@114: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@121: {0}, {1}
                  */
   114          if (action.getTriggers()[0].equals(CoreActionType.UNKNOWN_COMMAND)) {
   115              super.remove(action);
   116              
   117              final String commandName = getCommandName(action);
   118              
   119              aliases.remove(commandName);
   120              
   121              for (Server server : ServerManager.getServerManager().getServers()) {
   122                  server.getTabCompleter().removeEntry(TabCompletionType.COMMAND, commandName);
   123              }
   124          }
   125      }
   126      
   127      /**
   128       * Retrieves the command name of the specified alias action.
   129       *
   130       * @param action The action whose name is to be determined
   131       * @return The command name for the specified alias, or null if it has
   132       *         no appropriate conditions.
   133       */
   134      public static String getCommandName(final Action action) {
                 /* 
    P/P           *  Method: String getCommandName(Action)
                  * 
                  *  Preconditions:
                  *    action != null
                  *    action.conditions != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@135 != null
                  * 
                  *  Postconditions:
                  *    java.lang.StringBuilder:toString(...)._tainted == 0
                  *    return_value in Addr_Set{null,&amp;java.lang.StringBuilder:toString(...)}
                  * 
                  *  Test Vectors:
                  *    condition.arg@135: {-231..0, 2..232-1}, {1}
                  *    java.util.Iterator:hasNext(...)@135: {0}, {1}
                  */
   135          for (ActionCondition condition : action.getConditions()) {
   136              if (condition.getArg() == 1) {
   137                  return CommandManager.getCommandChar() + condition.getTarget();
   138              }
   139          }
   140          
   141          // How can we have an alias without a command name?
   142          return null;
   143      }
   144  
   145      /** {@inheritDoc} */
   146      @Override
   147      public boolean isDelible() {
                 /* 
    P/P           *  Method: bool isDelible()
                  * 
                  *  Postconditions:
                  *    return_value == 0
                  */
   148          return false;
   149      }
   150  
   151      /** {@inheritDoc} */
   152      @Override
   153      public String getDescription() {
                 /* 
    P/P           *  Method: String getDescription()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"Aliases allow you to create new commands that invoke one or more other ... anage aliases using the "Alias Manager", located in the Settings menu."
                  */
   154          return "Aliases allow you to create new commands that invoke one or "
   155                  + "more other commands. You can manage aliases using the \""
   156                  + "Alias Manager\", located in the Settings menu.";
   157      }
   158      
   159  }








SofCheck Inspector Build Version : 2.17854
AliasWrapper.java 2009-Jun-25 01:54:24
AliasWrapper.class 2009-Sep-02 17:04:14