View Javadoc
1 package org.xpcards; 2 3 import org.eclipse.swt.SWT; 4 import org.eclipse.swt.events.ModifyEvent; 5 import org.eclipse.swt.events.ModifyListener; 6 import org.eclipse.swt.events.SelectionAdapter; 7 import org.eclipse.swt.events.SelectionEvent; 8 import org.eclipse.swt.layout.GridData; 9 import org.eclipse.swt.layout.GridLayout; 10 import org.eclipse.swt.widgets.Button; 11 import org.eclipse.swt.widgets.Composite; 12 import org.eclipse.swt.widgets.Display; 13 import org.eclipse.swt.widgets.Label; 14 import org.eclipse.swt.widgets.Shell; 15 import org.eclipse.swt.widgets.Text; 16 17 /*** 18 * Dialog class uses <code>org.eclipse.swt</code> 19 * libraries to implement a dialog that accepts basic personal information that 20 * is added to a <code>Table</code> widget or edits a <code>TableItem</code> entry 21 * to represent the entered data. 22 */ 23 public class DataEntryDialog { 24 25 26 Shell shell; 27 String[] values; 28 String[] labels; 29 30 31 public DataEntryDialog(Shell parent) { 32 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL); 33 shell.setLayout(new GridLayout()); 34 } 35 36 private void addTextListener(final Text text) { 37 text.addModifyListener(new ModifyListener() { 38 public void modifyText(ModifyEvent e) { 39 Integer index = (Integer) (text.getData("index")); 40 values[index.intValue()] = text.getText(); 41 } 42 }); 43 } 44 45 private void createControlButtons() { 46 Composite composite = new Composite(shell, SWT.NULL); 47 composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); 48 GridLayout layout = new GridLayout(); 49 layout.numColumns = 2; 50 composite.setLayout(layout); 51 52 Button okButton = new Button(composite, SWT.PUSH); 53 // TODO OK Button I18n 54 okButton.setText("OK"); 55 okButton.addSelectionListener(new SelectionAdapter() { 56 public void widgetSelected(SelectionEvent e) { 57 shell.close(); 58 } 59 }); 60 61 Button cancelButton = new Button(composite, SWT.PUSH); 62 // TODO Concel I18n 63 cancelButton.setText("Cancel"); 64 cancelButton.addSelectionListener(new SelectionAdapter() { 65 public void widgetSelected(SelectionEvent e) { 66 values = null; 67 shell.close(); 68 } 69 }); 70 71 shell.setDefaultButton(okButton); 72 } 73 74 private void createTextWidgets() { 75 if (labels == null) 76 return; 77 78 Composite composite = new Composite(shell, SWT.NULL); 79 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 80 GridLayout layout = new GridLayout(); 81 layout.numColumns = 2; 82 composite.setLayout(layout); 83 84 if (values == null) 85 values = new String[labels.length]; 86 87 for (int i = 0; i < labels.length; i++) { 88 Label label = new Label(composite, SWT.RIGHT); 89 label.setText(labels[i]); 90 Text text = new Text(composite, SWT.BORDER); 91 GridData gridData = new GridData(); 92 gridData.widthHint = 400; 93 text.setLayoutData(gridData); 94 if (values[i] != null) { 95 text.setText(values[i]); 96 } 97 text.setData("index", new Integer(i)); 98 addTextListener(text); 99 } 100 } 101 102 public String[] getLabels() { 103 return labels; 104 } 105 106 public String getTitle() { 107 return shell.getText(); 108 } 109 /*** 110 * Returns the contents of the <code>Text</code> widgets in the dialog in a 111 * <code>String</code> array. 112 * 113 * @return String[] 114 * The contents of the text widgets of the dialog. 115 * May return null if all text widgets are empty. 116 */ 117 public String[] getValues() { 118 return values; 119 } 120 /*** 121 * Opens the dialog in the given state. Sets <code>Text</code> widget contents 122 * and dialog behaviour accordingly. 123 * 124 * @param dialogState int 125 * The state the dialog should be opened in. 126 */ 127 public String[] open() { 128 createTextWidgets(); 129 createControlButtons(); 130 shell.pack(); 131 shell.open(); 132 Display display = shell.getDisplay(); 133 while (!shell.isDisposed()) { 134 if (!display.readAndDispatch()) 135 display.sleep(); 136 } 137 138 return getValues(); 139 } 140 public void setLabels(String[] labels) { 141 this.labels = labels; 142 } 143 public void setTitle(String title) { 144 shell.setText(title); 145 } 146 /*** 147 * Sets the values of the <code>Text</code> widgets of the dialog to 148 * the values supplied in the parameter array. 149 * 150 * @param itemInfo String[] 151 * The values to which the dialog contents will be set. 152 */ 153 public void setValues(String[] itemInfo) { 154 if (labels == null) 155 return; 156 157 if (values == null) 158 values = new String[labels.length]; 159 160 int numItems = Math.min(values.length, itemInfo.length); 161 for (int i = 0; i < numItems; i++) { 162 values[i] = itemInfo[i]; 163 } 164 } 165 }

This page was automatically generated by Maven