File Source: PerformWrapper.java
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.actions.Action;
26 import com.dmdirc.actions.interfaces.ActionComponent;
27 import com.dmdirc.actions.ActionCondition;
28 import com.dmdirc.actions.ActionGroup;
29 import com.dmdirc.actions.interfaces.ActionType;
30 import com.dmdirc.actions.CoreActionComparison;
31 import com.dmdirc.actions.CoreActionComponent;
32 import com.dmdirc.actions.CoreActionType;
33 import com.dmdirc.logger.ErrorLevel;
34 import com.dmdirc.logger.Logger;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40 * An action wrapper for performs.
41 *
42 * @author Chris
43 */
44 public class PerformWrapper extends ActionGroup {
45
46 /** A singleton instance of the Perform Wrapper. */
/*
P/P * Method: com.dmdirc.actions.wrappers.PerformWrapper__static_init
*
* Postconditions:
* me == &new PerformWrapper(PerformWrapper__static_init#1)
* new ArrayList(ActionGroup#1) num objects == 1
* new HashMap(ActionGroup#2) num objects == 1
* new PerformWrapper(PerformWrapper__static_init#1) num objects == 1
* me.actions == &new ArrayList(ActionGroup#1)
* me.author == null
* me.description == null
* me.component == -1
* me.version == -1
* me.name == &"performs"
* ...
*/
47 private static PerformWrapper me = new PerformWrapper();
48
49 /**
50 * Creates a new instance of PerformWrapper.
51 */
52 private PerformWrapper() {
/*
P/P * Method: void com.dmdirc.actions.wrappers.PerformWrapper()
*
* Postconditions:
* this.actions == &new ArrayList(ActionGroup#1)
* this.author == null
* this.description == null
* this.component == -1
* this.version == -1
* this.name == &"performs"
* this.settings == &new HashMap(ActionGroup#2)
* new ArrayList(ActionGroup#1) num objects == 1
* new HashMap(ActionGroup#2) num objects == 1
*/
53 super("performs");
54 }
55
56 /**
57 * Retrieves a singleton instance of this perform wrapper.
58 *
59 * @return A singleton instance of PerformWrapper
60 */
61 public static PerformWrapper getPerformWrapper() {
/*
P/P * Method: PerformWrapper getPerformWrapper()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* return_value == me
* init'ed(return_value)
*/
62 return me;
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 public void add(final Action action) {
/*
P/P * Method: void add(Action)
*
* Preconditions:
* action != null
* action.triggers != null
* (soft) action.conditions != null
* (soft) init'ed(action.name)
* (soft) init'ed(action.triggers[0])
* (soft) init'ed(action.triggers[...])
* (soft) this.actions != null
*
* Presumptions:
* init'ed(com.dmdirc.logger.ErrorLevel.MEDIUM)
* java.util.List:get(...)@68 != null
*
* Test Vectors:
* action.triggers.length: {0, 2..+Inf}, {1}
* java.util.List:get(...).component@68: Addr_Set{&com.dmdirc.actions.CoreActionComponent__static_init.new CoreActionComponent$2(CoreActionComponent__static_init#2)}, Inverse{&com.dmdirc.actions.CoreActionComponent__static_init.new CoreActionComponent$2(CoreActionComponent__static_init#2)}
* java.util.List:get(...).component@68: Inverse{&com.dmdirc.actions.CoreActionComponent__static_init.new CoreActionComponent$1(CoreActionComponent__static_init#1)}, Addr_Set{&com.dmdirc.actions.CoreActionComponent__static_init.new CoreActionComponent$1(CoreActionComponent__static_init#1)}
* java.util.List:size(...)@68: {-231..0, 2..232-1}, {1}
*/
68 if (action.getTriggers().length == 1
69 && action.getTriggers()[0] == CoreActionType.SERVER_CONNECTED
70 && action.getConditions().size() == 1
71 && (action.getConditions().get(0).getComponent() == CoreActionComponent.SERVER_NETWORK
72 || action.getConditions().get(0).getComponent() == CoreActionComponent.SERVER_NAME)) {
73 super.add(action);
74 } else {
75 Logger.userError(ErrorLevel.MEDIUM, "Invalid perform action: " + action.getName());
76 }
77 }
78
79 /**
80 * Retrieve the action that handles the perform for the specified server,
81 * or null if no such action exists.
82 *
83 * @param server The server to look for
84 * @return The action that handles the server's perform, or null
85 */
86 public Action getActionForServer(final String server) {
/*
P/P * Method: Action getActionForServer(String)
*
* Preconditions:
* this.actions != null
*
* Postconditions:
* init'ed(return_value)
*/
87 return getAction(CoreActionComponent.SERVER_NAME, server);
88 }
89
90 /**
91 * Retrieve the action that handles the perform for the specified network,
92 * or null if no such action exists.
93 *
94 * @param network The network to look for
95 * @return The action that handles the network's perform, or null
96 */
97 public Action getActionForNetwork(final String network) {
/*
P/P * Method: Action getActionForNetwork(String)
*
* Preconditions:
* this.actions != null
*
* Postconditions:
* init'ed(return_value)
*/
98 return getAction(CoreActionComponent.SERVER_NETWORK, network);
99 }
100
101 /**
102 * Creates a new, empty, perform wrapper for the specified server.
103 *
104 * @param server The server to create the action for
105 * @return The new perform wrapper action
106 */
107 public Action createActionForServer(final String server) {
/*
P/P * Method: Action createActionForServer(String)
*
* Preconditions:
* server != null
*
* Postconditions:
* return_value == &new Action(createAction#4)
* new Action(createAction#4) num objects == 1
*/
108 return createAction(server, "");
109 }
110
111 /**
112 * Creates a new, empty, perform wrapper for the specified network.
113 *
114 * @param network The network to create the action for
115 * @return The new perform wrapper action
116 */
117 public Action createActionForNetwork(final String network) {
/*
P/P * Method: Action createActionForNetwork(String)
*
* Postconditions:
* return_value == &new Action(createAction#4)
* new Action(createAction#4) num objects == 1
*/
118 return createAction("", network);
119 }
120
121 /**
122 * Creates a new, empty, perform wrapper for the specified server or
123 * network. Note that both server and network must be specified, and
124 * exactly one of them must be empty.
125 *
126 * @param server The server to create the action for
127 * @param network The network to create the action for
128 * @return The new perform wrapper action
129 */
130 private Action createAction(final String server, final String network) {
/*
P/P * Method: Action createAction(String, String)
*
* Preconditions:
* server != null
*
* Postconditions:
* return_value == &new Action(createAction#4)
* new Action(createAction#4) num objects == 1
*/
131 final List<ActionCondition> conditions = new ArrayList<ActionCondition>();
132 final CoreActionComponent component =
133 server.isEmpty() ? CoreActionComponent.SERVER_NETWORK
134 : CoreActionComponent.SERVER_NAME;
135
136 conditions.add(new ActionCondition(0, component,
137 CoreActionComparison.STRING_EQUALS, server + network));
138
139 return new Action(getName(), server + network,
140 new ActionType[]{CoreActionType.SERVER_CONNECTED},
141 new String[0], conditions, null);
142 }
143
144 /**
145 * Retrieve an action with a condition that checks the specified component,
146 * and matches it against the specified target.
147 *
148 * @param component The action component to look for
149 * @param target The string the component is matched against
150 * @return The matching action if one exists, or null
151 */
152 private Action getAction(final ActionComponent component, final String target) {
/*
P/P * Method: Action getAction(ActionComponent, String)
*
* Preconditions:
* this.actions != null
*
* Presumptions:
* action.conditions@153 != null
* java.util.Iterator:next(...)@153 != null
* java.util.List:get(...).target@154 != null
* java.util.List:get(...)@154 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@154: {0}, {1}
* java.util.Iterator:hasNext(...)@153: {0}, {1}
*/
153 for (Action action : this) {
154 if (action.getConditions().get(0).getComponent() == component
155 && action.getConditions().get(0).getTarget().equalsIgnoreCase(target)) {
156 return action;
157 }
158 }
159
160 return null;
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 public boolean isDelible() {
/*
P/P * Method: bool isDelible()
*
* Postconditions:
* return_value == 0
*/
166 return false;
167 }
168
169 /** {@inheritDoc} */
170 @Override
171 public String getDescription() {
/*
P/P * Method: String getDescription()
*
* Postconditions:
* return_value == &"Performs allow you to automatically execute commands when you connect ... ver Settings" dialog, which can be accessed through the Settings menu."
*/
172 return "Performs allow you to automatically execute commands when"
173 + " you connect to a specific server or network. You can edit"
174 + " the perform for the current server or network in the "
175 + "\"Server Settings\" dialog, which can be accessed through "
176 + "the Settings menu.";
177 }
178
179 }
SofCheck Inspector Build Version : 2.17854
| PerformWrapper.java |
2009-Jun-25 01:54:24 |
| PerformWrapper.class |
2009-Sep-02 17:04:14 |