How to use gtk/glade in vala - gtk

I'm trying to make a simple app with glade/gtk/vala. So far I have this:
using Gtk;
class HelloWorldApp : GLib.Object {
const string UI = "test.glade";
public Window main_window;
[CCode (instance_pos = -1)]
public void on_btn_hello_clicked(Button source) {
stdout.printf("Hello, world");
}
construct {
Builder builder = new Builder();
builder.add_from_file(UI);
main_window = builder.get_object("window1") as Window;
main_window.destroy.connect(Gtk.main_quit);
builder.connect_signals(this);
}
}
class HelloWorld : GLib.Object {
public static int main(string[] args) {
Gtk.init (ref args);
HelloWorldApp h = new HelloWorldApp();
h.main_window.show_all();
Gtk.main();
return 0;
}
}
When I run this it outputs:
(helloworld:22641): Gtk-WARNING **: Could not find signal handler 'on_btn_hello_clicked'
but otherwise runs fine apart from the handler not being called
What am I doing wrong?

You are doing one or more of the following three things wrong:
When you compile a program using Gtk.Builder, you have to add --pkg gmodule-2.0 to your valac command line. (link)
When you place your signal handlers inside a class and/or namespace, you have to add the class and/or namespace name to the handler name in Glade, so you should be connecting to hello_world_app_on_btn_hello_clicked. (link)
If you are on Windows, you also have to add [CCode (cname="G_MODULE_EXPORT on_btn_hello_clicked")]. (link)

the live.gnome.org has all those informations, including the Windows quirks: http://live.gnome.org/Vala/GTKSample#Loading_User_Interface_from_XML_File

Related

Vala basics Goo & GTK+

