use of com.github.bordertech.wcomponents.WContent in project wcomponents by BorderTech.
the class WContentExample method addContentRow.
/**
* Adds components to the given container which demonstrate various ways of acessing the given content.
*
* @param contentDesc the description of the content, used to label the controls.
* @param contentAccess the content which will be displayed.
* @param target the container to add the UI controls to.
*/
private void addContentRow(final String contentDesc, final ContentAccess contentAccess, final MutableContainer target) {
// Demonstrate WButton + WContent, round trip
WButton button = new WButton(contentDesc);
final WContent buttonContent = new WContent();
button.setAction(new Action() {
@Override
public void execute(final ActionEvent event) {
buttonContent.setContentAccess(contentAccess);
buttonContent.display();
}
});
WContainer buttonCell = new WContainer();
buttonCell.add(buttonContent);
buttonCell.add(button);
target.add(buttonCell);
// Demonstrate WButton + WContent, using AJAX
WButton ajaxButton = new WButton(contentDesc);
final WContent ajaxContent = new WContent();
ajaxButton.setAction(new Action() {
@Override
public void execute(final ActionEvent event) {
ajaxContent.setContentAccess(contentAccess);
ajaxContent.display();
}
});
WContainer ajaxCell = new WContainer();
// The WContent must be wrapped in an AJAX targetable container
WPanel ajaxContentPanel = new WPanel();
ajaxContentPanel.add(ajaxContent);
ajaxCell.add(ajaxButton);
ajaxCell.add(ajaxContentPanel);
ajaxButton.setAjaxTarget(ajaxContentPanel);
target.add(ajaxCell);
// Demonstrate WContentLink - new window
WContentLink contentLinkNewWindow = new WContentLink(contentDesc) {
@Override
protected void preparePaintComponent(final Request request) {
super.preparePaintComponent(request);
setContentAccess(contentAccess);
}
};
target.add(contentLinkNewWindow);
// Demonstrate WContentLink - prompt to save
WContentLink contentLinkPromptToSave = new WContentLink(contentDesc) {
@Override
protected void preparePaintComponent(final Request request) {
super.preparePaintComponent(request);
setContentAccess(contentAccess);
}
};
contentLinkPromptToSave.setDisplayMode(DisplayMode.PROMPT_TO_SAVE);
target.add(contentLinkPromptToSave);
// Demonstrate WContentLink - inline
WContentLink contentLinkInline = new WContentLink(contentDesc) {
@Override
protected void preparePaintComponent(final Request request) {
super.preparePaintComponent(request);
setContentAccess(contentAccess);
}
};
contentLinkInline.setDisplayMode(DisplayMode.DISPLAY_INLINE);
target.add(contentLinkInline);
// Demonstrate targeting of content via a URL
WMenu menu = new WMenu(WMenu.MenuType.FLYOUT);
final WContent menuContent = new WContent();
menuContent.setDisplayMode(DisplayMode.PROMPT_TO_SAVE);
WMenuItem menuItem = new WMenuItem(contentDesc) {
@Override
protected void preparePaintComponent(final Request request) {
super.preparePaintComponent(request);
menuContent.setContentAccess(contentAccess);
setUrl(menuContent.getUrl());
}
};
menu.add(menuItem);
WContainer menuCell = new WContainer();
menuCell.add(menuContent);
menuCell.add(menu);
target.add(menuCell);
}
use of com.github.bordertech.wcomponents.WContent in project wcomponents by BorderTech.
the class WWindowInterceptor_Test method testServiceRequest.
@Test
public void testServiceRequest() {
final int initialServletStep = 123;
final int initialWindowStep = 111;
// Create app
WApplication app = new WApplication();
WWindow window = new WWindow();
window.setContent(new MockLabel("dummy"));
app.add(window);
WContent content = new WContent();
content.setContentAccess(new InternalResource("/wcomponents-test.properties", "test"));
app.add(content);
app.setLocked(true);
// Set up servlet env
WServlet.WServletEnvironment servletEnvironment = new WServlet.WServletEnvironment("/app", "", "");
servletEnvironment.setStep(initialServletStep);
// Set user session
UIContext uic = createUIContext();
uic.setUI(app);
uic.setEnvironment(servletEnvironment);
setActiveContext(uic);
window.setStep(initialWindowStep);
window.display();
// Target the content first - should pass through and update the environment's step
WWindowInterceptor interceptor = new WWindowInterceptor(true);
TestInterceptor testInterceptor = new TestInterceptor();
interceptor.setBackingComponent(testInterceptor);
interceptor.attachUI(app);
MockRequest request = new MockRequest();
request.setParameter(Environment.TARGET_ID, content.getId());
interceptor.serviceRequest(request);
Assert.assertEquals("Servlet step should have changed after serviceRequest", initialServletStep + 1, servletEnvironment.getStep());
Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
interceptor.preparePaint(request);
Assert.assertEquals("Servlet step should have changed after preparePaint", initialServletStep + 2, servletEnvironment.getStep());
Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
Assert.assertEquals("Servlet step should have changed after paint", initialServletStep + 3, servletEnvironment.getStep());
Assert.assertEquals("Window step should not have changed", initialWindowStep, window.getStep());
servletEnvironment.setStep(initialServletStep);
request = new MockRequest();
request.setParameter(WWindow.WWINDOW_REQUEST_PARAM_KEY, window.getId());
interceptor.serviceRequest(request);
Assert.assertEquals("Window step should have changed after serviceRequest", initialWindowStep + 1, window.getStep());
Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
interceptor.preparePaint(request);
Assert.assertEquals("Window step should have changed after preparePaintnot have changed", initialWindowStep + 2, window.getStep());
Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
interceptor.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
Assert.assertEquals("Window step should have changed after paint", initialWindowStep + 3, window.getStep());
Assert.assertEquals("Servlet step should not have changed", initialServletStep, servletEnvironment.getStep());
String actualTargetId = testInterceptor.hiddenParams.get(WWindow.WWINDOW_REQUEST_PARAM_KEY);
Assert.assertEquals("Hidden params target id should be window id", window.getId(), actualTargetId);
}
use of com.github.bordertech.wcomponents.WContent in project wcomponents by BorderTech.
the class WContentRenderer method doRender.
/**
* <p>
* Paints the given WContent.</p>
*
* <p>
* This paint method outputs a popup that opens browser window in which the content document will be displayed. The
* component is only rendered for requests in which the display() method of the content component has just been
* called.</p>
*
* <p>
* WContent's handleRequest() method will return the actual PDF document content via the use of an Escape.</p>
*
* @param component the WContent to paint.
* @param renderContext the RenderContext to paint to.
*/
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
WContent content = (WContent) component;
XmlStringBuilder xml = renderContext.getWriter();
if (!content.isDisplayRequested()) {
// This is the normal situation.
return;
}
Object contentAccess = content.getContentAccess();
if (contentAccess == null) {
LOG.warn("No content specified");
return;
}
// Ok, the content is available and should be shown
switch(content.getDisplayMode()) {
case DISPLAY_INLINE:
case PROMPT_TO_SAVE:
xml.appendTagOpen("ui:redirect");
xml.appendUrlAttribute("url", content.getUrl());
xml.appendEnd();
break;
case OPEN_NEW_WINDOW:
xml.appendTagOpen("ui:popup");
xml.appendUrlAttribute("url", content.getUrl());
xml.appendAttribute("width", content.getWidth().replaceAll("px", ""));
xml.appendAttribute("height", content.getHeight().replaceAll("px", ""));
xml.appendOptionalAttribute("resizable", content.isResizable(), "true");
xml.appendEnd();
break;
default:
throw new IllegalStateException("Invalid display mode: " + content.getDisplayMode());
}
}
use of com.github.bordertech.wcomponents.WContent in project wcomponents by BorderTech.
the class StepCountUtil method isCachedContentRequest.
/**
* Check if the request is for cached content.
*
* @param request the request being processed
* @return true if content is cached, otherwise false
*/
public static boolean isCachedContentRequest(final Request request) {
// Get target id on request
String targetId = request.getParameter(Environment.TARGET_ID);
if (targetId == null) {
return false;
}
// Get target
ComponentWithContext targetWithContext = WebUtilities.getComponentById(targetId, true);
if (targetWithContext == null) {
return false;
}
// Check for caching key
WComponent target = targetWithContext.getComponent();
UIContextHolder.pushContext(targetWithContext.getContext());
try {
// TODO Look at implementing CacheableTarget interface
String key = null;
if (target instanceof WContent) {
key = ((WContent) target).getCacheKey();
} else if (target instanceof WImage) {
key = ((WImage) target).getCacheKey();
} else if (target instanceof WVideo) {
key = ((WVideo) target).getCacheKey();
} else if (target instanceof WAudio) {
key = ((WAudio) target).getCacheKey();
}
return !Util.empty(key);
} finally {
UIContextHolder.popContext();
}
}
use of com.github.bordertech.wcomponents.WContent in project wcomponents by BorderTech.
the class WContentRenderer_Test method testDoPaint.
@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
MockRequest request = new MockRequest();
MockContentAccess content = new MockContentAccess();
content.setBytes(new byte[] { 1, 2, 3 });
// Shouldn't display have any content by default
WContent wContent = new WContent();
setActiveContext(createUIContext());
wContent.setContentAccess(content);
// this test needs the URLs to be consistent
wContent.setCacheKey("abcd{}<>");
wContent.handleRequest(request);
String xml = render(wContent);
Assert.assertEquals("Should not have painted any content", "", xml);
// Should produce mark-up when displayed
wContent.handleRequest(request);
wContent.display();
assertSchemaMatch(wContent);
// Display again (as reset after paint)
wContent.display();
assertXpathUrlEvaluatesTo(wContent.getUrl(), "//ui:popup/@url", wContent);
// Display again (as reset after paint)
wContent.display();
assertXpathNotExists("ui:redirect", wContent);
// Test all options
String width = "111";
String height = "222";
wContent.setWidth(width);
wContent.setHeight(height);
wContent.setResizable(true);
wContent.setDisplayMode(WContent.DisplayMode.PROMPT_TO_SAVE);
assertSchemaMatch(wContent);
// Display again (as reset after paint)
wContent.display();
assertXpathNotExists("//ui:popup", wContent);
// Display again (as reset after paint)
wContent.display();
assertXpathUrlEvaluatesTo(wContent.getUrl(), "//ui:redirect/@url", wContent);
// Test null content
wContent.setContentAccess(null);
xml = render(wContent);
Assert.assertEquals("Should not have painted any content", "", xml);
}
Aggregations