Rational Developer for System z


第 9 課:建立 CheckboxOptionControl 類別的程式碼

CheckboxOptionControl 類別會將 CARMA 預設文字欄位變更成 Option 參數上的一個勾選框。 由於此參數為「是或否」參數,所以對使用者而言,勾選框更方便。
  1. 套件瀏覽器視圖中,透過導覽 com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action,然後按兩下您建立的 ValueParamValidator 類別,來開啟該類別。
  2. 首先,您將新增勾選框按鈕物件,作為類別的實例資料。這會容許 CheckboxOptionControl 類別可以使用勾選框按鈕及其所有 meta 資料。 新增下列程式碼行:Button theButton;
  3. 接下來,您將置換 createControl 方法,以建立勾選框按鈕並將其傳回,而不是傳回預設文字欄位。下列虛擬碼會示範您要此方法所執行的動作:
    Create theButton with the checkbox style
    Give the theButton some text
    Return the theButton
    請利用下列範例程式碼來置換此方法:
    /* Create a checkbox for the yes/no option */
    public Control createControl(Composite parent, 
                                 RepositoryManager repositoryManager,
                                 Parameter param, 
                                 Action action,
                                 CustomActionAccepter custActionAccepter,
                                 Object defaultValue) 
    {
       theButton = new Button(parent, SWT.CHECK);
       theButton.setText("Check me!");
       return theButton;
    }
  4. 現在,您將置換 getValue 方法。 此方法會傳回參數值。由於您要使用的是勾選框,而非文字欄位,因此您撰寫的程式碼必須將勾選框的勾選或不勾選狀態轉換成預期的字串格式。下列虛擬碼對此進行了示範:
    if the checkbox is checked
    	return "Y" for yes
    else
    	return "N" for no
    請利用下列範例程式碼來置換此方法:
    	public Object getValue() 	{
    		if(theButton.getSelection())
    			return "Y";
    		else
    			return "N";
    	}
  5. 您需要置換的最後一個方法是 isUsingDefaultLabel 方法。 如果設為 false,則當您將參數新增至 RAM 時,此方法將不顯示您提供的預設標籤。如果設為 true,則標籤將正常顯示。針對本範例,程式碼將使用預設標籤。 請利用下列範例程式碼來置換此方法:
    public boolean isUsingDefaultLabel()
    {
       return true;
    }
  6. 最後,您需要確保所有必要的匯入項目都已列出。新增下列任何未列在類別中的 import 陳述式:
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    
    import com.ibm.carma.model.*;
    
    import com.ibm.carma.ui.action.custom.AbstractCustomParameterControl;
  7. 儲存原始檔,並對任何錯誤進行除錯。
CheckBoxOptionControl 的完整程式碼應如下所示:
package com.ibm.carma.plugin.howto.action;

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

import com.ibm.carma.model.*;

import com.ibm.carma.ui.action.custom.AbstractCustomParameterControl;

public class CheckboxOptionAction extends AbstractCustomParameterControl {
   	//Add the button to the instance data
   	Button theButton;
   
   	/* Create a check box for the yes/no option */
   public Control createControl(Composite parent, 
                                RepositoryManager repositoryManager,
                                Parameter param, 
                                Action action,
                                CustomActionAccepter customActionAccepter,
                                Object defaultValue)
      theButton = new Button(parent, SWT.CHECK);
      theButton.setText("Check me!");
      return theButton;
   }

   @Override
   	public Object getValue() 	{
      		if(theButton.getSelection())
			return "Y";
      else
         			return "N";
   }

   @Override 
   	public boolean isUsingDefaultLabel() 	{
      return true;
   }
}

使用條款 | 意見



本資訊中心採用 Eclipse 技術。(http://www.eclipse.org)