I just took a 15 min break from regular work and packaged the CSSInject component as an proper add-on. It’s the same component I’ve used in the Chameleon Theme color editor to dynamically change the colors as you use the color pickers.
CustomComponent doesn’t support addComponent, hence the exception. That’s just plain Vaadin stuff, not related to CSSInject.
You use the CSSInject as any other “non-visible” component: place it in some layout. It doesn’t really matter where, but I prefer the main layout, as early in the component hierarchy as possible, so the styles are updated before any other component rendering is done (they migth affect size calculations for example).
If I am guessing your need from your post, you wish to alter the DragAndDropWrapper’s style in some way. You then need to write custom CSS that will be injected to the client using CSSInject, that will target the wrapper element, e.g.
CSSInject does not provide any helpers for targeting individual components, it’s just plain CSS that will be added to the window you add the CSSInject component to.
Great idea for a widget. I’m having a problem getting it running. Since I am a Vaadin newbie, maybe I am missing something. I included the 0.1 JAR file in the library path, and got it integrated into my code the way I want. When I run my app, at the very top of the UI in the browser a message is output:
Widgetset does not contain implementation for org.vaadin.cssinject.CSSInject. Check its @ClientWidget mapping, widgetsets GWT module description file and re-compile your widgetset. In case you have downloaded a vaadin add-on package, you might want to refer to add-on instructions. Unrendered UIDL:
org.vaadin.cssinject.CSSInject(NO CLIENT IMPLEMENTATION FOUND)
Well, that’s the problem. I cannot tell if this CSS add-on is pre-compiled or requires it beforehand. It is not clear. I looked through the build-widgetset.xml script for Ant, and it would appear that the cssinject-*.jar was in fact generated by the Ant script already. Am I supposed to recompile an add-on, or is it missing something?
Once the JAR is in your classpath, simply running the Eclipse widgetset compiler or the Ant compiler should be enough. Be sure to enable verbose logging so you see what’s going on during the compilation. Are there any errors during the compilation?
Do you think it would make sense to have dynamic CSS file loading also as a part of this widget?
I’ve implemented this kind of mechanism in the
AboutBox to switch the whole CSS themes on-the-fly. The server-side API could be something very simple like:
css.loadFile(String id, Resource cssLink)
and here is an sample JSNI implementation:
private native void loadCss(String url, String media, String id) /*-{
var links = $doc.getElementsByTagName("link");
var es = null;
if (links.length > 0) {
for (var i = 0; i<links.length; i++) {
if (links[i]
["id"]
== id) {
if (links[i]
["href"]
== url) {
return;
} else {
es = links[i]
;
break;
}
}
}
}
if (es == null) {
es = $doc.createElement("link");
}
es.setAttribute("id", id);
es.setAttribute("rel", "stylesheet");
es.setAttribute("type", "text/css");
es.setAttribute("media", media);
es.setAttribute("href", url);
var head = $doc.getElementsByTagName("head")[0]
;
head.appendChild(es);
}-*/;
If the server-side API is based on Resource, one could also generate the CSS data on the fly in the application.
Sami: sounds like a good idea. Since the code is now
available in GitHub , I urge you to make a patch an send me a pull request. I’d be happy to include it.
If not, then I ask you to at least create a new enhancement issue in GitHub so the feature will be documented. Thanks!
I made a new version with the Resource functionality. So you can now do this:
CSSInject css = new CSSInject();
css.addStyleSheet(new ThemeResource("views/login-view.css"));
You can also use ClassResource and all the other possibilities that the Resource interface allows you to do.
With this you can now package your server-side-only add-on component’s styles more easily, I would argue.
It also makes it handy to include some view-specific styles in your application, so you’re not forcing the user to download all the CSS at init, but only when it is needed. But be careful, since the CSS might affect the layout calculations, and I’m not totally sure how the order of execution goes when adding new stylesheets on the fly during JavaScript execution (i.e. which gets done first: the loading of CSS or the rest of the JS execution).
How would i use CSSInject to style a single table row, without needing to call refreshRowCache() or refreshRenderedCells().
Actually the problem is getting a handle on the table row and adding style to it.
Now it occupies half the screen (the other half is for the next component I add). Perhaps this is not really a CSSInject question/issue, but I’m a bit confused.
Nobody seems to have replied, so: there is an implicit VerticalLayout in your window and the CSSInject gets expand ratio 1 by default, which means extra space is divided evenly between its slot in the layout and other components.
((VerticalLayout) mainWindow.getContent()).setExpandRatio(css, 0); or something like that should help. You could also explicitly create the main layout and do mainWindow.setContent(…) to avoid the cast.
I already knew about the expand ratio but I didn’t think about this way.
BTW: I love Vaadin and I find it’s a breeze to use: The only thing I struggle with a bit is the layout stuff. On the other hand it’s also difficult in most other frameworks like Swing, SWT etc.
What am I doing wrong?
Widgetset does not contain implementation for org.vaadin.cssinject.CSSInject. Check its component connector’s @Connect mapping, widgetsets GWT module description file and re-compile your widgetset. In case you have downloaded a vaadin add-on package, you might want to refer to add-on instructions.