Life & Technology

Handful lessons in different areas of technology and life in general.

Category Archives: VB.NET

GISMapWindow 4.5: Enhancing Document Launcher Plugin

I was introduced to this Open Source software some time in February of last year when some group of people made a demo on using their software together with this as a bundled product. I was personally amazed not by their custom made product but by the way GISMapWindow application operates. It eases the way and process of working with maps, yes maps, GISMapWindow is according to their website:

MapWindow GIS is an open source GIS mapping application and set of programmable mapping components. Because it is distributed as an open source application under the Mozilla Public License distribution license, MapWindow GIS can be reprogrammed to perform different or more specialized tasks.

And now, after that demo, I’ve been spending the last two weeks fiddling around and getting used to this wonderful application as this is required as part of my recent project which involves integrating maps to our solution. Spending hours reading post on the forum and learning more from them. One post from a member that catch my attention is an enhancement request on the functionality of the existing Document Launcher plugin. He specifically asks if this plugin will support to have an additional toggle button that allows him to choose whether or not to execute the attached document on the specified shape when clicked. The FileOrUrl field execute action will then be bypassed.

So I started to download the source codes for GISMapWindow and adjusting the plugin to grant this request.

Below are the codes added and adjusted to the DocLauncher plugin:

   1: Private Const _btnName As String = "LaunchDocument"
   2: Private Const _toolTip As String = "Enable/Disable Open external document"
   3: Private _bOpenDoc As Boolean
   4:  
   5: <CLSCompliant(False)> _
   6: Public Sub Initialize(ByVal MapWin As MapWindow.Interfaces.IMapWin, ByVal ParentHandle As Integer) Implements MapWindow.Interfaces.IPlugin.Initialize
   7:     'This event is fired when the user loads your plug-in either through the plug-in dialog 
   8:     'box, or by checkmarking it in the plug-ins menu.  This is where you would add buttons to the
   9:     'tool bar or menu items to the menu.  
  10:     '
  11:     'It is also standard to set a global reference to the IMapWin that is passed through here so that
  12:     'you can access it elsewhere in your project to act on MapWindow.
  13:     g_MapWin = MapWin
  14:  
  15:     ' Create a new toolbar and a new button with an icon attached on it
  16:     ' Added 04/01/2008 by Earljon Hidalgo
  17:     Try
  18:         Dim tbr As MapWindow.Interfaces.Toolbar = MapWin.Toolbar
  19:         tbr.AddToolbar(_toolbarName)
  20:  
  21:         Dim btn As MapWindow.Interfaces.ToolbarButton = tbr.AddButton(_btnName, _toolbarName, "", "")
  22:  
  23:         btn.BeginsGroup = True
  24:         ' if we like icon + text to appear, uncomment this
  25:         'btn.Text = "OpenDoc"
  26:         btn.Tooltip = _toolTip
  27:         btn.Category = _toolbarName
  28:         btn.Picture = New System.Drawing.Icon(My.Resources.Resource.AppIcon, New System.Drawing.Size(16, 16))
  29:  
  30:         ' initialized to not selected
  31:         _bOpenDoc = False
  32:         tbr = Nothing
  33:         btn = Nothing
  34:     Catch ex As Exception
  35:         g_MapWin.ShowErrorDialog(ex)
  36:     End Try
  37: End Sub
  38:  
  39: Public Sub Terminate() Implements MapWindow.Interfaces.IPlugin.Terminate
  40:     'This event is fired when the user unloads your plug-in either through the plug-in dialog 
  41:     'box, or by un-checkmarking it in the plug-ins menu.  This is where you would remove any
  42:     'buttons from the tool bar tool bar or menu items from the menu that you may have added.
  43:     'If you don't do this, then you will leave dangling menus and buttons that don't do anything.
  44:  
  45:     ' Remove button and the toolbar if plugin is unloaded
  46:     ' Added 04/01/2008 by Earljon Hidalgo
  47:     g_MapWin.Toolbar.RemoveButton(_btnName)
  48:     g_MapWin.Toolbar.RemoveToolbar(_toolbarName)
  49: End Sub
  50:  
  51: Public Sub ItemClicked(ByVal ItemName As String, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.ItemClicked
  52:     'This event fires when a menu item or toolbar button is clicked.  So if you added a button or menu
  53:     'on the Initialize event, then this is where you would handle it.
  54:  
  55:     ' Check and save the state of the button
  56:     ' Added 04/01/2008 by Earljon Hidalgo
  57:     Dim bToggle As Boolean = IIf(Not _bOpenDoc, True, False)
  58:     Try
  59:         If ItemName = _btnName Then
  60:             g_MapWin.Toolbar.ButtonItem(_btnName).Pressed = bToggle
  61:             _bOpenDoc = bToggle
  62:             Handled = True
  63:         End If
  64:     Catch ex As System.Exception
  65:         g_MapWin.ShowErrorDialog(ex)
  66:     End Try
  67: End Sub
  68:  
  69: <CLSCompliant(False)> _
  70: Public Sub ShapesSelected(ByVal Handle As Integer, ByVal SelectInfo As MapWindow.Interfaces.SelectInfo) Implements MapWindow.Interfaces.IPlugin.ShapesSelected
  71:     'This event fires when the user selects one or more shapes using the select tool in MapWindow. Handle is the 
  72:     'Layer handle for the shapefile on which shapes were selected. SelectInfo holds information abou the 
  73:     'shapes that were selected. 
  74:     Dim i As Integer
  75:     Dim sf As MapWinGIS.Shapefile
  76:     Dim FileOrURL As String = ""
  77:     Dim newProcess As New Process
  78:     Try
  79:         'make sure something was returned in SelectInfo
  80:         If SelectInfo Is Nothing Then Exit Sub
  81:  
  82:         ' Check the state of the button if we need to open external document
  83:         ' or ignore it
  84:         ' Added 04/01/2008 by Earljon Hidalgo
  85:         If Not _bOpenDoc Then Exit Sub
  86:  
  87:         'get the shapefile object on which a shape was selected
  88:         sf = g_MapWin.Layers(Handle).GetObject
  89:         'Check to see if this shapefile has an attribute field "FileOrURL"
  90:         For i = 0 To sf.NumFields - 1
  91:             If LCase(sf.Field(i).Name) = "fileorurl" Then
  92:                 'This shapefile has the needed attribute field.
  93:                 'We will only be launching one document, so if they selected more than one shape
  94:                 'then we will just launch the document associated with the first selected shape.
  95:                 'Also, i is the index of the field that has the FileOrURL that we are going to 
  96:                 'launch.
  97:                 FileOrURL = sf.CellValue(i, SelectInfo(0).ShapeIndex)
  98:                 Exit For
  99:             End If
 100:         Next
 101:         If FileOrURL <> "" Then
 102:             'Use a .NET process() to launch the file or URL
 103:             MapWinUtility.Logger.Dbg("DocLauncher: Launched File or URL: " + FileOrURL)
 104:             newProcess.StartInfo.FileName = FileOrURL
 105:             newProcess.Start()
 106:         End If
 107:     Catch ex As System.Exception
 108:         MapWinUtility.Logger.Msg(ex.Message)
 109:     End Try
 110: End Sub

When compiled produces this output:

GISMapWindow DocLauncher image

This patch already been uploaded to the SVN repository and hoping to see this changes on the next stable release of GISMapWindow.