alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.usertasks;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import org.netbeans.modules.tasklist.core.TLUtils;
import org.netbeans.modules.tasklist.core.Task;
import org.netbeans.modules.tasklist.core.TaskNode;
import org.openide.DialogDescriptor;
import org.openide.NotifyDescriptor;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.text.NbDocument;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.NodeAction;
import org.openide.windows.Mode;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.DialogDisplayer;
import org.openide.awt.Mnemonics;
import org.openide.text.CloneableEditor;
import org.openide.text.CloneableEditorSupport;

/**
 * Action which brings up a dialog where you can create
 * a new subtask.
 *
 * @author Tor Norbye
 * @author Trond Norbye
 */
public class NewTaskAction extends NodeAction {

    private static final long serialVersionUID = 1;

    private DialogDescriptor dd;
    private EditTaskPanel panel;
    private Dialog dialog;
    private JButton addAnotherButton;
    
    private UserTask parent;
    private UserTaskList utl;
    private String filename;
    private int line;
    private boolean associate;
    
    protected boolean enable(Node[] node) {
        return node.length == 1 && 
            (node[0] instanceof UserTaskNode || 
            node[0] instanceof UserTaskListNode);
    }

    /**
     * Creates a panel for editing a task
     *
     * @return created panel
     */
    private EditTaskPanel getEditTaskPanel() {
        if (panel == null) {
            panel = new EditTaskPanel(false);
            panel.setPreferredSize(new Dimension(600,500));
        }
        return panel;
    }
    
    /**
     * Returns the "New Task" dialog
     *
     * @return the created dialog
     */
    private Dialog getDialog() {
        if (dialog == null) {
            dialog = DialogDisplayer.getDefault().createDialog(
                getDialogDescriptor());
            dialog.pack();
        }
        return dialog;
    }
    
    /**
     * Creates a dialog descriptor for the "New Subtask" dialog
     *
     * @return created dialog descriptor
     */
    private DialogDescriptor getDialogDescriptor() {
        if (dd == null) {
            dd = new DialogDescriptor(getEditTaskPanel(),
                 NbBundle.getMessage(NewTaskAction.class,
                                     "TITLE_add_todo")); // NOI18N
            dd.setModal(true);
            dd.setHelpCtx(new HelpCtx("NewTask")); // NOI18N
            dd.setMessageType(NotifyDescriptor.PLAIN_MESSAGE);

            // dialog buttons
            addAnotherButton = new JButton();
            Mnemonics.setLocalizedText(addAnotherButton, NbBundle.getMessage(
                NewTaskAction.class, "BTN_AddAnother")); // NOI18N
            dd.setOptions(new Object[] {
                DialogDescriptor.OK_OPTION,
                DialogDescriptor.CANCEL_OPTION,
                addAnotherButton
            });
            dd.setClosingOptions(new Object[] {
                DialogDescriptor.OK_OPTION,
                DialogDescriptor.CANCEL_OPTION
            });
        dd.setButtonListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object src = e.getSource();
                if (src == addAnotherButton) {
                    addAnotherTask();
                }
            }
        });
        }
        return dd;
    }
    
    /**
     * Will be called if the user pressed "Add Another" button
     */
    private void addAnotherTask() {
        UserTask ut = new UserTask("", utl); // NOI18N
        panel.fillObject(ut);

        // See if the user wants to append or prepend
        boolean append = panel.getAppend();
        if (parent != null) {
            parent.addSubtask(ut, append);
        } else {
            utl.addTasks(Collections.singletonList(ut), append);
        }

        ut.updateAnnotation();

        // After the add - view the todo list as well!
        UserTaskView view = (UserTaskView) UserTaskView.getCurrent();
        if (view == null)
            view = UserTaskView.getDefault();
        view.showInMode();
        view.select(ut);
        view.scrollTo(ut);

        ut = new UserTask("", utl); // NOI18N
        if (filename != null) {
            ut.setFilename(filename);
            if (line != 0) {
                ut.setLineNumber(line);
            }
        }
        panel.fillPanel(ut);
        panel.focusSummary();
    }
    
    protected void performAction(Node[] nodes) {
        // Get the current filename and line number so we can initialize
        // the filename:linenumber columns
        
        // First try to get the editor window itself; if you right click
        // on a node in the Todo Window, that node becomes the activated
        // node (which is good - it makes the properties window show the
        // todo item's properties, etc.) but that means that we can't
        // find the editor position via the normal means.
        // So, we go hunting for the topmosteditor tab, and when we find it,
        // ask for its nodes.
        
        // find cursor position
        Object[] cursor = UTUtils.findCursorPosition(nodes);
        if (cursor == null) {
            Node[] editorNodes = UTUtils.getEditorNodes();
            if (editorNodes != null)
                cursor = UTUtils.findCursorPosition(editorNodes);
        }
        if (cursor != null) {
            filename = (String) cursor[0];
            line = ((Integer) cursor[1]).intValue();
        } else {
            filename = null;
            line = 0;
        }
        
        // find parent task
        if (nodes[0] instanceof UserTaskNode) {
            parent = ((UserTaskNode) nodes[0]).getTask();
            utl = parent.getList();
        } else {
            parent = null;
            utl = ((UserTaskListNode) nodes[0]).getUserTaskList();
        }
        
        associate = false;
        
        performTheAction();
    }

    /**
     * Performs the action
     *
     * @param utl user task list. null = default task list
     * @param parentNode default parent; if null the root node will be used
     * @param filename suggested filename
     * @parem line suggested line number (1-based)
     * @param associate if true, set the checkbox for the filename by default (only
     *         makes sense if filename != null)
     */
    public static void performAction(UserTaskList utl, UserTask parent, 
        String filename, int line,  boolean associate) {
        NewTaskAction nta = (NewTaskAction) NewTaskAction.get(NewTaskAction.class);
        nta.filename = filename;
        nta.line = line;
        nta.associate = associate;
        if (utl == null)
            utl = UserTaskList.getDefault();
        nta.utl = utl;
        nta.parent = parent;
        nta.performTheAction();
    }
    
    /**
     * Performs the action
     */
    private void performTheAction() {
        UserTask ut = new UserTask("", utl); // NOI18N
        if (filename != null) {
            ut.setFilename(filename);
            if (line != 0) {
                ut.setLineNumber(line);
            }
        }

        EditTaskPanel panel = getEditTaskPanel();
        panel.fillPanel(ut);
        panel.setAssociatedFilePos(associate);
        panel.focusSummary();
        
        getDialog().show();

        if (getDialogDescriptor().getValue() == NotifyDescriptor.OK_OPTION) {
            panel.fillObject(ut);

            // See if the user wants to append or prepend
            boolean append = panel.getAppend();
            if (parent != null) {
                parent.addSubtask(ut, append);
            } else {
                utl.addTasks(Collections.singletonList(ut), append);
            }

            ut.updateAnnotation();

            // After the add - view the todo list as well!
            UserTaskView view = (UserTaskView) UserTaskView.getCurrent();
            if (view == null)
                view = UserTaskView.getDefault();
            
            assert view != null;
            
            view.showInMode();
            view.select(ut);
            view.scrollTo(ut);
        }
    }
    
    public String getName() {
        return NbBundle.getMessage(NewTaskAction.class, 
            "LBL_NewSubtask"); // NOI18N
    }
    
    protected String iconResource() {
        return "org/netbeans/modules/tasklist/usertasks/newTask.gif"; // NOI18N
    }
    
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
        // If you will provide context help then use:
        // return new HelpCtx (NewTodoItemAction.class);
    }
    
    protected boolean asynchronous() {
        return false;
    }    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.