Skip to content

Commit

Permalink
moved GUI logic nested classes to proper packages; minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurnar committed Feb 11, 2018
1 parent 398681d commit c5d1f17
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 122 deletions.
14 changes: 14 additions & 0 deletions src/org/gsoft/showcase/diff/gui/Colors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.gsoft.showcase.diff.gui;

import java.awt.*;

public final class Colors {
public static final Color DELETED_LINES_HIGHLIGHT_COLOR = new Color(250, 180, 170);
public static final Color INSERTED_LINES_HIGHLIGHT_COLOR = new Color(174, 255, 202);
public static final Color MODIFIED_LINES_HIGHLIGHT_COLOR = new Color(221, 226, 255);
public static final Color MODIFIED_CHARS_HIGHLIGHT_COLOR = new Color(187, 211, 255);

private Colors() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.gsoft.showcase.diff.gui.components;

import org.gsoft.showcase.diff.gui.forms.DiffForm;
import org.gsoft.showcase.diff.gui.logic.DiffItemPosition;

import javax.swing.*;
import java.awt.*;
import java.util.List;

import static org.gsoft.showcase.diff.gui.Colors.*;

public class DiffMatchingImagePanel extends JPanel {
private static final int TOP_PADDING = 2; // TODO calculate this

Expand All @@ -16,7 +18,7 @@ public class DiffMatchingImagePanel extends JPanel {
//
// TODO use dedicated type to avoid confusion
//
private List<DiffPanesScrollController.DiffItemPosition> itemPositions;
private List<DiffItemPosition> itemPositions;

public DiffMatchingImagePanel() {
super(new BorderLayout());
Expand All @@ -38,7 +40,7 @@ public void paint(Graphics g) {
return;
}

for (DiffPanesScrollController.DiffItemPosition itemPosition : itemPositions) {
for (DiffItemPosition itemPosition : itemPositions) {
int[] xs, ys;

switch (itemPosition.getType()) {
Expand All @@ -49,21 +51,21 @@ public void paint(Graphics g) {
xs = new int[] { 0, getWidth(), getWidth(), 0};
ys = new int[] { itemPosition.getStartA() + TOP_PADDING, itemPosition.getStartB() + TOP_PADDING,
itemPosition.getEndB() + TOP_PADDING, itemPosition.getEndA() + TOP_PADDING};
g.setColor(DiffForm.MODIFIED_LINES_HIGHLIGHT_COLOR);
g.setColor(MODIFIED_LINES_HIGHLIGHT_COLOR);
break;

case INSERT:
xs = new int[] { 0, getWidth(), getWidth()};
ys = new int[] { itemPosition.getStartA() + TOP_PADDING, itemPosition.getStartB() + TOP_PADDING,
itemPosition.getEndB() + TOP_PADDING};
g.setColor(DiffForm.INSERTED_LINES_HIGHLIGHT_COLOR);
g.setColor(INSERTED_LINES_HIGHLIGHT_COLOR);
break;

case DELETE:
xs = new int[] { 0, getWidth(), 0};
ys = new int[] { itemPosition.getStartA() + TOP_PADDING, itemPosition.getStartB() + TOP_PADDING,
itemPosition.getEndA() + TOP_PADDING};
g.setColor(DiffForm.DELETED_LINES_HIGHLIGHT_COLOR);
g.setColor(DELETED_LINES_HIGHLIGHT_COLOR);
break;

default:
Expand All @@ -74,11 +76,11 @@ public void paint(Graphics g) {
}
}

public List<DiffPanesScrollController.DiffItemPosition> getItemPositions() {
public List<DiffItemPosition> getItemPositions() {
return itemPositions;
}

public void setItemPositions(List<DiffPanesScrollController.DiffItemPosition> itemPositions) {
public void setItemPositions(List<DiffItemPosition> itemPositions) {
this.itemPositions = itemPositions;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gsoft.showcase.diff.gui.components;

import org.gsoft.showcase.diff.gui.forms.DiffForm;
import org.gsoft.showcase.diff.gui.logic.DiffItemPosition;
import org.gsoft.showcase.diff.gui.logic.ExtendedDiffItemType;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
Expand All @@ -13,42 +14,6 @@
import java.util.List;

public final class DiffPanesScrollController {
public static final class DiffItemPosition {
private final int startA, startB;
private final int endA, endB;
private final DiffForm.ExtendedDiffItemType type;

public DiffItemPosition(int startA, int startB,
int endA, int endB,
DiffForm.ExtendedDiffItemType type) {
this.startA = startA;
this.startB = startB;
this.endA = endA;
this.endB = endB;
this.type = type;
}

public int getStartA() {
return startA;
}

public int getStartB() {
return startB;
}

public int getEndA() {
return endA;
}

public int getEndB() {
return endB;
}

public DiffForm.ExtendedDiffItemType getType() {
return type;
}
}

private static final class LinkedScrollRange {
final int startThis;
final int endThis;
Expand Down Expand Up @@ -117,7 +82,7 @@ public void scrollToPreviousChange() {

DiffItemPosition previousItem = diffItemPositions.get(currentDiffItemIndex - 1);

if (previousItem.type == DiffForm.ExtendedDiffItemType.EQUAL) {
if (previousItem.getType() == ExtendedDiffItemType.EQUAL) {
if (currentDiffItemIndex == 1) {
return; // no previous item
}
Expand All @@ -136,7 +101,7 @@ public void scrollToNextChange() {

DiffItemPosition nextItem = diffItemPositions.get(currentDiffItemIndex + 1);

if (nextItem.type == DiffForm.ExtendedDiffItemType.EQUAL) {
if (nextItem.getType() == ExtendedDiffItemType.EQUAL) {
if (currentDiffItemIndex == diffItemPositions.size() - 2) {
return; // no next item
}
Expand Down Expand Up @@ -231,14 +196,14 @@ private void updateScrollRanges() throws BadLocationException {

for (int i = 0; i < diffItemPositions.size(); i++) {
DiffItemPosition item = diffItemPositions.get(i);
switch (item.type) {
switch (item.getType()) {
case EQUAL:
case MODIFIED:
Rectangle firstCharRectA = textAreaA.modelToView(item.startA);
Rectangle lastCharRectA = textAreaA.modelToView(item.endA);
Rectangle firstCharRectA = textAreaA.modelToView(item.getStartA());
Rectangle lastCharRectA = textAreaA.modelToView(item.getEndA());

Rectangle firstCharRectB = textAreaB.modelToView(item.startB);
Rectangle lastCharRectB = textAreaB.modelToView(item.endB);
Rectangle firstCharRectB = textAreaB.modelToView(item.getStartB());
Rectangle lastCharRectB = textAreaB.modelToView(item.getEndB());

scrollRangesA.add(new LinkedScrollRange(
firstCharRectA.getLocation().y,
Expand All @@ -257,10 +222,10 @@ private void updateScrollRanges() throws BadLocationException {
break;

case DELETE:
firstCharRectA = textAreaA.modelToView(item.startA);
lastCharRectA = textAreaA.modelToView(item.endA);
firstCharRectA = textAreaA.modelToView(item.getStartA());
lastCharRectA = textAreaA.modelToView(item.getEndA());

firstCharRectB = textAreaB.modelToView(item.startB);
firstCharRectB = textAreaB.modelToView(item.getStartB());

scrollRangesA.add(new LinkedScrollRange(
firstCharRectA.getLocation().y,
Expand All @@ -272,10 +237,10 @@ private void updateScrollRanges() throws BadLocationException {
break;

case INSERT:
firstCharRectB = textAreaB.modelToView(item.startB);
lastCharRectB = textAreaB.modelToView(item.endB);
firstCharRectB = textAreaB.modelToView(item.getStartB());
lastCharRectB = textAreaB.modelToView(item.getEndB());

firstCharRectA = textAreaA.modelToView(item.startA);
firstCharRectA = textAreaA.modelToView(item.getStartA());

scrollRangesB.add(new LinkedScrollRange(
firstCharRectB.getLocation().y,
Expand All @@ -287,7 +252,7 @@ private void updateScrollRanges() throws BadLocationException {
break;

default:
throw new RuntimeException("unexpected diff item type: " + item.type);
throw new RuntimeException("unexpected diff item type: " + item.getType());
}
}

Expand All @@ -301,7 +266,7 @@ private void scrollLeftPaneToCurrentDiffItemPosition() {
Rectangle rect;

try {
rect = textAreaA.modelToView(diffItemPositions.get(currentDiffItemIndex).startA);
rect = textAreaA.modelToView(diffItemPositions.get(currentDiffItemIndex).getStartA());
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -340,14 +305,14 @@ private List<DiffItemPosition> getDiffItemPositionsInViewport() throws BadLocati

for (int i = minItemIndex; i <= maxItemIndex; i++) {
DiffItemPosition position = diffItemPositions.get(i);
Rectangle endARect = textAreaA.modelToView(position.endA);
Rectangle endBRect = textAreaB.modelToView(position.endB);
Rectangle endARect = textAreaA.modelToView(position.getEndA());
Rectangle endBRect = textAreaB.modelToView(position.getEndB());
result.add(new DiffItemPosition(
textAreaA.modelToView(position.startA).y - viewportAPosition,
textAreaB.modelToView(position.startB).y - viewportBPosition,
textAreaA.modelToView(position.getStartA()).y - viewportAPosition,
textAreaB.modelToView(position.getStartB()).y - viewportBPosition,
endARect.y + endARect.height - viewportAPosition,
endBRect.y + endBRect.height - viewportBPosition,
position.type
position.getType()
));
}

Expand Down
Loading

0 comments on commit c5d1f17

Please sign in to comment.