Skip to content

Commit

Permalink
Merge branch 'master' into fixes-3623-terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
cnsgithub committed May 2, 2018
2 parents e805ba8 + a3f90e4 commit 52dd59c
Show file tree
Hide file tree
Showing 17 changed files with 633 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import org.primefaces.model.terminal.TerminalAutoCompleteModel;
import org.primefaces.model.terminal.TerminalCommand;
import org.primefaces.model.terminal.TerminalAutoCompleteMatches;
import java.util.Arrays;
import java.util.ArrayDeque;

public static final String CONTAINER_CLASS = "ui-terminal ui-widget ui-widget-content ui-corner-all";
public static final String WELCOME_MESSAGE_CLASS = "ui-terminal-welcome";
Expand All @@ -8,4 +13,64 @@
public boolean isCommandRequest() {
FacesContext context = getFacesContext();
return context.getExternalContext().getRequestParameterMap().containsKey(this.getClientId(context) + "_command");
}
}

public boolean isAutoCompleteRequest() {
FacesContext context = getFacesContext();
return context.getExternalContext().getRequestParameterMap().containsKey(this.getClientId(context) + "_autocomplete");
}

TerminalAutoCompleteMatches traverseAutoCompleteModel(TerminalAutoCompleteModel commandModel, String input, String[] args) {
ArrayDeque argumentQueue = new ArrayDeque(Arrays.asList(args));
return traverseAutoCompleteModel(commandModel, input, argumentQueue);
}

private TerminalAutoCompleteMatches traverseAutoCompleteModel(TerminalAutoCompleteModel commandModel, String input, ArrayDeque<String> inputArguments) {
TerminalAutoCompleteMatches matches = new TerminalAutoCompleteMatches();

for (TerminalCommand command : commandModel.getCommands()) {
if (isPartialMatch(command, input)) {
if (isExactMatch(command, input) && command.hasArguments()) {
matches.extendBaseCommand(input);
return traverseArguments(command, matches, inputArguments);
}

matches.addMatch(command);
}
}

return matches;
}

private TerminalAutoCompleteMatches traverseArguments(TerminalCommand command, TerminalAutoCompleteMatches matches, ArrayDeque<String> inputArguments) {
if (command.getArguments() != null) {
for (TerminalCommand argument : command.getArguments()) {
if (!inputArguments.isEmpty()) {
String inputArgument = inputArguments.peek();

if (isPartialMatch(argument, inputArgument)) {
if (isExactMatch(argument, inputArgument) && argument.hasArguments()) {
matches.extendBaseCommand(argument);
inputArguments.removeFirst();
return traverseArguments(argument, matches, inputArguments);
}

matches.addMatch(argument);
}
}
else {
matches.addMatch(argument);
}
}
}

return matches;
}

private boolean isPartialMatch(TerminalCommand command, String input) {
return command.getText().startsWith(input);
}

private boolean isExactMatch(TerminalCommand command, String input) {
return command.getText().equalsIgnoreCase(input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected void encodeMarkup(FacesContext context, ProgressBar progressBar) throw
writer.writeAttribute("class", ProgressBar.LABEL_CLASS, null);
if (labelTemplate != null) {
writer.writeAttribute("style", "display:block", style);
writer.write(labelTemplate.replaceAll("\\{value\\}", String.valueOf(value)));
writer.writeText(labelTemplate.replaceAll("\\{value\\}", String.valueOf(value)), null);
}
writer.endElement("div");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ protected void encodeColumnsHeader(FacesContext context, SelectOneMenu menu, Lis
headerFacet.encodeAll(context);
}
else if (headerText != null) {
writer.write(headerText);
writer.writeText(headerText, null);
}

writer.endElement("th");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void encodeMarkup(FacesContext context, AbstractMenu abstractMenu) thr
writer.startElement("span", menu);
writer.writeAttribute("class", SlideMenu.BACKWARD_ICON_CLASS, null);
writer.endElement("span");
writer.write(menu.getBackLabel());
writer.writeText(menu.getBackLabel(), "backLabel");
writer.endElement("div");

//wrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

public class TerminalHandler extends ComponentHandler {

private static final MethodRule COMMAND_HANDLER
= new MethodRule("commandHandler", String.class, new Class[]{String.class, String[].class});
private static final MethodRule COMMAND_HANDLER =
new MethodRule("commandHandler", String.class, new Class[]{ String.class, String[].class });

public TerminalHandler(ComponentConfig config) {
super(config);
Expand All @@ -37,4 +37,5 @@ protected MetaRuleset createMetaRuleset(Class type) {

return metaRuleset;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

import java.io.IOException;
import java.util.Arrays;

import javax.el.MethodExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.primefaces.model.terminal.TerminalAutoCompleteModel;
import org.primefaces.model.terminal.TerminalAutoCompleteMatches;
import org.primefaces.renderkit.CoreRenderer;
import org.primefaces.util.WidgetBuilder;

Expand All @@ -33,14 +37,16 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
if (terminal.isCommandRequest()) {
handleCommand(context, terminal);
}
else if (terminal.isAutoCompleteRequest()) {
autoCompleteCommand(context, terminal);
}
else {
encodeMarkup(context, terminal);
encodeScript(context, terminal);
}
}

protected void encodeMarkup(FacesContext context, Terminal terminal) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = terminal.getClientId(context);
String style = terminal.getStyle();
String styleClass = terminal.getStyleClass();
Expand All @@ -49,6 +55,8 @@ protected void encodeMarkup(FacesContext context, Terminal terminal) throws IOEx
String prompt = terminal.getPrompt();
String inputId = clientId + "_input";

ResponseWriter writer = context.getResponseWriter();

writer.startElement("div", terminal);
writer.writeAttribute("id", clientId, "id");
writer.writeAttribute("class", styleClass, "styleClass");
Expand All @@ -66,11 +74,11 @@ protected void encodeMarkup(FacesContext context, Terminal terminal) throws IOEx
}
writer.endElement("div");
}

writer.startElement("div", null);
writer.writeAttribute("class", Terminal.CONTENT_CLASS, null);
writer.endElement("div");

writer.startElement("div", null);
writer.startElement("span", null);
writer.writeAttribute("class", Terminal.PROMPT_CLASS, null);
Expand All @@ -81,15 +89,15 @@ protected void encodeMarkup(FacesContext context, Terminal terminal) throws IOEx
writer.write(prompt);
}
writer.endElement("span");

writer.startElement("input", null);
writer.writeAttribute("id", inputId, null);
writer.writeAttribute("name", inputId, null);
writer.writeAttribute("type", "text", null);
writer.writeAttribute("autocomplete", "off", null);
writer.writeAttribute("class", Terminal.INPUT_CLASS, null);
writer.endElement("input");

writer.endElement("div");
writer.endElement("div");
}
Expand All @@ -102,17 +110,40 @@ protected void encodeScript(FacesContext context, Terminal terminal) throws IOEx
}

