/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_menu);

    DataService.setSyncSpeed(DataSourceListener.DEFAULT_SYNC_SPPED);

    DataService.setDataSource(tableOrders);
    DataService.setHandler(handler);
    if (tableOrders.getTableSize() != 6) updateTableOrderObject(DataService.getDataSource());

    WaiterOrdersActivity.dishes = tableOrders.getDishes();

    updateBtnColors(true);
  }
  private void updateTableOrderObject(DataSource dataSource) {
    Thread threadSync = new Thread(DataService.updateOnce);

    final DataSource tmpSrc = DataService.getDataSource();
    try {
      DataService.setDataSource(dataSource);
      threadSync.start();
      threadSync.join(SYNCDURATION_DELAY);
      if (threadSync.isAlive()) threadSync.interrupt();
    } catch (InterruptedException ex) {
      Logger.getLogger(WaiterTableActivity.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalThreadStateException ex) {
    } finally {
      DataService.setDataSource(tmpSrc);
    }
  }
  private void updateBtnColors(boolean b) {
    DataSource dataSource = DataService.getDataSource();
    if (!(dataSource instanceof TableDishRelations)) return;
    LinearLayout btnsParent = (LinearLayout) findViewById(R.id.order_btns);
    for (int i = btnsParent.getChildCount(); i > 0; i--) {
      Button btn = (Button) btnsParent.getChildAt(i - 1);
      int tableNr = Integer.parseInt(btn.getText().toString().replace("Bord ", ""));
      if (tableOrders.getDishesFromTable(tableNr - 1).size() > 0) {
        List<TableHasDish> tbl2 = tableOrders.getDishesFromTable(tableNr - 1);
        List<TableHasDish> tbl1 = tableOrdersLocal.getDishesFromTable(tableNr - 1);

        boolean isEqual = tbl1.size() == tbl2.size();
        if (isEqual) {
          for (TableHasDish hd : tbl1) {
            for (TableHasDish hd2 : tbl2) {
              if (hd.getDish().id != hd2.getDish().id) isEqual = false;
            }
          }
        }
        // om tableOrder == dataSource => lagar mat => orange
        if (isEqual) btn.setBackgroundResource(R.drawable.orange_button);
        // annars => kund äter => grön
        else btn.setBackgroundResource(R.drawable.green_button);
      } else btn.setBackgroundResource(R.drawable.default_button);
    }
  }
 @Override
 public void handleMessage(Message msg) {
   Bundle data = msg.getData();
   if (data != null) {
     if (data.containsKey("connectionError")) {
       DataService.handleError(data.getInt("connectionError"));
     }
     if (data.containsKey("dataUpdated")
         && data.getInt("dataUpdated") == DataSourceListener.UPDATE_CALL) {
       updateBtnColors(true);
     }
   }
 }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   DataService.setAutoLoad(true);
 }