'
' Creado por SharpDevelop.
' Usuario: german
' Fecha: 08/03/2008
' Hora: 12:20 p.m.
' 
' Para cambiar esta plantilla use Herramientas | Opciones | Codificación | Editar Encabezados Estándar
'
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Partial Class frmTabla
	Inherits Form
	
	private mwMapa as AxMapWinGIS.AxMap
	Private mwShapefile As MapWinGIS.Shapefile
	Private m_DataTable As DataTable
	Private Shared pInstancia As frmTabla 'Para manejar el patrón Singleton
		
	Private Sub New(byval Mapa as AxMapWinGIS.AxMap )
		' The Me.InitializeComponent call is required for Windows Forms designer support.
		Me.InitializeComponent()
			
		'
		' TODO : Add constructor code after InitializeComponents
		'
		mwMapa = Mapa
		
	End Sub
	
	
	Public Shared Function ObtenerInstancia(Mapa as AxMapWinGIS.AxMap) As frmTabla 
		
		If pInstancia Is Nothing Then
			pInstancia = new frmTabla(Mapa)	
		End If
		
		Return pInstancia
		
	End Function
	
	
	
	Sub FrmTablaLoad(ByVal sender As Object, ByVal e As EventArgs)
		dim i as integer 
		If mwMapa.NumLayers = 0 Then
			msgbox ("Debes cargar al menos una capa al mapa.",MsgBoxStyle.OkOnly,"Mostrar tabla de atributos")			
		Else
			For i = 0 To mwMapa.NumLayers -1
				cboCapas.Items.Add (mwMapa.get_layerName(i))			
			Next
			'Si solo hay una capa en el mapa, seleccionarla por defecto
			If mwMapa.NumLayers = 1 Then
				cboCapas.SelectedItem = mwMapa.get_LayerName(0)
			end if
		end if
	End Sub
		
	Sub CboCapasSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
		
		mwshapefile = mwmapa.get_GetObject(cboCapas.SelectedIndex)
		PopulateTable()
		
	End Sub
	
	Private Sub PopulateTable()
			
			If mwShapefile Is Nothing Then
				Return
			End If

			' Variables for the OCX
			Dim myField As MapWinGIS.Field

			' Variables related to the C# data table 
			m_DataTable = New DataTable()
			Dim myDataColumn As DataColumn
			Dim myDataRow As DataRow
			For fld As Integer = 0 To mwShapefile.NumFields - 1

				' Add the columns to the table based on the fields
				myDataColumn = New DataColumn()
				myField = mwShapefile.Field(fld)

				' Use the existing field name in order to give the column a name
				myDataColumn.ColumnName = myField.Name

				' Set up columns with different data types depending on the type specified in the shapefile
				If myField.Type = MapWinGIS.FieldType.DOUBLE_FIELD Then
					myDataColumn.DataType = GetType(Double)
				ElseIf myField.Type = MapWinGIS.FieldType.INTEGER_FIELD Then
					myDataColumn.DataType = GetType(Integer)
				Else
					myDataColumn.DataType = GetType(String)
				End If

				' Add each column
				m_DataTable.Columns.Add(myDataColumn)
			Next
			
			For shp As Integer = 0 To mwShapefile.NumShapes - 1

				' Add the rows to the table based on the cell values
				myDataRow = m_DataTable.NewRow()
				For fld As Integer = 0 To mwShapefile.NumFields - 1

					' Each row has information for all the columns.
					myField = mwShapefile.Field(fld)

					' This just explicitly casts from the object values into the correct
					' field type for the Data Grid
					If myField.Type = MapWinGIS.FieldType.DOUBLE_FIELD Then
						If Not TypeOf (mwShapefile.CellValue(fld, shp)) Is DBNull Then 
							myDataRow(fld) = CDbl(mwShapefile.CellValue(fld, shp))
						Else
							'Hacer algo si es nulo
						End If
					ElseIf myField.Type = MapWinGIS.FieldType.INTEGER_FIELD Then
						If Not TypeOf (mwShapefile.CellValue(fld, shp)) Is DBNull Then 
							myDataRow(fld) = CInt(mwShapefile.CellValue(fld, shp))
						Else
							'Hacer algo si es nulo
						End If						
					Else
						If Not TypeOf (mwShapefile.CellValue(fld, shp)) Is DBNull Then 
							myDataRow(fld) = DirectCast(mwShapefile.CellValue(fld, shp), String)
						Else
							myDataRow(fld) = ""
						End If
						
					End If
				Next
				' Once we add the values to a row, add the rows to the table.
				m_DataTable.Rows.Add(myDataRow)
			Next

			' This demonstrates using the table we just created as the data source
			dataGridView1.DataSource = m_DataTable
			
			If mwShapefile.NumShapes=1 Then	lblRegistros.Text="La capa tiene " & mwShapefile.NumShapes & " elemento espacial."
			If mwShapefile.NumShapes>1 Then	lblRegistros.Text="La capa tiene " & mwShapefile.NumShapes & " elementos espaciales."
			
		End Sub

	Sub Button1Click(ByVal sender As Object, ByVal e As EventArgs)
		me.Close()
	End Sub
	
	
	Private Sub frmTabla_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		pInstancia = nothing
	End Sub
		
	
End Class
