本課程以您在「練習 5」中建立並在「練習 6」中修改的外掛程式專案為建置基礎。在本課程中,您將建立 CustomContextProvider 類別,可以自訂該類別,以適合您要使用的任何檢視器。例如,如果您要使用欄位檢視器,則環境定義提供者可以延伸 CarmaFieldsContentProvider。
若要建立 CARMAContextProvider 類別:
- 確保您位於外掛程式開發視景中。
在套件瀏覽器視圖中,展開您在「練習 5」和「練習 6」中使用的 com.ibm.carma.plugin.view 外掛程式專案。
- 用滑鼠右鍵按一下包含 CARMADeveloperView 和 CustomLabelProvider 類別的 view 套件,然後選取新建 > 類別。
- 在開啟的新建 Java 類別對話框中,請在名稱文字欄位中輸入 CustomContextProvider。
- 按一下超類別文字欄位右側的瀏覽按鈕。在開啟的選取超類別對話框中,輸入過濾文字 CARMATreeContentProvider。選取相符的類別,然後按一下確定。
- 標示超類別中的建構子勾選框,然後按一下完成。這時將關閉新建 Java 類別對話框,並建立 CustomContentProvider 類別。
- 請從確保將下列匯入項目併入 Java 類別開始。新增任何遺漏項目。
import java.util.Vector;
import com.ibm.carma.model.RepositoryInstance;
import com.ibm.carma.ui.view.CarmaTreeContentProvider;
- 您將要修改 getChildren 方法,以變更提供給檢視器的內容。在此方法中,提供者可以控制在展開 RAM 時,將哪些項目傳送給檢視器。針對本指導教學,您將實作 getChildren 方法,其所傳回的儲存庫實例必須在名稱中含有 CARMA 記號,且不是清單、物件或負載儲存庫實例。
下列虛擬碼示範了 getChildren 方法應該執行的內容:
get the children of the object that would normally be returned;
for each child
{
if(the child is a repository instance)
{
if(the repository instance has a CARMA token and is not a listing, object, or load repository instance)
add the child to the list of displayable children;
}
}
請利用下面 getChildren 方法的範例程式碼:
public Object[] getChildren(Object parent)
{
Object[] children = super.getChildren(parent);
//Do not parse non-existant children
if(children == null)
{
return children;
}
Vector<Object> displayChildren = new Vector<Object>();
for(int i = 0; i < children.length; i++)
{
if(children[i] instanceof RepositoryInstance)
{
RepositoryInstance myContainer = (RepositoryInstance) children[i];
if (myContainer.getName().contains("CARMA"))
{
displayChildren.add(children[i]);
}
}
else
{
displayChildren.add(children[i]);
}
}
return displayChildren.toArray();
}
- 儲存原始檔,並對任何錯誤進行除錯。