/*********************************************************************/
/*                                                                   */
/*                     AgentService Framework                        */
/*                       www.agentservice.it                         */
/*                                                                   */
/* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy    */
/* GNU Lesser General Public License                                 */
/*                                                                   */
/* 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,             */
/* version 2.1 of the License.                                       */
/*                                                                   */
/* 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.                                      */
/*                                                                   */
/*********************************************************************/
// ReadANamedCustomProperty.cs
// <copyright>Copyright (c) Microsoft Corporation. All rights reserved. </copyright>
// <summary>This file contains the ReadANamedCustomPropertySample class.</summary>

using System;
using Microsoft.Office.Interop.Visio;
using System.Windows.Forms;

namespace lido.VSVisioEditor
{

    /// <summary>This class demonstrates how to read and display the values of the shape.</summary>
    public class ReadNamedCustomProperty
    {

        /// <summary>This method is the class constructor.</summary>
        public ReadNamedCustomProperty()
        {

            // No initialization is required.
        }

        /// <summary>This method reads a custom property and display its value.</summary>
        /// <param name="customPropertyShape">Shape which has the custom property</param>
        /// <param name="cellName">Name of the custom property to be accessed</param>
        /// <param name="isLocalName">Cell name is a universal or local name</param>
        public void ReadANamedCustomProperty(Shape customPropertyShape, string cellName, bool isLocalName)
        {

            if (customPropertyShape == null || cellName == null)
            {
                return;
            }

            const string CUST_PROP_PREFIX = "Prop.";

            Microsoft.Office.Interop.Visio.Application visioApplication = (Microsoft.Office.Interop.Visio.Application)customPropertyShape.Application;

            string propName;
            Cell customPropertyCell = null;

            try
            {

                // Verify that all incoming string parameters are not of zero 
                // length, except for the ones that have default values as ""
                // and the output parameters.
                if (cellName.Length == 0)
                {

                    throw new System.ArgumentNullException("cellName", "Zero length string input.");
                }

                // Custom properties have a prefix of "Prop".
                propName = CUST_PROP_PREFIX + cellName;

                // If the custom property exists, get the value of the cell from the shape.
                if (isLocalName)
                {
                    if (customPropertyShape.get_CellExists(propName, (short)VisExistsFlags.visExistsAnywhere) != 0)
                    {

                        // If the cell exists, the cell variable gets a valid value
                        customPropertyCell = customPropertyShape.get_Cells(propName);
                    }
                }
                else if (customPropertyShape.get_CellExistsU(propName, (short)VisExistsFlags.visExistsAnywhere) != 0)
                {

                    // If the cell exists, the cell variable gets a valid value
                    customPropertyCell = customPropertyShape.get_CellsU(propName);
                }

                // Only display message boxes if the AlertResponse is 0.
                if (visioApplication.AlertResponse == 0)
                {

                    // Proceed if the property exists.
                    if (customPropertyCell == null)
                    {

                        MessageBox.Show(propName + " was not found.");
                    }
                    else
                    {

                        // Show the value in page units.
                        MessageBox.Show(propName + ".Result in Page Units = " + customPropertyCell.get_ResultStr(VisUnitCodes.visPageUnits));

                        // Show the string value attached to the custom
                        // property.
                        MessageBox.Show(propName + ".ResultStr = " + customPropertyCell.get_ResultStr(VisUnitCodes.visNoCast));

                        // Show the value in inches.
                        MessageBox.Show(propName + ".Result in Inches = " + customPropertyCell.get_Result(VisUnitCodes.visInches));

                        // Show the value using no cast.
                        MessageBox.Show(propName + ".Result using No Cast = " + customPropertyCell.get_Result(VisUnitCodes.visNoCast));

                        // Show the formula attached to the custom property.
                        MessageBox.Show(propName + " FormulaU = " + customPropertyCell.FormulaU);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                throw;
            }
        }

        //modificata da me per fare tornare 1 stringa!! Non CONTROLLA SE IL CAMPO DELLA PROPIETA' Contine qualcosa!!!!

        public string ReadCustomProperty(Shape customPropertyShape, string cellName, bool isLocalName)
        {

            const string CUST_PROP_PREFIX = "Prop.";

            Microsoft.Office.Interop.Visio.Application visioApplication = (Microsoft.Office.Interop.Visio.Application)customPropertyShape.Application;

            string propName;
            Cell customPropertyCell = null;
            propName = CUST_PROP_PREFIX + cellName;
            if (isLocalName)
            {
                if (customPropertyShape.get_CellExists(propName, (short)VisExistsFlags.visExistsAnywhere) != 0)
                {

                        customPropertyCell = customPropertyShape.get_Cells(propName);
                }
             }
             else if (customPropertyShape.get_CellExistsU(propName, (short)VisExistsFlags.visExistsAnywhere) != 0)
                {

                    customPropertyCell = customPropertyShape.get_CellsU(propName);
                }


            return customPropertyCell.get_ResultStr(VisUnitCodes.visNoCast);

        }
    }
}
