rendered paste bodypackage org.hc.gui.gtk;import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;import org.gnu.glade.LibGlade;import org.gnu.gtk.Entry;import org.gnu.gtk.Gtk;import org.gnu.gtk.TreeIter;import org.gnu.gtk.TreeView;import org.gnu.gtk.event.GtkEvent;import org.gnu.gtk.event.KeyEvent;import org.gnu.gtk.event.TreeViewEvent;import org.hc.Command;import org.hc.CommandExecutionException;import org.hc.Debug;import org.hc.HumanCommander;import org.hc.Debug.DebugType;import org.hc.db.DBManager;/* * FIXME align the image to te right and set a green background *//** * The Gtk interface * * @author Antonio Riva * @author Fabio Marini */public class GtkGui extends Thread { /** * A reference to the graphic library */ private LibGlade libglade; /** * The reference to the commands updater */ private GtkCommandsUpdater gcu; /** * The query to execute */ private String query = ""; /** * the entry to the query field */ @SuppressWarnings("unused") private Entry queryField; /** * the entry to the command field */ private Entry commandField; /** * The entry to the command list field */ /* * http://java-gnome.sourceforge.net/cgi-bin/bin/view/Main/TreeViewTutorial */ private TreeView queryResult; /** * A reference to a HumanCommander */ private HumanCommander hc; /** * The thread controller flag */ private boolean runFlag = true; /** * The path to the glade file for the UI load */ private static final String GLADE_FILE = "./glade/progetto1.glade"; /** * Default constructor * * @throws IOException */ public GtkGui() throws IOException { this.libglade = new LibGlade(GLADE_FILE, this); } /** * Start a query * * @param event */ public void on_query_button_released(GtkEvent event) { Entry queryField = (Entry) this.libglade.getWidget("query_field"); Debug.printMessage("Query: " + queryField.getText(),DebugType.HC_GTK_GUI); this.query = queryField.getText(); // Entry entry1 = (Entry) this.libglade.getWidget("entry2"); // entry1.setText(entry.getText()); } /** * Store a query in the human commander db * * @param event */ public void on_command_insert_button_released(GtkEvent event) { this.hc.store(queryField.getText(), commandField.getText()); Debug.printMessage("Query: " + queryField.getText(),DebugType.HC_GTK_GUI); this.query = queryField.getText(); // Entry entry1 = (Entry) this.libglade.getWidget("entry2"); // entry1.setText(entry.getText()); }/** * A selected row is used as command * @param event */ public void on_query_result_row_selected(TreeViewEvent event){ TreeIter ti=event.getTreeIter(); this.commandField.setText(this.gcu.getCommandFromList(ti)); this.commandField.setBooleanProperty("has_focus",true); } /** * Close the program * * @param event */ public void on_human_commander_delete_event(GtkEvent event) { Debug.printMessage("Closing HumanCommander Gtk Interface",DebugType.HC_GTK_GUI); this.gcu.setRunFlag(false); this.runFlag = false; Gtk.mainQuit(); DBManager dbm = DBManager.getInstance(); try { dbm.disconnect(); } catch (SQLException e) { Debug.printError(e.getMessage(),DebugType.DATABASE); } } /** * Test of the key pressed is a ENTER key and launch the command store * @param event the gtk event handler */ public void on_command_field_key_release_event(KeyEvent event) { if(event.getKeyval()==65293){ on_command_insert_button_released(event); } } /** * Execute button wrapper * @param event * */ public void on_execute_button_released(GtkEvent event){ try { String cmd =this.gcu.getCandidateCommand(); Command.execute (cmd); Debug.printMessage("executing: " + cmd ,DebugType.HC_GTK_GUI); } catch (CommandExecutionException e) { System.out.println(e.getMessage()); } } /** * Test of the key pressed is a ENTER key and launch the query * @param event the gtk event handler */ public void on_query_field_key_release_event(KeyEvent event) { if(event.getKeyval()==65293){ on_query_button_released(event); } } /** * Update the command list un the gtk gui */ private void updateCommandsList() { /* * FIXME this method is unused */ Debug.printMessage("Trying for 10 seconds",DebugType.HC_GTK_GUI); int seconds = 10; try { while (seconds > 0) { Debug.printMessage("Wait " + seconds + " seconds",DebugType.HC_GTK_GUI); seconds--; Thread.sleep(500); ArrayList<Command> commandsList = this.hc.getCommandList(10); if (commandsList != null) { for (Command c : commandsList) { Debug.printMessage("Command: ".concat( c.getCommandString()).concat( " (" + c.getRelevance() + "%)"),DebugType.HC_GTK_GUI); } } else { Debug.printMessage("No command retrived.",DebugType.HC_GTK_GUI); } } } catch (InterruptedException e) { Debug.printError(e.getMessage(),DebugType.HC_GTK_GUI); } this.hc.getCommandList(10); } /** * @see java.lang.Thread#run() */ public void run() { super.run(); String cQuery = ""; while (this.runFlag) { try { Thread.sleep(2000); if (this.query.compareTo(cQuery) != 0) { cQuery = this.query; /* * TODO create the following method */ // this.hc.stopPreviowsQuery(); this.hc.query(cQuery); } //System.out.println("Checking form something"); //ArrayList<Command> lst = this.hc.getCommandList(10); //this.storeData(lst); //this.libglade.getWidget("human_commander"); } catch (InterruptedException e) { Debug.printError(e.getMessage(),DebugType.HC_GTK_GUI); } catch (Exception e) { e.printStackTrace(); } } } /** * Init gui components */ private void init() { this.queryField = (Entry) this.libglade.getWidget("query_field"); this.commandField = (Entry) this.libglade.getWidget("command_field"); this.queryResult = (TreeView) this.libglade.getWidget("query_result"); this.gcu= new GtkCommandsUpdater(this.queryResult,this.hc); } /** * @param args */ public static void main(String[] args) { Debug dbg = Debug.getInstance(); dbg.addDebugType(DebugType.HC_GTK_GUI); dbg.addDebugType(DebugType.HUMAN_COMMANDER); //dbg.addDebugType(DebugType.DATABASE); //dbg.addDebugType(DebugType.HC_ANALYZE); GtkGui gui; Gtk.init(args); try { gui = new GtkGui(); gui.hc = new HumanCommander(); gui.init(); gui.start(); gui.gcu.start(); } catch (IOException e) { Debug.printError(e.getMessage(),DebugType.HC_GTK_GUI); System.exit(-1); } Gtk.main(); } // /** // * A class for the update of the CommandsList // * @author Antonio Riva // * @author Fabio Marini // */ // class CommandsUpdater extends Thread { // /** // * The entry to the command list field // */ // private TreeView queryResult; // /** // * A HumanCommander reference // */ // private HumanCommander hc; // /** // * Default constructor // * @param hc the HumanCommander reference // */ // void CommandUpdater(HumanCommander hc){ // this.hc =hc; // } // // /** // * @see java.lang.Thread#run() // */ // public void run(){ // while(true){ // // // } // } // }}