<commit_before>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import wx

from robotide import context
from robotide import utils
from robotide.utils import RideEventHandler, RideHtmlWindow
from robotide.widgets import ButtonWithHandler, HorizontalSizer
from robotide.controller.chiefcontroller import ChiefController
from robotide.controller.settingcontrollers import (DocumentationController,
                                                    VariableController,
    TagsController)
from robotide.robotapi import (ResourceFile, TestCaseFile, TestDataDirectory,
                               TestCase, UserKeyword, Variable)

from gridcolorizer import ColorizationSettings
from kweditor import KeywordEditor
from listeditor import ListEditor
from popupwindow import Tooltip
from editordialogs import (EditorDialog, DocumentationDialog, MetadataDialog,
                           ScalarVariableDialog, ListVariableDialog,
                           LibraryDialog, ResourceDialog, VariablesDialog)
from robotide.publish.messages import (RideItemSettingsChanged,
                                       RideItemNameChanged,
                                       RideInitFileRemoved, RideImportSetting)
from robot.parsing.settings import _Setting
from robotide.controller.commands import UpdateVariable
from robotide.publish import PUBLISHER
from robotide.usages.UsageRunner import Usages
from robotide.editor.tags import TagsDisplay


class WelcomePage(RideHtmlWindow):
    undo = cut = copy = paste = delete = comment = uncomment = save \
        = show_content_assist = tree_item_selected = lambda *args: None

    def __init__(self, parent):
        RideHtmlWindow.__init__(self, parent, text=context.ABOUT_RIDE)

    def close(self):
        self.Show(False)

    def destroy(self):
        self.close()
        self.Destroy()


class EditorPanel(wx.Panel):
    """Base class for all editor panels"""
    # TODO: Move outside default editor package, document
    name = doc = ''
    title = None
    undo = cut = copy = paste = delete = comment = uncomment = save \
        = show_content_assist = lambda self: None

    def __init__(self, plugin, parent, controller, tree):
        wx.Panel.__init__(self, parent)
        self.plugin = plugin
        self.controller = controller
        self._tree = tree

    def tree_item_selected(self, item):
        pass


class _RobotTableEditor(EditorPanel):
    name = 'table'
    doc = 'table editor'

    def __init__(self, plugin, parent, controller, tree):
        EditorPanel.__init__(self, plugin, parent, controller, tree)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.SetSizer(self.sizer)
        if self.title:
            self.sizer.Add(self._create_header(self.title),
                           0, wx.EXPAND|wx.ALL, 5)
            self.sizer.Add((0,10))
        self._editors = []
        self.reset_last_show_tooltip()
        self._populate()

    def OnMotion(self, event):
        for editor in self._editors:
            editor.OnMotion(event)
        self.reset_last_show_tooltip()

    def tooltip_allowed(self, editor):
        if wx.GetMouseState().ControlDown() or \
                self._last_shown_tooltip is editor:
            return False
        self._last_shown_tooltip = editor
        return True

    def reset_last_show_tooltip(self):
        self._last_shown_tooltip = None

    def close(self):
        self.Unbind(wx.EVT_MOTION)
        self.Show(False)

    def destroy(self):
        self.close()
        self.Destroy()

    def _create_header(self, text):
        header = wx.StaticText(self, -1, text)
        header.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
        return header

    def _add_settings(self):
        settings = Settings(self)
        settings.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self._collabsible_changed)
        sizeri = wx.BoxSizer(wx.VERTICAL)
        for setting in self.controller.settings:
            editor = self._create_editor_for(settings.GetPane(), setting)
            sizeri.Add(editor, 0, wx.ALL|wx.EXPAND, 2)
            self._editors.append(editor)
        settings.GetPane().SetSizer(sizeri)
        settings.Expand()
        sizeri.SetSizeHints(settings.GetPane())
        self.sizer.Add(settings, 0, wx.ALL|wx.EXPAND, 2)
        self._settings = settings

    def _collabsible_changed(self, event):
        self.GetSizer().Layout()

    def _create_editor_for(self, parent, controller):
        editor_cls = self._get_editor_class(controller)
        return editor_cls(parent, controller, self.plugin, self._tree)

    def _get_editor_class(self, controller):
        if isinstance(controller, DocumentationController):
            return DocumentationEditor
        if isinstance(controller, TagsController):
            return TagsEditor
        return SettingEditor

    def highlight_cell(self, obj, row, column):
        '''Highlight the given object at the given row and column'''
        if obj and isinstance(obj, _Setting):
            setting_editor = self._get_settings_editor(obj)
            if setting_editor and hasattr(setting_editor, "highlight"):
                setting_editor.highlight(column)
        else:
            self.kweditor.select(row, column)

    def _get_settings_editor(self, setting):
        '''Return the settings editor for the given setting object'''
        for child in self.GetChildren():
            if isinstance(child, SettingEditor):
                if child._item == setting:
                    return child
        return None

    def highlight(self, text):
        for editor in self._editors:
            editor.highlight(text)


