Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 03fd4e10e1c208ce19541580dc54ef02c09136ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.examples.addressbook;


/* Imports */
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * DataEntryDialog class uses <code>org.eclipse.swt</code> 
 * libraries to implement a dialog that accepts basic personal information that
 * is added to a <code>Table</code> widget or edits a <code>TableItem</code> entry 
 * to represent the entered data.
 */
public class DataEntryDialog {

	private static ResourceBundle resAddressBook = ResourceBundle.getBundle("examples_addressbook");
	
	Shell shell;
	String[] values;
	String[] labels;
	
public DataEntryDialog(Shell parent) {
	shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
	shell.setLayout(new GridLayout());		
}

private void addTextListener(final Text text) {
	text.addModifyListener(new ModifyListener() {
		@Override
		public void modifyText(ModifyEvent e){
			Integer index = (Integer)(text.getData("index"));
			values[index.intValue()] = text.getText();
		}
	});
}
private void createControlButtons() {
	Composite composite = new Composite(shell, SWT.NONE);
	composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
	GridLayout layout = new GridLayout();
	layout.numColumns = 2;
	composite.setLayout(layout);
	
	Button okButton = new Button(composite, SWT.PUSH);
	okButton.setText(resAddressBook.getString("OK"));
	okButton.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			shell.close();
		}
	});
	
	Button cancelButton = new Button(composite, SWT.PUSH);
	cancelButton.setText(resAddressBook.getString("Cancel"));
	cancelButton.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			values = null;
			shell.close();
		}
	});
	
	shell.setDefaultButton(okButton);
}

private void createTextWidgets() {
	if (labels == null) return;
	
	Composite composite = new Composite(shell, SWT.NONE);
	composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	GridLayout layout= new GridLayout();
	layout.numColumns = 2;
	composite.setLayout(layout);
	
	if (values == null)
		values = new String[labels.length];
	
	for (int i = 0; i < labels.length; i++) {
		Label label = new Label(composite, SWT.RIGHT);
		label.setText(labels[i]);	
		Text text = new Text(composite, SWT.BORDER);
		GridData gridData = new GridData();
		gridData.widthHint = 400;
		text.setLayoutData(gridData);
		if (values[i] != null) {
			text.setText(values[i]);
		}
		text.setData("index", new Integer(i));
		addTextListener(text);	
	}
}

public String[] getLabels() {
	return labels;
}
public String getTitle() {
	return shell.getText();
}
/**
 * Returns the contents of the <code>Text</code> widgets in the dialog in a 
 * <code>String</code> array.
 *
 * @return	String[]	
 *			The contents of the text widgets of the dialog.
 *			May return null if all text widgets are empty.
 */ 
public String[] getValues() {
	return values;
}
/** 
 * Opens the dialog in the given state.  Sets <code>Text</code> widget contents 
 * and dialog behaviour accordingly.
 *
 * @param 	dialogState	int
 *					The state the dialog should be opened in.
 */
public String[] open() {
	createTextWidgets();
	createControlButtons();
	shell.pack();
	shell.open();
	Display display = shell.getDisplay();
	while(!shell.isDisposed()){
		if(!display.readAndDispatch())
			display.sleep();
	}
	
	return getValues();
}
public void setLabels(String[] labels) {
	this.labels = labels;
}
public void setTitle(String title) {
	shell.setText(title);
}
/**
 * Sets the values of the <code>Text</code> widgets of the dialog to
 * the values supplied in the parameter array.
 *
 * @param	itemInfo	String[]
 * 						The values to which the dialog contents will be set.
 */
public void setValues(String[] itemInfo) {
	if (labels == null) return;
	
	if (values == null)
		values = new String[labels.length];

	int numItems = Math.min(values.length, itemInfo.length);
	for(int i = 0; i < numItems; i++) {
		values[i] = itemInfo[i];
	}	
}
}

Back to the top