Permalink
Browse files

Move administrative code into new AdminView class

This keeps our main servlet clean.  This organization was inspired by
Polly the Pollster.
  • Loading branch information...
1 parent 6773c23 commit ea336d98e4247add7bdd434a4746b3b6d1fc7dc9 @emk committed Aug 31, 2009
Showing with 94 additions and 66 deletions.
  1. +85 −0 src/buglinky/AdminView.java
  2. +9 −66 src/buglinky/BugLinkyServlet.java
@@ -0,0 +1,85 @@
+package buglinky;
+
+import java.util.logging.Logger;
+
+import com.google.wave.api.Blip;
+import com.google.wave.api.ElementType;
+import com.google.wave.api.Event;
+import com.google.wave.api.FormElement;
+import com.google.wave.api.FormView;
+import com.google.wave.api.TextView;
+import com.google.wave.api.Wavelet;
+
+/** Display our configuration options and instructions for using buglinky. */
+public class AdminView {
+ private static final Logger LOG =
+ Logger.getLogger(AdminView.class.getName());
+
+ /** The URL to a specific bug in our bug tracker, minus the number. */
+ private static final String BUG_URL =
+ "http://code.google.com/p/google-wave-resources/issues/detail?id=";
+
+ /** The instructions to display when we join a wave. */
+ private static final String INSTRUCTIONS =
+ "buglinky will attempt to link \"issue #NNN\" to the Wave issue " +
+ "tracker.\n\n" +
+ "Note that the issue number must not be at the very end of " +
+ "a paragraph. This is temporary kludge to discourage buglinky from " +
+ "annotating your insertion point as you type.\n\n" +
+ "Once you've set your preferences, you can delete this blip.";
+
+ /** Get the URL prefix we'll use to link to bugs. */
+ static String getBugUrl(Wavelet wavelet) {
+ String bugUrl = wavelet.getDataDocument("buglinky-url");
+ if (bugUrl == null)
+ bugUrl = BUG_URL;
+ LOG.fine("Using issue URL " + bugUrl);
+ return bugUrl;
+ }
+
+ /** Add an admin view to the specified wavelet. */
+ static void createFor(Wavelet wavelet) {
+ LOG.fine("Adding instructions to wavelet " + wavelet.getWaveletId());
+ Blip blip = wavelet.appendBlip();
+ TextView textView = blip.getDocument();
+ textView.append(INSTRUCTIONS);
+
+ // Our form-handling code is heavily inspired by the original
+ // "Polly the Pollster" bot.
+ textView.append("\n\n");
+ textView.appendElement(new FormElement(ElementType.LABEL,
+ "bugUrl", "Enter your issue URL, minus the issue number:"));
+ textView.appendElement(new FormElement(ElementType.INPUT,
+ "bugUrl", getBugUrl(wavelet)));
+ textView.append("\n");
+ textView.appendElement(new FormElement(ElementType.BUTTON,
+ "saveButton", "Save Preferences"));
+ textView.setAnnotation("buglinky-admin", "");
+ }
+
+ /** Does the specified blip contain our AdminView? */
+ public static boolean isAdminBlip(Blip blip) {
+ return blip.getDocument().hasAnnotation("buglinky-admin");
+ }
+
+ /** The blip containing our admin view. */
+ private Blip blip;
+
+ /** Create an object which provides easy access to our AdminView. */
+ AdminView(Blip blip) {
+ this.blip = blip;
+ }
+
+ /** Called when a button is pressed in our AdminView. */
+ public void onFormButtonClicked(Event e) {
+ if (e.getButtonName().equals("saveButton")) {
+ LOG.fine("Buglinky save button clicked");
+ FormView form = blip.getDocument().getFormView();
+ String newUrl = form.getFormElement("bugUrl").getValue();
+ if (!newUrl.matches("^ *$")) {
+ LOG.fine("Setting issue URL to " + newUrl);
+ e.getWavelet().setDataDocument("buglinky-url", newUrl);
+ }
+ }
+ }
+}
@@ -16,24 +16,16 @@
package buglinky;
import java.util.ArrayList;
-import java.util.logging.Logger;
import com.google.wave.api.AbstractRobotServlet;
-import com.google.wave.api.Blip;
-import com.google.wave.api.ElementType;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
-import com.google.wave.api.FormElement;
-import com.google.wave.api.FormView;
import com.google.wave.api.RobotMessageBundle;
-import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;
/** Called via JSON-RPC whenever an event occurs on one of our waves. */
@SuppressWarnings("serial")
public class BugLinkyServlet extends AbstractRobotServlet {
- private static final Logger LOG =
- Logger.getLogger(BugLinkyServlet.class.getName());
/**
* The name of this application on Google App Engine. This is used
@@ -47,77 +39,28 @@
/** The wave address for this bot. Used to ignore our own edits. */
private static final String BOT_ADDRESS = APP_NAME + "@appspot.com";
-
- /** The instructions to display when we join a wave. */
- private static final String INSTRUCTIONS =
- "buglinky will attempt to link \"issue #NNN\" to the Wave issue " +
- "tracker.\n\n" +
- "Note that the issue number must not be at the very end of " +
- "a paragraph. This is temporary kludge to discourage buglinky from " +
- "annotating your insertion point as you type.\n\n" +
- "Once you've set your preferences, you can delete this blip.";
-
- /** The URL to a specific bug in our bug tracker, minus the number. */
- private static final String BUG_URL =
- "http://code.google.com/p/google-wave-resources/issues/detail?id=";
@Override
public void processEvents(RobotMessageBundle bundle) {
- if (bundle.wasSelfAdded())
- addInstructionsToWave(bundle);
- processButtonClicks(bundle);
- processBlips(bundle, getBugUrl(bundle));
- }
-
- /** Add an instruction blip to this wave if we were just added. */
- private void addInstructionsToWave(RobotMessageBundle bundle) {
Wavelet wavelet = bundle.getWavelet();
- LOG.fine("Adding instructions to wavelet " + wavelet.getWaveletId());
- Blip blip = wavelet.appendBlip();
- TextView textView = blip.getDocument();
- textView.append(INSTRUCTIONS);
-
- // Our form-handling code is heavily inspired by the original
- // "Polly the Pollster" bot.
- textView.append("\n\n");
- textView.appendElement(new FormElement(ElementType.LABEL,
- "bugUrlLabel",
- "Enter your issue URL, minus the issue number:"));
- textView.appendElement(new FormElement(ElementType.INPUT,
- "bugUrl", getBugUrl(bundle)));
- textView.append("\n");
- textView.appendElement(new FormElement(ElementType.BUTTON,
- "saveButton", "Save Preferences"));
- textView.setAnnotation("buglinky-admin", "");
+ if (bundle.wasSelfAdded())
+ AdminView.createFor(wavelet);
+ dispatchUiEvents(bundle);
+ processBlips(bundle, AdminView.getBugUrl(wavelet));
}
- private void processButtonClicks(RobotMessageBundle bundle) {
+ /** Dispatch UI events to the appropriate view. */
+ private void dispatchUiEvents(RobotMessageBundle bundle) {
for (Event e : bundle.getEvents()) {
if (e.getType() == EventType.FORM_BUTTON_CLICKED) {
- LOG.fine("Form button clicked");
- TextView doc = e.getBlip().getDocument();
- if (doc.hasAnnotation("buglinky-admin") &&
- e.getButtonName().equals("saveButton")) {
- LOG.fine("Buglinky save button clicked");
- FormView form = doc.getFormView();
- String newUrl = form.getFormElement("bugUrl").getValue();
- if (!newUrl.matches("^ *$")) {
- LOG.fine("Setting issue URL to " + newUrl);
- e.getWavelet().setDataDocument("buglinky-url", newUrl);
- }
+ if (AdminView.isAdminBlip(e.getBlip())) {
+ AdminView adminView = new AdminView(e.getBlip());
+ adminView.onFormButtonClicked(e);
}
}
}
}
- private String getBugUrl(RobotMessageBundle bundle) {
- String bugUrl = bundle.getWavelet().getDataDocument("buglinky-url");
- if (bugUrl == null)
- bugUrl = BUG_URL;
- LOG.fine("Using issue URL " + bugUrl);
- return bugUrl;
- }
-
/** Process any blips which have changed. */
private void processBlips(RobotMessageBundle bundle, String bugUrl) {
// We clean up URLs first, so that we can annotate the newly-created

0 comments on commit ea336d9

Please sign in to comment.