File: SourceListClickListener.java

package info (click to toggle)
mac-widgets 0.9.5%2Bsvn369-dfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,920 kB
  • sloc: java: 8,318; makefile: 13; sh: 12
file content (93 lines) | stat: -rw-r--r-- 3,445 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.explodingpixels.macwidgets;

import java.awt.event.MouseEvent;

/**
 * An interface to hook into clicks on {@link SourceListCategory}s and
 * {@link SourceListItem}s. This interface is similar to what
 * {@link SourceListSelectionListener} offers, but provides mouse clicks
 * including which mouse button was pressed the number of clicks that occured.
 * <p/>
 * It is, for example, possible to handle left mouse button double clicks on an
 * item, which can be done with an implementation like this:
 * <pre>
 * SourceListClickListener clickListener = new SourceListClickListener() {
 *     public void sourceListItemClicked(SourceListItem item, Button button,
 *                                       int clickCount) {
 *         boolean isLeftButton = button == SourceListClickListener.Button.LEFT;
 *         if (isLeftButton && clickCount == 2) {
 *             // do something here.
 *         }
 *     }
 *     public void sourceListCategoryClicked(SourceListCategory category,
 *                                           Button button, int clickCount) {
 *         // no implementation.
 *     }
 * };
 * </pre>
 */
public interface SourceListClickListener {

    /**
     * Called when a {@link SourceListItem} is clicked.
     *
     * @param item       the {@code SourceListItem} that was clicked. Will not be null.
     * @param button     the mouse button that was used to perform the click.
     * @param clickCount the number of times the mouse button was clicked.
     */
    void sourceListItemClicked(SourceListItem item, Button button,
                               int clickCount);

    /**
     * Called when a {@link SourceListCategory} is clicked.
     *
     * @param category   the {@code SourceListCategory} that was clicked. Will not
     *                   be null.
     * @param button     the mouse button that was used to perform the click.
     * @param clickCount the number of times the mouse button was clicked.
     */
    void sourceListCategoryClicked(SourceListCategory category, Button button,
                                   int clickCount);

    /**
     * Corresponds to a button on a mouse.
     */
    enum Button {
        LEFT(MouseEvent.BUTTON1),
        MIDDLE(MouseEvent.BUTTON2),
        RIGHT(MouseEvent.BUTTON3);

        final int fCorrespondingMouseEventButton;

        Button(int correspondingMouseEventButton) {
            fCorrespondingMouseEventButton = correspondingMouseEventButton;
        }

        private int getCorrespondingMouseEventButton() {
            return fCorrespondingMouseEventButton;
        }

        public static Button getButton(int correspondingMouseEventButton) {
            Button retVal = null;

            // loop over each Button comparing it's 
            // correspondingMouseEventButton value to the current Button's
            // correspondingMouseEventButton.
            for (Button button : Button.values()) {
                if (button.getCorrespondingMouseEventButton() == correspondingMouseEventButton) {
                    retVal = button;
                    break;
                }
            }

            // if no matching Button was found, throw an exception.
            if (retVal == null) {
                throw new IllegalArgumentException(
                        correspondingMouseEventButton + " is not a valid MouseEvent button integer.");
            }

            return retVal;
        }
    }

}