Skip to content

Commit

Permalink
Issue CruxFramework#2 - Make Showcase project compatible with Crux 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Cardoso committed Dec 22, 2015
1 parent 9fdb133 commit e17cc41
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 238 deletions.
3 changes: 2 additions & 1 deletion cross-device-showcase/src/main/resources/Crux.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#Visit http://www.cruxframework.org/ and open the lastest documentation at Crux.properties section.
#Visit http://www.cruxframework.org/ and open the lastest documentation at Crux.properties section.
restServiceResourceStateHandler=org.cruxframework.crux.core.server.rest.state.NoClusteredResourceStateHandler
11 changes: 8 additions & 3 deletions showcase-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cruxVersion>5.4.0</cruxVersion>
<smartfacesVersion>1.3.0</smartfacesVersion>
<widgetsVersion>5.4.0</widgetsVersion>
<cruxVersion>5.4.1-SNAPSHOT</cruxVersion>
<smartfacesVersion>1.3.1-SNAPSHOT</smartfacesVersion>
<widgetsVersion>5.4.1-SNAPSHOT</widgetsVersion>
<java.source.version>1.7</java.source.version>
<java.target.version>1.7</java.target.version>
</properties>
Expand Down Expand Up @@ -107,6 +107,11 @@
<version>${smartfacesVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.cruxframework.showcasecore.client.controller.showcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import org.cruxframework.crux.core.client.Crux;
import org.cruxframework.crux.core.client.controller.Controller;
import org.cruxframework.crux.core.client.controller.Expose;
import org.cruxframework.crux.core.client.ioc.Inject;
Expand All @@ -15,21 +17,30 @@
import org.cruxframework.crux.core.client.screen.views.ViewActivateHandler;
import org.cruxframework.crux.core.client.screen.views.WidgetAccessor;
import org.cruxframework.crux.core.client.utils.StringUtils;
import org.cruxframework.crux.smartfaces.client.dialog.MessageBox;
import org.cruxframework.crux.smartfaces.client.dialog.MessageBox.MessageType;
import org.cruxframework.crux.widgets.client.dialogcontainer.DialogViewContainer;
import org.cruxframework.crux.widgets.client.disposal.menutabsdisposal.MenuTabsDisposal;
import org.cruxframework.crux.widgets.client.disposal.panelchoicedisposal.PanelChoiceDisposal;
import org.cruxframework.crux.widgets.client.filter.Filter;
import org.cruxframework.crux.widgets.client.filter.Filterable;
import org.cruxframework.crux.widgets.client.swappanel.HorizontalSwapPanel.Direction;
import org.cruxframework.showcasecore.client.proxy.SourceCodeRestProxy;
import org.cruxframework.showcasecore.client.dto.GitResponse;
import org.cruxframework.showcasecore.client.proxy.SourceFilesProxy;
import org.cruxframework.showcasecore.client.resource.ShowcaseCoreMessages;
import org.cruxframework.showcasecore.client.resource.common.ShowcaseResourcesCommon;
import org.cruxframework.showcasecore.client.util.LoadingCallback;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;
Expand All @@ -40,18 +51,22 @@
@Controller("mainController")
public class MainController
{
public static final String RAWTYPE_ENDPOINT = "https://raw.githubusercontent.com/CruxFramework/crux-samples-showcase/master/";

private ShowcaseResourcesCommon bundle = GWT.create(ShowcaseResourcesCommon.class);

@Inject
private ShowcaseCoreMessages messages;

@Inject
public SourceCodeRestProxy service;

public SourceFilesProxy sourceFilesProxy;
private Logger logger = Logger.getLogger("");

private LanguageManager languageManager;

private HashMap<String,Integer> mapFilesToRead = new HashMap<String,Integer>();

@Expose
public void replaceText(AttachEvent event)
{
Expand Down Expand Up @@ -239,29 +254,168 @@ public void navigateToProject()
Window.open("https://github.com/CruxFramework", "_self", null);
}

private static class File
{
public File(String name, String path)
{
this.name = name;
this.path = path;
}

private String name;
private String path;

public String getName()
{
return name;
}
public String getPath()
{
return path;
}
}

@Expose
public void viewSourceCode()
{
String viewId = widgets.menuDisposal().getCurrentView();
final String viewId = widgets.menuDisposal().getCurrentView().toLowerCase();

mapFilesToRead.put(viewId, 3);

service.listSourceFilesForView(viewId, new LoadingCallback<ArrayList<String>>()
final ArrayList<File> sourceFiles = new ArrayList<File>();

Scheduler.get().scheduleFixedDelay(new RepeatingCommand()
{
@Override
public boolean execute()
{
if(mapFilesToRead.get(viewId) == 0)
{
mapFilesToRead.put(viewId, 3);

if(sourceFiles.size() > 0)
{
showSourcesDialog(sourceFiles);
}

return false;
}
return true;
}
}, 500);

sourceFilesProxy.listViewFiles(viewId, new LoadingCallback<ArrayList<GitResponse>>()
{
@Override
public void onComplete(ArrayList<GitResponse> viewFiles)
{
decreaseFilesToRead(viewId);
if(viewFiles != null)
{
for (GitResponse gitResponse : viewFiles)
{
sourceFiles.add(new File(gitResponse.getName(), gitResponse.getPath()));
}
}
}

@Override
public void onError(Exception e)
{
try
{
decreaseFilesToRead(viewId);
}
finally
{
if(e.getMessage() != null
&&
!StringUtils.isEmpty(e.getMessage())
&& (e.getMessage().contains("Not Found"))
)
{
super.closeWaitPopup();
}
else
{
MessageBox.show(messages.maxCalls(), MessageType.ERROR);
super.closeWaitPopup();
}
}
}
});

sourceFilesProxy.listControllerFiles(viewId, new LoadingCallback<ArrayList<GitResponse>>()
{
@Override
public void onComplete(ArrayList<GitResponse> controllerFiles)
{
decreaseFilesToRead(viewId);
if(controllerFiles != null)
{
for (GitResponse gitResponse : controllerFiles)
{
sourceFiles.add(new File(gitResponse.getName(), gitResponse.getPath()));
}
}
}

@Override
public void onError(Exception e)
{
try
{
decreaseFilesToRead(viewId);
}
finally
{
super.closeWaitPopup();
}
}
});

sourceFilesProxy.listServerFiles(viewId, new LoadingCallback<ArrayList<GitResponse>>()
{
@Override
public void onComplete(ArrayList<String> result)
public void onComplete(ArrayList<GitResponse> serverFiles)
{
if(result.size() > 0)
decreaseFilesToRead(viewId);
if(serverFiles != null)
{
showSourcesDialog(result);
for (GitResponse gitResponse : serverFiles)
{
sourceFiles.add(new File(gitResponse.getName(), gitResponse.getPath()));
}
}
}

@Override
public void onError(Exception e)
{
try
{
decreaseFilesToRead(viewId);
}
finally
{
super.closeWaitPopup();
}
}
});
}

private void decreaseFilesToRead(final String viewId)
{
Integer filesToRead = mapFilesToRead.get(viewId);
filesToRead--;
mapFilesToRead.put(viewId, filesToRead);
}

/**
* Shows a dialog box with the source files contents
* @param files
*/
private void showSourcesDialog(final List<String> files)
private void showSourcesDialog(final List<File> files)
{
DialogViewContainer dialog = DialogViewContainer.createDialog("sourcesPopup");
dialog.openDialog();
Expand All @@ -271,9 +425,8 @@ private void showSourcesDialog(final List<String> files)

for (int i = files.size() - 1; i >= 0; i--)
{
final String path = files.get(i);
final String fileName = getFileName(path);
addSourceChoice(sourceChoice, path, fileName);
final File file = files.get(i);
addSourceChoice(sourceChoice, file.getPath(), file.getName());
}

if(files.size() > 0)
Expand All @@ -283,7 +436,7 @@ private void showSourcesDialog(final List<String> files)
@Override
public void execute()
{
String fileName = getFileName(files.get(0));
String fileName = files.get(0).getName();
sourceChoice.choose(fileName, fileName);
}
});
Expand All @@ -308,45 +461,61 @@ public void onActivate(ViewActivateEvent event)
{
loaded = true;

service.getSourceFile(path, new LoadingCallback<String>()
RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, RAWTYPE_ENDPOINT + path);

requestBuilder.setCallback(new RequestCallback()
{
@Override
public void onComplete(String source)
public void onResponseReceived(Request request, Response response)
{
if(response.getStatusCode() == 200)
{
String source = response.getText();
View view = View.getView(fileName);
Widget sourceEditor = view.getWidget("sourceEditor");
Element editor = sourceEditor.getElement();
String brush = "class=\"language-" + (fileName.endsWith("java") ? "java": "markup") + "\"";
source = new SafeHtmlBuilder().appendEscaped(source).toSafeHtml().asString();
editor.setInnerHTML("<pre class=\"line-numbers\"><code " + brush + ">" + source + "</code></pre>");
syntaxHighlight();
}
else
{
Crux.getErrorHandler().handleError("Error to get source file from GitHub");
}
}

@Override
public void onError(Request request, Throwable exception)
{
View view = View.getView(fileName);
Widget sourceEditor = view.getWidget("sourceEditor");
Element editor = sourceEditor.getElement();
String brush = "class=\"language-" + (fileName.endsWith("java") ? "java": "markup") + "\"";
source = new SafeHtmlBuilder().appendEscaped(source).toSafeHtml().asString();
editor.setInnerHTML("<pre class=\"line-numbers\"><code " + brush + ">" + source + "</code></pre>");
syntaxHighlight();
Crux.getErrorHandler().handleError(exception);
}
});

try
{
requestBuilder.send();
}
catch (RequestException e)
{
Crux.getErrorHandler().handleError(e);
}
}
}
});
}

/**
* @param path
* @return
*/
private String getFileName(final String path)
{
return path.indexOf("/") >= 0 ? path.substring(path.lastIndexOf("/") + 1) : path;
}

public native void syntaxHighlight()/*-{
$wnd.Prism.highlightAll();
}-*/;

public void setService(SourceCodeRestProxy service)
{
this.service = service;
}

public void setMessages(ShowcaseCoreMessages messages)
{
this.messages = messages;
}

public void setSourceFilesProxy(SourceFilesProxy sourceFilesProxy)
{
this.sourceFilesProxy = sourceFilesProxy;
}
}
Loading

0 comments on commit e17cc41

Please sign in to comment.