001 /*
002 * Created on Sep 21, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2010 the original author or authors.
015 */
016 package org.fest.swing.core;
017
018
019 import static org.fest.swing.core.MouseButton.*;
020 import static org.fest.util.Strings.concat;
021
022 /**
023 * Understands information about clicking a mouse button.
024 * <p>
025 * Examples:
026 * </p>
027 * <p>
028 * Specify that the right button should be clicked once:
029 * <pre>
030 * // import static org.fest.swing.fixture.MouseClickInfo.*;
031 * MouseClickInfo i = rightButton();
032 * </pre>
033 * </p>
034 * <p>
035 * Specify that the left button should be clicked two times (similar to double-click):
036 * <pre>
037 * // import static org.fest.swing.fixture.MouseClickInfo.*;
038 * MouseClickInfo i = leftButton().times(2);
039 * </pre>
040 * </p>
041 *
042 * @author Alex Ruiz
043 */
044 public final class MouseClickInfo {
045
046 private final MouseButton button;
047 private int times;
048
049 /**
050 * Specifies that the left button should be clicked once.
051 * @return the created click info.
052 */
053 public static MouseClickInfo leftButton() {
054 return button(LEFT_BUTTON);
055 }
056
057 /**
058 * Specifies that the middle button should be clicked once.
059 * @return the created click info.
060 */
061 public static MouseClickInfo middleButton() {
062 return button(MIDDLE_BUTTON);
063 }
064
065 /**
066 * Specifies that the right button should be clicked once.
067 * @return the created click info.
068 */
069 public static MouseClickInfo rightButton() {
070 return button(RIGHT_BUTTON);
071 }
072
073 /**
074 * Specifies that the given button should be clicked once.
075 * @param button the mouse button to click.
076 * @return the created click info.
077 * @throws NullPointerException if <code>button</code> is <code>null</code>.
078 */
079 public static MouseClickInfo button(MouseButton button) {
080 return new MouseClickInfo(button, 1);
081 }
082
083 private MouseClickInfo(MouseButton button, int times) {
084 if (button == null) throw new NullPointerException("The MouseButton should not be null");
085 this.button = button;
086 this.times = times;
087 }
088
089 /**
090 * Returns the button to click.
091 * @return the button to click.
092 */
093 public MouseButton button() { return button; }
094
095 /**
096 * Returns how many times the <code>{@link #button() mouse button}</code> should be clicked.
097 * @return how many times the <code>{@link #button() mouse button}</code> should be clicked.
098 */
099 public int times() { return times; }
100
101 /**
102 * Specifies how many times the mouse button should be clicked.
103 * @param newTimes the specified number of times to click the mouse button.
104 * @return this object.
105 */
106 public MouseClickInfo times(int newTimes) {
107 times = newTimes;
108 return this;
109 }
110
111 /** @see java.lang.Object#toString() */
112 @Override public String toString() {
113 return concat(
114 getClass().getSimpleName(), "[",
115 "button=", button, ",",
116 "times=", String.valueOf(times), "]");
117 }
118 }