UnitsComboBoxHandler.cs :  » GUI » Paint.net » PaintDotNet » 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 » GUI » Paint.net 
Paint.net » PaintDotNet » UnitsComboBoxHandler.cs
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace PaintDotNet{
    public sealed class UnitsComboBoxHandler
        : IUnitsComboBox
    {
        private ComboBox comboBox;

        [Browsable(false)]
        public ComboBox ComboBox
        {
            get
            {
                return this.comboBox;
            }
        }

        public UnitsComboBoxHandler(ComboBox comboBox)
        {
            this.comboBox = comboBox;
            this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            this.comboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
            ReloadItems();
        }

        private bool lowercase = true;

        private Hashtable unitsToString;
        private Hashtable stringToUnits;

        // maps from MeasurementUnit->bool for whether that item should be in the list or not
        private Hashtable measurementItems;

        private UnitsDisplayType unitsDisplayType = UnitsDisplayType.Plural;

        [DefaultValue(UnitsDisplayType.Plural)]
        public UnitsDisplayType UnitsDisplayType
        {
            get
            {
                return this.unitsDisplayType;
            }

            set
            {
                if (this.unitsDisplayType != value)
                {
                    this.unitsDisplayType = value;
                    ReloadItems();
                }
            }
        }

        [DefaultValue(true)]
        public bool LowercaseStrings
        {
            get
            {
                return this.lowercase;
            }

            set
            {
                if (this.lowercase != value)
                {
                    this.lowercase = value;
                    ReloadItems();
                }
            }
        }

        [DefaultValue(MeasurementUnit.Pixel)]
        public MeasurementUnit Units
        {
            get
            {
                object selected = this.stringToUnits[ComboBox.SelectedItem];
                return (MeasurementUnit)selected;
            }

            set
            {
                object selectMe = this.unitsToString[value];
                ComboBox.SelectedItem = selectMe;
            }
        }

        [Browsable(false)]
        public string UnitsText
        {
            get
            {
                if (ComboBox.SelectedItem == null)
                {
                    return string.Empty;
                }
                else
                {
                    return (string)ComboBox.SelectedItem;
                }
            }
        }

        [DefaultValue(true)]
        public bool PixelsAvailable
        {
            get
            {
                return (bool)this.measurementItems[MeasurementUnit.Pixel];
            }

            set
            {
                if (value != this.PixelsAvailable)
                {
                    if (value)
                    {
                        AddUnit(MeasurementUnit.Pixel);
                    }
                    else
                    {
                        if (this.Units == MeasurementUnit.Pixel)
                        {
                            if (this.InchesAvailable)
                            {
                                this.Units = MeasurementUnit.Inch;
                            }
                            else if (this.CentimetersAvailable)
                            {
                                this.Units = MeasurementUnit.Centimeter;
                            }
                        }

                        RemoveUnit(MeasurementUnit.Pixel);
                    }
                }
            }
        }

        [DefaultValue(true)]
        public bool InchesAvailable
        {
            get
            {
                return (bool)this.measurementItems[MeasurementUnit.Inch];
            }
        }

        [DefaultValue(true)]
        public bool CentimetersAvailable
        {
            get
            {
                return (bool)this.measurementItems[MeasurementUnit.Centimeter];
            }
        }

        public void RemoveUnit(MeasurementUnit removeMe)
        {
            InitMeasurementItems();
            this.measurementItems[removeMe] = false;
            ReloadItems();
        }

        public void AddUnit(MeasurementUnit addMe)
        {
            InitMeasurementItems();
            this.measurementItems[addMe] = true;
            ReloadItems();
        }

        private void InitMeasurementItems()
        {
            if (this.measurementItems == null)
            {
                this.measurementItems = new Hashtable();
                this.measurementItems.Add(MeasurementUnit.Pixel, true);
                this.measurementItems.Add(MeasurementUnit.Centimeter, true);
                this.measurementItems.Add(MeasurementUnit.Inch, true);
            }
        }

        private void ReloadItems()
        {
            string suffix;

            switch (this.unitsDisplayType)
            {
                case UnitsDisplayType.Plural:
                    suffix = ".Plural";
                    break;

                case UnitsDisplayType.Singular:
                    suffix = string.Empty;
                    break;

                case UnitsDisplayType.Ratio:
                    suffix = ".Ratio";
                    break;

                default:
                    throw new InvalidEnumArgumentException("UnitsDisplayType");
            }

            InitMeasurementItems();

            MeasurementUnit oldUnits;

            if (this.unitsToString == null)
            {
                oldUnits = MeasurementUnit.Pixel;
            }
            else
            {
                oldUnits = this.Units;
            }

            ComboBox.Items.Clear();

            string pixelsString = PdnResources.GetString("MeasurementUnit.Pixel" + suffix);
            string inchesString = PdnResources.GetString("MeasurementUnit.Inch" + suffix);
            string centimetersString = PdnResources.GetString("MeasurementUnit.Centimeter" + suffix);

            if (lowercase)
            {
                // TODO: we shouldn't really be using ToLower() here, these should be separately localizable strings

                pixelsString = pixelsString.ToLower();
                inchesString = inchesString.ToLower();
                centimetersString = centimetersString.ToLower();
            }

            this.unitsToString = new Hashtable();
            this.unitsToString.Add(MeasurementUnit.Pixel, pixelsString);
            this.unitsToString.Add(MeasurementUnit.Inch, inchesString);
            this.unitsToString.Add(MeasurementUnit.Centimeter, centimetersString);

            this.stringToUnits = new Hashtable();

            if ((bool)this.measurementItems[MeasurementUnit.Pixel])
            {
                this.stringToUnits.Add(pixelsString, MeasurementUnit.Pixel);
                ComboBox.Items.Add(pixelsString);
            }

            if ((bool)this.measurementItems[MeasurementUnit.Inch])
            {
                this.stringToUnits.Add(inchesString, MeasurementUnit.Inch);
                ComboBox.Items.Add(inchesString);
            }

            if ((bool)this.measurementItems[MeasurementUnit.Centimeter])
            {
                this.stringToUnits.Add(centimetersString, MeasurementUnit.Centimeter);
                ComboBox.Items.Add(centimetersString);
            }

            if (!(bool)this.measurementItems[oldUnits])
            {
                if (ComboBox.Items.Count == 0)
                {
                    ComboBox.SelectedItem = null;
                }
                else
                {
                    ComboBox.SelectedIndex = 0;
                }
            }
            else
            {
                this.Units = oldUnits;
            }
        }

        public event EventHandler UnitsChanged;

        private void OnUnitsChanged()
        {
            if (UnitsChanged != null)
            {
                UnitsChanged(this, EventArgs.Empty);
            }
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.OnUnitsChanged();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.