Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz873525, rhbz872384 - unsaved indicator correction and try to make…
Browse files Browse the repository at this point in the history
… plain text area to work
  • Loading branch information
Patrick Huang committed Nov 9, 2012
1 parent f71d78d commit f1f870c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 76 deletions.
Expand Up @@ -232,8 +232,8 @@ public void validate(ToggleEditor editor)
}

/**
* Will fire a save event and also update cached targets so that a following navigation event won't cause another pending save event.
* If the save failed, TransUnitSaveService will revert the value back to what it was.
* Will fire a save event and a following navigation event will cause another pending save event.
* But TransUnitSaveService will ignore the second one.
* @see org.zanata.webtrans.client.service.TransUnitSaveService#onTransUnitSave(org.zanata.webtrans.client.events.TransUnitSaveEvent)
* @param transUnitId the state variable of the display that user has clicked on
*/
Expand Down Expand Up @@ -561,18 +561,23 @@ else if (!userWorkspaceContext.hasReadOnlyAccess())
public void setEditingState(TransUnitId transUnitId, TargetContentsDisplay.EditingState editingState)
{
Optional<TargetContentsDisplay> displayOptional = findDisplayById(transUnitId);
TargetContentsDisplay contentsDisplay = displayOptional.orNull();
if (contentsDisplay != null && editingState != contentsDisplay.getEditingState())
if (!displayOptional.isPresent())
{
if (editingState != TargetContentsDisplay.EditingState.SAVED)
{
contentsDisplay.setState(editingState);
}
else if (Objects.equal(contentsDisplay.getCachedTargets(), contentsDisplay.getNewTargets()))
{
// we set editing state to SAVED only if cached targets and in editor targets are equal
contentsDisplay.setState(TargetContentsDisplay.EditingState.SAVED);
}
return;
}

TargetContentsDisplay contentsDisplay = displayOptional.get();
if (editingState == TargetContentsDisplay.EditingState.SAVING)
{
contentsDisplay.setState(TargetContentsDisplay.EditingState.SAVING);
}
else if (!Objects.equal(contentsDisplay.getCachedTargets(), contentsDisplay.getNewTargets()))
{
contentsDisplay.setState(TargetContentsDisplay.EditingState.UNSAVED);
}
else
{
contentsDisplay.setState(TargetContentsDisplay.EditingState.SAVED);
}
}

Expand Down
Expand Up @@ -51,7 +51,7 @@ public void push(TransUnitSaveEvent event)
{
replacePreviousEventIfApplicable(comingEvent, prevEvent);
}
// Log.info("coming event has invalid state (i.e. old contents does not equal to previous event's new contents). Discard!");
Log.info("coming event will be discarded!" + event);
}

