getRequestCycle().urlFor not working in panel - java

I need the URL for a component in wicket. When I use a page it works properly, but when using panel it does not work.
public final class ImageP extends Panel {
public ImageP(String id) {
super(id);
List<Mapp> list = Mapp.loadall(); //load image from database
final Mapp asr = list.get(0);
ByteArrayResource resource = new ByteArrayResource("image/jpeg", asr.getImage());
Image image = new Image("img", resource);
add(image);
System.out.println(getRequestCycle().urlFor(image, IResourceListener.INTERFACE));
}
}
This code does not work and throws an exception, but when I use page instead of panel getRequestCycle().urlFor(image, IResourceListener.INTERFACE) it works properly.

I bet you've got the following exception:
java.lang.IllegalStateException: No Page found for component [Component id = img]
It's because RequestCycle object internally calls getPage() method of the component that's first parameter of the urlFor() method with the following signature:
urlFor(Component component, RequestListenerInterface interface)
In case of calling method urlFor() in the constructor of a panel it's impossible to get page of a panel's child because panel isn't attached to page yet. So Wicket throws "a nice exception".
To fix that problem you just can move your code to the onBeforeRender() method of the panel. Something like that:
#Override
protected void onBeforeRender() {
//
// ... init resource ...
//
Image image = new Image("img", resource);
addOrReplace(image);
System.out.println(getRequestCycle().urlFor(image, IResourceListener.INTERFACE));
super.onBeforeRender();
}
P.S. I also assume that you're using Wicket 1.4 or earlier because there's no RequestCycle.urlFor(component, listener) method in Wicket 1.5 and later. So I think neither your question nor my answer doesn't make sense in that case.

Related

Wicket - Set Model from another panel

I am quite new to Wicket. I am adding a model to a sub-panel(ChartPanel) from a main panel (MainPanel) on a button click.
MainPanel.java
On button click, I am re-adding the chartPanel after I change its model. Following is the code I am using in the buttonClick of the MainPanel. Here the onRenderAnnotations event is generated on some click in the UI.
#OnEvent
public void onRenderAnnotations(RenderAnnotationsEvent aEvent)
{
LOG.trace("clicked on the annotation");
renderChart( aEvent.getRequestHandler());
}
private void renderChart(IPartialPageRequestHandler aRequestHandler)
{
MultiValuedMap<String, Double> recommenderScoreMap = getLatestScores(aRequestHandler);
Map<String,String> curveData = new HashMap<String,String>();
LearningCurve learningCurve = new LearningCurve();
for (String recommenderName : recommenderScoreMap.keySet()) {
String data = recommenderScoreMap.get(recommenderName).stream().map(Object::toString)
.collect(Collectors.joining(", "));
curveData.put(recommenderName,data);
learningCurve.setCurveData(curveData);
learningCurve.setMaximumPointsToPlot(MAX_POINTS_TO_PLOT);
}
chartPanel.setDefaultModel(Model.of(learningCurve));
// to avoid the error, A partial update of the page is being rendered
try {
aRequestHandler.add(chartPanel);
}
catch (IllegalStateException e) {
LOG.warn("Not updating the chart. " + e.toString());
setResponsePage(getPage());
}
}
ChartPanel.java
After this in the chartPanel, I want to use the updated model to add component inside the chartpanel. What would be the best way to do that?
I want to do something like this in the class ChartPanel:
#Override
protected void onRender()
{
super.onModelChanged();
LearningCurve newLearningCurve = getModel().getObject();
requestTarget = ???
String js = createJavascript(newLearningCurve);
requestTarget.prependJavascript(js);
}
My question is, in the above code how to get the request target since it is not an ajax request neither do I get it in the arguments. Should I use some other function where I also get a requestTarget. But I want it to be called every time the model of ChartPanel is updated from anywhere.
Pardon my ignorance. I have been trying for a few days but I am still stuck. I tried to explain it enough but if any information is missing, please comment and I will add it right away.
Thanks.
You should override renderHead() instead:
#Override
public void renderHead(IHeaderResponse response)
{
super.renderHead(response);
response.render(OnLoadHeaderItem.forScript(
createJavascript(newLearningCurve)));
}
This way your chart will be shown correctly regardless whether it was added due to an AjaxRequest or simply when the page is rerendered.