protected void handleCommand(FacesContext context, Terminal terminal) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = terminal.getClientId(context);
String value = context.getExternalContext().getRequestParameterMap().get(clientId + "_input");
String tokens[] = value.split(" ");
String tokens[] = getValueTokens(context, terminal);
String command = tokens[0];
String[] args = Arrays.copyOfRange(tokens, 1, tokens.length);

MethodExpression commandHandler = terminal.getCommandHandler();
String result = (String) commandHandler.invoke(context.getELContext(), new Object[]{command, args});

ResponseWriter writer = context.getResponseWriter();
writer.writeText(result, null);
}

protected void autoCompleteCommand(FacesContext context, Terminal terminal) throws IOException {
String tokens[] = getValueTokens(context, terminal);
String command = tokens[0];
String[] args = Arrays.copyOfRange(tokens, 1, tokens.length);

TerminalAutoCompleteModel autoCompleteModel = terminal.getAutoCompleteModel();
ResponseWriter writer = context.getResponseWriter();
if (autoCompleteModel == null) {
writer.write("null");
}
else {
TerminalAutoCompleteMatches matches = terminal.traverseAutoCompleteModel(autoCompleteModel, command, args);
writer.write(matches.toString());
}
}

private String[] getValueTokens(FacesContext context, Terminal terminal) {
String clientId = terminal.getClientId(context);
String value = context.getExternalContext().getRequestParameterMap().get(clientId + "_input");
String tokens[] = value.trim().split(" ");

return tokens;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ protected void encodeTbody(FacesContext context, TreeTable tt, boolean dataOnly)
emptyFacet.encodeAll(context);
}
else {
writer.write(tt.getEmptyMessage());
writer.writeText(tt.getEmptyMessage(), "emptyMessage");
}

writer.endElement("td");
Expand Down Expand Up @@ -687,7 +687,7 @@ protected void encodeColumnHeader(FacesContext context, TreeTable tt, UIColumn c
header.encodeAll(context);
}
else if (headerText != null) {
writer.write(headerText);
writer.writeText(headerText, null);
}

writer.endElement("span");
Expand Down Expand Up @@ -874,7 +874,7 @@ protected void encodeColumnFooter(FacesContext context, TreeTable table, UIColum
footerFacet.encodeAll(context);
}
else if (footerText != null) {
writer.write(footerText);
writer.writeText(footerText, null);
}

writer.endElement("td");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;
import org.primefaces.util.BeanUtils;
import org.primefaces.util.CollectionUtils;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.ResourceUtils;

Expand Down Expand Up @@ -290,28 +289,23 @@ protected void startMetadataIfNecessary() throws IOException {
ArrayList<ResourceUtils.ResourceInfo> initialResources = DynamicResourcesPhaseListener.getInitialResources(context);
ArrayList<ResourceUtils.ResourceInfo> currentResources = ResourceUtils.getComponentResources(context);
if (initialResources != null && currentResources != null && currentResources.size() > initialResources.size()) {
startEval();


ArrayList<ResourceUtils.ResourceInfo> newResources = new ArrayList<>(currentResources);
newResources.removeAll(initialResources);

getWrapped().write("if(window.PrimeFaces){");

ArrayList<String> stylesheets = ResourceUtils.filterStylesheets(context, newResources);
if (stylesheets != null && !stylesheets.isEmpty()) {
String script = "PrimeFaces.ajax.Utils.loadStylesheets(['" + CollectionUtils.join(stylesheets, "','") + "']);";
getWrapped().write(script);

boolean updateStarted = false;
for (int i = 0; i < newResources.size(); i++) {
ResourceUtils.ResourceInfo resourceInfo = newResources.get(i);
if (!updateStarted) {
((PartialResponseWriter) getWrapped()).startUpdate("javax.faces.Resource");
updateStarted = true;
}
resourceInfo.getResource().encodeAll(context);
}

ArrayList<String> scripts = ResourceUtils.filterScripts(context, newResources);
if (scripts != null && !scripts.isEmpty()) {
String script = "PrimeFaces.ajax.Utils.loadScripts(['" + CollectionUtils.join(scripts, "','") + "']);";
getWrapped().write(script);
if (updateStarted) {
((PartialResponseWriter) getWrapped()).endUpdate();
}

getWrapped().write("}");

endEval();
}
}
}
Expand Down
Loading

0 comments on commit 52dd59c

Please sign in to comment.