package org.hc.gui.gtk;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import org.gnu.glade.LibGlade;import org.gnu.gtk.CellRendererText;import org.gnu.gtk.DataColumn;import org.gnu.gtk.DataColumnString;import org.gnu.gtk.Entry;import org.gnu.gtk.Gtk;import org.gnu.gtk.ListStore;import org.gnu.gtk.TreeIter;import org.gnu.gtk.TreePath;import org.gnu.gtk.TreeView;import org.gnu.gtk.TreeViewColumn;import org.gnu.gtk.event.GtkEvent;import org.gnu.gtk.event.KeyEvent;import org.gnu.gtk.event.TreeViewEvent;/** * The Gtk interface * * @author Antonio Riva * @author Fabio Marini */public class TryGtk extends Thread { /** * The place where add commands */ private ListStore listStore; /** * The local list of commands */ ArrayList<String[]> lst = new ArrayList<String[]>(); /** * Data column reference */ private DataColumn[] dataColumns; /** * A reference to the graphic library */ private LibGlade libglade; /** * 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 treeView; /** * 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 TryGtk() 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"); 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.query = queryField.getText(); System.out.println(this.query); // 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.getCommandFromList(ti)); this.commandField.setBooleanProperty("has_focus",true); } /** * Close the program * * @param event */ public void on_human_commander_delete_event(GtkEvent event) { System.out.println("Closing HumanCommander Gtk Interface"); this.setRunFlag(false); this.runFlag = false; Gtk.mainQuit(); } /** * 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){ String cmd =this.getCandidateCommand(); System.out.println("executing: " + cmd); } /** * 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); } } /** * Init gui components */ private void init() { this.queryField = (Entry) this.libglade.getWidget("query_field"); this.commandField = (Entry) this.libglade.getWidget("command_field"); this.treeView = (TreeView) this.libglade.getWidget("query_result"); this.initTreeView(); }/** * return a test command list based on query entry * @return a command list */private ArrayList<String[]> getCommandList(){ ArrayList<String[]> cmd = new ArrayList<String[]>(); cmd.add(new String[]{this.query+"cmd0",this.query+"rel0"}); cmd.add(new String[]{this.query+"cmd1",this.query+"rel1"}); cmd.add(new String[]{this.query+"cmd2",this.query+"rel2"}); cmd.add(new String[]{this.query+"cmd3",this.query+"rel3"}); return cmd;} /** * Check if is there new command avaiable * * @return true if there is new command, false if there isn't */ private boolean isNewListReady() { ArrayList<String[]> lstTemp = this.getCommandList(); if (lstTemp != null) { for (String[] cmd : lstTemp) { if (!this.isCommandInList(cmd)) { return true; } } } return false; } /** * Check if the command is in the local list * * @param cmd * the command to check * @return true if there is the command */ private boolean isCommandInList(String[] cmd) { if (this.lst != null) { for (String[] cmdLocal : this.lst) { /* * check if it's the same command */ if (cmd[0].compareTo(cmdLocal[0])==0) { /* * check if it's the same relevance */ if (cmd[1].compareTo(cmdLocal[1]) == 0) { return true; } } } } return false; } /** * @see java.lang.Thread#run() */ public void run() { super.run(); while (this.runFlag) { try { Thread.sleep(150); if (this.isNewListReady()) { System.out.println("Update commands list."); this.lst = this.getCommandList(); this.storeData(this.lst); } } catch (InterruptedException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("Commands list updater terminated"); } /** * Get te command string of the position given in the list * * @param item * The position of the command in the table * @return The command from the position */ public String getCommandFromList(TreeIter item) { if (item != null) { return this.listStore.getValue(item, (DataColumnString) this.dataColumns[0]).toString(); } return ""; } /** * Set the termination condition of the thread * * @param flag * false to stop the thread */ public void setRunFlag(boolean flag) { this.runFlag = flag; } /** * Return the selected command of the list or the first of the list * * @return The string of the command * */ public String getCandidateCommand() { TreePath[] tp = this.treeView.getSelection().getSelectedRows(); TreeIter item; if (tp.length == 1) { item = this.listStore.getIter(tp[0].toString()); } else { item = this.listStore.getFirstIter(); } return this.listStore.getValue(item, (DataColumnString) this.dataColumns[0]); } /** * Store data in the query_result * * @param lst * the commands to add */ private synchronized void storeData(ArrayList<String[]> lst) { /* * clear treeview */ try { this.clearCommandList(); if (lst != null) { for (Iterator iter = lst.iterator(); iter.hasNext();) { String[] cmd = (String[]) iter.next(); TreeIter it = null; it = this.listStore.appendRow(); this.listStore.setValue(it, (DataColumnString) this.dataColumns[0], cmd[0]); this.listStore.setValue(it, (DataColumnString) this.dataColumns[1],cmd[1]); } } else { TreeIter it = null; it = this.listStore.appendRow(); this.listStore.setValue(it, (DataColumnString) this.dataColumns[0], "Nessun comando"); this.listStore.setValue(it, (DataColumnString) this.dataColumns[1], ""); } } catch (Exception e) { e.printStackTrace(); } }/** * Initialize a TreeView */ private void initTreeView(){ this.dataColumns = new DataColumn[2]; this.dataColumns[0] = new DataColumnString(); this.dataColumns[1] = new DataColumnString(); this.listStore = new ListStore(this.dataColumns); this.treeView.setModel(this.listStore); TreeViewColumn col0 = new TreeViewColumn(); CellRendererText render1 = new CellRendererText(); col0.packStart(render1, true); col0.setTitle("Command"); col0.addAttributeMapping(render1, CellRendererText.Attribute.TEXT, this.dataColumns[0]); TreeViewColumn col1 = new TreeViewColumn(); CellRendererText render2 = new CellRendererText(); col1.packStart(render2, true); col1.setTitle("Relevance"); col1.addAttributeMapping(render2, CellRendererText.Attribute.TEXT, this.dataColumns[1]); this.treeView.appendColumn(col0); this.treeView.appendColumn(col1); } /** * Clear all item to the command list * */ public void clearCommandList() { System.out.println("Clearing the command list"); //TreePath[] tp tp= while (true) { TreeIter item = this.listStore.getIter("0"); if (item == null) break; this.listStore.removeRow(item); }// while ((item = this.listStore.getFirstIter()) != null) {// System.out.println("x");// this.listStore.removeRow(item);// System.out.println("x2");// } System.out.println("Clear completed"); } /** * @param args */ public static void main(String[] args) { TryGtk gui; Gtk.init(args); try { gui = new TryGtk(); gui.init(); gui.start(); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } Gtk.main(); }}