package org.potyl.gnome.terminal;import java.io.File;import org.gnu.gnome.App;import org.gnu.gnome.Program;import org.gnu.gnome.UIInfo;import org.gnu.gnomevte.Terminal;import org.gnu.gtk.AboutDialog;import org.gnu.gtk.Adjustment;import org.gnu.gtk.Box;import org.gnu.gtk.Gtk;import org.gnu.gtk.HBox;import org.gnu.gtk.ScrollBar;import org.gnu.gtk.VScrollBar;import org.gnu.gtk.event.LifeCycleEvent;import org.gnu.gtk.event.LifeCycleListener;import org.gnu.gtk.event.MenuItemEvent;import org.gnu.gtk.event.MenuItemListener;/** * * @author emmanuel */public class TerminalApplication extends App { /** * The name of the application, user friendly version. */ public static final String APPLICATION_NAME = "JD Shell"; /** * The application code name. */ public static final String APPLICATION_ID = "jterminal"; /** * The application version. */ public static final String APPLICATION_VERSION = "0.1"; /** * A description of the application. */ public static final String APPLICATION_DESCRIPTION = "Java Gnome Terminal"; /** * The application authors. */ private static final String [] APPLICATION_AUTHORS = { "Emmanuel Rodriguez Santiago", }; /** * The terminal widget. */ private final Terminal terminal; /** * Creates a Terminal Application. * @param appname the name of the application. * @param title the title of the application. */ public TerminalApplication(final String appname, final String title) { super(appname, title); // Create the main widgets this.terminal = createTerminalWithLoginShell(); // Add the listeners this.addListeners(); // Create the application menu this.createMenus(); // Add the contents to the application this.packWidgets(); } /** * This method starts the user's current shell in the given terminal. * If the user doesn't have a shell then <code>'/bin/sh'</code> will be used. * * @return a terminal on which the a shell is started. */ private static Terminal createTerminalWithLoginShell() { final Terminal terminal = new Terminal(); // Find the user's shell // FIXME should be using org.gnu.gnome.Util.getUserShell() instead String shell = System.getenv("SHELL"); if (shell == null) { shell = "/bin/sh"; } // Get the shell name - path without the folder (`basename $SHELL`) final String shellName = new File(shell).getName(); // Get the user home folder final String home = System.getProperty("user.home"); // Arguments for a login shell final String [] arguments = { shellName, "-l", }; // Start the shell terminal.forkCommand(shell, arguments, home, false, false, false); return terminal; } /** * This method adds all the necessary listerners. */ private void addListeners() { // Dispose of the application when closed this.addListener( new LifeCycleListener() { public void lifeCycleEvent(LifeCycleEvent event) { // Empty } public boolean lifeCycleQuery(LifeCycleEvent event) { quit(); return false; } } ); // When the terminal's application (the shell) is over the application quits this.terminal.addEventHandler("child-exited", "quit", this); } /** * Called when the application want's to quit. */ private void quit() { Gtk.mainQuit(); } /** * This method packs all the widgets that will be nested in the application. */ private void packWidgets() { final Box terminalScrollBox = new HBox(false, 0); // A vertical scroll bar that is associated with the terminal. final Adjustment adjustment = this.terminal.getAdjustment(); final ScrollBar terminalScrollBar = new VScrollBar(adjustment); terminalScrollBox.packStart(this.terminal, true, true, 0); terminalScrollBox.packEnd(terminalScrollBar, false, false, 0); this.setContent(terminalScrollBox); } private void createMenus() { // The file menu final UIInfo [] fileMenu = { UIInfo.quitItem(this.MENU_FILE_QUIT), // End UIInfo.end(), }; // The edit menu final UIInfo [] editMenu = { UIInfo.copyItem(this.MENU_EDIT_COPY), UIInfo.pasteItem(this.MENU_EDIT_PASTE),// UIInfo.separator(),// // UIInfo.findItem(this),// UIInfo.findAgainItem(this), // End UIInfo.end(), }; // The help menu final UIInfo [] helpMenu = { UIInfo.help(APPLICATION_NAME), UIInfo.aboutItem(this.MENU_HELP_ABOUT), // End UIInfo.end(), }; // The main menu final UIInfo [] mainMenu = { UIInfo.subtree("_File", fileMenu), UIInfo.subtree("_Edit", editMenu), UIInfo.subtree("_Help", helpMenu), // End UIInfo.end(), }; this.createMenus(mainMenu); } /** * Implementation of the File -> Quit action. */ private final MenuItemListener MENU_FILE_QUIT = new MenuItemListener () { public void menuItemEvent(MenuItemEvent event) { quit(); } }; /** * Implementation of the Edit -> Copy action. */ private final MenuItemListener MENU_EDIT_COPY = new MenuItemListener () { public void menuItemEvent(final MenuItemEvent event) { TerminalApplication.this.terminal.copyPrimary(); } }; /** * Implementation of the Edit -> Paste action. */ private final MenuItemListener MENU_EDIT_PASTE = new MenuItemListener () { public void menuItemEvent(final MenuItemEvent event) { TerminalApplication.this.terminal.pastePrimary(); } }; /** * Implementation of the Help -> About action. */ private final MenuItemListener MENU_HELP_ABOUT = new MenuItemListener () { public void menuItemEvent(final MenuItemEvent event) { final AboutDialog about = new AboutDialog(); about.setName(APPLICATION_NAME); about.setVersion(APPLICATION_VERSION); about.setComments(APPLICATION_DESCRIPTION); about.setAuthors(APPLICATION_AUTHORS); about.show(); } }; /** * The main entry point for the JVM. * @param args arguments received by the JVM */ public static void main(String [] args) { // Initialization Program.initGnomeUI(APPLICATION_ID, APPLICATION_VERSION, args); final TerminalApplication application = new TerminalApplication(APPLICATION_ID, APPLICATION_NAME); application.showAll(); Gtk.main(); }}