class Settings(wx.CollapsiblePane):

    def __init__(self, parent):
        wx.CollapsiblePane.__init__(self, parent, wx.ID_ANY, 'Settings')

    def GetPane(self):
        pane = wx.CollapsiblePane.GetPane(self)
        pane.reset_last_show_tooltip = self.GetParent().reset_last_show_tooltip
        pane.tooltip_allowed = self.GetParent().tooltip_allowed
        return pane


class ResourceFileEditor(_RobotTableEditor):

    def tree_item_selected(self, item):
        if isinstance(item, VariableController):
            self._var_editor.select(item.name)

    def _populate(self):
        datafile = self.controller.data
        self.sizer.Add(self._create_header(datafile.name), 0, wx.EXPAND|wx.ALL, 5)
        self.sizer.Add(self._create_source_label(datafile.source), 0, wx.ALL, 1)
        self.sizer.Add((0, 10))
        self._add_settings()
        self._add_import_settings()
        self._add_variable_table()
<commit_msg>editors: Refactor Settings build logic more to Settings and Collapse by default<commit_after>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import wx

from robotide import context
from robotide import utils
from robotide.utils import RideEventHandler, RideHtmlWindow
from robotide.widgets import ButtonWithHandler, HorizontalSizer
from robotide.controller.chiefcontroller import ChiefController
from robotide.controller.settingcontrollers import (DocumentationController,
                                                    VariableController,
    TagsController)
from robotide.robotapi import (ResourceFile, TestCaseFile, TestDataDirectory,
                               TestCase, UserKeyword, Variable)

from gridcolorizer import ColorizationSettings
from kweditor import KeywordEditor
from listeditor import ListEditor
from popupwindow import Tooltip
from editordialogs import (EditorDialog, DocumentationDialog, MetadataDialog,
                           ScalarVariableDialog, ListVariableDialog,
                           LibraryDialog, ResourceDialog, VariablesDialog)
from robotide.publish.messages import (RideItemSettingsChanged,
                                       RideItemNameChanged,
                                       RideInitFileRemoved, RideImportSetting)
from robot.parsing.settings import _Setting
from robotide.controller.commands import UpdateVariable
from robotide.publish import PUBLISHER
from robotide.usages.UsageRunner import Usages
from robotide.editor.tags import TagsDisplay


class WelcomePage(RideHtmlWindow):
    undo = cut = copy = paste = delete = comment = uncomment = save \
        = show_content_assist = tree_item_selected = lambda *args: None

    def __init__(self, parent):
        RideHtmlWindow.__init__(self, parent, text=context.ABOUT_RIDE)

    def close(self):
        self.Show(False)

    def destroy(self):
        self.close()
        self.Destroy()


class EditorPanel(wx.Panel):
    """Base class for all editor panels"""
    # TODO: Move outside default editor package, document
    name = doc = ''
    title = None
    undo = cut = copy = paste = delete = comment = uncomment = save \
        = show_content_assist = lambda self: None

    def __init__(self, plugin, parent, controller, tree):
        wx.Panel.__init__(self, parent)
        self.plugin = plugin
        self.controller = controller
        self._tree = tree

    def tree_item_selected(self, item):
        pass


