License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

/*
 * ProjectEditorView.fx
 *
 * Created on Apr 28, 2009, 8:42:43 PM
 */

package projectmanager;

import javafx.scene.control.*;
import javafx.scene.Group;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * Allows the user to edit project details such as name and description.
 * User can also delete the project that does not have any tasks.
 */
public class ProjectEditorView extends AppView {
    
    public var projCat: ProjectModel on replace {
        if (projCat != null) {
            initializeProject(true);
        } else {
            initializeProject(false)
        }
    };

    public function initializeProject(isCatNotNull: Boolean) {
        if (isCatNotNull) {
            nameTextBox.text = projCat.name;
            notesTextBox.text = projCat.description;
        } else {
            nameTextBox.text = "";
            notesTextBox.text = "";
        }
    }


    protected override function deleteData() {
        if( projCat != null ) {
            var success = dataHandler.deleteProjectCategory(projCat);
            if (success) {
                controller.showProjectDetails();
            } else {
                showMessage("Delete Unsuccessful. Some tasks may already exist for this project.");
            }
        }
    }

    var buttonWidth = (screenWidth - 10) / 3.0;
    
    var nameLabel: Label = Label {
        text: "Name"
        textFill: Color.WHITE
    }

    var nameTextBox: TextBox = TextBox {
        columns: 25
        text: if(projCat != null) then projCat.name else ""
        layoutInfo: LayoutInfo {
            hfill:false
            width:220
        }
        onKeyPressed: function (ke: KeyEvent) {
            if (ke.code == KeyCode.VK_DOWN) {
                notesTextBox.requestFocus();
            } else if (ke.code == KeyCode.VK_UP) {
                doneButton.requestFocus();
            }
        }
    }    
    var notesTextBox: TextBox = TextBox {
        columns: 25
        text: if(projCat != null) then projCat.description else ""
        layoutInfo: LayoutInfo {
            hfill:false
            width:220
        }
        onKeyPressed: function (ke: KeyEvent) {
            if (ke.code == KeyCode.VK_DOWN) {
                if (deleteButton.visible) {
                    deleteButton.requestFocus();
                } else {
                    cancelButton.requestFocus();
                }
            } else if (ke.code == KeyCode.VK_UP) {
                nameTextBox.requestFocus();
            }
        }
    }        
    var notesLabel: Label = Label {
        text: "Notes"
        textFill: Color.WHITE
    }
    var cancelButton: Button = Button {
        text: "Cancel"
        layoutInfo: LayoutInfo { width: bind buttonWidth }
        action: function() {
            controller.showProjectDetails();
        }

        onKeyPressed: function (ke: KeyEvent) {
            if (ke.code == KeyCode.VK_DOWN) {
                doneButton.requestFocus();
            } else if (ke.code == KeyCode.VK_UP) {
                if (deleteButton.visible) {
                    deleteButton.requestFocus();
                } else {
                    notesTextBox.requestFocus();
                }
            }
        }
    }

    

    var deleteButton: Button = Button {
        text: "Delete"
        layoutInfo: LayoutInfo { width: bind buttonWidth }
        visible: bind if (projCat == null) false else true
        action: function() {
            deleteConfirmation(projCat.name);
        }

        onKeyPressed: function (ke: KeyEvent) {
            if (ke.code == KeyCode.VK_DOWN) {
                cancelButton.requestFocus();
            } else if (ke.code == KeyCode.VK_UP) {
                notesTextBox.requestFocus();
            }
        }
    }

    var doneButton: Button = Button {
        text: "Done"
        layoutInfo: LayoutInfo { width: bind buttonWidth }

        onKeyPressed: function (ke: KeyEvent) {
            if (ke.code == KeyCode.VK_DOWN) {
                nameTextBox.requestFocus();
            } else if (ke.code == KeyCode.VK_UP) {
                cancelButton.requestFocus();
            }
        }

        action: function() {
            if (nameTextBox.text == null or nameTextBox.text == "") {
                showMessage("Please enter a project name.");
                return;
            }
           /*
            if (notesTextBox.text == null or notesTextBox.text == "") {
                showMessage("Please enter a project notes.");
                return;
            }
            */
            var pCat: ProjectModel = ProjectModel {
                    name: nameTextBox.rawText
                    description: notesTextBox.rawText
                }
            if( projCat != null ) {
                pCat.key = projCat.key;
                dataHandler.updateProjectCategory(pCat);
            } else {
                dataHandler.addProjectCategory(pCat);
            }
            controller.showProjectDetails();
        }
    }

    var headingText: Label = Label {
        font: Font {
            name: "Bitstream Vera Sans Bold"
            size: 14
        }
        text: bind if(projCat != null) then projCat.name else
        "New Project"
        translateX: bind (screenWidth - headingText.boundsInLocal.width) / 2.0
        translateY: 10
        textFill: Color.WHITE
    }

    var nameVbox: VBox = VBox {
        content: [nameLabel, nameTextBox]
        spacing: 5
    }

    var noteVbox:VBox = VBox {
        content: [notesLabel, notesTextBox]
        spacing: 5
    }

    var nameNoteVBox: VBox = VBox {
        translateY: bind headingText.boundsInLocal.height + 20
        translateX: 10
        content: [nameVbox, noteVbox]
        spacing: bind screenHeight/21.3333
    }


    var buttonBox: HBox = HBox {
        translateX: 4
        translateY: bind (screenHeight - buttonBox.boundsInLocal.height - 7)
        content: [deleteButton, cancelButton, doneButton]
        spacing: 2
    }

   
    protected override function createView(): Void {
        defControl = nameTextBox;
        view = Group {
            content: [headingText, nameNoteVBox, buttonBox]
        }
    }
}