refactor: do not move maximized or iconified frames

This commit is contained in:
Sergey Malenkov
2016-09-07 22:58:52 +03:00
parent 6168fd0d2c
commit 47f3109378
3 changed files with 56 additions and 24 deletions
@@ -83,6 +83,19 @@ abstract class WindowMouseListener extends MouseAdapter implements MouseInputLis
process(event, true);
}
/**
* @param view the component to move/resize
* @return {@code true} if the specified component cannot be moved/resized, or {@code false} otherwise
*/
protected boolean isDisabled(Component view) {
if (view instanceof Frame) {
int state = ((Frame)view).getExtendedState();
if (isStateSet(Frame.ICONIFIED, state)) return true;
if (isStateSet(Frame.MAXIMIZED_BOTH, state)) return true;
}
return false;
}
/**
* Updates a cursor and starts moving/resizing if the {@code start} is specified.
*/
@@ -92,7 +105,7 @@ abstract class WindowMouseListener extends MouseAdapter implements MouseInputLis
Component content = getContent(event);
Component view = getView(content);
if (view != null) {
myType = getCursorType(view, event.getLocationOnScreen());
myType = isDisabled(view) ? CUSTOM_CURSOR : getCursorType(view, event.getLocationOnScreen());
setCursor(content, getPredefinedCursor(myType == CUSTOM_CURSOR ? DEFAULT_CURSOR : myType));
if (start && myType != CUSTOM_CURSOR) {
myLocation = event.getLocationOnScreen();
@@ -113,7 +126,14 @@ abstract class WindowMouseListener extends MouseAdapter implements MouseInputLis
Component view = getView(content);
if (view != null) {
Rectangle bounds = new Rectangle(myViewBounds);
updateBounds(bounds, view, event.getXOnScreen() - myLocation.x, event.getYOnScreen() - myLocation.y);
int dx = event.getXOnScreen() - myLocation.x;
int dy = event.getYOnScreen() - myLocation.y;
if (myType == DEFAULT_CURSOR && view instanceof Frame) {
int state = ((Frame)view).getExtendedState();
if (isStateSet(Frame.MAXIMIZED_HORIZ, state)) dx = 0;
if (isStateSet(Frame.MAXIMIZED_VERT, state)) dy = 0;
}
updateBounds(bounds, view, dx, dy);
if (!bounds.equals(view.getBounds())) {
view.setBounds(bounds);
view.invalidate();
@@ -166,4 +186,8 @@ abstract class WindowMouseListener extends MouseAdapter implements MouseInputLis
public boolean isBusy() {
return myLocation != null;
}
static boolean isStateSet(int mask, int state) {
return mask == (mask & state);
}
}
@@ -20,6 +20,7 @@ import java.awt.*;
import java.awt.event.MouseEvent;
import static java.awt.Cursor.*;
import static java.awt.event.InputEvent.BUTTON1_MASK;
/**
* @author Sergey Malenkov
@@ -44,4 +45,23 @@ public class WindowMoveListener extends WindowMouseListener {
public void mouseMoved(MouseEvent event) {
// ignore cursor updating
}
@Override
public void mouseClicked(MouseEvent event) {
if (event.isConsumed()) return;
if (BUTTON1_MASK == (BUTTON1_MASK & event.getModifiers()) && 1 < event.getClickCount()) {
Component view = getView(getContent(event));
if (view instanceof Frame) {
Frame frame = (Frame)view;
int state = frame.getExtendedState();
if (!isStateSet(Frame.ICONIFIED, state) && frame.isResizable()) {
event.consume();
frame.setExtendedState(isStateSet(Frame.MAXIMIZED_BOTH, state)
? (state & ~Frame.MAXIMIZED_BOTH)
: (state | Frame.MAXIMIZED_BOTH));
}
}
}
super.mouseClicked(event);
}
}
@@ -67,18 +67,15 @@ public class WindowResizeListener extends WindowMouseListener {
return myBorder;
}
@Override
protected boolean isDisabled(Component view) {
if (view instanceof Dialog && !((Dialog)view).isResizable()) return true;
if (view instanceof Frame && !((Frame)view).isResizable()) return true;
return super.isDisabled(view);
}
@Override
int getCursorType(Component view, Point location) {
if (view instanceof Dialog) {
Dialog dialog = (Dialog)view;
if (!dialog.isResizable()) return CUSTOM_CURSOR;
}
else if (view instanceof Frame) {
Frame frame = (Frame)view;
if (!frame.isResizable()) return CUSTOM_CURSOR;
if (isStateSet(frame, Frame.MAXIMIZED_BOTH)) return CUSTOM_CURSOR;
if (isStateSet(frame, Frame.ICONIFIED)) return CUSTOM_CURSOR;
}
Component parent = view instanceof Window ? null : view.getParent();
if (parent != null) {
convertPointFromScreen(location, parent);
@@ -104,12 +101,12 @@ public class WindowResizeListener extends WindowMouseListener {
Insets expected = getResizeBorder(view);
if (expected != null) {
if (view instanceof Frame) {
Frame frame = (Frame)view;
if (isStateSet(frame, Frame.MAXIMIZED_HORIZ)) {
int state = ((Frame)view).getExtendedState();
if (isStateSet(Frame.MAXIMIZED_HORIZ, state)) {
left = Integer.MAX_VALUE;
right = Integer.MAX_VALUE;
}
if (isStateSet(frame, Frame.MAXIMIZED_VERT)) {
if (isStateSet(Frame.MAXIMIZED_VERT, state)) {
top = Integer.MAX_VALUE;
bottom = Integer.MAX_VALUE;
}
@@ -140,11 +137,6 @@ public class WindowResizeListener extends WindowMouseListener {
@Override
void updateBounds(Rectangle bounds, Component view, int dx, int dy) {
if (myType == DEFAULT_CURSOR && view instanceof Frame) {
Frame frame = (Frame)view;
if (isStateSet(frame, Frame.MAXIMIZED_HORIZ)) dx = 0;
if (isStateSet(frame, Frame.MAXIMIZED_VERT)) dy = 0;
}
Dimension minimum = view.getMinimumSize();
if (myType == NE_RESIZE_CURSOR || myType == E_RESIZE_CURSOR || myType == SE_RESIZE_CURSOR || myType == DEFAULT_CURSOR) {
bounds.width += fixMinSize(dx, bounds.width, minimum.width);
@@ -167,8 +159,4 @@ public class WindowResizeListener extends WindowMouseListener {
private static int fixMinSize(int delta, int value, int min) {
return delta + value < min ? min - value : delta;
}
private static boolean isStateSet(Frame frame, int mask) {
return mask == (mask & frame.getExtendedState());
}
}