package org.safehaus.penrose.studio.nis.dialog; 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; /** * @author Endi S. Dewata */ public class NISScheduleDialog extends Dialog { Logger log = Logger.getLogger(getClass()); public final static int CANCEL = 0; public final static int OK = 1; Shell shell; Label nameText; Text scheduleText; int action; private String name; private String schedule; public NISScheduleDialog(Shell parent, int style) { super(parent, style); } public void open() { setText("NIS Schedule 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); scheduleText.setText(schedule == null ? "" : schedule); } 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 Label(composite, SWT.NONE); nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); Label scheduleLabel = new Label(composite, SWT.NONE); scheduleLabel.setText("Schedule:"); scheduleLabel.setLayoutData(new GridData()); scheduleText = new Text(composite, SWT.BORDER); scheduleText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); new Label(composite, SWT.NONE); Label example = new Label(composite, SWT.NONE); example.setLayoutData(new GridData(GridData.FILL_BOTH)); example.setText( "\nCron Expression:\n"+ " 1. Seconds\n" + " 2. Minutes\n" + " 3. Hours\n" + " 4. Day-of-Month\n" + " 5. Month\n" + " 6. Day-of-Week\n" + " 7. Year (optional)\n\n"+ "Example:\n"+ " - Everyday at 2:30am : 0 30 2 * * ?\n"+ " - Every 5 minutes : 0 0/5 * * * ?" ); 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(); schedule = scheduleText.getText().equals("") ? null : scheduleText.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 getSchedule() { return schedule; } public void setSchedule(String schedule) { this.schedule = schedule; } }