package org.safehaus.penrose.studio.federation.nis.ownership;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.graphics.Point;
import org.apache.log4j.Logger;
import org.safehaus.penrose.studio.PenroseImage;
import org.safehaus.penrose.studio.PenroseStudio;
import java.rmi.registry.Registry;
/**
* @author Endi S. Dewata
*/
public class NISHostDialog extends Dialog {
Logger log = Logger.getLogger(getClass());
public final static int CANCEL = 0;
public final static int OK = 1;
Shell shell;
Text nameText;
Text pathsText;
Text portText;
int action;
private String name;
private String paths;
private Integer port;
public NISHostDialog(Shell parent, int style) {
super(parent, style);
}
public void open() {
setText("NIS Host Editor");
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
init();
reset();
Point size = new Point(600, 400);
shell.setSize(size);
Point l = getParent().getLocation();
Point s = getParent().getSize();
shell.setLocation(l.x + (s.x - size.x)/2, l.y + (s.y - size.y)/2);
shell.setText(getText());
shell.setImage(PenroseStudio.getImage(PenroseImage.LOGO));
shell.open();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
}
public void init() {
createControl(shell);
}
public void reset() {
nameText.setText(name == null ? "" : name);
pathsText.setText(paths == null ? "" : paths);
portText.setText(port == null ? ""+ Registry.REGISTRY_PORT : ""+port);
}
public void createControl(Shell parent) {
parent.setLayout(new GridLayout());
Composite composite = createMainPanel(parent);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
composite = createButtonsPanel(parent);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalAlignment = GridData.END;
composite.setLayoutData(gd);
}
public Composite createMainPanel(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(2, false));
Label nameLabel = new Label(composite, SWT.NONE);
nameLabel.setText("Name:");
GridData gd = new GridData();
gd.widthHint = 100;
nameLabel.setLayoutData(gd);
nameText = new Text(composite, SWT.BORDER);
nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label portLabel = new Label(composite, SWT.NONE);
portLabel.setText("Port:");
portLabel.setLayoutData(new GridData());
portText = new Text(composite, SWT.BORDER);
portText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Label pathsLabel = new Label(composite, SWT.NONE);
pathsLabel.setText("Paths:");
pathsLabel.setLayoutData(new GridData());
pathsText = new Text(composite, SWT.BORDER);
pathsText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
return composite;
}
public Composite createButtonsPanel(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new RowLayout());
Button cancelButton = new Button(composite, SWT.PUSH);
cancelButton.setText(" Cancel ");
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
action = CANCEL;
shell.close();
}
});
Button okButton = new Button(composite, SWT.PUSH);
okButton.setText(" OK ");
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
name = nameText.getText().equals("") ? null : nameText.getText();
paths = pathsText.getText().equals("") ? null : pathsText.getText();
port = portText.getText().equals("") ? Registry.REGISTRY_PORT : new Integer(portText.getText());
action = OK;
shell.close();
}
});
return composite;
}
public int getAction() {
return action;
}
public void setAction(int action) {
this.action = action;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPaths() {
return paths;
}
public void setPaths(String paths) {
this.paths = paths;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}