package org.hc.gui.gtk;import java.util.ArrayList;import java.util.Iterator;import org.gnu.gtk.CellRendererText;import org.gnu.gtk.DataColumn;import org.gnu.gtk.DataColumnString;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.hc.Command;import org.hc.Debug;import org.hc.HumanCommander;import org.hc.Debug.DebugType;/** * * @author Antonio Riva * @author Fabio Marini */public class GtkCommandsUpdater extends Thread { /** * The place where add commands */ private ListStore listStore; /** * The local list of commands */ ArrayList<Command> lst = new ArrayList<Command>(); /** * The reference to the HumanCommander */ private HumanCommander hc; /** * The thread termination conditions */ private boolean runFlag = true; /** * Data column reference */ private DataColumn[] dataColumns; /** * The tree view reference from the main class */ private TreeView treeView; /** * Default Constructor * * @param tv * The tree view reference from the main class * @param hc * The Human commandere reference * */ public GtkCommandsUpdater(TreeView tv,HumanCommander hc) { super(); this.hc = hc; this.treeView = tv; this.initTreeView(); } /** * Check if is there new command avaiable * * @return true if there is new command, false if there isn't */ private boolean isNewListReady() { ArrayList<Command> lstTemp = this.hc.getCommandList(10); if (lstTemp != null) { for (Command 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(Command cmd) { if (this.lst != null) { for (Command cmdLocal : this.lst) { /* * check if it's the same command */ if (cmd.equals(cmdLocal)) { /* * check if it's the same relevance */ if (cmd.compareTo(cmdLocal) == 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()) { Debug.printMessage("Update commands list.", DebugType.HC_GTK_GUI); this.lst = this.hc.getCommandList(100); this.storeData(this.lst); } } catch (InterruptedException e) { Debug.printError(e.getMessage(), DebugType.HC_GTK_GUI); } catch (Exception e) { e.printStackTrace(); } } Debug.printMessage("Commands list updater terminated", DebugType.HC_GTK_GUI); } /** * 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<Command> lst) { /* * clear treeview */ try { this.clearCommandList(); if (lst != null) { for (Iterator iter = lst.iterator(); iter.hasNext();) { Command cmd = (Command) iter.next(); TreeIter it = null; it = this.listStore.appendRow(); this.listStore.setValue(it, (DataColumnString) this.dataColumns[0], cmd .getCommandString()); this.listStore.setValue(it, (DataColumnString) this.dataColumns[1], String .valueOf(cmd.getRelevance())); } } 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() { Debug.printMessage("Clearing the command list", DebugType.HC_GTK_GUI); //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) {// /*// * FIXME this remove crash after a command execution// */// System.out.println("x");// this.listStore.removeRow(item);// System.out.println("x2");// } Debug.printMessage("Clear completed", DebugType.HC_GTK_GUI); }}