How to change theme in SWT application - java

I'm new to developing SWT applications and am looking for an easy way to change the theme of a small application which I have already developed.
After doing some googling I'm able to tell that there seems to be something called presentations which seems like a way I can download a ready made theme and just apply it to my application. Is that right?
Alternatively can anyone point me to a good tutorial on the right way to go about it?
thanks

If you are just talking about SWT without Ecipse RCP, then there is no way to theme the application.
One of the main advantage of SWT is that it uses OS resources to resemble system applications. Using themes would contradict this approach.
If you are however using Eclipse RCP 4, look at #Ran's answer.

The org.eclipse.e4.ui.css.swt.theme extension point supports the creation of extensions for themes. This extension defines an ID for the style and a pointer to the CSS file.
You can also define the default theme via the cssTheme property in your org.eclipse.core.runtime.products extension. This can also be used to define a fixed styling.
To switch the styling you use the IThemeEngine.
package com.example.e4.rcp.todo.handlers;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
public class ThemeSwitchHandler {
// Remember the state
static boolean defaulttheme= true;
#Execute
public void switchTheme(IThemeEngine engine) {
System.out.println("ThemeSwitchHandler called");
// The last argument controls
// whether the change should be persisted and
// restored on restart
if (!defaulttheme) {
engine.setTheme("com.vogella.e4.todo.defaulttheme", true);
} else {
engine.setTheme("com.vogella.e4.todo.redtheme", true);
}
defaulttheme= !defaulttheme;
}
}

Related

How to use CellTable.Style with GWT 2.7.0 without UiBinder?

I joined a GWT application project a few weeks ago. The codebase was started back in 2009. I am trying to replace a FlexTable with a CellTable so that I can take advantage of the sortable columns. The current version of GWT in the project is 2.7.0, but looking through the code, it looks like there are some features still used that have gone out of style. I am new to GWT, I could be wrong.
So far, things are functionally good. However, GWT seems to be overriding my attempts to update the CSS. I used the GWT dynatablerf sample as a model to add CSS to the CellTable. The TimeSlotWidget uses CellTable.Style:
interface TableResources extends CellTable.Resources {
#Override
#Source(value = {CellTable.Style.DEFAULT_CSS, "CellTablePatch.css"})
CellTable.Style cellTableStyle();
}
And then applies it to the CellTable like this:
table = new CellTable<TimeSlotListWidget.ScheduleRow>(ROWS_IN_A_DAY,
GWT.<TableResources> create(TableResources.class));
I tried to use this approach in my code. I even omitted the CellTable.Style.DEFAULT_CSS from the value list and created my own CSS stylesheet which started as a copy of the GWT CellTable.css
I noticed that GWT TimeSlotListWidget sample has an ui.xml file with UIBinder. My project does not currently use UIBinder.
When I run my code, there is a <style> block inserted into the page that seems to be the standard GWT CellTable.css. Then directly after that, another <style> block is inserted with my CSS. And my CSS is not overriding the standard GWT CSS.
How can I keep the GWT CellTable.css from being inserted? Why is it being inserted?
The problem is that in your code, you reuse CellTable.Style, and two different ClientBundles have used the same type but each bound their own CSS to it.
Instead, extend the CellTable.Style interface, and use that in your bundle:
interface MyCellTableStyle extends CellTable.Style {}
interface TableResources extends CellTable.Resources {
#Override
#Source(value = {CellTable.Style.DEFAULT_CSS, "CellTablePatch.css"})
MyCellTableStyle cellTableStyle();
}

Gwtbootstrap3. Using bootstrap component only with java code- without ui-binder

Is there possibile to use bootstrap3 elements (from gwtboostrap3 library) without using ui binder, but using java code like it is done with gwt regular widgets?
I could not find a word about it in Documentation.
F.E. Lets take button widget from gwt:
public void onModuleLoad() {
// Make a new button that does something when you click it.
Button b = new Button("Jump!", new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("How high?");
}
});
// Add it to the root panel.
RootPanel.get().add(b);
}
}
Which will create:
<button type="button" class="gwt-Button">Jump!</button>
In gwtbootstrap3 I have to do something like that:
<b:Button type="DEFAULT"/>
Please help me with that.
Is it possible to use gwtbootstrap3 library components like button with pure java instead of uibinder xml files?
Is there solution that works out of box?
Or maybe I should write my own classess that extends native gwt widget an add bootstrap releated css classes?
Yes!
You can use the widget as you please, refer to the their javadoc to see what constructors they have! They are a different set of widgets and might not be analogue of GWT standard widgets.
Bonus
GWT UiBinder does no different than you, all it does is reduce the boilerplate, generating java classes that automate the instantiate of the widgets. You can add the compiler option -gen to see these transient classes. Usually , they are deleted after GWT compiles your application, as they are no longer needed. Take a look the the compiler documentation for more info!

Menu bar not displaying in eclipse e4 on restore state

