Create your own Ant task : Custom Task « Ant « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Ant » Custom TaskScreenshots 
Create your own Ant task

/*
Code revised from 



Cooking with Java XP
by Eric M. Burke and Brian M. Coyner

ISBN: 0-596-00387-0
Pages: 288
*/
<?xml version="1.0"?>
<project name="Ant Task" default="compile" basedir=".">
  <property name="dir.build" value="build"/>
  <property name="dir.dist" value="dist"/>
  <property name="dir.src" value="src"/>

  <path id="classpath.project">
    <pathelement path="${dir.build}"/>
  </path>

  <target name="compile" description="Compile all source code.">
    <javac srcdir="${dir.src}" destdir="${dir.build}">
      <classpath refid="classpath.project"/>
    </javac>
  </target>

  <target name="demoDialogBox" depends="compile">
      <taskdef name="dialogbox"
              classname="DialogBoxTask"
              classpath="${dir.build}"/>

      <dialogbox message="Are you ready?"
                 title="Important Question"
                 property="response"
                 optiontype="yes_no"/>

      <dialogbox message="You entered ${response}!"/>

      <dialogbox title="First response: ${response}">This is a dialog with a multi-line message.</dialogbox>
  </target>
</project>



-------------------------------------------------------------------

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.EnumeratedAttribute;

import javax.swing.*;

public class DialogBoxTask extends Task {
    private String message;
    private String title = "Question";
    private int optionType = -1;
    private String propertyName;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setProperty(String propertyName) {
        this.propertyName = propertyName;
    }

    public void setOptiontype(OptionType ot) {
        log("Calling setOptionType: " + ot.getValue(),
                Project.MSG_DEBUG);

        String value = ot.getValue();
        if ("ok".equals(value)) {
            optionType = -1;
        else if ("ok_cancel".equals(value)) {
            optionType = JOptionPane.OK_CANCEL_OPTION;
        else if ("yes_no".equals(value)) {
            optionType = JOptionPane.YES_NO_OPTION;
        else {
            // only remaining possibility
            optionType = JOptionPane.YES_NO_CANCEL_OPTION;
        }
    }

    public void setMessage(String msg) {
        // ant always replaces properties for attributes
        message = msg;
    }

    public void addText(String msg) {
        if (message == null) {
            message = "";
        }
        // we must manually replace properties for nested text
        message += ProjectHelper.replaceProperties(
                getProject(), msg, getProject().getProperties());
    }

    public void execute() throws BuildException {
        validateAttributes();

        log("optionType = " + optionType, Project.MSG_DEBUG);

        if (optionType == -1) {
            JOptionPane.showMessageDialog(
                    null, // parent
                    message,
                    title,
                    JOptionPane.INFORMATION_MESSAGE);
        else {
            int response = JOptionPane.showConfirmDialog(
                    null, // parent
                    message,
                    title,
                    optionType,
                    JOptionPane.QUESTION_MESSAGE);
            if (propertyName != null) {
                String responseText = formatResponseCode(response);
                log("Setting " + propertyName + " to " + responseText,
                        Project.MSG_VERBOSE);
                getProject().setProperty(propertyName, responseText);
            }
        }
    }

    protected void validateAttributes() {
        if (message == null) {
            throw new BuildException("Message must be specified using the "
                    "message attribute or nested text.");
        }

        if (optionType == -&& propertyName != null) {
            throw new BuildException(
                    "Cannot specify property unless optionType is "
                    "'ok_cancel', 'yes_no', or 'yes_no_cancel'");
        }
    }

    public static class OptionType extends EnumeratedAttribute {
        public String[] getValues() {
            return new String[]{
                "ok",
                "ok_cancel",
                "yes_no",
                "yes_no_cancel",
            };
        }
    }

    private String formatResponseCode(int optionPaneResponse) {
        switch (optionPaneResponse) {
            // note: JOptionPane.OK_OPTION is the same as YES_OPTION
            case JOptionPane.YES_OPTION:
                return "yes";
            case JOptionPane.NO_OPTION:
                return "no";
            case JOptionPane.CANCEL_OPTION:
            case JOptionPane.CLOSED_OPTION:
                return "cancel";
            default:
                throw new BuildException("Internal error: Unknown option " +
                        "pane response: " + optionPaneResponse);
        }
    }
}

           
       
AntYourOwnTask.zip( 4 k)
Related examples in the same category
1. The third-party tasks
2. Ant Write Our Own Task
3. Extend Javadoc Task
4. Life Cycle Task
5. How to use a Class argument in a custom class attribute
w__w__w._j___av__a__2s.__c___o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.