1 package com.sslexplorer.agent.client.gui.swt; 2 3 import org.eclipse.swt.SWT; 4 import org.eclipse.swt.events.SelectionAdapter; 5 import org.eclipse.swt.events.SelectionEvent; 6 import org.eclipse.swt.layout.GridData; 7 import org.eclipse.swt.layout.GridLayout; 8 import org.eclipse.swt.layout.RowLayout; 9 import org.eclipse.swt.widgets.Button; 10 import org.eclipse.swt.widgets.Composite; 11 import org.eclipse.swt.widgets.Dialog; 12 import org.eclipse.swt.widgets.Display; 13 import org.eclipse.swt.widgets.Label; 14 import org.eclipse.swt.widgets.Shell; 15 16 public class SWTOptionDialog extends Dialog { 17 private Object message; 18 private String okText; 19 private String cancelText; 20 private boolean ok; 21 private Shell shell; 22 private boolean open; 23 24 27 public SWTOptionDialog(Shell parent, int style, String okText, String cancelText, String title, Object message) { 28 super(parent, style); 30 this.okText = okText; 31 this.cancelText = cancelText; 32 setText(title); 33 setMessage(message); 34 35 shell = new Shell(getParent(), getStyle()); 37 shell.setText(getText()); 38 } 39 40 45 public Object getMessage() { 46 return message; 47 } 48 49 54 public void setMessage(Object message) { 55 this.message = message; 56 } 57 58 63 public boolean open() { 64 createContents(shell); 65 shell.pack(); 66 SWTUtil.center(shell); 67 open = true; 68 shell.open(); 69 Display display = getParent().getDisplay(); 70 while (open) { 71 if (!display.readAndDispatch()) { 72 display.sleep(); 73 } 74 } 75 return ok; 77 } 78 79 82 public void close() { 83 if(!shell.isDisposed()) 84 shell.close(); 85 } 86 87 92 private void createContents(final Shell shell) { 93 shell.setLayout(new GridLayout(1, true)); 94 95 Composite c = new Composite(shell, 0); 96 GridLayout gridLayout = new GridLayout (); 97 c.setLayout (gridLayout); 98 99 if (message instanceof String ) { 100 Label label = new Label(shell, SWT.NONE); 101 label.setText((String ) message); 102 GridData data = new GridData (); 103 data.horizontalAlignment = GridData.CENTER; 104 data.grabExcessHorizontalSpace = true; 105 label.setLayoutData (data); 106 } else if (message != null) { 107 GridData data = new GridData(GridData.FILL_BOTH); 108 ((Composite) message).setLayoutData(data); 109 } 110 111 Composite c2 = new Composite(shell, 0); 112 RowLayout rowLayout = new RowLayout (); 113 c2.setLayout (rowLayout); 114 115 Button ok = new Button(c2, SWT.PUSH); 119 ok.setText(okText); 120 ok.addSelectionListener(new SelectionAdapter() { 121 public void widgetSelected(SelectionEvent event) { 122 SWTOptionDialog.this.ok = true; 123 open = false; 124 } 125 }); 126 127 if (cancelText != null) { 130 Button cancel = new Button(c2, SWT.PUSH); 131 cancel.setText(cancelText); 132 cancel.addSelectionListener(new SelectionAdapter() { 133 public void widgetSelected(SelectionEvent event) { 134 open = false; 135 } 136 }); 137 } 138 139 GridData data = new GridData (); 140 data.horizontalAlignment = GridData.CENTER; 141 data.grabExcessHorizontalSpace = true; 142 c2.setLayoutData(data); 143 144 shell.pack(); 145 146 shell.setDefaultButton(ok); 150 } 151 152 public Shell getShell() { 153 return shell; 154 } 155 } 156 | Popular Tags |