From 762a9f13ebd9096715ded0cb94415ceb45a621cf Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 2 Aug 2010 01:49:10 +0000 Subject: [PATCH] Text facet's choice count limit is now configurable through preference page. Preference page needs polishing. git-svn-id: http://google-refine.googlecode.com/svn/trunk@1127 7d457c2a-affb-35e4-300a-418c747d4874 --- .../metaweb/gridworks/GridworksServlet.java | 1 + .../gridworks/browsing/facets/ListFacet.java | 20 +++- .../commands/GetAllPreferencesCommand.java | 48 ++++++++ .../commands/SetPreferenceCommand.java | 3 +- .../gridworks/preference/PreferenceStore.java | 14 ++- .../webapp/modules/core/MOD-INF/controller.js | 29 ++++- main/webapp/modules/core/preferences.vt | 24 ++++ .../modules/core/scripts/preferences.js | 109 ++++++++++++++++++ .../modules/core/styles/preferences.css | 22 ++++ 9 files changed, 263 insertions(+), 7 deletions(-) create mode 100644 main/src/com/metaweb/gridworks/commands/GetAllPreferencesCommand.java create mode 100644 main/webapp/modules/core/preferences.vt create mode 100644 main/webapp/modules/core/scripts/preferences.js create mode 100644 main/webapp/modules/core/styles/preferences.css diff --git a/main/src/com/metaweb/gridworks/GridworksServlet.java b/main/src/com/metaweb/gridworks/GridworksServlet.java index 11e84ba5b..4fb202bdc 100644 --- a/main/src/com/metaweb/gridworks/GridworksServlet.java +++ b/main/src/com/metaweb/gridworks/GridworksServlet.java @@ -116,6 +116,7 @@ public class GridworksServlet extends Butterfly { {"mqlwrite", "com.metaweb.gridworks.commands.freebase.MQLWriteCommand"}, {"get-preference", "com.metaweb.gridworks.commands.GetPreferenceCommand"}, + {"get-all-preferences", "com.metaweb.gridworks.commands.GetAllPreferencesCommand"}, {"set-preference", "com.metaweb.gridworks.commands.SetPreferenceCommand"}, }; diff --git a/main/src/com/metaweb/gridworks/browsing/facets/ListFacet.java b/main/src/com/metaweb/gridworks/browsing/facets/ListFacet.java index 03abdb9dd..9d9d5f7e4 100644 --- a/main/src/com/metaweb/gridworks/browsing/facets/ListFacet.java +++ b/main/src/com/metaweb/gridworks/browsing/facets/ListFacet.java @@ -9,6 +9,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.json.JSONWriter; +import com.metaweb.gridworks.ProjectManager; import com.metaweb.gridworks.browsing.DecoratedValue; import com.metaweb.gridworks.browsing.FilteredRecords; import com.metaweb.gridworks.browsing.FilteredRows; @@ -71,7 +72,7 @@ public class ListFacet implements Facet { if (_errorMessage != null) { writer.key("error"); writer.value(_errorMessage); - } else if (_choices.size() > 2000) { + } else if (_choices.size() > getLimit()) { writer.key("error"); writer.value("Too many choices"); } else { writer.key("choices"); writer.array(); @@ -98,6 +99,23 @@ public class ListFacet implements Facet { writer.endObject(); } + + protected int getLimit() { + Object v = ProjectManager.singleton.getPreferenceStore().get("ui.browsing.listFacet.limit"); + if (v != null) { + if (v instanceof Number) { + return ((Number) v).intValue(); + } else { + try { + int n = Integer.parseInt(v.toString()); + return n; + } catch (NumberFormatException e) { + // ignore + } + } + } + return 2000; + } @Override public void initializeFromJSON(Project project, JSONObject o) throws Exception { diff --git a/main/src/com/metaweb/gridworks/commands/GetAllPreferencesCommand.java b/main/src/com/metaweb/gridworks/commands/GetAllPreferencesCommand.java new file mode 100644 index 000000000..4d52e92ba --- /dev/null +++ b/main/src/com/metaweb/gridworks/commands/GetAllPreferencesCommand.java @@ -0,0 +1,48 @@ +package com.metaweb.gridworks.commands; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.JSONException; +import org.json.JSONWriter; + +import com.metaweb.gridworks.ProjectManager; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.preference.PreferenceStore; + +public class GetAllPreferencesCommand extends Command { + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + Project project = request.getParameter("project") != null ? getProject(request) : null; + PreferenceStore ps = project != null ? + project.getMetadata().getPreferenceStore() : + ProjectManager.singleton.getPreferenceStore(); + + try { + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + + JSONWriter writer = new JSONWriter(response.getWriter()); + + writer.object(); + + for (String key : ps.getKeys()) { + Object pref = ps.get(key); + if (pref == null || pref instanceof String || pref instanceof Number || pref instanceof Boolean) { + writer.key(key); + writer.value(pref); + } + } + + writer.endObject(); + } catch (JSONException e) { + respondException(response, e); + } + } + +} diff --git a/main/src/com/metaweb/gridworks/commands/SetPreferenceCommand.java b/main/src/com/metaweb/gridworks/commands/SetPreferenceCommand.java index 313a89686..288e2a425 100644 --- a/main/src/com/metaweb/gridworks/commands/SetPreferenceCommand.java +++ b/main/src/com/metaweb/gridworks/commands/SetPreferenceCommand.java @@ -27,8 +27,7 @@ public class SetPreferenceCommand extends Command { String valueString = request.getParameter("value"); try { - JSONTokener t = new JSONTokener(valueString); - Object o = t.nextValue(); + Object o = valueString == null ? null : new JSONTokener(valueString).nextValue(); ps.put(prefName, PreferenceStore.loadObject(o)); diff --git a/main/src/com/metaweb/gridworks/preference/PreferenceStore.java b/main/src/com/metaweb/gridworks/preference/PreferenceStore.java index 043a0354d..3d8dc15d3 100644 --- a/main/src/com/metaweb/gridworks/preference/PreferenceStore.java +++ b/main/src/com/metaweb/gridworks/preference/PreferenceStore.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; +import java.util.Set; import org.json.JSONException; import org.json.JSONObject; @@ -16,13 +17,21 @@ public class PreferenceStore implements Jsonizable { protected Map _prefs = new HashMap(); public void put(String key, Object value) { - _prefs.put(key, value); + if (value == null) { + _prefs.remove(key); + } else { + _prefs.put(key, value); + } } public Object get(String key) { return _prefs.get(key); } + public Set getKeys() { + return _prefs.keySet(); + } + @Override public void write(JSONWriter writer, Properties options) throws JSONException { writer.object(); @@ -58,13 +67,12 @@ public class PreferenceStore implements Jsonizable { } } - @SuppressWarnings("unchecked") static public Object loadObject(Object o) { if (o instanceof JSONObject) { try { JSONObject obj2 = (JSONObject) o; String className = obj2.getString("class"); - Class klass = Class.forName(className); + Class klass = Class.forName(className); Method method = klass.getMethod("load", JSONObject.class); return method.invoke(null, obj2); diff --git a/main/webapp/modules/core/MOD-INF/controller.js b/main/webapp/modules/core/MOD-INF/controller.js index f6b06092f..6c65bbe8c 100644 --- a/main/webapp/modules/core/MOD-INF/controller.js +++ b/main/webapp/modules/core/MOD-INF/controller.js @@ -6,7 +6,8 @@ var bundle = true; var templatedFiles = { // Requests with last path segments mentioned here // will get served from .vt files with the same names - "project" : true + "project" : true, + "preferences" : true }; /* @@ -113,6 +114,32 @@ function init() { "styles/protograph/schema-alignment-dialog.css" ] ); + + ClientSideResourceManager.addPaths( + "preferences/scripts", + module, + [ + "externals/jquery-1.4.2.min.js", + "externals/jquery.cookie.js", + "externals/suggest/suggest-1.2.min.js", + "externals/jquery-ui/jquery-ui-1.8.custom.min.js", + "externals/imgareaselect/jquery.imgareaselect.js", + "externals/date.js", + "scripts/preferences.js" + ] + ); + ClientSideResourceManager.addPaths( + "preferences/styles", + module, + [ + "externals/suggest/css/suggest-1.2.min.css", + "externals/jquery-ui/css/ui-lightness/jquery-ui-1.8.custom.css", + + "styles/common.css", + "styles/jquery-ui-overrides.css", + "styles/preferences.css" + ] + ); } /* diff --git a/main/webapp/modules/core/preferences.vt b/main/webapp/modules/core/preferences.vt new file mode 100644 index 000000000..b03a7a43a --- /dev/null +++ b/main/webapp/modules/core/preferences.vt @@ -0,0 +1,24 @@ + + + + + Freebase Gridworks - Preferences + + + $scriptInjection + + + $styleInjection + + + + + +
+
starting up ...
+
+ + diff --git a/main/webapp/modules/core/scripts/preferences.js b/main/webapp/modules/core/scripts/preferences.js new file mode 100644 index 000000000..5dbd5c0c9 --- /dev/null +++ b/main/webapp/modules/core/scripts/preferences.js @@ -0,0 +1,109 @@ +var preferenceUIs = []; + +function onLoad() { + $.post( + "/command/get-all-preferences", + null, + populatePreferences, + "json" + ); +} + +$(onLoad); + +function populatePreferences(prefs) { + var body = $("#body").empty(); + + $('

').text("Preferences").appendTo(body); + + var table = $('').addClass("preferences").html('').appendTo(body)[0]; + for (var k in prefs) { + var tr = table.insertRow(table.rows.length); + preferenceUIs.push(new PreferenceUI(tr, k, prefs[k])); + } + + var trLast = table.insertRow(table.rows.length); + var tdLast0 = trLast.insertCell(0); + $('
KeyValue