HubWindow.cs :  » Business-Application » DC » DCSharp » GUI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » DC 
DC » DCSharp » GUI » HubWindow.cs
/*
 * Copyright (C) 2006 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;

using Gtk;
using Glade;
using Mono.Unix;

using DCSharp.Backend.Connections;
using DCSharp.Backend.Managers;
using DCSharp.Backend.Objects;

namespace DCSharp.GUI{
  public class HubWindow : GladeWindow
  {
    private HubManager hubManager;
    private IFavoriteHubManager favoriteHubManager;
    
    private static Widget widget = new Invisible();
    private static Gdk.Pixbuf hubIcon = widget.RenderIcon(Stock.Network,
      IconSize.Menu, null);
    
    private ListStore listStore;
    private ListComparer comparer;
    
    [Widget]
    private Button closeButton;
    
    [Widget]
    private TreeView hubTreeView;

    [Widget]
    private VBox infoBox;
    
    [Widget]
    private Entry nameEntry;
    
    [Widget]
    private Entry addressEntry;
    
    [Widget]
    private Entry descriptionEntry;
    
    [Widget]
    private CheckButton identificationNeededButton;
    
    [Widget]
    private Table identificationTable;
    
    [Widget]
    private Entry usernameEntry;
    
    [Widget]
    private Entry passwordEntry;
    
    [Widget]
    private CheckButton autoConnectCheckButton;
    
    [Widget]
    private Button addHubButton;
    
    [Widget]
    private Button removeHubButton;
    
    [Widget]
    private Button connectButton;
    
    #region Constructor
    
    public HubWindow(HubManager hubManager, IFavoriteHubManager favoriteHubManager,
      Window parent) : base("HubWindow.glade", parent)
    {
      if (hubManager == null)
      {
        throw new ArgumentNullException("hubManager");
      }
      if (favoriteHubManager == null)
      {
        throw new ArgumentNullException("favoriteHubManager");
      }
      this.hubManager = hubManager;
      this.favoriteHubManager = favoriteHubManager;
      
      infoBox.Sensitive = false;
      removeHubButton.Sensitive = false;
      connectButton.Sensitive = false;
      identificationTable.Sensitive = identificationNeededButton.Active;  
      
      // Hub list
      listStore = new ListStore(typeof(object));
      hubTreeView.Model = listStore;
      hubTreeView.Selection.Changed += OnSelectionChanged;
      hubTreeView.RowActivated += OnRowActivated;
      
      TreeViewColumn column = new TreeViewColumn();
      column.Title = Catalog.GetString("Name");
      column.Spacing = 3;
      hubTreeView.AppendColumn(column);
      
      CellRendererPixbuf pixbufRenderer = new CellRendererPixbuf();
      column.PackStart(pixbufRenderer , false);
      column.SetCellDataFunc(pixbufRenderer, SetPixbuf);
      
      CellRendererText textRenderer = new CellRendererText();
      textRenderer.Ellipsize = Pango.EllipsizeMode.End;
      column.PackStart(textRenderer , true);
      column.SetCellDataFunc(textRenderer, SetName);
      
      // Events
      addHubButton.Clicked += OnAddHubButtonClicked;
      removeHubButton.Clicked += OnRemoveHubButtonClicked;
      
      nameEntry.FocusOutEvent += delegate
      {
        if (SelectedHub != null && nameEntry.Text.Length > 0)
        {
          SelectedHub.Name = nameEntry.Text;
          hubTreeView.QueueDraw();
        }
        else if (SelectedHub != null)
        {
          nameEntry.Text = SelectedHub.Name;
        }
      };
      addressEntry.Changed += delegate
      {
        UpdateConnectButton();
      };
      addressEntry.FocusOutEvent += delegate
      {
        if (SelectedHub != null && addressEntry.Text.Length > 0)
          SelectedHub.Hostname = addressEntry.Text;
        else if (SelectedHub != null)
          addressEntry.Text = SelectedHub.Hostname;
      };
      descriptionEntry.FocusOutEvent += delegate
      {
        if (SelectedHub != null)
          SelectedHub.Description = descriptionEntry.Text;
      };      
      usernameEntry.FocusOutEvent += delegate
      {
        if (SelectedHub != null)
          SelectedHub.Nick = usernameEntry.Text;
      };
      passwordEntry.FocusOutEvent += delegate
      {
        if (SelectedHub != null)
          SelectedHub.Password = passwordEntry.Text;
      };
      autoConnectCheckButton.Toggled += delegate
      {
        if (SelectedHub != null)
          SelectedHub.AutoConnect = autoConnectCheckButton.Active;
      };
      
      identificationNeededButton.Toggled += delegate
      {
        if (SelectedHub != null)
        {
          identificationTable.Sensitive = identificationNeededButton.Active;
          
          if (identificationNeededButton.Active)
          {
            SelectedHub.Nick = usernameEntry.Text;
            SelectedHub.Password = passwordEntry.Text;
          }
          else
          {
            SelectedHub.Nick = "";
            SelectedHub.Password = "";
          }
        }
      };    
      
      // Sort the hubs when the list is reordered
      comparer = new ListComparer(listStore);
      listStore.RowDeleted += delegate
      {
        favoriteHubManager.Sort(comparer);
      };
      
      // Events
      connectButton.Clicked += OnConnectButtonClicked;
      closeButton.Clicked += OnCloseButtonClicked;
      
      hubManager.HubStateChanged += OnHubStateChanged;
      hubManager.HubAdded += OnHubCountChanged;
      hubManager.HubRemoved += OnHubCountChanged;
      
      // Initialize
      lock (favoriteHubManager.SyncRoot)
      {
        foreach (FavoriteHubInfo hubInfo in favoriteHubManager.Hubs)
        {
          listStore.AppendValues(hubInfo);
        }
        favoriteHubManager.HubAdded += OnHubAdded;
        favoriteHubManager.HubRemoved += OnHubRemoved;
      }
    }
    
    #endregion
    
    #region Classes
    
    private class ListComparer : IComparer<FavoriteHubInfo>
    {
      private ListStore listStore;
      
      public ListComparer(ListStore listStore)
      {
        this.listStore = listStore;
      }
      
      public int Compare(FavoriteHubInfo a, FavoriteHubInfo b)
      {
        if (a == b)
        {
          return 0;
        }
        
        TreeIter iter;
        if (listStore.GetIterFirst(out iter))
        {
          int row = 0, rowA = 0, rowB = 0;
          do
          {
            object obj = listStore.GetValue(iter, 0);
            if (obj == a)
            {
              rowA = row;
            }
            else if (obj == b)
            {
              rowB = row;
            }
            row++;
          }
          while (listStore.IterNext(ref iter));
          
          return rowA.CompareTo(rowB);
        }
        return 0;
      }
    }
    
    #endregion
    
    #region Properties
    
    private FavoriteHubInfo SelectedHub
    {
      get { return selectedHub; }
    }
    private FavoriteHubInfo selectedHub;
    
    #endregion
    
    #region Methods
    
    public override void Destroy()
    {
      hubManager.HubStateChanged -= OnHubStateChanged;
      hubManager.HubAdded -= OnHubCountChanged;
      hubManager.HubRemoved -= OnHubCountChanged;
      
      favoriteHubManager.HubAdded -= OnHubAdded;
      favoriteHubManager.HubRemoved -= OnHubRemoved;
      
      base.Destroy();
    }
    
    private void Load(FavoriteHubInfo hubInfo)
    {
      removeHubButton.Sensitive = hubInfo != null;
      connectButton.Sensitive = hubInfo != null;
      infoBox.Sensitive = hubInfo != null;
      
      if (hubInfo != null)
      {
        Util.SetText(nameEntry, hubInfo.Name);
        Util.SetText(addressEntry, hubInfo.Hostname);
        Util.SetText(descriptionEntry, hubInfo.Description);
        Util.SetText(usernameEntry, hubInfo.Nick);
        Util.SetText(passwordEntry, hubInfo.Password);
        
        autoConnectCheckButton.Active = hubInfo.AutoConnect;
        identificationNeededButton.Active = !(String.IsNullOrEmpty(hubInfo.Nick) &&
          String.IsNullOrEmpty(hubInfo.Password));
      }
      else
      {
        nameEntry.Text = addressEntry.Text = descriptionEntry.Text = "";
        usernameEntry.Text = passwordEntry.Text = "";
        autoConnectCheckButton.Active = false;
      }
    }
    
    private void UpdateConnectButton()
    {
      HubConnection connection = hubManager.GetHub(addressEntry.Text);
      bool disconnected = connection == null || (connection != null &&
        connection.State == ConnectionState.Disconnected);
      
      connectButton.Label = disconnected ? Stock.Connect : Stock.Disconnect;
    }
    
    private void SetPixbuf(TreeViewColumn column, CellRenderer renderer,
      TreeModel model, TreeIter iter)
    {
      CellRendererPixbuf pixbufRenderer = renderer as CellRendererPixbuf;
      pixbufRenderer.Pixbuf = hubIcon;
    }
    
    private void SetName(TreeViewColumn column, CellRenderer renderer,
      TreeModel model, TreeIter iter)
    {
      CellRendererText textRenderer = renderer as CellRendererText;
      FavoriteHubInfo hubInfo = model.GetValue(iter, 0) as FavoriteHubInfo;
      
      textRenderer.Text = hubInfo.Name;
    }
    
    private void OnSelectionChanged(object obj, EventArgs args)
    {
      TreeIter iter;
      if (hubTreeView.Selection.GetSelected(out iter))
      {
        selectedHub = listStore.GetValue(iter, 0) as FavoriteHubInfo;
      }
      else
      {
        selectedHub = null;
      }
      Load(selectedHub);
    }
    
    private void OnRowActivated(object obj, RowActivatedArgs args)
    {
      TreeIter iter;
      if (listStore.GetIter(out iter, args.Path))
      {
        FavoriteHubInfo hubInfo = (FavoriteHubInfo)listStore.GetValue(iter, 0);
        
        HubConnection hub = Runtime.ConnectToHub(hubInfo);
        GUI.ShowHub(hub, true);
      }
    }
    
    private void OnAddHubButtonClicked(object obj, EventArgs args)
    {
      FavoriteHubInfo hubInfo = new FavoriteHubInfo(
        Catalog.GetString("Hub"), Catalog.GetString("Address"));
      
      favoriteHubManager.Add(hubInfo);
    }
    
    private void OnRemoveHubButtonClicked(object obj, EventArgs args)
    {
      TreeIter iter;
      if (hubTreeView.Selection.GetSelected(out iter))
      {
        FavoriteHubInfo hubInfo = (FavoriteHubInfo)listStore.GetValue(iter, 0);
        favoriteHubManager.Remove(hubInfo);
      }
    }
    
    private void OnConnectButtonClicked(object obj, EventArgs args)
    {
      if (SelectedHub != null)
      {
        HubConnection connection = hubManager.GetHub(addressEntry.Text);
        if (connection != null &&
          connection.State != ConnectionState.Disconnected)
        {
          connection.Disconnect();
        }
        else
        {
          Runtime.ConnectToHub(SelectedHub);
        }
      }
    }
    
    private void OnCloseButtonClicked(object obj, EventArgs args)
    {
      Destroy();
    }
    
    private void OnHubStateChanged(object obj, ConnectionEventArgs args)
    {
      HubConnection connection = (HubConnection)args.Connection;
      
      Application.Invoke(delegate
      {
        if (connection.Hostname == addressEntry.Text ||
          connection.Address == addressEntry.Text)
        {
          UpdateConnectButton();
        }
      });
    }
    
    private void OnHubCountChanged(object obj, EventArgs args)
    {
      Application.Invoke(delegate
      {
        UpdateConnectButton();
      });
    }
    
    private void OnHubAdded(object obj, FavoriteHubEventArgs args)
    {
      Application.Invoke(delegate
      {
        TreeIter iter = listStore.AppendValues(args.FavoriteHubInfo);
        hubTreeView.Selection.SelectIter(iter);
        
        nameEntry.GrabFocus();
      });
    }
    
    private void OnHubRemoved(object obj, FavoriteHubEventArgs args)
    {
      Application.Invoke(delegate
      {
        TreeIter iter;
        if (!GetHubIter(args.FavoriteHubInfo, out iter))
        {
          return;
        }
        
        if (args.FavoriteHubInfo == SelectedHub)
        {
          TreePath path = listStore.GetPath(iter);
          int row = path.Indices[0];
          
          listStore.Remove(ref iter);
          
          // Select the next or previous row
          int rows = hubTreeView.Model.IterNChildren();
          if (row >= rows)
          {
            row = rows - 1;
          }
          if (row >= 0 && listStore.IterNthChild(out iter, row))
          {
            hubTreeView.Selection.SelectIter(iter);
          }
        }
        else
        {
          listStore.Remove(ref iter);
        }
      });
    }
    
    private bool GetHubIter(FavoriteHubInfo hubInfo, out TreeIter retIter)
    {
      TreeIter iter;
      if (listStore.GetIterFirst(out iter))
      {
        do
        {
          FavoriteHubInfo info = (FavoriteHubInfo)listStore.GetValue(iter, 0);
          if (info == hubInfo)
          {
            retIter = iter;
            return true;
          }
        }
        while (listStore.IterNext(ref iter));
      }
      retIter = TreeIter.Zero;
      return false;
    }
    
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.