Insert a new comment editor when a patch line is double clicked

The editor currently doesn't save state, but it does insert into
the UI and the Cancel button removes it from the UI.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2008-12-19 18:58:39 -08:00
parent f1f00d5a6c
commit 91b1f7187a
7 changed files with 194 additions and 3 deletions

View File

@@ -15,13 +15,20 @@
package com.google.gerrit.client.patches;
import com.google.gerrit.client.FormatUtil;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.changes.Util;
import com.google.gerrit.client.data.AccountInfoCache;
import com.google.gerrit.client.reviewdb.Patch;
import com.google.gerrit.client.reviewdb.PatchLineComment;
import com.google.gerrit.client.ui.ComplexDisclosurePanel;
import com.google.gerrit.client.ui.FancyFlexTable;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwtjsonrpc.client.VoidResult;
import java.sql.Timestamp;
import java.util.ArrayList;
@@ -30,6 +37,7 @@ import java.util.List;
public abstract class AbstractPatchContentTable extends FancyFlexTable<Object> {
private static final long AGE = 7 * 24 * 60 * 60 * 1000L;
protected AccountInfoCache accountCache = AccountInfoCache.empty();
protected Patch.Id patchKey;
private final Timestamp aged =
new Timestamp(System.currentTimeMillis() - AGE);
@@ -37,11 +45,59 @@ public abstract class AbstractPatchContentTable extends FancyFlexTable<Object> {
table.setStyleName("gerrit-PatchContentTable");
}
@Override
protected MyFlexTable createFlexTable() {
return new DoubleClickFlexTable();
}
@Override
protected Object getRowItemKey(final Object item) {
return null;
}
/** Invoked when the user clicks on a table cell. */
protected abstract void onCellDoubleClick(int row, int column);
protected PatchLineComment newComment(final int line, final short side) {
final PatchLineComment r =
new PatchLineComment(new PatchLineComment.Id(patchKey, "blargh"), line,
Gerrit.getUserAccount().getId());
r.setSide(side);
r.setMessage("");
return r;
}
protected void createCommentEditor(final int row, final int column,
final int line, final short side) {
if (!Gerrit.isSignedIn()) {
Gerrit.doSignIn(new AsyncCallback<VoidResult>() {
public void onSuccess(final VoidResult result) {
createCommentEditor(row, column, line, side);
}
public void onFailure(Throwable caught) {
}
});
return;
}
final PatchLineComment newComment = newComment(line, side);
table.insertRow(row);
table.setWidget(row, column, new CommentEditorPanel(newComment) {
@Override
void onCancel() {
final int n = table.getRowCount();
for (int i = 0; i < n; i++) {
if (column < table.getCellCount(i)
&& table.getWidget(i, column) == this) {
table.removeRow(i);
break;
}
}
}
});
}
@Override
protected void onOpenItem(final Object item) {
if (item instanceof CommentList) {
@@ -56,6 +112,10 @@ public abstract class AbstractPatchContentTable extends FancyFlexTable<Object> {
accountCache = aic;
}
public void setPatchKey(final Patch.Id id) {
patchKey = id;
}
protected void bindComment(final int row, final int col,
final PatchLineComment line, final boolean isLast) {
final LineCommentPanel mp = new LineCommentPanel(line);
@@ -104,4 +164,31 @@ public abstract class AbstractPatchContentTable extends FancyFlexTable<Object> {
final List<ComplexDisclosurePanel> panels =
new ArrayList<ComplexDisclosurePanel>();
}
protected class DoubleClickFlexTable extends MyFlexTable {
public DoubleClickFlexTable() {
sinkEvents(Event.ONDBLCLICK);
}
@Override
public void onBrowserEvent(final Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK: {
// Find out which cell was actually clicked.
Element td = getEventTargetCell(event);
if (td == null) {
return;
}
Element tr = DOM.getParent(td);
Element body = DOM.getParent(tr);
int row = DOM.getChildIndex(body, tr);
int column = DOM.getChildIndex(tr, td);
onCellDoubleClick(row, column);
break;
}
default:
super.onBrowserEvent(event);
}
}
}
}

View File

@@ -0,0 +1,58 @@
package com.google.gerrit.client.patches;
import com.google.gerrit.client.reviewdb.PatchLineComment;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
public class CommentEditorPanel extends Composite implements ClickListener {
private final PatchLineComment comment;
private final TextArea text;
private final Button save;
private final Button cancel;
public CommentEditorPanel(final PatchLineComment plc) {
comment = plc;
final FlowPanel body = new FlowPanel();
body.setStyleName("gerrit-CommentEditor");
text = new TextArea();
text.setCharacterWidth(60);
text.setVisibleLines(5);
body.add(text);
final FlowPanel buttons = new FlowPanel();
buttons.setStyleName("gerrit-CommentEditor-Buttons");
body.add(buttons);
save = new Button();
save.setText("Save");
save.addClickListener(this);
buttons.add(save);
cancel = new Button();
cancel.setText("Cancel");
cancel.addClickListener(this);
buttons.add(cancel);
initWidget(body);
}
public void onClick(Widget sender) {
if (sender == save) {
onSave();
} else if (sender == cancel) {
onCancel();
}
}
void onSave() {
}
void onCancel() {
}
}

View File

@@ -50,6 +50,7 @@ public class PatchUnifiedScreen extends PatchScreen {
}
private void display(final UnifiedPatchDetail detail) {
diffTable.setPatchKey(detail.getPatch().getKey());
diffTable.setAccountInfoCache(detail.getAccounts());
diffTable.display(detail.getLines());
}

View File

@@ -28,8 +28,26 @@ public class SideBySideTable extends AbstractPatchContentTable {
private int fileCnt;
private int maxLineNumber;
@Override
protected void onCellDoubleClick(final int row, final int column) {
if (column > 1 && getRowItem(row) instanceof SideBySideLineList) {
final SideBySideLineList pl = (SideBySideLineList) getRowItem(row);
final short file = (short) ((column - 1) / 2);
final SideBySideLine line = pl.lines.get(file);
switch (line.getType()) {
case DELETE:
case EQUAL:
case INSERT: {
createCommentEditor(row + 1, column, line.getLineNumber(), file);
break;
}
}
}
}
public void display(final SideBySidePatchDetail detail) {
setAccountInfoCache(detail.getAccounts());
setPatchKey(detail.getPatch().getKey());
fileCnt = detail.getFileCount();
maxLineNumber = detail.getLineCount();
@@ -67,7 +85,7 @@ public class SideBySideTable extends AbstractPatchContentTable {
}
prior = pLine;
setRowItem(row, pLine);
setRowItem(row, new SideBySideLineList(pLine));
int nextComment = row;
int lastComment = row;
@@ -226,4 +244,12 @@ public class SideBySideTable extends AbstractPatchContentTable {
nc.append("</tr>");
}
private static class SideBySideLineList {
final List<SideBySideLine> lines;
SideBySideLineList(final List<SideBySideLine> a) {
lines = a;
}
}
}

View File

@@ -21,6 +21,22 @@ import java.util.Iterator;
import java.util.List;
public class UnifiedDiffTable extends AbstractPatchContentTable {
@Override
protected void onCellDoubleClick(final int row, final int column) {
if (column == 1 && getRowItem(row) instanceof PatchLine) {
final PatchLine pl = (PatchLine) getRowItem(row);
switch (pl.getType()) {
case PRE_IMAGE:
case CONTEXT:
createCommentEditor(row + 1, column, pl.getOldLineNumber(), (short) 0);
break;
case POST_IMAGE:
createCommentEditor(row + 1, column, pl.getOldLineNumber(), (short) 1);
break;
}
}
}
public void display(final List<PatchLine> list) {
final StringBuilder nc = new StringBuilder();
for (final PatchLine pLine : list) {

View File

@@ -113,7 +113,6 @@ public final class PatchLineComment {
author = a;
writtenOn = new Timestamp(System.currentTimeMillis());
setStatus(Status.DRAFT);
setSide((short) 1);
}
public PatchLineComment.Id getKey() {

View File

@@ -60,7 +60,7 @@ public abstract class FancyFlexTable<RowItem> extends Composite implements
protected FancyFlexTable() {
pointer = Gerrit.ICONS.arrowRight().createImage();
table = new MyFlexTable();
table = createFlexTable();
table.addStyleName(MY_STYLE);
focusy = new FocusPanel(table);
focusy.addKeyboardListener(new KeyboardListenerAdapter() {
@@ -89,6 +89,10 @@ public abstract class FancyFlexTable<RowItem> extends Composite implements
table.getCellFormatter().addStyleName(0, C_ARROW, S_ICON_HEADER);
}
protected MyFlexTable createFlexTable() {
return new MyFlexTable();
}
protected RowItem getRowItem(final int row) {
return FancyFlexTable.<RowItem> getRowItem(table.getCellFormatter()
.getElement(row, 0));