Wicket manipulate javascript import order - java

I'm trying to use the very nice ConfirmerAjaxLink from Visural. However, I'm getting a js script when loading the page: 'jQuery is not defined'.
I wrote a little test page to identify the problem:
public class ConfirmAjaxLinkTestPage extends WebPage {
public ConfirmAjaxLinkTestPage() {
this.add(new ConfirmerAjaxLink("confirm") {
#Override
public void onClick(AjaxRequestTarget target) {
System.out.println("OK");
}
});
}
#Override
public void renderHead(IHeaderResponse response) {
response.renderJavaScriptReference(new JavaScriptResourceReference(
JavascriptLibraryUtil.class,
"jquery/jquery-1.6.1.min.js"));
}
}
After a quick search I saw that the ConfirmerAjaxLink will add another javascript that uses jQuery. However, since this is done through a behavior that is added to the component (above link) this script gets added to the markup before the jQuery one (since this is called before the renderHead of my page).
If I change the headerRenderStrategy to parent first like this:
System.setProperty("Wicket_HeaderRenderStrategy","org.apache.wicket.markup.renderStrategy.ParentFirstHeaderRenderStrategy");
it works but this is more a hack than a solution (also, as stated in in AbstractHeaderRenderStrategy:NOT OFFICIALLY SUPPORTED BY WICKET).
Is there a clean way to this (I would think not exceptional) problem?

