| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.debug.ui.liveedit; | 5 package org.chromium.debug.ui.liveedit; |
| 6 | 6 |
| 7 import static org.chromium.debug.ui.DialogUtils.createErrorOptional; | 7 import static org.chromium.debug.ui.DialogUtils.createErrorOptional; |
| 8 import static org.chromium.debug.ui.DialogUtils.createOptional; | 8 import static org.chromium.debug.ui.DialogUtils.createOptional; |
| 9 | 9 |
| 10 import org.chromium.debug.core.model.PushChangesPlan; | 10 import org.chromium.debug.core.model.PushChangesPlan; |
| 11 import org.chromium.debug.ui.DialogUtils.Message; | 11 import org.chromium.debug.ui.DialogUtils.Message; |
| 12 import org.chromium.debug.ui.DialogUtils.MessagePriority; | 12 import org.chromium.debug.ui.DialogUtils.MessagePriority; |
| 13 import org.chromium.debug.ui.DialogUtils.Optional; | 13 import org.chromium.debug.ui.DialogUtils.Optional; |
| 14 import org.chromium.debug.ui.DialogUtils.Scope; | 14 import org.chromium.debug.ui.DialogUtils.Scope; |
| 15 import org.chromium.debug.ui.DialogUtils.Updater; | 15 import org.chromium.debug.ui.DialogUtils.Updater; |
| 16 import org.chromium.debug.ui.DialogUtils.ValueConsumer; | 16 import org.chromium.debug.ui.DialogUtils.ValueConsumer; |
| 17 import org.chromium.debug.ui.DialogUtils.ValueSource; | 17 import org.chromium.debug.ui.DialogUtils.ValueSource; |
| 18 import org.chromium.sdk.UpdatableScript; | 18 import org.chromium.sdk.UpdatableScript; |
| 19 import org.chromium.sdk.UpdatableScript.ChangeDescription; | 19 import org.chromium.sdk.UpdatableScript.CompileErrorFailure; |
| 20 import org.eclipse.osgi.util.NLS; | 20 import org.eclipse.osgi.util.NLS; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * An asynchronous loader of LiveEdit update preview data. It deals with outer w
orld in terms of | 23 * An asynchronous loader of LiveEdit update preview data. It deals with outer w
orld in terms of |
| 24 * {@link Updater} sources/consumers. | 24 * {@link Updater} sources/consumers. |
| 25 * It implements {@link ValueSource} interface that provides a loaded result, or
null data | 25 * It implements {@link ValueSource} interface that provides a loaded result, or
null data |
| 26 * if the result is not loaded yet. Updater gets notified whenever result is del
ivered. | 26 * if the result is not loaded yet. Updater gets notified whenever result is del
ivered. |
| 27 * <p>The input parameter may be changed at any moment. | 27 * <p>The input parameter may be changed at any moment. |
| 28 * <p>The loader may be in active or passive state. Since the preview data this
class loads | 28 * <p>The loader may be in active or passive state. Since the preview data this
class loads |
| 29 * is optional and is not required for wizard's work, the loader should be kept
passive until | 29 * is optional and is not required for wizard's work, the loader should be kept
passive until |
| 30 * user actually needs its result (turns the page). | 30 * user actually needs its result (turns the page). |
| 31 */ | 31 */ |
| 32 class PreviewLoader implements ValueSource<Optional<PreviewLoader.Data>> { | 32 class PreviewLoader implements ValueSource<Optional<PreviewLoader.Data>> { |
| 33 private final Updater updater; | 33 private final Updater updater; |
| 34 private final ValueSource<PushChangesPlan> inputParameterSource; | 34 private final ValueSource<PushChangesPlan> inputParameterSource; |
| 35 private boolean active = false; | 35 private boolean active = false; |
| 36 private final Monitor dataMonitor = new Monitor(); | 36 private final Monitor dataMonitor = new Monitor(); |
| 37 | 37 |
| 38 PreviewLoader(Updater updater, ValueSource<PushChangesPlan> inputParameterSour
ce) { | 38 PreviewLoader(Updater updater, |
| 39 ValueSource<PushChangesPlan> inputParameterSource) { |
| 39 this.updater = updater; | 40 this.updater = updater; |
| 40 this.inputParameterSource = inputParameterSource; | 41 this.inputParameterSource = inputParameterSource; |
| 41 } | 42 } |
| 42 | 43 |
| 43 void registerSelf(Scope scope) { | 44 void registerSelf(Scope scope) { |
| 44 updater.addSource(scope, this); | 45 updater.addSource(scope, this); |
| 45 updater.addConsumer(scope, parametersConsumer); | 46 updater.addConsumer(scope, parametersConsumer); |
| 46 updater.addDependency(parametersConsumer, inputParameterSource); | 47 updater.addDependency(parametersConsumer, inputParameterSource); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void setActive(boolean active) { | 50 void setActive(boolean active) { |
| 50 this.active = active; | 51 this.active = active; |
| 51 if (active) { | 52 if (active) { |
| 52 requestPreview(); | 53 requestPreview(); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 private void requestPreview() { | 57 private void requestPreview() { |
| 57 final PushChangesPlan plan = inputParameterSource.getValue(); | 58 final PushChangesPlan plan = inputParameterSource.getValue(); |
| 58 boolean inputIsNew = dataMonitor.updateInputAndStarted(plan); | 59 boolean inputIsNew = dataMonitor.updateInputAndStarted(plan); |
| 59 if (!inputIsNew) { | 60 if (!inputIsNew) { |
| 60 return; | 61 return; |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Report about our value becoming empty. | 64 // Report about our value becoming empty. |
| 64 updater.reportChanged(this); | 65 updater.reportChanged(this); |
| 65 | 66 |
| 66 UpdatableScript.UpdateCallback callback = new UpdatableScript.UpdateCallback
() { | 67 UpdatableScript.UpdateCallback callback = new UpdatableScript.UpdateCallback
() { |
| 67 public void failure(String message, UpdatableScript.Failure failure) { | 68 public void failure(final String message, UpdatableScript.Failure failure)
{ |
| 68 Optional<Data> error = createErrorOptional( | 69 Optional<Data> result = failure.accept( |
| 69 new Message(NLS.bind(Messages.PreviewLoader_FAILED_TO_GET, message), | 70 new UpdatableScript.Failure.Visitor<Optional<Data>>() { |
| 70 MessagePriority.WARNING)); | 71 @Override |
| 71 done(error); | 72 public Optional<Data> visitUnspecified() { |
| 73 return createErrorOptional( |
| 74 new Message(NLS.bind(Messages.PreviewLoader_FAILED_TO_GET, messa
ge), |
| 75 MessagePriority.WARNING)); |
| 76 } |
| 77 |
| 78 @Override |
| 79 public Optional<Data> visitCompileError(final CompileErrorFailure comp
ileError) { |
| 80 Data data = new Data() { |
| 81 @Override |
| 82 public <R> R accept(Visitor<R> visitor) { |
| 83 return visitor.visitCompileError(compileError); |
| 84 } |
| 85 @Override public PushChangesPlan getChangesPlan() { |
| 86 return plan; |
| 87 } |
| 88 }; |
| 89 return createOptional(data); |
| 90 } |
| 91 }); |
| 92 done(result); |
| 72 } | 93 } |
| 73 public void success(Object report, | 94 public void success(Object report, |
| 74 final UpdatableScript.ChangeDescription changeDescription) { | 95 final UpdatableScript.ChangeDescription changeDescription) { |
| 75 Optional<Data> result; | 96 Optional<Data> result; |
| 76 if (changeDescription == null) { | 97 if (changeDescription == null) { |
| 77 result = EMPTY_DATA; | 98 result = EMPTY_DATA; |
| 78 } else { | 99 } else { |
| 79 Data data = new Data() { | 100 Data data = new Data() { |
| 80 @Override public PushChangesPlan getChangesPlan() { | 101 @Override public PushChangesPlan getChangesPlan() { |
| 81 return plan; | 102 return plan; |
| 82 } | 103 } |
| 83 @Override public ChangeDescription getChangeDescription() { | 104 @Override public <R> R accept(Visitor<R> visitor) { |
| 84 return changeDescription; | 105 return visitor.visitSuccess(changeDescription); |
| 85 } | 106 } |
| 86 }; | 107 }; |
| 87 result = createOptional(data); | 108 result = createOptional(data); |
| 88 } | 109 } |
| 89 done(result); | 110 done(result); |
| 90 } | 111 } |
| 91 private void done(Optional<Data> result) { | 112 private void done(Optional<Data> result) { |
| 92 boolean resultTaken = dataMonitor.updateResult(result, plan); | 113 boolean resultTaken = dataMonitor.updateResult(result, plan); |
| 93 if (resultTaken) { | 114 if (resultTaken) { |
| 94 updater.reportChanged(PreviewLoader.this); | 115 updater.reportChanged(PreviewLoader.this); |
| 95 updater.updateAsync(); | 116 updater.updateAsync(); |
| 96 } | 117 } |
| 97 } | 118 } |
| 98 }; | 119 }; |
| 99 | 120 |
| 100 plan.execute(true, callback, null); | 121 plan.execute(true, callback, null); |
| 101 } | 122 } |
| 102 | 123 |
| 103 public interface Data { | 124 public interface Data { |
| 104 UpdatableScript.ChangeDescription getChangeDescription(); | 125 interface Visitor<R> { |
| 126 R visitSuccess(UpdatableScript.ChangeDescription changeDescription); |
| 127 R visitCompileError(UpdatableScript.CompileErrorFailure compileError); |
| 128 } |
| 129 |
| 130 <R> R accept(Visitor<R> visitor); |
| 131 |
| 105 PushChangesPlan getChangesPlan(); | 132 PushChangesPlan getChangesPlan(); |
| 106 } | 133 } |
| 107 | 134 |
| 108 public Optional<Data> getValue() { | 135 public Optional<Data> getValue() { |
| 109 return dataMonitor.getValue(); | 136 return dataMonitor.getValue(); |
| 110 } | 137 } |
| 111 | 138 |
| 112 // A consumer that receive the actual input parameter (ScriptTargetMapping). | 139 // A consumer that receive the actual input parameter (ScriptTargetMapping). |
| 113 private final ValueConsumer parametersConsumer = new ValueConsumer() { | 140 private final ValueConsumer parametersConsumer = new ValueConsumer() { |
| 114 public void update(Updater updater) { | 141 public void update(Updater updater) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 this.result = result; | 192 this.result = result; |
| 166 return true; | 193 return true; |
| 167 } | 194 } |
| 168 } | 195 } |
| 169 | 196 |
| 170 private static final Optional<Data> NO_DATA = createErrorOptional( | 197 private static final Optional<Data> NO_DATA = createErrorOptional( |
| 171 new Message(Messages.PreviewLoader_WAITING_FOR_DIFF, MessagePriority.NONE)
); | 198 new Message(Messages.PreviewLoader_WAITING_FOR_DIFF, MessagePriority.NONE)
); |
| 172 private static final Optional<Data> EMPTY_DATA = | 199 private static final Optional<Data> EMPTY_DATA = |
| 173 createErrorOptional(new Message(Messages.PreviewLoader_FAILED_TO_LOAD, Mes
sagePriority.NONE)); | 200 createErrorOptional(new Message(Messages.PreviewLoader_FAILED_TO_LOAD, Mes
sagePriority.NONE)); |
| 174 } | 201 } |
| OLD | NEW |