I am creating an RCP application using Eclipse 4.4.1 with the Compatibility Layer (migration from 3.x to 4.x). I have defined menus in the application model. Menus are displaying properly when the application is launched for the first time, but restarting the application is hiding the menu bar completely and only showing the toolbar.
Why might no menus be displaying when the RCP Application is restored?
This sounds like this bug here that I just recently myself encountered:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=388808
Personally, the workaround in the bug report did not exactly work for me (it may for you however). What did work for me was based off of the final post here by Karl Puperze, (slightly modified):
https://www.eclipse.org/forums/index.php/t/446433/
public class ForceMainMenuProcessor
{
#Execute
public void execute(#Optional MApplication application, #Optional EModelService modelService)
{
MTrimmedWindow window = (MTrimmedWindow)
modelService.find("<id of your main trimmed window>", application);
if (window == null || window.getMainMenu() != null)
{ return; }
final MMenu mainMenu = modelService.createModelElement(MMenu.class);
mainMenu.setElementId("org.eclipse.ui.main.menu");
window.setMainMenu(mainMenu);
}
}
From that, the last steps were to make sure I defined (with no content) a menu in the main e4xmi file that had the org.eclipse.ui.main.menu id, then defined a fragment that contained the menu contents.
To the plugin.xml, added to the org.eclipse.e4.workbench.model extension point a fragment that pointed to the .e4xmi model fragment that was just created and set the 'apply' to always.
Finally, to the same extension point, added a processor and pointed it to the class above. beforefragment was true and apply was always.
The e4xmi files were still used to define the menu, but in code, because of the defined processor above, the menu is forced to appear regardless of whatever persistent state was saved in the workspace. I ended up with this solution after already splitting the menu into a separate model fragment, so I am not sure if that part of the solution is definitely required, but most certainly defining the processor is.

How will I create a new music player screen on run time using reflection?

This is a interview question I was asked a week back, The problem was that
You have to develop a music player which gives the user the ability to create a new theme and use it.
I said that let's say there is a theme class like this
Class theme{
par1;
par2;
par3;
getter,setter
}
When the user tells our player to create a new theme, we will ask for these parameters and by using setter, we can have a theme object with parameters than can be used for rendering.
However he told me that this a java reflection question, he said you are supposed use reflection to get the class name etc.
Does anybody have any idea how reflection can be used here? or Have they ever been asked a question similar to this? Did they use reflection for solving?
I think it might have something to do with Factory design pattern but I can't find similarities between the question given and the design pattern?
What could be meant is class loading - to dynamically load the theme which also includes class loading - you get the needed class by calling Class.forName("org.package.YourTheme") and then create new instance by using reflection. And this instance or a plugin could customize your app.
EDIT:
This is how usually plugins are done in Java application. The developer creates a JAR containing it's implementation. In the simple case scenario he extends an interface in the app:
interface Theme{
void initialize();
void handleEvent(AppEvent e);
}
class MyTheme implements Theme{
public void initialize(){
// init logic
}
public void handleEvent(AppEvent e){
// handles the event
}
}
These two classes are packed by the developer into my-theme-0.1.jar.
A user downloads them on the web and copies them into the themes folder of his app.
When the app starts (or this could also be done during runtime) it scans the themes folder and loads all plugins by using URLClassLoader.
Then you load your theme:
final Class<?> settingsClass = classLoader.loadClass("MyTheme");
Theme theme = (Theme) settingsClass.newInstance();
theme.initialize();
I think one possible the solution could be as follows:
We'll use reflection to get all the attributes, these attributes would then be used to bring the customizable parameters up to the user.
Let's assume this is our theme class:
Class Theme{
//Customizable Theme related attributes
private Color background;
private Color baseColor;
//Business Domain related attributes
private User user;
}
We can use reflection to get the list of attributes Theme::getDeclaredFields, then we can get their names and inform the user about these attributes and "ask" him for what values he wants to customize.
The problem here is our class have attributes that aren't relevant to the theme 'look & feel' fields. For example the theme user field (owner?) shouldn't be brought to the user. This problem can be solved by using annotations (>= JDK 1.5)
Our class would then look like this :
Class Theme{
//Customizable Theme related attributes
#LookAndFeel(alias="Background", description="The background color of the player")
private Color background;
#LookAndFeel(alias="Base Color", description="The base color of the player")
private Color baseColor;
//Business Domain related attributes
private User user;
}
Now using reflection again we can bring up only the Look and feel related attributes (by inspecting the annotations), and we have as a bonus a user-friendly information about the fields : Ex. we will be able to tell the user that there is some "Base Color" parameter (better than using our attribute name "baseColor") that he can adjust.

ZK Custom Validation Style

Is there a way to style the validation message (WrongValueException etc.)
Because it is absolutly not touch friendly.
I searched in the docs of zk but i found nothing.
Currently I use Zk 6.5.2
The validation messages are displayed in an ErrorBox widget.
The z class is z-errbox. Unfortunately, I don't believe ZK gives you a hook to specify a custom z class for any particular ErrorBox. This means the only way to override the style for an ErrorBox is to override the style for all ErrorBoxes. It sounds like that might work for you, if so, you can see the the ZK style class definitions in the source code.
For example, you could override the validation text color with..
.z-errbox-center {
color: blue;
}
As a side note, you'll probably want to hook your custom CSS file into ZK rather than manually loading it on the page. This will ensure it gets loaded as fast as possible and the user won't see the styles change as the page loads.
(in WEB-INF/zk.xml)
<desktop-config>
<theme-uri>/css/style.min.css</theme-uri>
</desktop-config>
You can read more about the theme-uri tag in the documentation.

Categories

Resources