The Visural example application includes jQuery in the constructor for the main WebApplication class. Code from the Visural example app:
public class ExamplesApplication extends WebApplication {
public ExamplesApplication() {
addRenderHeadListener(JavascriptPackageResource.getHeaderContribution(new JQueryResourceReference(Version.V1_4_2)));
}
You could also just put the jQuery include in the head of your page HTML. I tested it and that worked fine.

Related

GWT Initialize ClientBundle With Multiple CssResources

I'm working on updating some legacy code to GWT 2 and I'm running into some odd behaviour. I have a custom interface that extends ClientBundle as per the gwt docs. Within that bundle I define several CssResources to point to the various .css documents for my module. The problem comes when I go to actually initialize my module. I have some code in the initializer that gets the static reference to each CssResource and calls ensureInjected(). The problem is, only the first call actually does anything. Any subsequent calls seem to be getting ignored and the css styles are not getting added to the application. What do I need to do to work with multiple css documents for a single module?
CssBundle.java
public interface CssBundle extends ClientBundle {
public static final CssBundle INSTANCE = (CssBundle) GWT.create(CssBundle.class);
/* CSS */
#Source("mypath/public/Client.css")
public ClientCss mainCSS();
#Source("mypath/resources/css/mini/ext-all.css")
public ExtAllCss extAllCSS();
}
ClientCss.java
public interface ClientCss extends CssResource {
String applicationTitle();
String branding();
String bugReportDirections();
#ClassName("Caption")
String caption();
}
ExtAllCss.java
public interface ExtAllCss extends CssResource {
#ClassName("close-icon")
String closeIcon();
#ClassName("close-over")
String closeOver();
#ClassName("col-move-bottom")
String colMoveBottom();
}
MyModule.java
public class MyModule extends Composite
{
public void initialize()
{
//this css shows up in the client
CssBundle.INSTANCE.mainCSS().ensureInjected();
//this does nothing
CssBundle.INSTANCE.extAllCSS().ensureInjected();
}
}
That code looks exactly right, but might not function the way you expect - instead of each ensureInjected() causing a new <style> block to be created, instead they just enqueue the styles that they need to be made available, and at the end of the current event loop a single <style> is added with all of the various collected styles. This limits the number of times that the document potentially needs to be restyled, and also helps reduce the number of style tags (old IE had a bug where there was a max number of tags possible).
To confirm this, check the entire contents of the <style> tag, you should see that both css files are appended there, one after the other.

How would I go about this? Different projects communicating? C#

Okay, I'm not sure how to go about explaining this, nor how to do it, but I will try to explain what I want step-by-step.
I want to make an API that contains, for example an EntitySpawnEvent object. It might look something like this:
namespace ExampleAPI
{
class EntitySpawnEvent
{
private bool cancelled;
private Entity entity;
public EntitySpawnEvent(Entity entity)
{
this.entity = entity;
this.cancelled = false;
}
public void SetCancelled(bool cancelled)
{
this.cancelled = cancelled;
}
public bool IsCancelled()
{
return this.cancelled;
}
}
}
Then I have I will have a server that uses this API. This server will also load plugins that also uses the API. The server might be something like this:
using System.Generics;
using ExampleAPI;
namespace ExampleServer
{
class Server
{
private List<Plugin> plugins;
public OnEnable()
{
LoadPlugins();
}
private void LoadPlugins()
{
// Loop through all "plugins" in the "/plugins" folder.
// Add them all to the list of plugins.
}
}
}
Then later when the server wants to spawn an entity, it throws the event to all plugins, the plugins can manipulate the event's information. For example, whether or not to cancel the event. The plugin's event listener could look something like this:
using ExampleAPI;
namespace ExamplePlugin
{
class Plugin : EventListener
{
public void onEntitySpawn(EntitySpawnEvent event)
{
event.SetCancelled(true);
}
}
}
And the server would throw it something like this:
using ExampleAPI;
namespace ExampleServer
{
class ExampleEventThrower
{
private Server server;
public ExampleEventThrower(Server server)
{
this.server = server;
SpawnEntity();
}
void SpawnEntity()
{
EntitySpawnEvent event = new EntitySpawnEvent(new Entity()); // Entity would also be part of the API
foreach (Plugin plugin in server.GetPlugins())
{
plugin.onEntitySpawn(event); // Here the plugin could manipulate the values of the EntitySpawnEvent
}
if (!event.IsCancelled())
{
// Spawn entity
}
}
}
}
Of course these are just extremely basic code examples, but they should help explain what I want.
Basically, what I want to know and do is the following:
I have an exported Server.
The Server have a /plugins folder
The user can make their own plugins using the API, export them and put them in the /plugins folder
The Server would load the plugin and let it modify all the events and such.
My key question is, how should the plugins be exported and loaded, so they can manipulate the events and such? Do I export them as DDLs? I have no idea.
I guess it's sort of similar to the way Bukkit works, but there everything's in Java and you just export it is a .jar file.
Any help would be greatly appreciated. Thanks!
So few things to take a look at...
It sounds like you want to have plugins run off of an interface that you know about, and load the plugins at runtime.
This should help you build the DLLs:
http://msdn.microsoft.com/en-us/library/3707x96z.aspx
This should help load the DLL dynamically at runtime:
Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

GWT Auto Refresh Code Jumps To Home Page

We want to auto refresh a page that is built using GWT 2. We used a lot of solutions to do it:
GWT auto refresh
automatic refresh of GWT screen
But neither of them worked properly. The problem is a bit complicated:
The auto refresh works in the home page/tab called "Kazalar":
http://dl.dropbox.com/u/103580364/temp/000766.jpg
But if the user is in another tab then after auto refresh the browser jumps to home page/tab:
http://dl.dropbox.com/u/103580364/temp/000767.jpg
In the above question's answer, the answerer says that we should replace the reloadAll() function with code that recreates that part's view (with some Ajax calls to re-fetch data from server if needed). We couldn't test this part because we don't know how to write the code that recreates a specific part's view. Could someone please give an example on how to do it?
public class TimerExample implements EntryPoint, ClickListener {
public void onModuleLoad() {
Button b = new Button("Click and wait 5 minutes");
b.addClickListener(this);
RootPanel.get().add(b);
}
public void onClick(Widget sender) {
Timer t = new Timer
public void run() {
reloadAll();
}
};
// Schedule the timer to run once in 5 minutes.
t.schedule(5*1000*60);
}
private void reloadAll() {
Window.Location.reload();
}
}
Using a timer is fine.
Assuming you know about GWT activities and places.
The harsh way would be to reload the full module using
Window.Location.replace("url#kalazar:");
You already mentionned it; but a really nicer way (assuming you are implemeting the MVP pattern) would be to create a refresh method on the presenter of the Kalazar view. This way you won't need to reload the page.
private void reloadAll() {
myKalazarPresenter.refresh();
}
private void myKalazarPresenter() {
myKalazarView.clear();
myKalazerView.reInit(kalazarInitializationData);
}
Since you say you can't reInit the view, maybe you could just try to delete and recreate it ?

