Skip to content

Commit

Permalink
Application: API cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Aug 10, 2024
1 parent 4ae88df commit b792181
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public void perform(@NotNull MenuItemContext ctx) {
Editor editor = ctx.getData(PlatformDataKeys.EDITOR_KEY);
EditorStack source = ctx.getData(PlatformDataKeys.EDITOR_STACK_KEY);
EditorStack target = source.getOpposite();

if (target != null) {
target.moveFrom(source, editor, target.getTabCount());
source.move(editor, target);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void perform(@NotNull MenuItemContext ctx) {
final EditorStack stack = ctx.getData(PlatformDataKeys.EDITOR_STACK_KEY);
final Editor editor = ctx.getData(PlatformDataKeys.EDITOR_KEY);

stack.splitFrom(editor, SwingConstants.SOUTH);
stack.split(editor, stack, SwingConstants.SOUTH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void perform(@NotNull MenuItemContext ctx) {
final EditorStack stack = ctx.getData(PlatformDataKeys.EDITOR_STACK_KEY);
final Editor editor = ctx.getData(PlatformDataKeys.EDITOR_KEY);

stack.splitFrom(editor, SwingConstants.EAST);
stack.split(editor, stack, SwingConstants.EAST);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,37 @@ public void paint(Graphics g) {
}
}

public boolean splitFrom(@NotNull Editor sourceEditor, int targetPosition) {
return splitFrom(this, sourceEditor, targetPosition);
public boolean split(@NotNull Editor sourceEditor, @NotNull EditorStack targetStack, int targetPosition) {
for (int i = 0; i < getTabCount(); i++) {
final JComponent component = (JComponent) getComponentAt(i);
final Editor editor = EDITOR_KEY.get(component);

if (editor == sourceEditor) {
return split(this, i, targetStack, targetPosition);
}
}

return false;
}

public boolean splitFrom(@NotNull EditorStack sourceStack, @NotNull Editor sourceEditor, int targetPosition) {
for (int i = 0; i < sourceStack.getTabCount(); i++) {
final JComponent component = (JComponent) sourceStack.getComponentAt(i);
public boolean move(@NotNull Editor sourceEditor, @NotNull EditorStack targetStack) {
return move(sourceEditor, targetStack, targetStack.getTabCount());
}

public boolean move(@NotNull Editor sourceEditor, @NotNull EditorStack targetStack, int targetIndex) {
for (int i = 0; i < getTabCount(); i++) {
final JComponent component = (JComponent) getComponentAt(i);
final Editor editor = EDITOR_KEY.get(component);

if (editor == sourceEditor) {
return splitFrom(sourceStack, i, targetPosition);
return move(this, i, targetStack, targetIndex);
}
}

return false;
}

private boolean splitFrom(@NotNull EditorStack sourceStack, int sourceIndex, int targetPosition) {
private static boolean split(@NotNull EditorStack sourceStack, int sourceIndex, @NotNull EditorStack targetStack, int targetPosition) {
if (targetPosition != SwingConstants.CENTER &&
targetPosition != SwingConstants.NORTH &&
targetPosition != SwingConstants.SOUTH &&
Expand All @@ -190,16 +203,16 @@ private boolean splitFrom(@NotNull EditorStack sourceStack, int sourceIndex, int
throw new IllegalArgumentException("Invalid position: " + targetPosition);
}

if (sourceStack == this && getTabCount() < 2) {
if (sourceStack == targetStack && targetStack.getTabCount() < 2) {
return false;
}

final EditorStack targetStack;
final int targetIndex;
final EditorStack destinationStack;
final int destinationIndex;

if (targetPosition == SwingConstants.CENTER) {
targetStack = this;
targetIndex = getTabCount();
destinationStack = targetStack;
destinationIndex = targetStack.getTabCount();
} else {
final int orientation;
final boolean leading;
Expand All @@ -212,61 +225,44 @@ private boolean splitFrom(@NotNull EditorStack sourceStack, int sourceIndex, int
leading = targetPosition == SwingConstants.WEST;
}

targetStack = getContainer().split(orientation, 0.5, leading).targetStack();
targetIndex = 0;
}

return targetStack.moveFrom(sourceStack, sourceIndex, targetIndex);
}

public boolean moveFrom(@NotNull EditorStack sourceStack, @NotNull Editor sourceEditor) {
return moveFrom(sourceStack, sourceEditor, getTabCount());
}

public boolean moveFrom(@NotNull EditorStack sourceStack, @NotNull Editor sourceEditor, int targetIndex) {
for (int i = 0; i < sourceStack.getTabCount(); i++) {
final JComponent component = (JComponent) sourceStack.getComponentAt(i);
final Editor editor = EDITOR_KEY.get(component);

if (editor == sourceEditor) {
return moveFrom(sourceStack, i, targetIndex);
}
destinationStack = targetStack.getContainer().split(orientation, 0.5, leading).targetStack();
destinationIndex = 0;
}

return false;
return move(sourceStack, sourceIndex, destinationStack, destinationIndex);
}

private boolean moveFrom(@NotNull EditorStack sourceStack, int sourceIndex, int targetIndex) {
private static boolean move(@NotNull EditorStack sourceStack, int sourceIndex, @NotNull EditorStack targetStack, int targetIndex) {
final var title = sourceStack.getTitleAt(sourceIndex);
final var icon = sourceStack.getIconAt(sourceIndex);
final var component = sourceStack.getComponentAt(sourceIndex);
final var tip = sourceStack.getToolTipTextAt(sourceIndex);

if (sourceStack != this) {
if (sourceStack != targetStack) {
sourceStack.remove(sourceIndex);

if (targetIndex == getTabCount()) {
addTab(title, icon, component, tip);
if (targetIndex == targetStack.getTabCount()) {
targetStack.addTab(title, icon, component, tip);
} else {
insertTab(title, icon, component, tip, targetIndex);
targetStack.insertTab(title, icon, component, tip, targetIndex);
}

setSelectedIndex(targetIndex);
targetStack.setSelectedIndex(targetIndex);
} else if (sourceIndex != targetIndex && targetIndex >= 0) {
if (targetIndex == getTabCount()) {
if (getTabCount() > 1) {
if (targetIndex == targetStack.getTabCount()) {
if (targetStack.getTabCount() > 1) {
sourceStack.remove(sourceIndex);
addTab(title, icon, component, tip);
setSelectedIndex(getTabCount() - 1);
targetStack.addTab(title, icon, component, tip);
targetStack.setSelectedIndex(targetStack.getTabCount() - 1);
}
} else if (sourceIndex > targetIndex) {
sourceStack.remove(sourceIndex);
insertTab(title, icon, component, tip, targetIndex);
setSelectedIndex(targetIndex);
targetStack.insertTab(title, icon, component, tip, targetIndex);
targetStack.setSelectedIndex(targetIndex);
} else {
sourceStack.remove(sourceIndex);
insertTab(title, icon, component, tip, targetIndex - 1);
setSelectedIndex(targetIndex - 1);
targetStack.insertTab(title, icon, component, tip, targetIndex - 1);
targetStack.setSelectedIndex(targetIndex - 1);
}
} else {
return false;
Expand Down Expand Up @@ -448,9 +444,9 @@ public void drop(DropTargetDropEvent event) {
final TabData source = getTransferData(event.getTransferable(), TabTransferable.tabFlavor);

if (dropIndex >= 0) {
success = moveFrom(source.stack(), source.index(), dropIndex);
success = move(source.stack(), source.index(), EditorStack.this, dropIndex);
} else if (splitPosition >= 0) {
success = splitFrom(source.stack(), source.index(), splitPosition);
success = split(source.stack(), source.index(), EditorStack.this, splitPosition);
} else {
success = false;
}
Expand Down

0 comments on commit b792181

Please sign in to comment.