class _RobotTableEditor(EditorPanel):
    name = 'table'
    doc = 'table editor'

    def __init__(self, plugin, parent, controller, tree):
        EditorPanel.__init__(self, plugin, parent, controller, tree)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.SetSizer(self.sizer)
        if self.title:
            self.sizer.Add(self._create_header(self.title),
                           0, wx.EXPAND|wx.ALL, 5)
            self.sizer.Add((0,10))
        self._editors = []
        self.reset_last_show_tooltip()
        self._populate()

    def OnMotion(self, event):
        for editor in self._editors:
            editor.OnMotion(event)
        self.reset_last_show_tooltip()

    def tooltip_allowed(self, editor):
        if wx.GetMouseState().ControlDown() or \
                self._last_shown_tooltip is editor:
            return False
        self._last_shown_tooltip = editor
        return True

    def reset_last_show_tooltip(self):
        self._last_shown_tooltip = None

    def close(self):
        self.Unbind(wx.EVT_MOTION)
        self.Show(False)

    def destroy(self):
        self.close()
        self.Destroy()

    def _create_header(self, text):
        header = wx.StaticText(self, -1, text)
        header.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
        return header

    def _add_settings(self):
        settings = Settings(self, self.plugin, self._tree)
        settings.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self._collabsible_changed)
        for setting in self.controller.settings:
            editor = settings.create_editor_for(setting)
            settings.add(editor)
            self._editors.append(editor)
        settings.build()
        self.sizer.Add(settings, 0, wx.ALL|wx.EXPAND, 2)
        self._settings = settings

    def _collabsible_changed(self, event):
        self.GetSizer().Layout()

    def highlight_cell(self, obj, row, column):
        '''Highlight the given object at the given row and column'''
        if obj and isinstance(obj, _Setting):
            setting_editor = self._get_settings_editor(obj)
            if setting_editor and hasattr(setting_editor, "highlight"):
                setting_editor.highlight(column)
        else:
            self.kweditor.select(row, column)

    def _get_settings_editor(self, setting):
        '''Return the settings editor for the given setting object'''
        for child in self.GetChildren():
            if isinstance(child, SettingEditor):
                if child._item == setting:
                    return child
        return None

    def highlight(self, text):
        for editor in self._editors:
            editor.highlight(text)


class Settings(wx.CollapsiblePane):

    def __init__(self, parent, plugin, tree):
        wx.CollapsiblePane.__init__(self, parent, wx.ID_ANY, 'Settings')
        self._sizer = wx.BoxSizer(wx.VERTICAL)
        self._plugin = plugin
        self._tree = tree

    def GetPane(self):
        pane = wx.CollapsiblePane.GetPane(self)
        pane.reset_last_show_tooltip = self.GetParent().reset_last_show_tooltip
        pane.tooltip_allowed = self.GetParent().tooltip_allowed
        return pane

    def create_editor_for(self, controller):
        editor_cls = self._get_editor_class(controller)
        return editor_cls(self.GetPane(), controller, self._plugin, self._tree)

    def _get_editor_class(self, controller):
        if isinstance(controller, DocumentationController):
            return DocumentationEditor
        if isinstance(controller, TagsController):
            return TagsEditor
        return SettingEditor

    def add(self, editor):
        self._sizer.Add(editor, 0, wx.ALL|wx.EXPAND, 2)

    def build(self):
        self.GetPane().SetSizer(self._sizer)
        self.Collapse()
        self._sizer.SetSizeHints(self.GetPane())


class ResourceFileEditor(_RobotTableEditor):

    def tree_item_selected(self, item):
        if isinstance(item, VariableController):
            self._var_editor.select(item.name)

    def _populate(self):
        datafile = self.controller.data
        self.sizer.Add(self._create_header(datafile.name), 0, wx.EXPAND|wx.ALL, 5)
        self.sizer.Add(self._create_source_label(datafile.source), 0, wx.ALL, 1)
        self.sizer.Add((0, 10))
        self._add_settings()
        self._add_import_settings()
        self._add_variable_table()

{'fraction_non_alphanumeric': 0.08378970427163199, 'fraction_numerical': 0.004244249726177437, 'mean_word_length': 2.714467327739639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2206074', 'n_tokens_mistral': 4125, 'n_tokens_neox': 3957, 'n_words': 1036}