//==============================================================================
HostFilterComponent::~HostFilterComponent()
{
     DBG ("HostFilterComponent::~HostFilterComponent");

    knownPluginList.removeChangeListener (this);

    // register as listener to transport
    getFilter()->getTransport()->removeChangeListener (this);

    // deregister ouselves from the plugin (in this case the host)
    getFilter()->removeChangeListener (this);
    // getFilter()->removeListenerToParameters (this);

    // save toolbar layout
    Config::getInstance()->toolbarSet = toolbar->toString ();

    // close and free editor window
    closePluginEditorWindows ();

    // clear all childrens and objects
    deleteAndZero (navigator);
    deleteAndZero (toolbar);
    deleteAndZero (factory);
    deleteAndZero (main);
    deleteAndZero (browser);
    deleteAndZero (verticalDividerBar);
    deleteAndZero (horizontalDividerBar);
    deleteAndZero (tooltipWindow);
    deleteAndZero (resizer);

    // clear command manager commands... save keymappings before this point !
    CommandManager* commandManager = CommandManager::getInstance();
    commandManager->setFirstCommandTarget (0);
    commandManager->clearCommands ();
}
//==============================================================================
HostFilterComponent::HostFilterComponent (HostFilterBase* const ownerFilter_)
    : AudioProcessorEditor (ownerFilter_),
      tooltipWindow (0),
      toolbar (0),
      main (0),
      navigator(0),
      browser (0),
      verticalDividerBar (0),
      horizontalDividerBar (0)
{
    DBG ("HostFilterComponent::HostFilterComponent");

    Config* config = Config::getInstance();

    // global look and feel
    lookAndFeel.readColourFromConfig ();
    LookAndFeel::setDefaultLookAndFeel (&lookAndFeel);

    // register tooltip window
    tooltipWindow = 0;
    if (config->showTooltips)
        tooltipWindow = new TooltipWindow (0, 1000);

    // register ourselves with the plugin - it will use its ChangeBroadcaster base
    // class to tell us when something has changed.
    getFilter()->addChangeListener (this);
    // getFilter()->addListenerToParameters (this);

    // add toolbar / main tabbed component / tabbed browser / divider
    factory = new ToolbarMainItemFactory (this);
    
    addAndMakeVisible (toolbar = new Toolbar ());
    addAndMakeVisible (main = new MainTabbedComponent (this));
//    addAndMakeVisible (browser = new BrowserTabbedComponent (this));
//    addAndMakeVisible (navigator = new ViewportNavigator (0));

    addAndMakeVisible (resizer = new ResizableCornerComponent (this, &resizeLimits));
    resizeLimits.setSizeLimits (150, 150, 1280, 1024);

    // And use our item factory to add a set of default icons to it...
    toolbar->setVertical (false);
    if (config->toolbarSet != String::empty)
        toolbar->restoreFromString (*factory, config->toolbarSet);
    else
        toolbar->addDefaultItems (*factory);

    // build layout
    setBrowserVisible (config->showBrowser,
                       config->browserLeft,
                       false);

    // set its size
    int initialWidth = 800, initialHeight = 600;
    setSize (initialWidth, initialHeight);

    // get the command manager
    CommandManager* commandManager = CommandManager::getInstance();
    commandManager->registerAllCommandsForTarget (this);
    commandManager->setFirstCommandTarget (this);

//    commandManager->getKeyMappings()->resetToDefaultMappings();
//    addKeyListener (commandManager->getKeyMappings());

#ifndef JOST_VST_PLUGIN
    addKeyListener (commandManager->getKeyMappings());
#endif

   // beautiful annoying splash icon in your face
   commandManager->invokeDirectly (CommandIDs::appAbout, false);

    // register as listener to transport
    getFilter()->getTransport()->addChangeListener (this);
    
        XmlElement* const savedPluginList = ApplicationProperties::getInstance()
                                          ->getUserSettings()
                                          ->getXmlValue (T("pluginList"));

    if (savedPluginList != 0)
    {
        knownPluginList.recreateFromXml (*savedPluginList);
        delete savedPluginList;
    }

    // add internal VST plugins
    const File deadMansPedalFile (ApplicationProperties::getInstance()->getUserSettings()->getFile().getSiblingFile ("RecentlyCrashedPluginsList"));
#if JUCE_MAC
    File internalPluginFolder = File::getSpecialLocation(File::currentApplicationFile).getChildFile("./Contents/PlugIns"); // nicely hidden inside bundle on mac os x
#else
    File internalPluginFolder = File::getSpecialLocation(File::currentApplicationFile).getChildFile("../Plugins"); // plugin folder alongside app on other platforms.. for now
#endif
    VSTPluginFormat vst;
    PluginDirectoryScanner internalPluginScanner(internalPluginList, vst, FileSearchPath(internalPluginFolder.getFullPathName()), false, deadMansPedalFile);
    while (internalPluginScanner.scanNextFile(true)) {
       // keep looking
    }    

    knownPluginList.addChangeListener (this);
    pluginSortMethod = (KnownPluginList::SortMethod) ApplicationProperties::getInstance()->getUserSettings()
                            ->getIntValue (T("pluginSortMethod"), KnownPluginList::sortByManufacturer);
}