#  Draco Econometrics
#  Copyright (C) 2006-2008 Approximatrix, LLC
#  http://www.approximatrix.com
#  
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

include Java

import com.approximatrix.draco.application.plugin.VariableSelectionColumn
import com.approximatrix.draco.application.plugin.PluginType
import com.approximatrix.draco.application.plugin.SelectionColumnList

require 'pengine/model/options'

# This require should work if the jlmatrix jar file is on the classpath
require 'jlmatrix'

# Here you need to define your plugin
#register(Plugin::PluginSkeleton) # For example

module Plugin
  
  class PluginSkeleton < java.lang.Object
    
    include com.approximatrix.draco.application.plugin.IRubyPlugin
    
    # Defines the name of this plugin for menus and windows
    @@NAME = "Skeleton"
    
    # Defines the type of plugin
    @@TYPE = PluginType.regression
    
    def initialize(model)
      super()
  	  @model = model
      @selectioncolumns = SelectionColumnList.new
      @options = PluginOptions.new
      
      initialize_columns
      initialize_options
           
    end
    
    def columns
      if @selectioncolumns.length == 0
        self.initialize_columns
      end
      
      return @selectioncolumns
    end
    
    def options
      @options.optionlist
    end
    
    # This method should be overridden and provide
    # all necessary VariableSelectionColumn's that will
    # appear in the GUI
    def initialize_columns
      # simply add the columns here in your override
      #@selectioncolumns.add(PluginSkeleton.createColumn(name, true))

      @selectioncolumns
    end
    
    # This method should be overridden to provide a list
    # of options for this plugin to be displayed in an
    # options dialog in the GUI
    def initialize_options
      # add you options via a simple command
      #@options.add "Ignore Matrix Conditioning", "boolean", "true"
    
      # This option can later be retrieved by its label
      
      @options
    end
    
    # Returns a list of names of selected variables from the specified
    # selection column
    def selections(column)
      if(column.is_a? String)
        @selectioncolumns.size.times do |i|
          return selections(i) if @selectioncolumns.get(i).getTitle == column
        end
      elsif(column.is_a? Fixnum)
        col = @selectioncolumns.get(column)
        ret = []
        col.getSelected().size().times do |i|
          ret << col.getSelected()[i]
        end
        return ret
      end
      return nil
    end
    
    def run      
     
    end
    
    def results
      "Results of the plugin..."
    end
    
    def name
      @@NAME      
    end
    
    def PluginSkeleton.name
      @@NAME
    end
    
    def PluginSkeleton.type
      @@TYPE
    end 
    
    protected
    
    # This convenience method allows for the creation of
    # the VariableSelectionColumn objects for defining
    # the look of the variable selection window in the GUI.
    def PluginSkeleton.createColumn(name, multiSelect, exclusive)
      begin
        VariableSelectionColumn.new(name, multiSelect, exclusive)
      rescue
        nil
      end
    end
    
  end
end
