Applied patch for Issue 222: save favorite transforms.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2079 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-06-06 18:49:36 +00:00
parent f7c33fba45
commit b75a5efe71
9 changed files with 209 additions and 11 deletions

View File

@ -466,5 +466,6 @@ public abstract class ProjectManager {
*/
static protected void preparePreferenceStore(PreferenceStore ps) {
ps.put("scripting.expressions", new TopList(s_expressionHistoryMax));
ps.put("scripting.starred-expressions", new TopList(Integer.MAX_VALUE));
}
}

View File

@ -67,6 +67,7 @@ public class GetExpressionHistoryCommand extends Command {
localExpressions = localExpressions.size() > 20 ? localExpressions.subList(0, 20) : localExpressions;
List<String> globalExpressions = toExpressionList(ProjectManager.singleton.getPreferenceStore().get("scripting.expressions"));
Set<String> starredExpressions = new HashSet<String>(((TopList)ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions")).getList());
Set<String> done = new HashSet<String>();
@ -81,6 +82,7 @@ public class GetExpressionHistoryCommand extends Command {
writer.object();
writer.key("code"); writer.value(s);
writer.key("global"); writer.value(false);
writer.key("starred"); writer.value(starredExpressions.contains(s));
writer.endObject();
done.add(s);
}
@ -89,6 +91,7 @@ public class GetExpressionHistoryCommand extends Command {
writer.object();
writer.key("code"); writer.value(s);
writer.key("global"); writer.value(true);
writer.key("starred"); writer.value(starredExpressions.contains(s));
writer.endObject();
}
}

View File

@ -0,0 +1,44 @@
package com.google.refine.commands.expr;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.google.refine.ProjectManager;
import com.google.refine.commands.Command;
import com.google.refine.preference.TopList;
public class GetStarredExpressionsCommand extends Command {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List<String> starredExpressions = ((TopList)ProjectManager.singleton.getPreferenceStore().get("scripting.starred-expressions")).getList();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("expressions");
writer.array();
for (String s : starredExpressions) {
writer.object();
writer.key("code"); writer.value(s);
writer.endObject();
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -0,0 +1,51 @@
package com.google.refine.commands.expr;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
import com.google.refine.ProjectManager;
import com.google.refine.commands.Command;
import com.google.refine.preference.TopList;
public class ToggleStarredExpressionCommand extends Command {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String expression = request.getParameter("expression");
TopList starredExpressions = ((TopList) ProjectManager.singleton.getPreferenceStore().get(
"scripting.starred-expressions"));
if (starredExpressions.getList().contains(expression))
starredExpressions.remove(expression);
else
starredExpressions.add(expression);
if(request.getParameter("returnList") != null)
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("expressions");
writer.array();
for (String s : starredExpressions) {
writer.object();
writer.key("code");
writer.value(s);
writer.endObject();
}
writer.endArray();
writer.endObject();
} catch (Exception e) {
respondException(response, e);
}
}
}

View File

@ -34,6 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.preference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
@ -46,7 +47,7 @@ import org.json.JSONWriter;
import com.google.refine.Jsonizable;
public class TopList implements Jsonizable {
public class TopList implements Jsonizable, Iterable<String> {
private static final long serialVersionUID = 2666669643063493350L;
final protected int _top;
@ -68,6 +69,11 @@ public class TopList implements Jsonizable {
_list.remove(_list.size() - 1);
}
}
public void remove(String element)
{
_list.remove(element);
}
@Override
public void write(JSONWriter writer, Properties options) throws JSONException {
@ -102,4 +108,9 @@ public class TopList implements Jsonizable {
_list.add(a.getString(i));
}
}
@Override
public Iterator<String> iterator() {
return _list.iterator();
}
}

View File

@ -120,9 +120,12 @@ function registerCommands() {
RS.registerCommand(module, "get-expression-language-info", new Packages.com.google.refine.commands.expr.GetExpressionLanguageInfoCommand());
RS.registerCommand(module, "get-expression-history", new Packages.com.google.refine.commands.expr.GetExpressionHistoryCommand());
RS.registerCommand(module, "get-starred-expressions", new Packages.com.google.refine.commands.expr.GetStarredExpressionsCommand());
RS.registerCommand(module, "toggle-starred-expression", new Packages.com.google.refine.commands.expr.ToggleStarredExpressionCommand());
RS.registerCommand(module, "log-expression", new Packages.com.google.refine.commands.expr.LogExpressionCommand());
RS.registerCommand(module, "preview-expression", new Packages.com.google.refine.commands.expr.PreviewExpressionCommand());
RS.registerCommand(module, "get-preference", new Packages.com.google.refine.commands.GetPreferenceCommand());
RS.registerCommand(module, "get-all-preferences", new Packages.com.google.refine.commands.GetAllPreferencesCommand());
RS.registerCommand(module, "set-preference", new Packages.com.google.refine.commands.SetPreferenceCommand());

View File

@ -14,6 +14,7 @@
<ul>
<li><a href="#expression-preview-tabs-preview">Preview</a></li>
<li><a href="#expression-preview-tabs-history">History</a></li>
<li><a href="#expression-preview-tabs-starred">Starred</a></li>
<li><a href="#expression-preview-tabs-help">Help</a></li>
</ul>
<div id="expression-preview-tabs-preview">
@ -22,6 +23,9 @@
<div id="expression-preview-tabs-history" style="display: none;">
<div class="expression-preview-container" bind="expressionPreviewHistoryContainer"></div>
</div>
<div id="expression-preview-tabs-starred" style="display: none;">
<div class="expression-preview-container" bind="expressionPreviewStarredContainer"></div>
</div>
<div id="expression-preview-tabs-help" style="display: none;">
<div class="expression-preview-help-container" bind="expressionPreviewHelpTabBody"></div>
</div>

View File

@ -119,6 +119,7 @@ ExpressionPreviewDialog.Widget = function(
$("#expression-preview-tabs").tabs();
$("#expression-preview-tabs-history").css("display", "");
$("#expression-preview-tabs-starred").css("display", "");
$("#expression-preview-tabs-help").css("display", "");
this._elmts.expressionPreviewLanguageSelect[0].value = language;
@ -140,6 +141,7 @@ ExpressionPreviewDialog.Widget = function(
this.update();
this._renderExpressionHistoryTab();
this._renderStarredExpressionsTab();
this._renderHelpTab();
};
@ -266,15 +268,93 @@ ExpressionPreviewDialog.Widget.prototype._renderExpressionHistory = function(dat
var table = $(
'<table>' +
'<tr><th></th><th>From</th><th colspan="2">Expression</th><th></th></tr>' +
'<tr><th></th><th></th><th>From</th><th colspan="2">Expression</th><th></th></tr>' +
'</table>'
).appendTo($('<div>').addClass("expression-preview-table-wrapper").appendTo(elmt))[0];
var renderEntry = function(entry) {
var tr = table.insertRow(table.rows.length);
var renderEntry = function(self,tr,entry) {
$(tr).empty();
var o = Scripting.parse(entry.code);
$('<a href="javascript:{}">&nbsp;</a>')
.addClass(entry.starred ? "data-table-star-on" : "data-table-star-off")
.appendTo(tr.insertCell(0))
.click(function() {
$.post(
"/command/core/toggle-starred-expression",
{ expression: entry.code },
function(data) {
entry.starred = !entry.starred;
renderEntry(self,tr,entry);
self._renderStarredExpressionsTab();
},
"json"
);
});
$('<a href="javascript:{}">Reuse</a>').appendTo(tr.insertCell(0)).click(function() {
$('<a href="javascript:{}">Reuse</a>').appendTo(tr.insertCell(1)).click(function() {
self._elmts.expressionPreviewTextarea[0].value = o.expression;
self._elmts.expressionPreviewLanguageSelect[0].value = o.language;
$("#expression-preview-tabs").tabs('option', 'selected', 0);
self._elmts.expressionPreviewTextarea.select().focus();
self.update();
});
$(tr.insertCell(2)).html(entry.global ? "Other&nbsp;projects" : "This&nbsp;project");
$(tr.insertCell(3)).text(o.language + ":");
$(tr.insertCell(4)).text(o.expression);
};
for (var i = 0; i < data.expressions.length; i++) {
var tr = table.insertRow(table.rows.length);
var entry = data.expressions[i];
renderEntry(self,tr,entry);
}
};
ExpressionPreviewDialog.Widget.prototype._renderStarredExpressionsTab = function() {
var self = this;
$.getJSON(
"/command/core/get-starred-expressions",
null,
function(data) {
self._renderStarredExpressions(data);
},
"json"
);
};
ExpressionPreviewDialog.Widget.prototype._renderStarredExpressions = function(data) {
var self = this;
var elmt = this._elmts.expressionPreviewStarredContainer.empty().width(this._tabContentWidth);
var table = $(
'<table>' +
'<tr><th></th><th></th><th colspan="2">Expression</th><th></th></tr>' +
'</table>'
).appendTo($('<div>').addClass("expression-preview-table-wrapper").appendTo(elmt))[0];
var renderEntry = function(entry) {
var tr = table.insertRow(table.rows.length);
var o = Scripting.parse(entry.code);
$('<a href="javascript:{}">Remove</a>').appendTo(tr.insertCell(0)).click(function() {
$.post(
"/command/core/toggle-starred-expression",
{ expression: entry.code, returnList: true },
function(data) {
self._renderStarredExpressions(data);
self._renderExpressionHistoryTab();
},
"json"
);
});
$('<a href="javascript:{}">Reuse</a>').appendTo(tr.insertCell(1)).click(function() {
self._elmts.expressionPreviewTextarea[0].value = o.expression;
self._elmts.expressionPreviewLanguageSelect[0].value = o.language;
@ -285,7 +365,6 @@ ExpressionPreviewDialog.Widget.prototype._renderExpressionHistory = function(dat
self.update();
});
$(tr.insertCell(1)).html(entry.global ? "Other&nbsp;projects" : "This&nbsp;project");
$(tr.insertCell(2)).text(o.language + ":");
$(tr.insertCell(3)).text(o.expression);
};
@ -294,8 +373,7 @@ ExpressionPreviewDialog.Widget.prototype._renderExpressionHistory = function(dat
var entry = data.expressions[i];
renderEntry(entry);
}
};
}
ExpressionPreviewDialog.Widget.prototype._scheduleUpdate = function() {
if (this._timerID !== null) {
window.clearTimeout(this._timerID);

View File

@ -52,21 +52,24 @@ textarea.expression-preview-code {
#expression-preview-tabs-preview,
#expression-preview-tabs-help,
#expression-preview-tabs-history {
#expression-preview-tabs-history,
#expression-preview-tabs-starred {
padding: @padding_tight;
overflow: hidden;
}
#expression-preview-tabs-preview > div,
#expression-preview-tabs-help > div,
#expression-preview-tabs-history > div {
#expression-preview-tabs-history > div,
#expression-preview-tabs-starred {
height: 200px;
overflow: auto;
}
#expression-preview-tabs-preview td, #expression-preview-tabs-preview th,
#expression-preview-tabs-help td, #expression-preview-tabs-help th,
#expression-preview-tabs-history td, #expression-preview-tabs-history th {
#expression-preview-tabs-history td, #expression-preview-tabs-history th,
#expression-preview-tabs-starred td, #expression-preview-tabs-starred th {
padding: @padding_tight;
}