Bug 799094 - Refactor handle-picking code into a reusable method. r=margaret
authorKartikaya Gupta <kgupta@mozilla.com>
Thu, 18 Oct 2012 13:41:06 -0400
changeset 110815 842804bae59b4802d356c90d4ef507f3cca26009
parent 110814 3b1ee99a8e618753d44f2b0677f10f2fa7a0ebc5
child 110816 bc0f08257eda7eaa4ed0adcb2cf77fc3f3eab7b9
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersmargaret
bugs799094
milestone19.0a1
Bug 799094 - Refactor handle-picking code into a reusable method. r=margaret
mobile/android/base/TextSelection.java
--- a/mobile/android/base/TextSelection.java
+++ b/mobile/android/base/TextSelection.java
@@ -49,32 +49,36 @@ class TextSelection extends Layer implem
     }
 
     void destroy() {
         unregisterEventListener("TextSelection:ShowHandles");
         unregisterEventListener("TextSelection:HideHandles");
         unregisterEventListener("TextSelection:PositionHandles");
     }
 
+    private TextSelectionHandle getHandle(String name) {
+        if (name.equals("START")) {
+            return mStartHandle;
+        } else if (name.equals("MIDDLE")) {
+            return mMiddleHandle;
+        } else {
+            return mEndHandle;
+        }
+    }
+
     public void handleMessage(String event, JSONObject message) {
         try {
             if (event.equals("TextSelection:ShowHandles")) {
                 final JSONArray handles = message.getJSONArray("handles");
                 GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
                     public void run() {
                         try {
                             for (int i=0; i < handles.length(); i++) {
                                 String handle = handles.getString(i);
-
-                                if (handle.equals("START"))
-                                    mStartHandle.setVisibility(View.VISIBLE);
-                                else if (handle.equals("MIDDLE"))
-                                    mMiddleHandle.setVisibility(View.VISIBLE);
-                                else
-                                    mEndHandle.setVisibility(View.VISIBLE);
+                                getHandle(handle).setVisibility(View.VISIBLE);
                             }
 
                             mViewLeft = 0.0f;
                             mViewTop = 0.0f;
                             mViewZoom = 0.0f;
                             LayerView layerView = GeckoApp.mAppContext.getLayerView();
                             if (layerView != null) {
                                 layerView.addLayer(TextSelection.this);
@@ -89,44 +93,34 @@ class TextSelection extends Layer implem
                         try {
                             LayerView layerView = GeckoApp.mAppContext.getLayerView();
                             if (layerView != null) {
                                 layerView.removeLayer(TextSelection.this);
                             }
 
                             for (int i=0; i < handles.length(); i++) {
                                 String handle = handles.getString(i);
-                                if (handle.equals("START"))
-                                    mStartHandle.setVisibility(View.GONE);
-                                else if (handle.equals("MIDDLE"))
-                                    mMiddleHandle.setVisibility(View.GONE);
-                                else
-                                    mEndHandle.setVisibility(View.GONE);
+                                getHandle(handle).setVisibility(View.GONE);
                             }
 
                         } catch(Exception e) {}
                     }
                 });
             } else if (event.equals("TextSelection:PositionHandles")) {
                 final JSONArray positions = message.getJSONArray("positions");
                 GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
                     public void run() {
                         try {
                             for (int i=0; i < positions.length(); i++) {
                                 JSONObject position = positions.getJSONObject(i);
                                 String handle = position.getString("handle");
                                 int left = position.getInt("left");
                                 int top = position.getInt("top");
 
-                                if (handle.equals("START"))
-                                    mStartHandle.positionFromGecko(left, top);
-                                else if (handle.equals("MIDDLE"))
-                                    mMiddleHandle.positionFromGecko(left, top);
-                                else
-                                    mEndHandle.positionFromGecko(left, top);
+                                getHandle(handle).positionFromGecko(left, top);
                              }
                         } catch (Exception e) { }
                     }
                 });
             }
         } catch (Exception e) {
             Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
         }