WebSphere Message Broker バージョン 8.0.0.5 オペレーティング・システム: AIX、HP-Itanium、Linux、Solaris、Windows、z/OS

製品の最新バージョンについては、IBM Integration Bus バージョン 9.0 をご覧ください。

変更通知を扱うためのユーザー定義エディター・コードの例

サンプル Java™ コードを使用して、他のパターン・パラメーターが変更されたときにパラメーターの値を更新するユーザー定義エディターを作成します。

以下のコード例は、他のパターン・パラメーターからの変更通知を扱うユーザー定義エディター・コードの作成方法を示しています。 パターン・パラメーター months に割り当てられたユーザー定義エディターは一年の月のリストを表示し、パターン・ユーザーはチェック・ボックスを使ってその 1 つ以上を選択できます。 2 番目のパターン・パラメーター uncheck month は、一年の月のリストをドロップダウン・リストで表示します。 パターン・ユーザーが uncheck month パラメーターで 1 つの月を選択すると、ユーザー定義エディター・コードは uncheck month から変更通知を受け取り、ユーザー定義エディターの月のリストでその月がクリアされます。

MyEditor クラスは、エディターの機能を制御します。 MyComposite クラスは、SWT ツールキットのメソッドとオブジェクトを使用してエディターの外観を制御します。

この例では、以下の手順が完了されていることを想定しています。
  1. パターン・パラメーター months および uncheck month がユーザー定義パターンに追加されていること。 パターン・パラメーターを追加するには、ユーザー・インターフェースの定義を参照してください。
  2. 一年の月が入った列挙タイプが作成されていること。 この列挙タイプがパターン・パラメーター uncheck month に割り当てられます。 列挙タイプを使用するには、パターン・パラメーターでの列挙値の使用を参照してください。
  3. ユーザー定義エディターを使用するようパターン・パラメーター months が構成されていること (ユーザー・インターフェースの定義を参照)。
  4. 以下の手順を完了することにより、パターン・パラメーター months 用のユーザー定義エディターが構成されていること。 ユーザー定義エディターの構成について、詳しくはユーザー定義エディターの構成を参照してください。
    1. MyEditor クラスが「ユーザー定義エディターの構成」ウィンドウの「クラス名」フィールドに入力されていること。
    2. 「ユーザー定義エディターの構成」ウィンドウの「このユーザー定義エディターに変更通知を送信する可視パラメーターを選択します」フィールドで、パターン・パラメーター uncheck month が選択されていること。
MyEditor クラス
MyEditor クラスは BasePatternPropertyEditor クラスを継承します。 パターン・ユーザーがパターン・インスタンス・エディターを開いたときに MyEditor クラスのインスタンスが自動的に作成されます。
  1. MyEditor クラスの作成後に、configureEditor() メソッドが自動的に呼び出されます。
  2. エディターのユーザー・インターフェースを作成するために、createControls() メソッドが呼び出されます。 MyComposite クラスでコントロールが定義されます。
  3. valueChanged() メソッドの呼び出し後に、isValid() メソッドが自動的に呼び出されます。 この例では、isValid() はパターン・パラメーターの現行値を検査して、値が選択されていない場合はエラー・メッセージを戻します。 エラー・メッセージはパターン・インスタンス・エディターでパターン・ユーザーに対して表示されます。 パラメーター値が有効である場合、メソッドは null を戻します。
  4. MyComposite クラスで setValue()getValue()、および setEnabled() メソッドが次のように定義されます。
    1. setValue() メソッドは、ユーザー定義エディターでの初期値を設定します。
    2. getValue() メソッドは、パターン・パラメーターの現行値をパターン・インスタンス・エディターに戻します。
    3. XPath 式によってパターン・パラメーターが有効化または無効化されるとき、setEnabled() メソッドが呼び出されます。
  5. notifyChanged() メソッドは MyComposite クラスの uncheckMonth() メソッドを呼び出し、uncheck month パラメーターの値を渡します。 なお、notifyChanged() メソッドは更新されたパターン・パラメーターの ID も受け入れますが、この例ではそれが使用されません。
package com.your.company.domain.MyPattern.code;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;

import com.ibm.broker.config.appdev.patterns.ui.BasePatternPropertyEditor;
import com.ibm.broker.config.appdev.patterns.ui.PatternPropertyEditorSite;

public class MyEditor extends BasePatternPropertyEditor {
   private MyComposite composite;
	
   @Override
   public void configureEditor(PatternPropertyEditorSite site, boolean required, String configurationValues) {
      super.configureEditor(site, required, configurationValues);
   }

   @Override
   public void createControls(Object parent) {
      Composite parentComposite = (Composite) parent;
      PatternPropertyEditorSite site = getSite();
      composite = new MyComposite(parentComposite, SWT.NONE, site);
   }

   @Override
   public String isValid() {
      String selection = getValue();
      if (selection != null) {
         return null;
      }
      return "Nothing currently selected..!";
   }
	
   @Override
   public void setValue(String value) {
      if (value != null) {
         composite.setValue(value);
      }
   }
	
   @Override
   public String getValue() {
      return composite.getValue();
   }
	
   @Override
   public void setEnabled(boolean enabled) {
      composite.setEnabled(enabled);
   }
	
