Back to index

lightning-sunbird  0.9+nobinonly
PrefsDialog.cs
Go to the documentation of this file.
00001 /* -*- Mode: C#; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Manticore.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Silverstone Interactive.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 namespace Silverstone.Manticore.Browser
00041 {
00042   using System;
00043   using System.Drawing;
00044   using System.Collections;
00045   using System.ComponentModel;
00046   using System.Windows.Forms;
00047 
00048   using System.IO;
00049   using System.Xml;
00050 
00051   using Silverstone.Manticore.Toolkit;
00052   using Silverstone.Manticore.Core;
00053 
00057        public class PrefsDialog : ManticoreDialog
00058        {
00059     private System.Windows.Forms.TreeView treeView1;
00060     private System.Windows.Forms.Button cancelButton;
00061     private System.Windows.Forms.Button okButton;
00065               private System.ComponentModel.Container components = null;
00066 
00067     private Hashtable mNodes = null;
00068     private Hashtable mPanels = null;
00069     private PrefPanel mCurrentPanel = null;
00070 
00071     // LAME find a way to do global application service. 
00072     private Preferences mPrefs = null;
00073 
00074               public PrefsDialog(Form aOpener) : base(aOpener)
00075               {
00076                      //
00077                      // Required for Windows Form Designer support
00078                      //
00079                      InitializeComponent();
00080 
00081       okButton.Click += new EventHandler(OnOK);
00082 
00083       mNodes = new Hashtable();
00084       mPanels = new Hashtable();
00085 
00086       BrowserWindow window = mOpener as BrowserWindow;
00087       mPrefs = ServiceManager.Preferences;
00088 
00089       //
00090       // Initialize all the preference panels.
00091       //
00092       InitializePanels();
00093 
00094       // Add select handler for tree view so that we can
00095       // switch panels.
00096       treeView1.AfterSelect += new TreeViewEventHandler(OnTreeSelect);
00097 
00098       //
00099       // Initialize the category list. 
00100       //
00101       InitializeCategoryList();
00102 
00103       // XXX - eventually we want to remember user state. This will do
00104       //       for now. 
00105       treeView1.ExpandAll();
00106     }
00107 
00108     public void OnOK(object sender, EventArgs e)
00109     {
00110       // Call |Save| on each preferences panel...
00111       IEnumerator panels = mPanels.Values.GetEnumerator();
00112       while (panels.MoveNext()) {
00113         PrefPanel currPanel = panels.Current as PrefPanel;
00114         currPanel.Save();
00115       }
00116 
00117       // ... then flush preferences to disk for safe keepin'.
00118       // XXX not just yet. 
00119       // mPrefs.FlushUserPreferences();
00120     }
00121 
00122     public void OnTreeSelect(Object sender, TreeViewEventArgs e) 
00123     {
00124       TreeNode selectedNode = e.Node;
00125       String panelID = mNodes[selectedNode.GetHashCode()] as String;
00126       PrefPanel newPanel = mPanels[panelID] as PrefPanel;
00127       if (mCurrentPanel != null) 
00128         mCurrentPanel.Visible = false;
00129       if (newPanel != null) {
00130         newPanel.Visible = true;
00131         mCurrentPanel = newPanel;
00132       }
00133       else {
00134         if (selectedNode.FirstNode != null) {
00135           TreeView treeview = sender as TreeView;
00136           treeview.SelectedNode = selectedNode.FirstNode;
00137         }
00138       }
00139     }
00140 
00144     public void InitializePanels() 
00145     {
00146       BrowserDisplayPanel bdp = new BrowserDisplayPanel(this, mOpener, mPrefs);
00147       mPanels.Add("browser-display", bdp);
00148       this.Controls.Add(bdp);
00149     }
00150 
00155     private void InitializeCategoryList() 
00156     {
00157       mNodes = new Hashtable();
00158 
00159       XmlTextReader reader;
00160       reader = new XmlTextReader("browser\\PrefPanels.xml");
00161       
00162       reader.WhitespaceHandling = WhitespaceHandling.None;
00163       reader.MoveToContent();
00164 
00165       CatListRecurse(reader, treeView1);
00166     }
00167 
00168     private void CatListRecurse(XmlTextReader aReader, Object aRootNode) 
00169     {
00170       String inner = aReader.ReadInnerXml();
00171     
00172       NameTable nt = new NameTable();
00173       XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
00174       XmlParserContext ctxt = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
00175       XmlTextReader reader2 = new XmlTextReader(inner, XmlNodeType.Element, ctxt);
00176 
00177       TreeNode node;
00178 
00179       while (reader2.Read()) {
00180         if (reader2.NodeType == XmlNodeType.Element) {
00181           switch (reader2.LocalName) {
00182           case "panel":
00183             // Tree node with children. Retrieve label and id. Label is 
00184             // used for visual presentation, id is hashed against node
00185             // and is used as a key when looking for which panel to 
00186             // load. 
00187             String[] values = new String[2] {"", ""};
00188             String[] names = new String[2] {"label", "id"};
00189             for (int i = 0; i < names.Length; ++i) {
00190               if (reader2.MoveToAttribute(names[i]) &&
00191                 reader2.ReadAttributeValue())
00192                 values[i] = reader2.Value; 
00193               reader2.MoveToElement();
00194             }
00195 
00196             node = new TreeNode(values[0], 0, 0);
00197             if (aRootNode is TreeView) {
00198               TreeView rootView = aRootNode as TreeView;
00199               rootView.Nodes.Add(node);
00200             }
00201             else if (aRootNode is TreeNode) {
00202               TreeNode rootNode = aRootNode as TreeNode;
00203               rootNode.Nodes.Add(node);
00204             }
00205 
00206             mNodes.Add(node.GetHashCode(), values[1]);
00207             CatListRecurse(reader2, node);
00208             break;
00209           }
00210         }
00211       }
00212     }
00213 
00217               protected override void Dispose( bool disposing )
00218               {
00219                      if( disposing )
00220                      {
00221                             if(components != null)
00222                             {
00223                                    components.Dispose();
00224                             }
00225                      }
00226                      base.Dispose( disposing );
00227               }
00228 
00229               #region Windows Form Designer generated code
00230 
00231 
00232 
00233 
00234               private void InitializeComponent()
00235               {
00236       this.cancelButton = new System.Windows.Forms.Button();
00237       this.treeView1 = new System.Windows.Forms.TreeView();
00238       this.okButton = new System.Windows.Forms.Button();
00239       this.SuspendLayout();
00240       // 
00241       // cancelButton
00242       // 
00243       this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
00244       this.cancelButton.Location = new System.Drawing.Point(408, 296);
00245       this.cancelButton.Name = "cancelButton";
00246       this.cancelButton.TabIndex = 2;
00247       this.cancelButton.Text = "Cancel";
00248       this.cancelButton.FlatStyle = FlatStyle.System;
00249       // 
00250       // treeView1
00251       // 
00252       this.treeView1.ImageIndex = -1;
00253       this.treeView1.Location = new System.Drawing.Point(16, 16);
00254       this.treeView1.Name = "treeView1";
00255       this.treeView1.SelectedImageIndex = -1;
00256       this.treeView1.Size = new System.Drawing.Size(136, 264);
00257       this.treeView1.TabIndex = 0;
00258       // 
00259       // okButton
00260       // 
00261       this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
00262       this.okButton.Location = new System.Drawing.Point(328, 296);
00263       this.okButton.Name = "okButton";
00264       this.okButton.TabIndex = 3;
00265       this.okButton.Text = "OK";
00266       this.okButton.FlatStyle = FlatStyle.System;
00267       // 
00268       // PrefsDialog
00269       // 
00270       this.AcceptButton = this.okButton;
00271       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
00272       this.CancelButton = this.cancelButton;
00273       this.ClientSize = new System.Drawing.Size(496, 328);
00274       this.ControlBox = false;
00275       this.Controls.AddRange(new System.Windows.Forms.Control[] {
00276                                                                   this.okButton,
00277                                                                   this.cancelButton,
00278                                                                   this.treeView1});
00279       this.HelpButton = true;
00280       this.Name = "PrefsDialog";
00281       this.ShowInTaskbar = false;
00282       this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
00283       this.Text = "Options";
00284       this.ResumeLayout(false);
00285 
00286     }
00287               #endregion
00288        }
00289 }