/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef _WORKBENCH_PHYSICAL_TABLEFIGURE_IMPL_H_
#define _WORKBENCH_PHYSICAL_TABLEFIGURE_IMPL_H_

#include "wb_config.h"

#include "model_figure_impl.h"
#include "grts/structs.workbench.physical.h"

#include "table_figure.h"


/** Table figure implementation.
 * Implements inner working of db table figures.
 * 
 * Figure contents are updated when the following changes:
 * 
 * <li>figure attributes (color, state etc)
 * <li>table name
 * <li>column list (column added, removed)
 * <li>column name changed
 * <li>column type changed
 * <li>primary key
 * <li>foreign key list (fk added, removed)
 * <li>foreign key columns list (column added, removed)
 * <li>index list (index added, removed)
 * <li>index name changed
 * 
 * @ingroup Figures
 */
class WBPUBLICBACKEND_PUBLIC_FUNC workbench_physical_TableFigure::ImplData : public model_Figure::ImplData
{
  typedef model_Figure::ImplData super;
  
protected:
  wbfig::Table *_figure;

  bool _pending_columns_sync;
  bool _pending_index_sync;
  
  sigc::connection _table_fk_conn;
  sigc::connection _refresh_conn;
  sigc::connection _changed_conn;
  
  std::vector<int> _list_listeners;
  
  virtual bool realize();
  
  virtual void update_options(const std::string &key);

  void sync_columns();
  void sync_indexes();

  void toggle_title(bool expanded, wbfig::Titlebar *sender);

  void member_changed(const std::string &name, const grt::ValueRef &ovalue);
  void table_member_changed(const std::string &name, const grt::ValueRef &ovalue);

  void content_changed(const std::string &where);

  void fk_changed(const db_ForeignKeyRef &fk);

  virtual void set_in_view(bool flag);
  
public:
  ImplData(workbench_physical_TableFigure *self);
  
  void set_table(const db_TableRef &table);
  
  db_ColumnRef get_column_at(mdc::CanvasItem *item);
  db_IndexRef get_index_at(mdc::CanvasItem *item);
  
  void set_column_highlighted(const db_ColumnRef &column, const mdc::Color *color= 0);
  void set_column_unhighlighted(const db_ColumnRef &column);
  
  virtual mdc::CanvasItem* get_canvas_item() const { return _figure; }
  virtual bool is_realizable();

  virtual void unrealize();

private:
  workbench_physical_TableFigure *self() const { return (workbench_physical_TableFigure*)_self; }
};


#endif