private static boolean stateEqual(TransUnitSaveEvent comingEvent, TransUnitSaveEvent prevEvent)
Expand Down
Expand Up @@ -78,6 +78,7 @@ public TransUnitSaveService(EventBus eventBus, CachingDispatchAsync dispatcher,
public void onTransUnitSave(TransUnitSaveEvent event)
{
TransUnitId idToSave = event.getTransUnitId();
Log.info("TU save event: new[" + event.getTargets() + "] + old: [" + event.getOldContents());
if (stateHasNotChanged(event))
{
Log.info("NO OP! state has not changed for " + idToSave);
Expand Down
60 changes: 24 additions & 36 deletions zanata-war/src/main/java/org/zanata/webtrans/client/ui/Editor.java
Expand Up @@ -8,7 +8,6 @@
import org.zanata.webtrans.client.resources.NavigationMessages;
import org.zanata.webtrans.client.view.TargetContentsDisplay;
import org.zanata.webtrans.shared.model.TransUnitId;
import com.allen_sauer.gwt.log.client.Log;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.gwt.core.client.GWT;
Expand All @@ -31,34 +30,15 @@

public class Editor extends Composite implements ToggleEditor
{
private String originalValue;
private TargetContentsDisplay.Listener listener;

interface EditorUiBinder extends UiBinder<Widget, Editor>
{
}

interface Styles extends CssResource
{

String rootContainer();

String hasValidationError();

String copyButton();

String targetWrapper();

String targetContainer();
}

private static EditorUiBinder uiBinder = GWT.create(EditorUiBinder.class);

private final int index;
private final TransUnitId id;

private final TransUnitId id;
private boolean isFocused;

private TargetContentsDisplay.Listener listener;

@UiField
Styles style;

Expand All @@ -71,8 +51,6 @@ interface Styles extends CssResource
@UiField
TranslatorListWidget translatorList;

NavigationMessages messages = GWT.create(NavigationMessages.class);

@UiField(provided = true)
EditorTextArea textArea;

Expand All @@ -84,7 +62,6 @@ interface Styles extends CssResource

public Editor(String displayString, int index, final TargetContentsDisplay.Listener listener, TransUnitId id)
{
this.originalValue = displayString;
this.listener = listener;
this.index = index;
this.id = id;
Expand Down Expand Up @@ -113,23 +90,22 @@ private void fireValidationEvent()
public void onValueChange(ValueChangeEvent<String> event)
{
fireValidationEvent();
boolean contentHasChanged = !Objects.equal(textArea.getText(), originalValue);
Log.info("value changed? " + contentHasChanged + " new: " + textArea.getText() + " old: " + originalValue);
TargetContentsDisplay.EditingState editingState = contentHasChanged ? UNSAVED : SAVED;
listener.setEditingState(id, editingState);
listener.setEditingState(id, UNSAVED);
}

@UiHandler("rootContainer")
public void onEditorClick(ClickEvent event)
{
listener.onEditorClicked(id, index);
textArea.startTypingTimer();
fireValidationEvent();
}

@UiHandler("textArea")
public void onTextAreaBlur(BlurEvent event)
{
isFocused = false;
textArea.stopTypingTimer();
}

@UiHandler("textArea")
Expand Down Expand Up @@ -269,12 +245,6 @@ public void refresh()
textArea.refresh();
}

@Override
public void updateCachedValue(String displayText)
{
originalValue = displayText;
}

@Override
public void removeTranslator(String name, String color)
{
Expand All @@ -286,4 +256,22 @@ public boolean isFocused()
{
return isFocused;
}

interface EditorUiBinder extends UiBinder<Widget, Editor>
{
}

interface Styles extends CssResource
{

String rootContainer();

String hasValidationError();

String copyButton();

String targetWrapper();

String targetContainer();
}
}
Expand Up @@ -30,8 +30,15 @@
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.TextArea;

public class EditorTextArea extends TextArea
Expand All @@ -40,10 +47,39 @@ public class EditorTextArea extends TextArea

private JavaScriptObject codeMirrorEditor;

// this timer is used when NOT using code mirror editor. We need this to fire validation and change editing state
private final Timer typingTimer = new Timer()
{
@Override
public void run()
{
ValueChangeEvent.fire(EditorTextArea.this, getText());
}
};

public EditorTextArea(boolean isUseCodeMirror)
{
super();
useCodeMirrorFlag = isUseCodeMirror;
if (!useCodeMirrorFlag)
{
addKeyDownHandler(new KeyDownHandler()
{
@Override
public void onKeyDown(KeyDownEvent keyDownEvent)
{
setVisibleLines(2);
while (getElement().getScrollHeight() > getElement().getClientHeight())
{
setVisibleLines(getVisibleLines() + 1);
}
if (keyDownEvent.getNativeKeyCode() == KeyCodes.KEY_ENTER)
{
setVisibleLines(getVisibleLines() + 1);
}
}
});
}
}

// see http://codemirror.net/doc/manual.html#usage
Expand Down Expand Up @@ -93,7 +129,9 @@ public void setText(String text)
@Override
public String getText()
{
return useCodeMirrorFlag ? getCodeMirrorContent() : super.getText();
String text = useCodeMirrorFlag ? getCodeMirrorContent() : super.getText();
// Log.info("editor text: [" + text + "]");
return text;
}

@Override
Expand Down Expand Up @@ -123,7 +161,10 @@ private void onBlur()
// callback function for the code mirror instance. Gets called when code mirror editor content has changed.
private void onChange()
{
ValueChangeEvent.fire(this, getCodeMirrorContent());
if (useCodeMirrorFlag)
{
ValueChangeEvent.fire(this, getCodeMirrorContent());
}
}

@Override
Expand All @@ -139,7 +180,10 @@ public void setValue(String value, boolean fireEvents)
{
setCodeMirrorContent(value);
}
super.setValue(value, fireEvents);
else
{
super.setValue(value, fireEvents);
}
}

private native String getCodeMirrorContent() /*-{
Expand All @@ -165,6 +209,10 @@ public void setFocus(boolean focused)
else
{
super.setFocus(focused);
if (focused)
{
typingTimer.scheduleRepeating(50);
}
}
}

Expand Down Expand Up @@ -274,4 +322,17 @@ private native void refreshCodeMirror() /*-{
editor.refresh();
}
}-*/;

public void startTypingTimer()
{
if (!useCodeMirrorFlag)
{
typingTimer.scheduleRepeating(50);
}
}

public void stopTypingTimer()
{
typingTimer.cancel();
}
}
Expand Up @@ -26,8 +26,6 @@ public interface ToggleEditor extends IsWidget, HasText, HasUpdateValidationWarn

void refresh();

void updateCachedValue(String displayText);

static enum ViewMode
{
VIEW, EDIT
Expand Down
Expand Up @@ -66,12 +66,8 @@ public interface TargetContentsDisplay extends WidgetDisplay, IsWidget, HasTrans

void setState(EditingState editingState);

EditingState getEditingState();

void updateCachedTargetsAndVersion(List<String> targets, Integer verNum, ContentState status);

TransUnit getCachedValue();

interface Listener
{
void validate(ToggleEditor editor);
Expand Down
Expand Up @@ -191,6 +191,10 @@ private static String resolveStyleName(ContentState status)
@Override
public void setState(EditingState editingState)
{
if (this.editingState == editingState)
{
return;
}
this.editingState = editingState;
if (editingState == EditingState.UNSAVED)
{
Expand All @@ -210,29 +214,13 @@ else if (editingState == EditingState.SAVING)
}
}

@Override
public EditingState getEditingState()
{
return editingState;
}

@Override
public void updateCachedTargetsAndVersion(List<String> targets, Integer verNum, ContentState status)
{
cachedValue = TransUnit.Builder.from(cachedValue).setTargets(targets).setVerNum(verNum).setStatus(status).build();
for (int i = 0; i < editors.size(); i++)
{
editors.get(i).updateCachedValue(targets.get(i));
}
editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus()));
}

@Override
public TransUnit getCachedValue()
{
return cachedValue;
}

@UiHandler("saveIcon")
public void onSaveAsApproved(ClickEvent event)
{
Expand Down Expand Up @@ -313,7 +301,6 @@ public void revertEditorContents()
{
String target = cachedTargets.get(i);
editors.get(i).setTextAndValidate(target);
editors.get(i).updateCachedValue(target);
}
editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus()));
}
Expand Down

0 comments on commit f1f870c

Please sign in to comment.