我正在設計一個Eclipse插件,它圍繞一個新的視角設計,並帶有一個編輯器,該編輯器在突出顯示它們時存儲代碼/評論片段。其中的部分包括:視角,編輯器和一個mouselistener。IWorkbenchPart.openEditor()不打開自定義編輯器
我有視角,可以打開它。我編輯了編輯器類代碼,但是,通過編程方式通過IWorkbenchPart.openEditor()
打開編輯器,我的自定義編輯器似乎沒有以任何方式初始化。只有默認的Eclipse編輯器出現。我可以說,因爲我的自定義鼠標事件不會觸發。
我用vogella tutorial作爲參考。
爲什麼我的編輯器的init()
方法在打開時不會被調用?我可以告訴它,因爲init()
和createPartControl()
中的打印語句都未執行。
在谷歌搜索這個問題,我發現了一些命中,但他們都圍繞遇到的錯誤消息(無法找到編輯器,無法找到文件,...)。我沒有收到錯誤消息,只是意外的行爲。所以那些文章是無用的。
(我會非常喜歡TextViewer
代替,因爲我不想讓他們編輯的內容在此模式下,無論如何,但我決定在這裏開始。)下面
代碼。
視角:
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
public class PluginPerspective implements IPerspectiveFactory {
@Override
public void createInitialLayout(IPageLayout layout) {
layout.setEditorAreaVisible(true);
layout.setFixed(true);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorInput iei = page.getActiveEditor().getEditorInput();
try
{
// Open Editor code nabbed from Vogella tutorial.
// He creates an action to do so - I force it to happen when the
// perspective is created.
// I get the name of the current open file as expected.
System.out.println(iei.getName());
page.openEditor(iei, myplugin.PluginEditor.ID, true);
// This message prints, as expected.
System.out.println("open!");
} catch (PartInitException e) {
throw new RuntimeException(e);
}
}
}
編輯:(刪除了其他基本編輯器存根(isDirty,DoSave就會),因爲它們是不相關)
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.texteditor.ITextEditor;
public class PluginEditor extends EditorPart implements MouseListener {
public static final String ID = "myplugin.plugineditor";
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
// TODO Auto-generated method stub
System.out.println("editor init!");
setSite(site);
setInput(input);
}
@Override
public void createPartControl(Composite parent) {
// TODO Auto-generated method stub
System.out.println("editor partcontrol!");
//TextViewer tv = new TextViewer(parent, 0);
//tv.setDocument(getCurrentDocument());
}
@Override
public void mouseDoubleClick(MouseEvent e) {
// TODO Auto-generated method stub
// nothing?
}
@Override
public void mouseDown(MouseEvent e) {
// TODO Auto-generated method stub
// grab start location?
System.out.println("down!");
}
@Override
public void mouseUp(MouseEvent e) {
// TODO Auto-generated method stub
// do stuff!
System.out.println("up!");
}
// to be used for grabbing highlight-selection grabbing later
public IDocument getCurrentDocument() {
final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (!(editor instanceof ITextEditor)) return null;
ITextEditor ite = (ITextEditor)editor;
IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
return doc;
//return doc.get();
}
}
是的,先生,我有。對於它的價值,我會在回家後用我的.xml進行編輯。然而,我敢肯定,我誤解了一個IEditorInput是什麼 - 一個關於vogella的例子似乎沒有什麼重要,進一步的研究/試驗錯誤只是告訴我你想用edtior打開什麼文件? – unfuse
作爲名稱的編輯器輸入暗示爲您的編輯器提供了自定義輸入。在'init'方法中,您可以將編輯器輸入轉換爲您自己的實現,檢索必要的數據並相應地更新編輯器。例如,您可以在編輯器輸入中提供一些用戶標識,然後在init方法中獲取它,通過此標識從DB加載用戶,並在編輯器中顯示有關具體用戶的信息。另外,編輯器輸入用於從備忘錄中恢復編輯器(基於可能創建編輯器輸入的備忘錄信息)。但也檢查plugin.xml,可能是你有一個拼寫錯誤的地方。 –
即使我確定eclipse自動爲我添加了.xml文件,但是對於EditorInput更多 - 因爲我想要做的就是在當前文件中顯示原始文本,我不應該需要自定義EditorInput , 對?否則,我可以再次嘗試使用無操作編輯器輸入。 – unfuse