Skip to content

Commit 2c38016

Browse files
authored
Clear bucket validation error message after validation success / remove unnecessary code (#2120)
* Clear error message / code cleanup
1 parent 64ae61a commit 2c38016

3 files changed

Lines changed: 60 additions & 95 deletions

File tree

plugins/com.google.cloud.tools.eclipse.dataflow.ui.test/src/com/google/cloud/tools/eclipse/dataflow/ui/util/SelectFirstMatchingPrefixListenerTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static org.mockito.Mockito.verify;
2424
import static org.mockito.Mockito.when;
2525

26-
import com.google.cloud.tools.eclipse.dataflow.ui.util.SelectFirstMatchingPrefixListener.OnCompleteListener;
2726
import com.google.common.collect.ImmutableSortedSet;
2827
import java.util.SortedSet;
2928
import org.eclipse.swt.events.ModifyEvent;
@@ -48,16 +47,13 @@ public class SelectFirstMatchingPrefixListenerTest {
4847
private SortedSet<String> elements =
4948
ImmutableSortedSet.of("collidingText1", "collidingText222", "exampleText");
5049
private SelectFirstMatchingPrefixListener listener;
51-
@Mock
52-
private OnCompleteListener onCompleteListener;
5350

5451
@Before
5552
public void setup() {
5653
MockitoAnnotations.initMocks(this);
5754

5855
listener = new SelectFirstMatchingPrefixListener(combo);
5956
listener.setContents(elements);
60-
listener.addOnCompleteListener(onCompleteListener);
6157
}
6258

6359
@Test

plugins/com.google.cloud.tools.eclipse.dataflow.ui/src/com/google/cloud/tools/eclipse/dataflow/ui/preferences/RunOptionsDefaultsComponent.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
import com.google.cloud.tools.eclipse.dataflow.ui.util.ButtonFactory;
3232
import com.google.cloud.tools.eclipse.dataflow.ui.util.DisplayExecutor;
3333
import com.google.cloud.tools.eclipse.dataflow.ui.util.SelectFirstMatchingPrefixListener;
34-
import com.google.cloud.tools.eclipse.dataflow.ui.util.SelectFirstMatchingPrefixListener.OnCompleteListener;
3534
import com.google.cloud.tools.eclipse.ui.util.databinding.BucketNameValidator;
3635
import com.google.common.base.Strings;
36+
import java.util.Locale;
37+
import java.util.SortedSet;
38+
import java.util.concurrent.ExecutionException;
39+
import java.util.concurrent.Future;
3740
import org.eclipse.core.runtime.IStatus;
3841
import org.eclipse.core.runtime.NullProgressMonitor;
3942
import org.eclipse.jface.wizard.WizardPage;
@@ -53,10 +56,6 @@
5356
import org.eclipse.swt.widgets.Label;
5457
import org.eclipse.swt.widgets.Text;
5558
import org.eclipse.ui.statushandlers.StatusManager;
56-
import java.util.Locale;
57-
import java.util.SortedSet;
58-
import java.util.concurrent.ExecutionException;
59-
import java.util.concurrent.Future;
6059

6160
/**
6261
* Collects default run options for Dataflow Pipelines and provides means to create and modify them.
@@ -134,16 +133,16 @@ public RunOptionsDefaultsComponent(
134133

135134
completionListener = new SelectFirstMatchingPrefixListener(stagingLocationInput);
136135
stagingLocationInput.addModifyListener(completionListener);
137-
completionListener.addOnCompleteListener(new OnCompleteListener() {
136+
stagingLocationInput.addModifyListener(new ModifyListener() {
138137
@Override
139-
public void onComplete(String contents) {
140-
verifyStagingLocation(contents);
138+
public void modifyText(ModifyEvent event) {
139+
verifyStagingLocation(stagingLocationInput.getText());
141140
}
142141
});
143142
createButton.addSelectionListener(new CreateStagingLocationListener());
144143

145144
stagingLocationInput.addModifyListener(new EnableCreateButton());
146-
145+
147146
updateStagingLocations(project);
148147
messageTarget.setInfo("Set Pipeline Run Option Defaults");
149148
}
@@ -219,25 +218,31 @@ private void updateStagingLocations(String project) {
219218
* Ensure the staging location specified in the input combo is valid.
220219
*/
221220
private void verifyStagingLocation(final String stagingLocation) {
221+
setPageComplete(false);
222+
messageTarget.clear();
223+
222224
if (verifyJob != null) {
223225
// Cancel any existing verifyJob
226+
// FIXME: this has no effect, as "VerifyStagingLocationJob" doesn't honor cancellation.
224227
verifyJob.cancel();
225228
}
226-
if (client == null) {
227-
// We can't verify the staging locations because we don't have a GCS client
228-
return;
229-
}
230-
if (stagingLocation.isEmpty()) {
229+
if (trimBucketName().isEmpty()) {
231230
// If the staging location is empty, we don't have anything to verify; and we don't have any
232231
// interesting messaging.
233-
messageTarget.clear();
234232
setPageComplete(true);
235233
return;
236-
} else if (!bucketNameOk()) {
237-
setPageComplete(false);
234+
}
235+
236+
IStatus status = bucketNameStatus();
237+
if (!status.isOK()) {
238+
messageTarget.setError(status.getMessage());
239+
return;
240+
}
241+
242+
if (client == null) {
243+
// We can't verify the staging location because we don't have a GCS client
238244
return;
239245
}
240-
241246
verifyJob = VerifyStagingLocationJob.create(client, stagingLocation);
242247
verifyJob.schedule(VERIFY_LOCATION_DELAY_MS);
243248
final ListenableFutureProxy<VerifyStagingLocationResult> resultFuture =
@@ -278,7 +283,7 @@ public void focusLost(FocusEvent event) {
278283
updateStagingLocations(getProject());
279284
}
280285
}
281-
286+
282287
/**
283288
* Create a GCS bucket in the project specified in the project input at the location specified in
284289
* the staging location input.
@@ -302,7 +307,7 @@ public void widgetSelected(SelectionEvent event) {
302307
}
303308
}
304309
}
305-
310+
306311
private void setPageComplete(boolean complete) {
307312
if (page != null) {
308313
page.setPageComplete(complete);
@@ -346,28 +351,26 @@ public void run() {
346351
}
347352
}
348353
}
349-
354+
350355
private static final BucketNameValidator bucketNameValidator = new BucketNameValidator();
351-
352-
private boolean bucketNameOk() {
356+
357+
private String trimBucketName() {
353358
String bucketName = stagingLocationInput.getText().trim();
354359
if (bucketName.toLowerCase(Locale.US).startsWith("gs://")) {
355360
bucketName = bucketName.substring(5);
356361
}
357-
IStatus status = bucketNameValidator.validate(bucketName);
358-
if (!status.isOK()) {
359-
messageTarget.setError(status.getMessage());
360-
setPageComplete(false);
361-
}
362-
boolean enabled = status.isOK() && !bucketName.isEmpty();
363-
return enabled;
362+
return bucketName;
364363
}
365-
364+
365+
private IStatus bucketNameStatus() {
366+
return bucketNameValidator.validate(trimBucketName());
367+
}
368+
366369
private class EnableCreateButton implements ModifyListener {
367370

368371
@Override
369372
public void modifyText(ModifyEvent event) {
370-
boolean enabled = bucketNameOk();
373+
boolean enabled = !trimBucketName().isEmpty() && bucketNameStatus().isOK();
371374
createButton.setEnabled(enabled);
372375
}
373376

plugins/com.google.cloud.tools.eclipse.dataflow.ui/src/com/google/cloud/tools/eclipse/dataflow/ui/util/SelectFirstMatchingPrefixListener.java

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@
1717
package com.google.cloud.tools.eclipse.dataflow.ui.util;
1818

1919
import com.google.common.collect.ImmutableSortedSet;
20-
20+
import java.util.SortedSet;
2121
import org.eclipse.swt.events.ModifyEvent;
2222
import org.eclipse.swt.events.ModifyListener;
2323
import org.eclipse.swt.graphics.Point;
2424
import org.eclipse.swt.widgets.Combo;
2525

26-
import java.util.Collection;
27-
import java.util.LinkedHashSet;
28-
import java.util.SortedSet;
29-
3026
/**
3127
* Select the first string that contains the current input as a prefix whenever input is provided
3228
* to the provided Combo.
@@ -37,13 +33,10 @@ public class SelectFirstMatchingPrefixListener implements ModifyListener {
3733

3834
private SortedSet<String> contents;
3935

40-
private final Collection<OnCompleteListener> onCompleteListeners;
41-
4236
public SelectFirstMatchingPrefixListener(Combo targetCombo) {
4337
this.targetCombo = targetCombo;
4438
lastString = "";
4539
contents = ImmutableSortedSet.of();
46-
onCompleteListeners = new LinkedHashSet<>();
4740
}
4841

4942
public void setContents(SortedSet<String> contents) {
@@ -54,60 +47,33 @@ public void setContents(SortedSet<String> contents) {
5447
public void modifyText(ModifyEvent event) {
5548
String lastAsOfEvent = lastString;
5649
String prefix = targetCombo.getText();
57-
try {
58-
lastString = prefix;
59-
// If content was deleted, don't immediately replace it
60-
if (prefix.length() < lastAsOfEvent.length()) {
61-
return;
62-
}
63-
// Don't autofill if the user isn't at the end of the string
64-
if (targetCombo.getCaretPosition() < prefix.length() || prefix.length() < 6) {
65-
return;
66-
}
67-
SortedSet<String> matchesAndLater = contents.tailSet(prefix);
68-
if (matchesAndLater.isEmpty()) {
69-
return;
70-
}
71-
String firstMatch = matchesAndLater.first();
72-
if (!firstMatch.startsWith(prefix) || firstMatch.equals(prefix)) {
73-
return;
74-
}
75-
int comboIndex = contents.size() - matchesAndLater.size();
76-
// Don't fire on this modification of the combo
77-
targetCombo.removeModifyListener(this);
78-
targetCombo.select(comboIndex);
79-
80-
// Select the text that was added
81-
targetCombo.setSelection(new Point(prefix.length(), firstMatch.length()));
8250

83-
// Fire on later user modifications of the combo
84-
targetCombo.addModifyListener(this);
85-
} finally {
86-
notifyListeners(targetCombo.getText());
51+
lastString = prefix;
52+
// If content was deleted, don't immediately replace it
53+
if (prefix.length() < lastAsOfEvent.length()) {
54+
return;
8755
}
88-
}
89-
90-
/**
91-
* Add a listener that is notified after the target combo is updated.
92-
*/
93-
public void addOnCompleteListener(OnCompleteListener onComplete) {
94-
onCompleteListeners.add(onComplete);
95-
}
96-
97-
private void notifyListeners(String contents) {
98-
for (OnCompleteListener listener : onCompleteListeners) {
99-
listener.onComplete(contents);
56+
// Don't autofill if the user isn't at the end of the string
57+
if (targetCombo.getCaretPosition() < prefix.length() || prefix.length() < 6) {
58+
return;
10059
}
101-
}
60+
SortedSet<String> matchesAndLater = contents.tailSet(prefix);
61+
if (matchesAndLater.isEmpty()) {
62+
return;
63+
}
64+
String firstMatch = matchesAndLater.first();
65+
if (!firstMatch.startsWith(prefix) || firstMatch.equals(prefix)) {
66+
return;
67+
}
68+
int comboIndex = contents.size() - matchesAndLater.size();
69+
// Don't fire on this modification of the combo
70+
targetCombo.removeModifyListener(this);
71+
targetCombo.select(comboIndex);
72+
73+
// Select the text that was added
74+
targetCombo.setSelection(new Point(prefix.length(), firstMatch.length()));
10275

103-
/**
104-
* A listener that is notified when the {@link SelectFirstMatchingPrefixListener} completes with
105-
* the contents of the Combo.
106-
*/
107-
public interface OnCompleteListener {
108-
/**
109-
* @param contents the contents of the {@link SelectFirstMatchingPrefixListener} target.
110-
*/
111-
void onComplete(String contents);
76+
// Fire on later user modifications of the combo
77+
targetCombo.addModifyListener(this);
11278
}
11379
}

0 commit comments

Comments
 (0)