Queue an event before rendering a view in taskflow

I have a Task Flow with two views: listOfClients and newClient. Shown here:
listOfClients view has a table which I want to sort before rendering it. To do it, I want to create a SortEvent with the table as source (as shown in the docs, section 29.4.4 ), but I cannot access the table before rendering the view.
I call the method queueSortIdEvent in a pageFlow-scoped managed bean but findComponent cannot find the table (returns null ). Tried also view-scoped, same result.
My code in the bean is:
public UIComponent findComponent(final String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot root = context.getViewRoot();
final UIComponent[] found = new UIComponent[1];
root.visitTree(new FullVisitContext(context), new VisitCallback() {
#Override
public VisitResult visit(VisitContext context, UIComponent component) {
if(component.getId().equals(id)){
found[0] = component;
return VisitResult.COMPLETE;
}
return VisitResult.ACCEPT;
}
});
return found[0];
}
public void queueSortIdEvent(){
SortCriterion sc = new SortCriterion("ClientId", true);
List<SortCriterion> listSC = new ArrayList<>();
listSC.add(sc);
SortEvent new = new SortEvent(findComponent("t1"), listSC); // the table id is "t1"
new.queue();
}
Is there a way to queue the event before rendering the view?
Note: findComponent function works fine in other parts of the code, got it from here
My JDeveloper version is 12.1.3
What JDeveloper version are you using?
The findComponent won't work because nothing has been rendered yet. So your component isn't available (= doesn't exists yet) at that moment.
I think there is an easier way to do sorting.
On your page/fragment, go to 'Bindings', select your iterator (middle column), click on Edit (pencil icon) and set the sorting you want in the 'Sort criteria' tab.

How to dispose of an SWT Font set on a Draw2D Label?

In a GEF editor, I have the following EditPart:
public class MyLabelEditPart extends AbstractGraphicalEditPart {
#Override
protected IFigure createFigure() {
return new Label();
}
#Override
protected void refreshVisuals() {
MyModel model = (MyModel) getModel();
Label figure = (Label) getFigure();
EditPart parent = getParent();
Font font = new Font(Display.getCurrent(), "sansserif", 11, SWT.BOLD);
figure.setFont(font);
figure.setForegroundColor(ColorConstants.darkGray);
figure.setText(model.getValueString());
parent.refresh();
}
All works fine with most models, but - you will have spotted the error already - I never dispose of the font. So, with a large-ish model of 10k+ tokens, this throws an org.eclipse.swt.SWTError: No more handles. At least I think (hope) this is what causes the error.
Now I cannot figure out how to dispose the font, as the figure for the EditPart is a Draw2D Label, not an SWT Widget. How can I make sure the dreaded error can be circumvented?
Apart from Baz' solution to make the font a static field, a good solution is to use a JFace FontRegistry, as detailed in this strangeoptics blog post.
If you want to keep and reuse all your resources ( images, fonts, colors..etc) at one point rather than using registries, follow this windows builder class
http://code.google.com/p/goclipse/source/browse/trunk/goclipse-n/src/org/eclipse/wb/swt/SWTResourceManager.java?r=445

GXT pagination without RPC proxy

Any idea how I can implement proper pagination without a RPCProxy in GXT? I am currently setting the loader like this:
final PagingLoader<PagingLoadResult<ModelData>> loader = new BasePagingLoader<PagingLoadResult<ModelData>>(null);
store = new ListStore<T>(loader);
And then pass the store to the grid constructor.
Now, if I set null instead of a proxy in the constructor, my pagingToolbar just freezes and goes disabled and displays what appears to be a loading circle.
I read the ideas in here http://www.sencha.com/forum/showthread.php?61780-Pagination-without-RPC, but can anyone be a bit more explicit on how to achieve this?
I am creating the grid and then adding the data and I'm working with RequestFactory so no RCPProxy needed.
You can just implement the DataProxy interface and use your custom data-obtaining method:
BasePagingLoader<PagingLoadResult<ModelData>> loader = new BasePagingLoader<PagingLoadResult<ModelData>>(new DataProxy<PagingLoadResult<ModelData>>() {
#Override
public void load(DataReader<PagingLoadResult<ModelData>> reader,
Object loadConfigAsObject, AsyncCallback<PagingLoadResult<ModelData>> callback) {
BasePagingLoadConfig loadConfig = (BasePagingLoadConfig) loadConfigAsObject;
// Get the results for the requested page...
BasePagingLoadResult<ModelData> pagingLoadResult = new BasePagingLoadResult<ModelData>(...);
callback.onSuccess(pagingLoadResult);
}
});

Wicket: Crash when opening ModalWindow (Popup)

In a Wicket Panel i implemented a method called showAttentiePopup(AjaxRequestTarget) which shows an ModalWindow popup screen. This method works fine when i call it from the same Panel. But when I try to call the method from another Panel it crashes with the following error:
java.lang.IllegalStateException: No Page found for component [MarkupContainer [Component id = createAttentie]]
at org.apache.wicket.Component.getPage(Component.java:1665)
at org.apache.wicket.RequestCycle.urlFor(RequestCycle.java:851)
at org.apache.wicket.Component.urlFor(Component.java:3170)
at org.apache.wicket.behavior.AbstractAjaxBehavior.getCallbackUrl(AbstractAjaxBehavior.java:123)
at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.getCallbackScript(AbstractDefaultAjaxBehavior.java:116)
at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.getCallbackScript(AbstractDefaultAjaxBehavior.java:104)
at org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow$CloseButtonBehavior.getCallbackScript(ModalWindow.java:876)
at org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.getWindowOpenJavascript(ModalWindow.java:1005)
at org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.show(ModalWindow.java:286)
at nl.topicuszorg.fks.web.client.dossier.overzichten.documenten.AttentieOverzichtPanel.showAttentiePopup(AttentieOverzichtPanel.java:171)
at nl.topicuszorg.fks.web.client.dossier.overzichten.documenten.EditFileDocumentPanel$2.onSubmit(EditFileDocumentPanel.java:195)
at org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink$1.onSubmit(AjaxSubmitLink.java:94)
at org.apache.wicket.ajax.form.AjaxFormSubmitBehavior.onEvent(AjaxFormSubmitBehavior.java:128)
at org.apache.wicket.ajax.AjaxEventBehavior.respond(AjaxEventBehavior.java:163)
at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.onRequest(AbstractDefaultAjaxBehavior.java:297)
Someone an idea what the problem might be?
The method showAttentiePopup:
public void showAttentiePopup(AjaxRequestTarget target) {
Component content = new EditAttentiePanel(createAttentie.getContentId(), new Attentie(), EditFunctie.AANMAKEN) {
/** */
private static final long serialVersionUID = 1L;
#Override
protected void annuleren(AjaxRequestTarget target) {
FKSModalWindow.closeCurrentWindow(target);
}
#Override
protected void opslaan(AjaxRequestTarget target, Attentie attentie) {
//Inhoud even weggelaten
}
};
createAttentie.setOutputMarkupId(true);
createAttentie.setInitialWidth(900);
createAttentie.setInitialHeight(450);
createAttentie.setTitle("Nieuwe Attentie Aanmaken");
createAttentie.setContent(content);
createAttentie.show(target);
}
Greetings,
Rick
Your stack trace indicates the ModalWindow createAttentie is not part of a page at the time your code is trying to render it.
Trace through your page construction logic and component structure and make sure everything is wired together correctly.

Categories

Resources