Imported Upstream version 0.18.1.1
[platform/upstream/gettext.git] / gettext-tools / examples / hello-java-qtjambi / Hello.java
1 // Example for use of GNU gettext.
2 // This file is in the public domain.
3 //
4 // Source code of the Java/QtJambi program.
5
6 import java.util.*;
7 import java.io.*;
8 import java.text.*;
9 import com.trolltech.qt.core.*;
10 import com.trolltech.qt.gui.*;
11 import gnu.gettext.*;
12
13 public class Hello {
14   public static void main (String[] args) {
15     ResourceBundle catalog = ResourceBundle.getBundle("hello-java-qtjambi");
16
17     QApplication.initialize(args);
18
19     QMainWindow window = new QMainWindow();
20     window.setWindowTitle("Hello example");
21
22     QWidget panel = new QWidget();
23     QVBoxLayout panelLayout = new QVBoxLayout();
24     panelLayout.setSpacing(2);
25
26     QLabel label1 =
27       new QLabel(GettextResource.gettext(catalog,"Hello, world!"));
28     panelLayout.addWidget(label1);
29
30     QLabel label2 =
31       new QLabel(
32           MessageFormat.format(
33               GettextResource.gettext(catalog,
34                   "This program is running as process number {0}."),
35               new Object[] { getPid() }));
36     panelLayout.addWidget(label2);
37
38     QWidget buttonBar = new QWidget();
39     QHBoxLayout buttonBarLayout = new QHBoxLayout();
40     QWidget filler = new QWidget(); // makes the button right-aligned
41     buttonBarLayout.addWidget(filler);
42     QPushButton button = new QPushButton("OK");
43     button.setMaximumWidth(button.sizeHint().width()+20);
44     button.clicked.connect(window, "close()");
45     buttonBarLayout.addWidget(button);
46     buttonBar.setLayout(buttonBarLayout);
47     panelLayout.addWidget(buttonBar);
48
49     panel.setLayout(panelLayout);
50
51     window.setCentralWidget(panel);
52
53     window.show();
54
55     QApplication.exec();
56   }
57
58   /* Return the process ID of the current process.  */
59   private static String getPid () {
60     try {
61       String[] args = new String[] { "/bin/sh", "-c", "echo $PPID" };
62       Process p = Runtime.getRuntime().exec(args);
63       InputStream p_out = p.getInputStream();
64       String s = (new BufferedReader(new InputStreamReader(p_out))).readLine();
65       p.destroy();
66       if (s != null)
67         return s;
68     } catch (IOException e) {
69       e.printStackTrace();
70     }
71     return "???";
72   }
73 }