:: ADVANCE ::

[Plug-in] Dialog 본문

Study/Eclipse

[Plug-in] Dialog

KSJ14 2015. 9. 19. 02:57
반응형

[Plug-in] Dialog


Dialog가 Window를 확장하기 때문에, 모든 것은 createContents()로 시작한다.

이 메소드에서 초기화를 한 후에 createDialogArea()를 호출한다.

이 메소드는 대화창의 최상위 부분을 만든다.


createDialogArea()에서 돌아온 후에는, createButtonBar()를 호출하여 대화창의 바닥에 있는 바를 위해 새로운 Composite와 Layout을 생성한다. 

마지막으로, createButtonBar()는 createButtonsForButtonBar()를 호출하여 대화창에 나타날 버튼을 인스턴스화 한다.

디폴트로는 확인과 취소 버튼을 생성한다.



적절한 메소드를 오버라이딩하여 제어권을 확보할 수 있다.

하지만 일반적으로는 createDialogArea()나 createButtonsForButtonBar()만 구현한다.



@Override

protected Control createDialogArea(Composite parent) {

// createDialogArea()는 Composite을 취하며 이 Composite는 생성하는 어떤 컨트롤이든 부모로서 사용된다.

// 그리고 createDialogArea()는 Control을 반환해야 한다.

// 이 컨트롤의 레이아웃 데이터는 GridData의 인스턴스이다.

// 이 계약을 충족시키는 가장 쉬운 방법은 자체적인 작업을 하기 전에 디폴트 구현부를 호출하는 것이다.

Composite composite = (Composite) super.createDialogArea(parent);


.....// 커스텀 컨트롤

      return composite



예제 ]


사용자 맞춤 username + password 대화창 


package metricvisualizer.actions;


import org.eclipse.jface.dialogs.Dialog;

import org.eclipse.jface.dialogs.IDialogConstants;

import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.*;


public class UsernamePasswordDialog extends Dialog{

private static final int RESET_ID = IDialogConstants.NO_TO_ALL_ID + 1;

private Text usernameField;

private Text passwordField;

public UsernamePasswordDialog(Shell parentShell) {

super(parentShell);

}

protected Control createDialogArea(Composite parent) {

Composite comp = (Composite)super.createDialogArea(parent);

GridLayout layout = (GridLayout)comp.getLayout();

layout.numColumns = 2;

Label usernameLabel = new Label(comp, SWT.RIGHT);

usernameLabel.setText("Username : ");

usernameField = new Text(comp, SWT.SINGLE);

GridData data = new GridData(GridData.FILL_HORIZONTAL);

usernameField.setLayoutData(data);


Label passwordLabel = new Label(comp, SWT.RIGHT);

passwordLabel.setText("Password : ");

passwordField = new Text(comp, SWT.SINGLE | SWT.PASSWORD);

data = new GridData(GridData.FILL_HORIZONTAL);

passwordField.setLayoutData(data);

return comp;

}

protected void createButtonsForButtonBar(Composite parent) {

super.createButtonsForButtonBar(parent);

createButton(parent, RESET_ID, "Reset All", false);

}

protected void buttonPressed(int buttonId) {

System.out.println("button : " + buttonId);


if(buttonId == RESET_ID) {

usernameField.setText("");

passwordField.setText("");

}

else if(buttonId == 0) {

System.out.println("username : " + usernameField.getText());

System.out.println("password : " + passwordField.getText());

super.buttonPressed(buttonId);

}

else {

super.buttonPressed(buttonId);

}

}

}



반응형

'Study > Eclipse' 카테고리의 다른 글

[Plug-in] Input Dialog  (0) 2015.09.19
[Plug-in] MessageDialog  (0) 2015.09.17
[Plug-in] Dialog  (0) 2015.09.17
Comments