   @Override
   public void notifyChanged(String parameterId, String value) {
      composite.uncheckMonth(value);
   }
}
MyComposite クラス
MyComposite クラスは、ユーザー定義エディターのユーザー・インターフェース・コントロールを作成します。 MyComposite クラスは SWT ツールキット Composite クラスを拡張します。
  1. コントロールのレイアウトが設定され、新しい表コントロールが作成されます。
  2. 表コントロール上のリスナーで valueChanged() メソッドが使用されます。 これにより、表内の選択された値が変更されたとき、このパラメーターの値を使用するすべての XPath 式またはエディターに確実に変更通知が送られます。
  3. 一年の各月に対応する表項目が作成されます。
  4. 以下のように、setValue()getValue()、および setEnabled() メソッドは MyComposite クラスで定義されますが、MyEditor クラスから呼び出されます。
    1. setValue() メソッドはパターン・パラメーターの値を受け入れて、選択された値を表示するよう表コントロールを更新します。 パラメーターの値は XML 文書として保管されます (月のリスト、および各月が選択されているかどうかの情報が入ったストリング形式になります)。 setValue() メソッドは各月が選択されているかどうか読み取り、表のチェック・ボックスを更新します。
    2. getValue() メソッドは、ユーザー定義エディターで現在選択されている値を表から取得します。 月のリスト、および各月のチェック・ボックスの状況が XML 文書に保管され、それがストリング値として保存されます。 このストリングは MyEditor クラスに戻された後、パターン・インスタンス・エディターに戻されます。 ストリングはパラメーターの現行値として保管されます。
    3. uncheckMonth() メソッドは、MyEditor クラスから渡されるパラメーター uncheck month の値を受け入れます。 表内の月リストと比べて月が検査され、一致が見つかった場合には、ユーザー定義エディターでその月のチェック・ボックスがクリアされます。
    4. setEnabled() メソッドは、渡されるブール値を使用して、表コントロールを有効または無効にします。
package com.your.company.domain.MyPattern.code;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.ibm.broker.config.appdev.patterns.ui.PatternPropertyEditorSite;

public class MyComposite extends Composite {
   private PatternPropertyEditorSite site;
   private Table table;
	
   public static String[] MONTHS = { 
      "January", "February", "March", "April", "May", "June", "July", 
         "August", "September", "October", "November", "December" };
	
   public MyComposite(Composite parent, int style, final PatternPropertyEditorSite site) {
      super(parent, SWT.NONE);
      this.site = site;
		 		
      GridLayout gridLayout = new GridLayout(1, false);
      setLayout(gridLayout); 		
 		
      setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 		
      table = new Table(this, SWT.BORDER | SWT.CHECK);
      table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
      table.setLinesVisible(true);

      table.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent event) {
            site.valueChanged();
         }
      });

      for (String currentMonth : MONTHS) {
         TableItem currentItem = new TableItem(table, SWT.NONE);
         currentItem.setText(currentMonth);
      }
   }
	
   public void setValue(String value) {
		
      try {

         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
         InputSource inputSource = new InputSource(new StringReader(value));
         Document document = builder.parse(inputSource);
         NodeList listOfMonths = document.getDocumentElement().getChildNodes();
			
         for (int index = 0; index < listOfMonths.getLength(); index++) {
            Node monthNode = listOfMonths.item(index);			
            String monthName = monthNode.getNodeName();
            boolean checked = Boolean.parseBoolean(monthNode.getTextContent());
                
            for (TableItem tableItem : table.getItems()) {
               String itemName = tableItem.getText();
               if (itemName.equals(monthName) == true) {
                  tableItem.setChecked(checked); break;
               }
            }
         }
      } catch (Exception exception) { }
   }

   public String getValue() {
		
      try {
			
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.newDocument();
         Element rootElement = document.createElement("months");
	        
         document.appendChild(rootElement);
            
         for (TableItem currentItem : table.getItems()) {
            String monthName = currentItem.getText();
            boolean isChecked = currentItem.getChecked();
            Element monthElement = document.createElement(monthName);
            monthElement.setTextContent(Boolean.toString(isChecked));
            rootElement.appendChild(monthElement);
         }
            
         TransformerFactory transformerFactory = TransformerFactory.newInstance();
         Transformer transformer = transformerFactory.newTransformer();
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");

         StringWriter stringWriter = new StringWriter();
         StreamResult result = new StreamResult(stringWriter);
         DOMSource source = new DOMSource(document);
         transformer.transform(source, result);
         return stringWriter.toString();
            
      } catch (Exception exception) { }

      return null;
   }
	
   public void uncheckMonth(String monthName) {
      for (TableItem tableItem : table.getItems()) {
         String itemName = tableItem.getText();
         if (itemName.equals(monthName) == true) {
            tableItem.setChecked(false); break;
         }
      }
   }
	
   public void setEnabled(boolean enabled) {
      table.setEnabled(enabled);
   }
}
特記事項 | 商標 | ダウンロード | ライブラリー | サポート | フィードバック

Copyright IBM Corporation 1999, 2014Copyright IBM Corporation 1999, 2014.

        
        最終更新:
        
        最終更新: 2015-02-28 17:48:44


タスク・トピックタスク・トピック | バージョン 8.0.0.5 | bc31440_