Ejemplo n.º 1
0
 def __init__(self, grammarController):
     self.grammarController = grammarController
     self.idState = str(uuid.uuid4())
     self._view = CreateStateView(self, self.idState)
     self.log = logging.getLogger(__name__)
Ejemplo n.º 2
0
class CreateStateController(object):
    """Manages the creation of a new state"""

    def __init__(self, grammarController):
        self.grammarController = grammarController
        self.idState = str(uuid.uuid4())
        self._view = CreateStateView(self, self.idState)
        self.log = logging.getLogger(__name__)

    @property
    def view(self):
        return self._view

    def displayErrorMessage(self, errorMessage):
        if errorMessage is None:
            self._view.errorImage.hide()
            self._view.errorLabel.set_label("")
            self._view.errorLabel.hide()
        else:
            self._view.errorLabel.set_label(errorMessage)
            self._view.errorLabel.show()
            self._view.errorImage.show()

    def cancelButton_clicked_cb(self, event):
        """Callback executed when the user clicks on the cancel button"""
        self._view.destroy()

    def createButton_clicked_cb(self, event):
        currentProject = self.grammarController.getCurrentProject()

        """callback executed when the user clicks on the create button"""
        initialState = self._view.initialStateCheckButton.get_active()
        stateName = self._view.nameEntry.get_text()

        automata = currentProject.getGrammar().getAutomata()

        errorMessage = None
        # verify initialState is valid
        if not initialState and automata is None:
            errorMessage = _("The first created state must be an initial state")
            self.displayErrorMessage(errorMessage)
            return

        # verify the name of the state is unique
        found = False
        if automata is not None:
            for state in automata.getStates():
                if state.getName() == stateName:
                    found = True
                    break

        if found:
            errorMessage = _("A state already has this name, please specify another one")
            self.displayErrorMessage(errorMessage)
            return

        newState = NormalState(self.idState, stateName)
        if automata is None:
            automata = MMSTD(newState, currentProject.getVocabulary())
            currentProject.getGrammar().setAutomata(automata)

        automata.addState(newState)
        self._view.destroy()
        self.grammarController.restart()

    def run(self):
        self._view.run()