Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swing mouse #15

Merged
merged 14 commits into from
Dec 2, 2018
36 changes: 27 additions & 9 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.goop</groupId>
<artifactId>goop</artifactId>
<version>10.0.3</version>
<version>13.2.3</version>

<properties>
<sonar.sources>src/main/java</sonar.sources>
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/graphic/j2d/J2DContainerInsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2018 Eugen Deutsch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package graphic.j2d;

import java.awt.Container;
import logic.functional.QuadFunction;
import logic.metric.Insets;

/**
* The insets of a java 2d based container ({@link Container} to be
* precise).
* <p><b>This class is lazy.</b></p> It will use the given container only when
* {@link #result(QuadFunction)} is called. Additionally, it <b>doesn't
* cache</b>. Every call of {@link #result(QuadFunction)} will ask the given
* container for its insets.
* <p>This class is immutable by itself, but whether it really is and whether
* it's thread-safe, depends on the implementation of
* {@link Container#getInsets()} of the given container.</p>
* @since 13.2.2
*/
public class J2DContainerInsets implements Insets {
/**
* The container to get the insets from. Note that it will be used lazy and
* every time {@link #result(QuadFunction)} is called.
*/
private final Container container;

/**
* Ctor.
* @param container The container to get the insets from. Note that
*/
public J2DContainerInsets(final Container container) {
this.container = container;
}

@Override
public final <T> T result(
final QuadFunction<Integer, Integer, Integer, Integer, T> target
) {
final var insets = this.container.getInsets();
return target.apply(
insets.top,
insets.left,
insets.right,
insets.bottom
);
}
}
37 changes: 37 additions & 0 deletions src/main/java/graphic/j2d/event/J2DMouseTarget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018 Eugen Deutsch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package graphic.j2d.event;

import graphic.j2d.event.mouse.J2DMouse;

/**
* The observer of a certain event.
* @since 12.4.0
*/
public interface J2DMouseTarget {
/**
* Can register itself for an event source. Whether the object actually
* registers itself depends on whether it needs an event.
* @param source The source of the event.
*/
void registerFor(J2DMouse source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,45 @@
* DEALINGS IN THE SOFTWARE.
*/

package logic.functional;
package graphic.j2d.event.mouse;

import java.awt.Component;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;

/**
* An action that will be applied only once. Further calls of {@link #run()}
* won't be executed.
* <p>This class is mutable and not thread-safe, because it mutates its
* state when {@link #run()} is called.</p>
* @since 9.0.0
* Just registers and delegates the events to the event classes.
* <p>This class is mutable and not thread-safe, because it mutates the state
* of the given JFrame..</p>
* @since 12.2.0
*/
public class SingleAction implements Action {
/**
* The action to be called.
*/
private final Action action;

public class J2DBaseMouse implements J2DMouse {
/**
* Boolean whether the action has been called or not.
* The component who will get the listeners.
*/
private boolean called;
private final Component component;

/**
* Ctor.
* @param action The action to be called.
* @param component The component to register the events for.
*/
public SingleAction(final Action action) {
this(action, false);
public J2DBaseMouse(final Component component) {
this.component = component;
}

/**
* Ctor.
* @param action The action to be called.
* @param called Boolean whether the action has been called or not. Passing
* true as a value would mean that the action won't be called.
*/
public SingleAction(final Action action, final boolean called) {
this.action = action;
this.called = called;
@Override
public final void register(final MouseListener target) {
this.component.addMouseListener(target);
}

@Override
public final void register(final MouseMotionListener target) {
this.component.addMouseMotionListener(target);
}

@Override
public final void run() {
if (!this.called) {
this.action.run();
this.called = true;
}
public final void register(final MouseWheelListener target) {
this.component.addMouseWheelListener(target);
}
}
74 changes: 74 additions & 0 deletions src/main/java/graphic/j2d/event/mouse/J2DClick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2018 Eugen Deutsch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package graphic.j2d.event.mouse;

import graphic.j2d.shape.J2DShapeTarget;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import logic.functional.Action;
import logic.metric.PosOverlap;
import logic.metric.pos.Pos2D;

/**
* A mouse click bound on a component to apply some action on activation. The
* action (thus the click) will only be applied when the mouse has been pressed
* and released.
* <p>This class is immutable, but does mutate the state of the mouse.</p>
* @since 12.5.0
*/
public class J2DClick implements J2DShapeTarget {
/**
* The action to be applied when the click occurs.
*/
private final Action action;

/**
* Ctor.
* @param action The action to be applied when the click occurs.
*/
public J2DClick(final Action action) {
this.action = action;
}

@Override
public final void registerFor(
final J2DMouse source,
final PosOverlap overlap
) {
source.register(
(MouseListener) new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent event) {
if (overlap.contains(
new Pos2D(
event.getX(),
event.getY()
)
)) {
J2DClick.this.action.run();
}
}
}
);
}
}
60 changes: 60 additions & 0 deletions src/main/java/graphic/j2d/event/mouse/J2DMouse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2018 Eugen Deutsch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package graphic.j2d.event.mouse;

import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;

/*
The alternative to this would be to pass the actual JFrame to the classes,
but because they just need "addMouseListener" and the other mouse methods,
it would give the client much more control than he needs. Besides, one could
argue that it would be simpler to add a getter "mouseListener" to the
event classes but that would mean that each class has to define at least an
empty MouseListener and the Window would add them all to the JFrame. Using the
J2DMouse, I can omit the empty MouseListeners
*/
/**
* Serves as a layer between a {@link java.awt.Component} and the concrete
* event classes.
* @since 12.2.0
*/
public interface J2DMouse {
/**
* Registers a MouseListener.
* @param target The MouseListener who will get the events.
*/
void register(MouseListener target);

/**
* Registers a MouseMotionListener.
* @param target The MouseMotionListener who will get the events.
*/
void register(MouseMotionListener target);

/**
* Registers a MouseWheelListener.
* @param target The MouseWheelListener who will get the events.
*/
void register(MouseWheelListener target);
}