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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.