Made Connection Sidebar a singleton

This commit is contained in:
Timothy Warren 2012-02-28 10:38:13 -05:00
parent 2fefe6e949
commit 4cd7932f9b
3 changed files with 82 additions and 45 deletions

View File

@ -112,7 +112,7 @@ class Main extends GtkWindow {
$scrolled_win->add(new DataGrid()); $scrolled_win->add(new DataGrid());
// Add the connection sidebar // Add the connection sidebar
$this->connection_sidebar = new Connection_Sidebar(); $this->connection_sidebar =& Connection_Sidebar::get_instance();
// Add the left column to the hpane // Add the left column to the hpane
$hpane->pack1($this->connection_sidebar, FALSE); $hpane->pack1($this->connection_sidebar, FALSE);

View File

@ -14,8 +14,23 @@
class Connection_Sidebar extends GtkVBox { class Connection_Sidebar extends GtkVBox {
protected $settings, $menu; protected $settings, $menu, $treeview, $model;
private static $instance;
public static function &get_instance()
{
if( ! isset(self::$instance))
{
$name = __CLASS__;
self::$instance = new $name();
}
return self::$instance;
}
/**
* Constructor method
*/
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
@ -36,45 +51,55 @@ class Connection_Sidebar extends GtkVBox {
// Treeview to show database connections // Treeview to show database connections
{ {
// Create a Storage object for connection list // Create a Storage object for connection list
$model = new GtkListStore(GObject::TYPE_PHP_VALUE, GObject::TYPE_STRING); $this->model = new GtkListStore(GObject::TYPE_PHP_VALUE, GObject::TYPE_STRING);
// Add the existing connections to the model // Render the treeview
$db_conns = $this->settings->get_dbs(); $this->_render();
if( ! empty($db_conns))
{
foreach($db_conns as $name => $props)
{
$db = $props;
$db->name = $name;
$iter = $model->append(); // Set up context menu event
$model->set($iter, 0, $db); $this->treeview->connect('button-press-event', array(&$this, 'on_button'));
}
}
// Initialize the treeview with the data
$treeview = new GtkTreeView($model);
// Icon column
$cell_renderer = new GtkCellRendererPixbuf();
$treeview->insert_column_with_data_func(0, 'Type', $cell_renderer, array(&$this, 'set_icon'));
// Label column
$cell_renderer = new GtkCellRendererText();
$treeview->insert_column_with_data_func(1, 'Connection name', $cell_renderer, array(&$this, 'set_label'));
$treeview->connect('button-press-event', array(&$this, 'on_button'));
$selection = $treeview->get_selection(); $selection = $this->treeview->get_selection();
$selection->set_mode(GTK::SELECTION_SINGLE); $selection->set_mode(GTK::SELECTION_SINGLE);
} }
$this->pack_start($treeview); $this->pack_start($this->treeview);
$this->pack_start($add_button, FALSE); $this->pack_start($add_button, FALSE);
} }
/**
* Renders the connection sidebar widget
*/
protected function _render()
{
// Add the existing connections to the model
$db_conns = $this->settings->get_dbs();
if( ! empty($db_conns))
{
foreach($db_conns as $name => $props)
{
$db = $props;
$db->name = $name;
$iter = $this->model->append();
$this->model->set($iter, 0, $db);
}
}
// Initialize the treeview with the data
$this->treeview = new GtkTreeView($this->model);
// Icon column
$cell_renderer = new GtkCellRendererPixbuf();
$this->treeview->insert_column_with_data_func(0, 'Type', $cell_renderer, array(&$this, 'set_icon'));
// Label column
$cell_renderer = new GtkCellRendererText();
$this->treeview->insert_column_with_data_func(1, 'Connection name', $cell_renderer, array(&$this, 'set_label'));
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
@ -82,13 +107,13 @@ class Connection_Sidebar extends GtkVBox {
* *
* @param GtkTreeView Column $col * @param GtkTreeView Column $col
* @param GtkCellRenderer $cell * @param GtkCellRenderer $cell
* @param GtkTreeModel $model * @param GtkTreeModel $this->model
* @param GtkTreeIter $iter * @param GtkTreeIter $iter
*/ */
public function set_icon($col, $cell, $model, $iter) public function set_icon($col, $cell, $model, $iter)
{ {
$col->set_reorderable(TRUE); $col->set_reorderable(TRUE);
$info = $model->get_value($iter, 0); $info = $this->model->get_value($iter, 0);
$db_type = strtolower($info->type); $db_type = strtolower($info->type);
$img_file = BASE_DIR."/images/{$db_type}-logo-32.png"; $img_file = BASE_DIR."/images/{$db_type}-logo-32.png";
@ -111,13 +136,13 @@ class Connection_Sidebar extends GtkVBox {
* *
* @param GtkTreeViewColumn $col * @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell * @param GtkCellRenderer $cell
* @param GtkTreeModel $model * @param GtkTreeModel $this->model
* @param GtkTreeIter $iter * @param GtkTreeIter $iter
*/ */
public function set_label($col, $cell, $model, $iter) public function set_label($col, $cell, $model, $iter)
{ {
$col->set_reorderable(TRUE); $col->set_reorderable(TRUE);
$info = $model->get_value($iter, 0); $info = $this->model->get_value($iter, 0);
$cell->set_property('text', $info->name); $cell->set_property('text', $info->name);
} }
@ -148,9 +173,9 @@ class Connection_Sidebar extends GtkVBox {
if($event->button == 3) if($event->button == 3)
{ {
// get the row and column // get the row and column
$path_array = $view->get_path_at_pos($event->x, $event->y); list($path_array, $col, $x, $y)= $view->get_path_at_pos($event->x, $event->y);
$path = $path_array[0][0]; $path = $path_array[0];
$col = $path_array[1]; //$col = $path_array[1];
$col_title = $col->get_title(); $col_title = $col->get_title();
} }
@ -160,12 +185,12 @@ class Connection_Sidebar extends GtkVBox {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Creates a context menu for the selected connection * Creates and displays a context menu for the selected connection
* *
* @param [type] $pos [description] * @param aray $pos
* @param [type] $title [description] * @param string $title
* @param [type] $event [description] * @param object $event
* @return [type] * @return void
*/ */
public function conn_popup_menu($pos, $title, $event) public function conn_popup_menu($pos, $title, $event)
{ {
@ -181,19 +206,28 @@ class Connection_Sidebar extends GtkVBox {
/** /**
* Remove a connection from the connection manager * Remove a connection from the connection manager
*
* @param string $key
* @return void
*/ */
public function remove_connection() public function remove_connection($key)
{ {
$model = $this->treeview->get_model();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Add a connection to the connection manager * Add a connection to the connection manager
*
* @param string $key
* @param array $vals
* @return void
*/ */
public function add_connection() public function add_connection($key, $vals)
{ {
$model = $this->treeview->get_model();
} }

View File

@ -215,6 +215,9 @@ class DB_Info_Widget extends GtkTable {
$this->settings->add_db($this->conn->get_text(), $data); $this->settings->add_db($this->conn->get_text(), $data);
// Pass to connection sidebar to update
// Destroy the parent window // Destroy the parent window
$parent_window =& $this->get_parent_window(); $parent_window =& $this->get_parent_window();