カスタム・タグ・インサイト・エディタの作成

独自のカスタム・タグ・インサイト・エディタを作成して、JSP 1.1および1.2のカスタム・タグ・テクノロジで使用できます。独自のカスタム・タグ・インサイト・エディタを実装する手順は、次のとおりです。

次の手順では、単純なDate Picker Insight Editorの作成方法を説明します。この手順では、ワークスペース、プロジェクト、およびファイルの作成など、基本的なJDeveloperの機能を理解していることを前提としています。

単純なDate Picker Insight EditorのUIを作成するには、次のようにします。

  1. 「ファイル」->「新規」->「General」->「ワークスペース」を選択して「新規のワークスペース」ダイアログを開き、新しいワークスペースと空のプロジェクトを作成します。
  2. 「ファイル」->「新規」->「General」->「Javaクラス」を選択してプロジェクトに新しいクラス・ファイルを作成し、DateInsightという名前を付けます。
  3. プロジェクト・ファイル(.jpr)をダブルクリックして、「プロジェクトの設定」ダイアログを開きます。 ツリーで「構成」を開き、「開発」を開いて「ライブラリ」を選択し、プロジェクトの選択済のライブラリのリストに「JDeveloper」ライブラリを追加します。
  4. DateInsight.javaに、次のコードを追加します。

    package package1;

    import java.util.ArrayList;
    import javax.swing.Icon;
    import javax.swing.JComponent;
    import oracle.ide.addin.Context;
    import oracle.ideimpl.resource.PaletteArb;
    import oracle.jdevimpl.jsp.tags.insight.AttributeEditor;
    import oracle.jdevimpl.jsp.tags.insight.TagCompletion;
    import oracle.jdevimpl.jsp.tags.insight.WebTags;
    import oracle.jdevimpl.jsp.tags.insight.model.WebTag;

    public class DateInsight implements TagCompletion
    {
     private String _value;
     private WebTags _webTags;
     
     /**
     *  Set the context and "active" tag.
     */
     public void setTagInfo( Context context, WebTags webTags )
     {
       _webTags = webTags;
     }
     
     /**
     * Sets String Value
     */
     public void setValue( String value )
     {
      _value = value;
     }
     
     /**
     * Returns String Value
     */
     public String getValue()
     {
       return _value;
     }
     
     /**
     * Returns a GUI.  The AttributeEditor and TagCompletion are used to
     * easily insert attribute values and close the dialog from inside your
     * UI.  However, you can also write your own listener and do this yourself
     * since it is not the most elegant code.
     */
     public JComponent createPanel( AttributeEditor dlg, TagCompletion editor )
     {
       /* To not invoke a dialog, just setIsInvokable to false.
           Ex. dlg.setIsInvokable( false );
           otherwise, just return the UI to display.
           return yourUI();
           Note: You can pass the AttributeEditor and TagCompletion to your UI
           to allow you to insert values into CodeEditor and close the dialog.
           Ex. return new DateUI( dlg, editor );
       */

        return null;
     }
     
     /**
     * Returns a List of Default Values
     * The first name added to the List should be the editor name.
     * Important: If you return an empty ArrayList, insight will not appear.
     * @return ArrayList of values
     */
     public ArrayList getValueList()
     {
       ArrayList list = new ArrayList();

       if( false )
       {
         // To display a UI, you must return an empty list.
       }
       else // return an list of items for insight to display
       {
         // Show your own value list
         list.add("Date Picker");
         list.add("Today");
         list.add("Tomorrow");
         list.add("The Next Day");
       }
       
       return list;
     }
     
     
     /**
     * Retrieves the name of the editor
     * Note: This will be as the title in your UI  */
     public String getName()
     {
       return "Date Chooser";
     }
     
     /**
     * Retrieves Icon to use in code insight
     */
     public Icon getIcon()
     {
       return PaletteArb.getIcon( PaletteArb.JSP_ICON );
     }
     
     /**
     * Specify whether there is an associated editor to use.
     * @return true or false If there is an associated editor.
     */
     public boolean hasEditor()
     {
       return true;
     }

     /**
     * Called whenver a new Tag Library has been added in the Java Code Editor
     */
     public void tagAdded( WebTag tag )
     {
       // Implementation needed.
     }

     /**
     * Call Help for certain Tag.
     */
     public void showTagHelp( WebTag tag )
     {
       // Implementation needed.
     }
     
    }

  5. 「ファイル」->「新規」->「General」->「Javaクラス」を選択して、新しいクラス・ファイルを作成し、DateInsightAddinという名前を付けます。
  6. DateInsightAddin.javaに、次のコードを追加します。

    package package2;

    import oracle.ide.addin.Addin;
    import oracle.jdevimpl.jsp.tags.insight.*;

    public class DateInsightAddin implements Addin
    {
     private final TagInsightManager _mgr = TagInsightManager.getInstance();
     private final DateInsight _dateInsight = new DateInsight();
     
     public DateInsightAddin()
     {
     }

     public void initialize()
     {
       // To register your TagCompletion, you must specify the tag prefix to listen for.
       // Ex. for a simple utilities tag with prefix utils ( <utils:foo use
       _mgr.register( "utils", _dateInsight );
     }

     public void shutdown()
     {
     }

     public float version()
     {
       return 0;
     }

     public float ideVersion()
     {
       return 0;
     }

     public boolean canShutdown()
     {
       return false;
     }
    }


  7. 「プロジェクト」->「再ビルド」を選択し、作業内容をコンパイルして保存します。
  8. プロジェクトをJARファイルとして、<JDEV_HOME\jdev\lib\ext>へデプロイします。「ファイル」->「新規」を選択して「General」を開き、「Deployment Profiles」を選択して「JARファイル - シンプル・アーカイブ」をダブルクリックします。 次に、アプリケーション・ナビゲータで.deployファイルを右クリックし、「JARファイルにデプロイ」を選択します。
  9. JDeveloperを終了して再起動し、変更を有効にします。
  10. タグ・インサイトの内容を確認するには、新しいJSPファイルを作成します。「ファイル」->「新規」を選択して「Web Tier」を開き、「JavaServer Pages(JSP)」を選択して「JSPページ」をダブルクリックします。
  11. Javaコード・エディタで、次のように入力してタグ・インサイトを表示します。

    <utils:foo test="

    getValueListに指定した値のリストが表示されます。リスト・ボックスではなくエディタを表示するには、次のようにします。

  12. JDeveloperに、DatePickerというクラスを作成します。
  13. DatePicker.javaに、次のコードを追加します。

    package package2;

    import java.awt.event.ActionEvent;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import oracle.jdeveloper.layout.XYConstraints;
    import oracle.jdeveloper.layout.XYLayout;
    import oracle.jdevimpl.jsp.tags.insight.AttributeEditor;
    import oracle.jdevimpl.jsp.tags.insight.TagCompletion;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import javax.swing.JLabel;
    import java.awt.event.*;

    /**
     * A Simple Swing-based panel to select a date.  This is truly trivial.
     */
    public class DatePicker extends JPanel implements ActionListener
    {
     private final JTextField _dateFld = new JTextField();
     private final JButton _okBtn = new JButton();
     private final GridBagLayout gridBagLayout1 = new GridBagLayout();
     private final JLabel _dateLbl = new JLabel();

     private AttributeEditor _dlg = null;
     private TagCompletion _editor = null;  

     /**
      * Constructs a new instance.
      */
     public DatePicker( AttributeEditor dlg, TagCompletion editor )
     {
       _dlg = dlg;
       _editor = editor;
       
       try 
       {
         jbInit();
       }
       catch (Exception e)
       {
         e.printStackTrace();
       }
     }

     /**
      *
      */
     private void jbInit() throws Exception
     {
       setLayout( gridBagLayout1 );
       _okBtn.setText("Ok");
       _okBtn.addActionListener( this );
       _dateLbl.setText("Create Date:");
       _dateFld.setText(new java.util.Date().toString());
       this.add(_dateFld, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
       this.add(_okBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
       this.add(_dateLbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
     }
     
     public void actionPerformed( ActionEvent e )
     {
       // Insert Attribute Value into Java Code Editor
       _editor.setValue("<%= new java.util.Date(\""+_dateFld.getText()+"\") %>\"");

       // Close Dialog. 
       _dlg.close();
     }

    }

  14. DateInsight.javaで、次の変更を加えます。

     createPanelメソッドにおいて

       return new DatePicker( dlg, editor );

     getValueListメソッドにおいて

       return new ArrayList();

  15. プロジェクトをコンパイルして保存し、再度デプロイします。
  16. JDeveloperを終了して再起動し、前述のタグ・インサイトに関する手順を繰り返します。

    単純なDate Picker Insight EditorのUIが表示されます。


JDeveloperのJSPカスタム・タグ・ライブラリについて
カスタムJSPタグ・ライブラリの作成
カスタム・タグ・エディタの作成

詳細は、次のURLでJavaServer Pages仕様を参照してください。
http://java.sun.com/products/jsp/index.html

 

Copyright © 1997, 2004, Oracle. All rights reserved.