コンテキストメニューを使用してフィギュアを追加-Eclipse GEF
-
13-10-2019 - |
質問
全て、
コンテキストメニューを介してカスタムエディターにフィギュアを追加しているが、それを行う方法を見つけていないパレットの少ないEclipseプラグインを作成しています。コンテキストメニュー、つまりアクション/コマンドの追加メニューを介して、エディターにフィギュアを動的に追加する方法については、誰でも教えてください。
Eclipse GEFプラグインの開発は、見るべき例が少ないので、ソリューションを追加して、他の人がそれが役立つと感じています。このコードは、エディターにノードをレンダリングするのに役立ちます。
アクションクラスのソースコードは、編集者に数値をレンダリングします。
public class AddNodeAction extends EditorPartAction
{
public static final String ADD_NODE = "ADDNODE";
public AddNodeAction(IEditorPart editor) {
super(editor);
setText("Add a Node");
setId(ADD_NODE); // Important to set ID
}
public void run()
{
<ParentModelClass> parent= (<ParentModelClass>)getEditorPart().getAdapter(<ParentModelClass>.class);
if (parent== null)
return;
CommandStack command = (CommandStack)getEditorPart().getAdapter(CommandStack.class);
if (command != null)
{
CompoundCommand totalCmd = new CompoundCommand();
<ChildModelToRenderFigureCommand>cmd = new <ChildModelToRenderFigureCommand>(parent);
cmd.setParent(parent);
<ChildModelClass> newNode = new <ChildModelClass>();
cmd.setNode(newNode);
cmd.setLocation(getLocation()); // Any location you wish to set to
totalCmd.add(cmd);
command.execute(totalCmd);
}
}
@Override
protected boolean calculateEnabled()
{
return true;
}
}
解決
ここでは複数の異なるものが必要だと思います。 GEFは、独自のモデルを持っている適切なMVCパターンを使用して、ビューとして表示され、コントローラーとして編集することを忘れないでください。
私の頭の上から、私はあなたが少なくともこれらのことが必要だと言うでしょう:
- CreateCommand
- 新しいデータをデータモデルに追加するために実行する必要があるすべてのモデルレベルの変更が含まれています(元に戻し、トランザクション)
- CreateAction
- そのcreatecommandインスタンスを作成し、現在の選択で初期化し、editdomainでそのコマンドを実行します
- ContextMenuprovider
- CreateAuttion to Contextメニューを提供します
GMFを使用している場合、コマンド内でモデルの変更を行うと、標準メカニズムが自動的に編集されますが、GMFを使用していない場合は、独自のモデルと編集パートが処理およびリフレッシュしていることを確認する必要があります。適切に追加する新しいアイテム。
編集:わかりました、ここにリクエストを再生することに関するコードの提案があります。
public void run() {
// Fetch viewer from editor part (might not work, if not, try some other way)
EditPartViewer viewer = (EditPartViewer) part.getAdapter(EditPartViewer.class);
// get Target EditPart that is under the mouse
EditPart targetEditPart = viewer.findObjectAt(getLocation());
// If nothing under mouse, set root item as target (just playing safe)
if(targetEditPart == null)
targetEditPart = viewer.getContents();
// Make and initialize create request with proper information
CreateRequest createReq = new CreateRequest();
createReq.setLocation(getLocation());
createReq.setFactory(new OwnFactoryImplementation());
// Ask from target editpart command for this request
Command command = targetEditPart.getCommand(createReq);
// If command is ok, and it can be executed, go and execute it on commandstack
if(command != null && command.canExecute()) {
viewer.getEditDomain().getCommandStack().execute(command);
}
}
さて、editpartが作成の要求が要求されることです。そのため、アクション自体はコマンドがどのように機能するか、それがコマンドを客観的にするものを知りません。
したがって、物事を機能させるには、編集パルトに新しい編集をインストールする必要があります。 editPoliciesは、editparts createdEfaultEditPolicies()関数にインストールできます。このEditPolicyは、Createrequestがあるときにコマンドを反応して返す必要があります。これにより、子供は自分のために子供を作成するための独自のコマンドを提供できます。
これがそれがどのように機能するか(コントローラーが編集されている)の良い画像です:
私があなたをもう少し助けることができるかどうか尋ねてください。これはもう少し複雑に見えることを知っていますが、これはあなた自身の人生をはるかに簡単にします。そして、あなたがそれをした後、あなたは実際にコマンド・リクエストパターンを非常によく理解し、それは多くの異なる場所で再利用することができます。