I'm trying to write my first Vala application and I'm desperatly in need for a
jumpstart as I have some VAPI conflict.
The app is bare minimum (like you would expect):
public class RoxyApplication : Gtk.Application {
public RoxyApplication () {
Object (application_id: "roxytesting",
flags: ApplicationFlags.FLAGS_NONE);
}
protected override void activate () {
Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
window.set_default_size (800, 600);
window.title = "Roxy App 0.0.1";
Goo.Canvas canvas = new Goo.Canvas ();
window.add (canvas);
Gtk.Label label = new Gtk.Label ("Roxxy moxxy");
window.add (label);
window.show_all();
}
public static int main (string[] args) {
RoxyApplication roxapp = new RoxyApplication ();
return roxapp.run (args);
}
}
when I'm compiling with:
valac-0.40 -o main --pkg goocanvas --pkg GTK+-3.0 main.vala
or
valac-0.40 -o main --pkg goocanvas main.vala
respectively I get:
3115 errors all in sense of
"gtk+-2.0.vapi:8397.2-8397.42: error: `Gtk' already contains a definition for `tree_get_row_drag_data'
and 2 errors:
main.vala:1.32-1.46: error: The type name `Gtk.Application' could not be found
public class RoxyApplication : Gtk.Application {
Is it even possible to use these libraries together?
Als far as I can get a grasp Goo is extension of GTK+
and not a subset. (or is it still?)
Thanks in advance for your pointers.

Not able to inject CommandService to execute the command manually

I have handler (to create new parts) assigned to particular command, which is defined as below and working fine,
Handler class :
..
#Execute
public void execute(EPartService partService,MApplication application,EModelService modelService) {
MPart part = MBasicFactory.INSTANCE.createPart();
part.setLabel("New Part");
part.setContributionURI("platform:/plugin/com.my.First.app/src/com/my/first/Parts/EditorPart/EmptyPart");
List<MPartStack> stacks = modelService.findElements(application, null, MPartStack.class, null);
stacks.get(2).getChildren().add(part);
partService.showPart(part, PartState.ACTIVATE);
}
#Execute
public void execute() {
//This Hello is getting printed.
System.out.println("Hello ");
}
I have one controller class to control the Fxml Objects (the click event on treeView). I want to execute that command manually from event handler treeview.
Controller Class :
#Inject
org.eclipse.e4.core.commands.ECommandService commandService;
#Inject
org.eclipse.e4.core.commands.EHandlerService service;
..
..
locationTreeView.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getClickCount() == 2 && mouseEvent.getButton().equals(MouseButton.PRIMARY)
&& locationTreeView.getSelectionModel().getSelectedItem().isLeaf()) {
try {
Command command = commandService.getCommand(S_CMD_MY_COMMAND_ID);
if (!command.isDefined())
return;
ParameterizedCommand myCommand = commandService.createCommand(S_CMD_MY_COMMAND_ID, null);
service.activateHandler(S_CMD_MY_COMMAND_ID, new ShowImmobilizerPart());
if (!service.canExecute(myCommand))
return;
service.executeHandler(myCommand);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
Here, Injected CommandService is null so not able to execute this parametric execute method of my handler class.
I believe there is another CommandService (org.eclipse.fx.core.command.CommandService) for e(fx)clipse but that is also comming null. I am using that as below
Controller Class :
#Inject org.eclipse.fx.core.command.CommandService commandService;
..
..
if (commandService.exists(S_CMD_MY_COMMAND_ID) && commandService.canExecute(S_CMD_MY_COMMAND_ID, null)) {
commandService.execute(S_CMD_MY_COMMAND_ID, null);
}
[Edit] :
My aim is to open new Part in Partstack. So in first code snippet you can see how i am opening part in execute(..). Here, Is there any way to get partService , application and modelService using ContextInjectionFactory or IEclipseContext?
Controller Class :
MyHandler myHandler = new MyHandler();
ContextInjectionFactory.inject(myHandler, iEclipseContext);
//myHandler.execute(); This is woroking if i define execute() method in handler Class.
myHandler.execute(partService,application,modelService); // This is not working as i am injecting this arguments at top of class.
Only classes created by Eclipse from descriptions in the e4xmi file are injected.
If you have a class created some other way you can do injection using:
ContextInjectionFactory.inject(object, context);
where object is the class instance to be injected and context is the IEclipseContext to use. Note that this will not inject the Constructor.
You can also use ContextInjectionFactory.make to create a class using injection.

How to use interface MyCssResource in Utility class (GWT/ GWTP)?

I have
public interface MyResource extends ClientBundle{
#NotStrict
#Source("/myResource.css")
MyCssResource css();
}
public interface MyCssResource extends CssResource {
String gridEvenRow();
String gridOddRow();
.... more styling here....
}
in TestView.java
#UiField MyResource res;
#Inject
public TestView(final Binder binder) {
widget = binder.createAndBindUi(this);
res.css().ensureInjected();
}
In TestPresenter.java, I can style Grid without any problem.
for (int i = 1; i < myGrid.getRowCount(); i++) {
if((i%2) == 0){
myGrid.getRowFormatter().addStyleName(i, getView().getRes().css().gridEvenRow());
}
else{
myGrid.getRowFormatter().addStyleName(i, getView().getRes().css().gridOddRow());
}
}
But I don't want to repeat this code every time I initialize a Grid. So I want to put this code into a Utility class so that I can use it by just 1 line of code. Utility.formatGridOddEvenRow(myGrid);
Here is code in Utility
public class Utility {
public static MyResource res;
public Utility(){
res.css().ensureInjected();
}
public static void formatGridOddEvenRow(Grid grid){
for (int i = 1; i < grid.getRowCount(); i++) {
if((i%2) == 0){
grid.getRowFormatter().addStyleName(i, res.css().gridEvenRow());
}
else{
grid.getRowFormatter().addStyleName(i, res.css().gridOddRow());
}
}
}
}
However, it got run-time error [ERROR] - Uncaught exception escaped ? or some kind of error i don't know.
so, How to use interface MyCssResource in Utility class (GWT/ GWTP)?
public static final MyResource res=GWT.create(MyResource.class)
static{res.css().ensureInjected()}
add this code anywhere and use it. if you give all of your code, may be more helpfull.
I Hope constructor is not called can you check place an alert and see is ensureInjected() is called.
As per Java Static method calling doesn't need object creation. so Utility Object is not created so constructor will not be called.
For GWT CssResources it is must ensureInjected() called before applying the css.

I get an error while trying to run a class about a main method not being found in the class

Error: Main method not found in class Menu, please define the main method as:
public static void main(String[] args)
I always get that error and I tried to put that everywhere in the code but it just won't run...I'm using Slick2D for a menu.
Here's my code for the Menu class.
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class Menu {
Image background = null;
Image startGameOption = null;
Image exitOption = null;
float startGameScale = 1;
float exitScale = 1; {
try {
background = new Image("Sprites/MenuBackground.gif");
} catch (SlickException e1) {
e1.printStackTrace();
}
// load the menu images
Image menuOptions;
try {
menuOptions = new Image("Sprites/MenuOptions.gif");
} catch (SlickException e) {
e.printStackTrace();
}
startGameOption = menuOptions.getSubImage(0, 0, 377, 71);
exitOption = menuOptions.getSubImage(0, 71, 377, 71);
}
}
I'm also using Eclipse. I can never make it stop giving that error though..that's the only error that comes up when I try to run the Menu class. I checked everywhere and it doesn't seem to help me get through this error.
Every application that you want to run must have a public static void main(String[] args) method. As written at the time I am viewing this, you don't have any methods at all. You just have a "block" of code.
I'd suggest try to put the public static void main(String[] args) between the float exitScale = 1; and the { on the same line. Without studying the rest of your code too closely, it looks like you will still have fairly broken code, but I think it should get you past the first problem you are having.
And then go back and read chapter 1 of any Java book to review the requirements of a Java program, and what a method is.

error when i run my RMI project

what do i have to do when I have this error:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException
I added the path of the bin in JDK in the properties of "MY computer": this one "C:\Program Files\Java\jdk1.6.0_19\bin"
and I entered to run-cmd-
cd C:\Users\user\Documents\NetBeansProjects\CountRMI\src\countrmi
start rmiregistry
and i run the server, so this error appear
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException
Thank u
Consider looking at the Cajo project. It wraps RMI so you don't have to worry about starting rmi registries and the such.
See the example below from one of the Cajo wiki pages
Duck.java
public interface Duck {
boolean looks();
boolean walks();
boolean talks();
}
DuckServer.java
import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.ItemServer;
public class DuckServer implements Duck {
public boolean looks() {
System.out.println("hi there!");
return true;
}
public boolean walks() {
System.out.println("waddle waddle");
return true;
}
public boolean talks() {
System.out.println("quack quack!");
return true;
}
public static void main(String args[]) throws Exception { // simple unit test
Remote.config(null, 1198, null, 0); // use cajo port 1198
ItemServer.bind(new DuckServer(), "Donald");
System.out.println("duck server running");
}
}
DuckClient.java
import gnu.cajo.utils.extra.TransparentItemProxy;
public class DuckClient { // try out DuckServer
public static void main(String args[]) throws Exception {
Duck duck = (Duck)TransparentItemProxy.getItem(
"//serverHost:1198/Donald",
new Class[] { Duck.class }
);
System.out.println("looks like = " + duck.looks());
System.out.println("walks like = " + duck.walks());
System.out.println("talks like = " + duck.talks());
}
}
The time has passed, but maybe this helps someone. When you put
cd C:\Users\user\Documents\NetBeansProjects\CountRMI\src\countrmi
you are setting de path to sources files, but yo have to set this to the classes files, in this way
cd C:\Users\user\Documents\NetBeansProjects\CountRMI\build\classes
and start rmiregistry, of course
at least, it worked fine for me.