Eclipse RCP 로 무언가를 만들어보려는데, 콘솔에 출력되는 내용 (System.out 으로 출력되는 내용)을 UI 에 표시하는 기능이 필요했다. 이미 Eclipse 에 Console 뷰가 있으므로 이걸 써먹기 위해 인터넷을 열심히 뒤진 결과 쓰는 방법을 알아냈다.
아래 예제는 특정 뷰에서 텍스트를 System.out 에 출력하면 그 내용이 Console 뷰에 보이게 하는 프로그램이다.
다만 아쉬운 것은 Eclipse 에서는 System.err 에 출력되는 것은 빨간색으로 표시되는데, 내가 테스트해본 바로는 System.err 에 출력해도 검은색 글자로 표시된다. 뭔가 바꾸는 방법이 있겠지만 귀찮아서...
참고한 곳 : http://jprog.blogspot.com/2005/09/using-eclipse-console-view-in-rcp.html
아래 예제는 특정 뷰에서 텍스트를 System.out 에 출력하면 그 내용이 Console 뷰에 보이게 하는 프로그램이다.
- rcp 프로젝트를 만든 후 플러그인 설정 파일 편집화면에서 dependencies 에 org.eclipse.ui.console 을 추가한다.
- 타겟 RCP 의 plugins 디렉토리에 아래의 플러그인 파일이 있는지 보고 없으면 다른 곳에서 복사해 넣는다. (Eclipse 에는 포함되어 있다.)
- org.eclipse.ui.console
- org.eclipse.jface.text
- org.eclipse.core.variables
- org.eclipse.text
- org.eclipse.ui.workbench.texteditor
- IPerspectiveFactory 구현클래스를 아래처럼 만든다.
import java.io.PrintStream;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
MessageConsole console = new MessageConsole("Console" , null) ;
ConsolePlugin.getDefault().getConsoleManager()
.addConsoles(new IConsole[]{console});
MessageConsoleStream stream = console.newMessageStream();
PrintStream myS = new PrintStream(stream);
System.setOut(myS);
System.setErr(myS);
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);
layout.setFixed(false);
layout.addView(InputBox.ID, IPageLayout.LEFT, 1.0f, editorArea);
layout.addView(IConsoleConstants.ID_CONSOLE_VIEW,
IPageLayout.BOTTOM, 0.4f, InputBox.ID);
}
} - 텍스트를 입력할 수 있는 간단한 뷰를 만든다.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
public class InputBox extends ViewPart {
public static final String ID = "test.consoleview.view";
private Text text;
public void createPartControl(Composite parent) {
Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(new FillLayout());
this.text = new Text(panel, SWT.MULTI);
Button btn = new Button(panel, SWT.PUSH);
btn.setText("click !!!");
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
printString();
}
@Override
public void widgetSelected(SelectionEvent e) {
printString();
}
private void printString() {
System.out.println(text.getText());
text.setText("");
}
});
}
public void setFocus() {
this.text.setFocus();
}
} - Overview 페이지의 Launch an Eclipse Application 링크를 클릭하여 실행시켜본다.
- 잘 안되면 Run 메뉴의 Run Configurations 의 해당 항목의 Plug-ins 탭에서 Add Required Plug-ins 버튼을 클릭하고 다시 실행해본다.
- 이래도 안 될 경우 Validate Plug-ins 버튼을 클릭하여 필요한 플러그인이 모두 있는지 확인한다.
다만 아쉬운 것은 Eclipse 에서는 System.err 에 출력되는 것은 빨간색으로 표시되는데, 내가 테스트해본 바로는 System.err 에 출력해도 검은색 글자로 표시된다. 뭔가 바꾸는 방법이 있겠지만 귀찮아서...
참고한 곳 : http://jprog.blogspot.com/2005/09/using-eclipse-console-view-in-rcp.html