特定のパースペクティブに切り替えるためのコマンド/ハンドラを提供しようとしています。プログラムでパースペクティブを開く
私は以下のクラスを思い付いた:
public class OpenPerspectiveHandler {
private static final Logger logger = Logger.getLogger(OpenPerspectiveHandler.class);
@Inject
private MApplication application;
@Inject
private EModelService modelService;
@Inject
private EPartService partService;
private final String perspectiveId;
public OpenPerspectiveHandler(String perspectiveId) {
super();
this.perspectiveId = perspectiveId;
}
public void changePerspective(String perspectiveId) {
Optional<MPerspective> perspective = findPerspective();
if(perspective.isPresent()) {
partService.switchPerspective(perspective.get());
} else {
logger.debug("Perspective not found (" + perspectiveId + ")");
}
}
@Execute
public void execute() {
changePerspective(perspectiveId);
}
private Optional<MPerspective> findPerspective() {
MUIElement element = modelService.find(perspectiveId, application);
if(element instanceof MPerspective) {
return Optional.of((MPerspective)element);
} else {
logger.debug("Wrong type " + element);
}
return Optional.empty();
}
@Override
public String toString() {
return "OpenPerspectiveHandler [application=" + application + ", modelService=" + modelService + ", partService=" + partService + ", perspectiveId=" + perspectiveId + "]";
}
}
興味深いことに、これは一度だけ動作します。この問題を回避するには、MPerspective
が見つかったらキャッシュし、もう一度modelService.find(perspectiveId, application)
を使用しないでください。
なぜ1回だけ動作しますか? modelService.find(perspectiveId, application)
は、最初の実行後にnull
を返します。
EDIT:
別のアプローチは、(グレッグ-449により示唆されるように)以下の通りです:
public class OpenPerspectiveHandler {
private static final Logger logger = Logger.getLogger(OpenPerspectiveHandler.class);
private final String perspectiveId;
public OpenPerspectiveHandler(String perspectiveId) {
super();
this.perspectiveId = perspectiveId;
}
@Execute
public void changePerspective(MApplication application, EModelService modelService, EPartService partService) {
Optional<MPerspective> perspective = findPerspective(application, modelService);
if(perspective.isPresent()) {
partService.switchPerspective(perspective.get());
} else {
logger.debug("Perspective not found (" + perspectiveId + ")");
}
}
private Optional<MPerspective> findPerspective(MApplication application, EModelService modelService) {
MUIElement element = modelService.find(perspectiveId, application);
if(element instanceof MPerspective) {
return Optional.of((MPerspective)element);
} else {
logger.debug("Wrong type " + element);
}
return Optional.empty();
}
}
しかし、このアプローチはまた、一度だけ視点を変更します。 modelService.find(perspectiveId, application);
は、最初の実行後にnull
を返します。
さて?たとえば、モデルサービスが機能していることがわかりますか?ハンドラーでフィールド注入を使用するのは最良ではありません**常に@Executeメソッドの引数としてすべてを注入することに注意してください。パーツサービスのようなものは、状況に応じて変わることがあります。 –
'modelService.find(perspectiveId、application)'は、最初の実行後に 'null'を返します。 – kerner1000
@Executeメソッドへの注入に関するヒントをありがとう。更新された質問をご覧ください。 – kerner1000