Wicket head section hierarchy

I am a little confused about tags. I know from wicket 1.5 there was a change of head render strategy from parent->child to child->parent.
Now I use wicket 6.9 and I have simple menu panel which I want to use some jquery effects.
I want to use the same jquery (for example for google) file for whole application.
I cannot use jquery link only in main page, because in while rendering menu panel there is " $(document).ready" and it is not recognized. Reading some forum i found opinion that panel should contain jquery itselft - it is reasonable, because it can be reusable independently.
So now my page consist:
<head>
...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
...
</head>
And my menu panel consist the same. As a result in rendered html I load jquery.js twice.
How should I resolve it? I want to load it only once. I know i can back to old strategy and do application.getResourcesSettings().setHeaderItemComparator() but as i read it is not the best solution.
I can found such class like PriorityHeaderItem in wicket, but documentation is very poor for wicket and did not found any example of use it.
Best regards
Since wicket 1.6 jQuery is now the javascript library used by the framework. So you may see jQuery twice because of the one you included and the wicket version? If you want to override the jQuery version you can create a Resource Reference and then set it in your init method of the Application class.
First you need the resource reference file and put the js file in same package structure.
public final class JQueryResourceReference extends JavaScriptResourceReference {
private static final JQueryResourceReference INSTANCE = new JQueryResourceReference();
private JQueryResourceReference() {
super(JQueryResourceReference.class, "jquery.js");
}
public static JQueryResourceReference get() {
return INSTANCE;
}
}
Then in the application init method do this:
public MyApplication extends AuthenticatedWebApplication {
#Override
protected void init() {
super.init();
getJavaScriptLibrarySettings().setJQueryReference(JQueryResourceReference.get());
....
}
....
}

java annotations - problem with calling a locator class from a Vaadin Project

I'm not sure how to explain this without writing several pages so I hope the actual code is more expressive.
I've made a jar containing multiple annotation declaration similar to the following:
#Target(ElementType.PACKAGE)
#Retention(RetentionPolicy.RUNTIME)
public #interface MarkedPackage {
}
then I have made a test jar containing several classes in several packages and marked just one package with the above annotation (with package-info.java) like below:
#myPackage.MarkedPackage
package package.test.jar;
this jar had in its build path the jar containing the annotations.
then I made a static class that has a method (LoadPlugins) that retrieves a list with all the jars of a directory. Then it searches through the jars for the 'package-info' class and checks if that classes package contains the MarkedPackage annotation. by calling this:
if (checkPackageAnnotation(thisClass.getPackage()))
where thisClass is the package-info class retrieved via a classloader. and:
public static boolean checkPackageAnnotation(AnnotatedElement elem) {
System.out.println(elem.getAnnotations().length);
if (elem == null || !elem.isAnnotationPresent(MarkedPackage.class))
return false;
return true;
}
the elem.getAnnotatios().length is there for debug purposes.
And the problem appears when I call the method from the static class:
if I call it from a main function:
public class MyMain {
public static void main(String[] args){
PluginUtils.LoadPlugins();
}
}
everything works perfectly it displays '1' from that System.out.println(elem.getAnnotations().length);
But if I call it from a button from my Vaadin project:
header.addComponent(new Button("CallThat",
new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
PluginUtils.LoadPlugins();
}
}));
It displays '0' from that System.out.println(elem.getAnnotations().length);
Also I should mention that I created the main inside my Vaadin project so it would have the exact same build path and resources.
Is there a problem with web applications and that "#Retention(RetentionPolicy.RUNTIME)" ?
hope I was clear enough... Hope someone has a solution for me... If you need more information - let me know.
Thank you.
yes,
because there is only one package-info class in one package.

Categories