From dabb5b153579b76bb74d287c3a3a1647d832b735 Mon Sep 17 00:00:00 2001 From: raquel-ucl Date: Fri, 17 Apr 2015 17:28:51 +0100 Subject: [PATCH] New, Open, Save and Close options work now. Temporary logging functionality added. --- python/nammu/controller/NammuController.py | 153 +++++++++++++++++---- 1 file changed, 125 insertions(+), 28 deletions(-) diff --git a/python/nammu/controller/NammuController.py b/python/nammu/controller/NammuController.py index 3d89da08..6ac338f6 100644 --- a/python/nammu/controller/NammuController.py +++ b/python/nammu/controller/NammuController.py @@ -8,11 +8,11 @@ @author: raquel-ucl ''' -from javax.swing import JFileChooser +from javax.swing import JFileChooser, JOptionPane from javax.swing.filechooser import FileNameExtensionFilter from java.io import FileWriter, IOException from java.lang import System, Integer -import codecs +import codecs, time from ..view.NammuView import NammuView from MenuController import MenuController @@ -31,20 +31,38 @@ def __init__(self): 2. Create main view that'll bind all the components 3. Create event/action handlers - EventBus? ''' + #Create this controller first since it's where the log will be displayed + self.consoleController = ConsoleController(self) + + #TODO replace with proper Logging functionality + self.consoleController.addText("NammuController: Creating subcontrollers...") self.menuController = MenuController(self) self.toolbarController = ToolbarController(self) self.atfAreaController = AtfAreaController(self) - self.consoleController = ConsoleController(self) - + + self.consoleController.addText(" OK\n") + + self.consoleController.addText("NammuController: Creating views...") + self.view = NammuView(self) self.view.addMenuBar(self.menuController.view) self.view.addToolBar(self.toolbarController.view) self.view.addAtfArea(self.atfAreaController.view) self.view.addConsole(self.consoleController.view) + self.consoleController.addText(" OK\n") + + self.consoleController.addText("NammuController: Display main view...") + #Display view self.view.display() + + self.consoleController.addText(" OK\n") + + #Save current ATF filename + #TODO: save array with all opened ATFs + self.currentFilename = None #Handle actions - eventBus? @@ -58,7 +76,6 @@ def __init__(self): #subcontroller) def newFile(self): - print "Creating new file from controller" """ 1. Check if current file in text area has unsaved changes 1.1 Prompt user for file saving @@ -66,10 +83,22 @@ def newFile(self): 2. Clear text area 3. See GitHub issue: https://github.com/UCL-RITS/nammu/issues/6 """ + self.consoleController.addText("NammuController: Creating new file...") + + if self.unsavedChanges(): + option = self.promptOptionPane("There are unsaved changes. Save now?") + print option + if option == 0: + self.saveFile() + + self.atfAreaController.clearAtfArea() + + self.currentFilename = None + + self.consoleController.addText(" OK\n") def openFile(self): - print "Opening file from controller" """ 1. Check if current file in text area has unsaved changes 1.1 Prompt user for file saving @@ -77,7 +106,14 @@ def openFile(self): 2. Display browser for user to choose file 3. Load file in text area """ + self.consoleController.addText("NammuController: Opening file...") + if self.unsavedChanges(): + option = self.promptOptionPane("There are unsaved changes. Save now?") + print option + if option == 0: + self.saveFile() + fileChooser = JFileChooser() filter = FileNameExtensionFilter("ATF files", ["atf"]) fileChooser.setFileFilter(filter) @@ -86,14 +122,18 @@ def openFile(self): if status == JFileChooser.APPROVE_OPTION: atfFile = fileChooser.getSelectedFile() - atfText = self.readTextFile(atfFile) + filename = atfFile.getCanonicalPath() + atfText = self.readTextFile(filename) + self.currentFilename = atfFile.getCanonicalPath() self.atfAreaController.setAtfAreaText(atfText) #TODO: Else, prompt user to choose again before closing + + + self.consoleController.addText(" OK\n") - def readTextFile(self, file): - filename = file.getCanonicalPath() + def readTextFile(self, filename): text = codecs.open(filename, encoding='utf-8').read() return text #TODO: Check if selected file is ATF or at least text file! @@ -107,30 +147,33 @@ def readTextFile(self, file): def saveFile(self): - print "Saving file from controller" """ 1. Check if current file has a filename 2. Save current file in destination given by user """ + + self.consoleController.addText("NammuController: Saving file...") + fileChooser = JFileChooser() status = fileChooser.showSaveDialog(self.view) if status == JFileChooser.APPROVE_OPTION: atfFile = fileChooser.getSelectedFile() + filename = atfFile.getCanonicalPath() atfText = self.atfAreaController.getAtfAreaText() - self.writeTextFile(atfFile, atfText) + self.writeTextFile(filename, atfText) #TODO check returned status? + + self.consoleController.addText(" OK\n") + - - def writeTextFile(self, file, text): - filename = file.getCanonicalPath() + def writeTextFile(self, filename, text): f = codecs.open(filename, "w", "utf-8") f.write(text) f.close() #TODO return status? -# f = self.outer._fileChooser.getSelectedFile() # try: # writer = FileWriter(f) # self.outer._editArea.write(writer) # TextComponent write @@ -138,73 +181,123 @@ def writeTextFile(self, file, text): # JOptionPane.showMessageDialog(self.outer, ioex) # System.exit(1) - + def closeFile(self): - print "Closing file from controller" """ 1. Check if file has unsaved changes 2. Clear text area """ + self.consoleController.addText("NammuController: Closing file...") + + if self.unsavedChanges(): + option = self.promptOptionPane("There are unsaved changes. Save now?") + print option + if option == 0: + self.saveFile() + + self.currentFilename = None + + self.consoleController.addText(" OK\n") + def unsavedChanges(self): + ''' + 1. Check of any file is opened + 2. Load contents in text area + 3. Load file content + 4. Check if 2 and 3 differ and return the appropriate value + ''' + if self.currentFilename != None: + savedText = self.readTextFile(self.currentFilename) + nammuText = self.atfAreaController.getAtfAreaText() + + if (savedText != nammuText): + return True + else: + return False + + def promptOptionPane(self, question): + ''' + 1. Show popup with given question text + 2. Give Yes No Cancel options + 3. Return chosen option + ''' + result = JOptionPane.showConfirmDialog( \ + self.view.getContentPane(), question, "Question", \ + JOptionPane.YES_NO_CANCEL_OPTION) + return result; + + def quit(self): - print "Exiting from controller" """ 1. Check if file has unsaved changes 2. Exit """ + self.consoleController.addText("NammuController: Exiting...") + + self.consoleController.addText(" OK\n") + + self.consoleController.addText("Bye! :)") + System.exit(0) def undo(self): - print "Undoing from controller" """ 1. Check if any action happened since application was launched 2. Come back to previous state (handle stack or rever last action) 3. Update state stack Note: Check java's Undoable """ + self.consoleController.addText("NammuController: Undoing last action...") + + self.consoleController.addText(" OK\n") def redo(self): - print "Redoing from controller" """ 1. Check if any action has been undone 2. Handle actions stack and update it """ - + self.consoleController.addText("NammuController: Redoing last undone action...") + + self.consoleController.addText(" OK\n") def copy(self): - print "Copying from controller" """ Note: check if JTextArea already has this functionality """ + self.consoleController.addText("NammuController: Copying selected text...") + self.consoleController.addText(" OK\n") def cut(self): - print "Cutting from controller" """ Note: check if JTextArea already has this functionality """ + self.consoleController.addText("NammuController: Cutting selected text...") + + self.consoleController.addText(" OK\n") def paste(self): - print "Pasting from controller" """ Note: check if JTextArea already has this functionality """ - + self.consoleController.addText("NammuController: Pasting clipboard text...") + + self.consoleController.addText(" OK\n") def validate(self, atfFile): - print "Validating from controller" """ 1. Parse content of text area 2. Any errors parsing? 3. Display OK/NOK message in Console """ - + self.consoleController.addText("NammuController: Validating ATF file...") + self.consoleController.addText(" OK\n") + def lemmatise(self, atfFile): - print "Lemmatising from controller" """ 1. Connect with UPenn DB 2. Send text area content @@ -212,4 +305,8 @@ def lemmatise(self, atfFile): 4. Display response in text area 5. Display OK/NOK message in Console """ + self.consoleController.addText("NammuController: Lemmatising ATF file...") + + self.consoleController.addText(" OK\n") + \ No newline at end of file