1 | package de.uka.ipd.sdq.sensorframework.dialogs.dataset; |
2 | |
3 | import org.eclipse.jface.dialogs.IDialogConstants; |
4 | import org.eclipse.jface.dialogs.TitleAreaDialog; |
5 | import org.eclipse.jface.viewers.ISelection; |
6 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
7 | import org.eclipse.jface.viewers.IStructuredSelection; |
8 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
9 | import org.eclipse.jface.viewers.TableViewer; |
10 | import org.eclipse.swt.SWT; |
11 | import org.eclipse.swt.events.SelectionListener; |
12 | import org.eclipse.swt.events.ShellAdapter; |
13 | import org.eclipse.swt.events.ShellEvent; |
14 | import org.eclipse.swt.graphics.Point; |
15 | import org.eclipse.swt.layout.FormAttachment; |
16 | import org.eclipse.swt.layout.FormData; |
17 | import org.eclipse.swt.layout.FormLayout; |
18 | import org.eclipse.swt.layout.GridData; |
19 | import org.eclipse.swt.widgets.Button; |
20 | import org.eclipse.swt.widgets.Composite; |
21 | import org.eclipse.swt.widgets.Control; |
22 | import org.eclipse.swt.widgets.Label; |
23 | import org.eclipse.swt.widgets.Shell; |
24 | import org.eclipse.swt.widgets.Table; |
25 | |
26 | import de.uka.ipd.sdq.sensorframework.dao.memory.MemoryDAOFactory; |
27 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
28 | |
29 | public class DatasourceDialog extends TitleAreaDialog { |
30 | |
31 | private static String DIALOG_TITLE = "Create/Load the data source."; |
32 | |
33 | private Button addButton, removeButton, okButton, openButton; |
34 | private Object input; |
35 | private IDAOFactory selectedDataSet; |
36 | private TableViewer viewer; |
37 | private boolean buttonValidation; |
38 | private String dialogTitle; |
39 | |
40 | /** Create the dialog */ |
41 | public DatasourceDialog(Shell parentShell, String dialogTitle, |
42 | Object input, boolean makeButtonValidation) { |
43 | super(parentShell); |
44 | this.dialogTitle = dialogTitle; |
45 | this.input = input; |
46 | this.buttonValidation = makeButtonValidation; |
47 | |
48 | /** |
49 | * the result of combining the constants which are required to produce a |
50 | * typical application top level shell |
51 | */ |
52 | setShellStyle(SWT.RESIZE | SWT.MAX | SWT.CLOSE); |
53 | } |
54 | |
55 | /* (non-Javadoc) |
56 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
57 | */ |
58 | @Override |
59 | protected void configureShell(Shell newShell) { |
60 | super.configureShell(newShell); |
61 | newShell.setText(dialogTitle); |
62 | newShell.addShellListener(new ShellAdapter(){ |
63 | |
64 | /* (non-Javadoc) |
65 | * @see org.eclipse.swt.events.ShellAdapter#shellClosed(org.eclipse.swt.events.ShellEvent) |
66 | */ |
67 | @Override |
68 | public void shellClosed(ShellEvent e) { |
69 | selectedDataSet= null; |
70 | } |
71 | }); |
72 | } |
73 | |
74 | /* (non-Javadoc) |
75 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
76 | */ |
77 | @Override |
78 | protected Control createDialogArea(Composite parent) { |
79 | Composite area = (Composite) super.createDialogArea(parent); |
80 | Composite container = new Composite(area, SWT.NONE); |
81 | container.setLayout(new FormLayout()); |
82 | container.setLayoutData(new GridData(GridData.FILL_BOTH)); |
83 | |
84 | /** 'Add..' button */ |
85 | addButton = new Button(container, SWT.NONE); |
86 | final FormData fd_addButton = new FormData(); |
87 | fd_addButton.right = new FormAttachment(100, -5); |
88 | fd_addButton.bottom = new FormAttachment(0, 31); |
89 | fd_addButton.top = new FormAttachment(0, 5); |
90 | addButton.setLayoutData(fd_addButton); |
91 | addButton.setText("Add.."); |
92 | |
93 | /** 'Remove' button */ |
94 | removeButton = new Button(container, SWT.NONE); |
95 | final FormData fd_removeButton = new FormData(); |
96 | fd_removeButton.right = new FormAttachment(100, -5); |
97 | fd_removeButton.bottom = new FormAttachment(0, 61); |
98 | fd_removeButton.top = new FormAttachment(0, 35); |
99 | removeButton.setLayoutData(fd_removeButton); |
100 | removeButton.setText("Remove"); |
101 | |
102 | /** 'Open..' button */ |
103 | openButton = new Button(container, SWT.NONE); |
104 | final FormData fd_openButton = new FormData(); |
105 | fd_openButton.left = new FormAttachment(100, -84); |
106 | fd_openButton.right = new FormAttachment(100, -5); |
107 | fd_openButton.bottom = new FormAttachment(0, 91); |
108 | fd_openButton.top = new FormAttachment(0, 65); |
109 | openButton.setLayoutData(fd_openButton); |
110 | openButton.setText("Open.."); |
111 | |
112 | Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL); |
113 | final FormData fd_label = new FormData(); |
114 | fd_label.bottom = new FormAttachment(100, 2); |
115 | fd_label.right = new FormAttachment(100, 2); |
116 | fd_label.left = new FormAttachment(0, -6); |
117 | separator.setLayoutData(fd_label); |
118 | separator.setText("Label"); |
119 | |
120 | Table list = new Table(container, SWT.BORDER); |
121 | fd_label.top = new FormAttachment(list, 28, SWT.DEFAULT); |
122 | fd_removeButton.left = new FormAttachment(list, 6, SWT.DEFAULT); |
123 | fd_addButton.left = new FormAttachment(list, 6, SWT.DEFAULT); |
124 | final FormData fd_list = new FormData(); |
125 | fd_list.bottom = new FormAttachment(100, -32); |
126 | fd_list.right = new FormAttachment(100, -89); |
127 | fd_list.top = new FormAttachment(0, 4); |
128 | fd_list.left = new FormAttachment(0, 5); |
129 | list.setLayoutData(fd_list); |
130 | |
131 | /** create a ListViewer */ |
132 | viewer = new TableViewer(list); |
133 | viewer.setContentProvider(new DatasourceListContentProvider()); |
134 | viewer.setLabelProvider(new DatasourceListLabelProvider()); |
135 | viewer.addSelectionChangedListener(new ISelectionChangedListener(){ |
136 | |
137 | public void selectionChanged(SelectionChangedEvent event) { |
138 | ISelection selection = event.getSelection(); |
139 | if (selection instanceof IStructuredSelection) { |
140 | IStructuredSelection sel = (IStructuredSelection) selection; |
141 | Object object = sel.getFirstElement(); |
142 | selectedDataSet = (IDAOFactory) object; |
143 | validationOKButton(selectedDataSet); |
144 | } |
145 | |
146 | } |
147 | }); |
148 | viewer.setInput(input); |
149 | |
150 | setTitle(DIALOG_TITLE); |
151 | |
152 | return container; |
153 | } |
154 | |
155 | /* (non-Javadoc) |
156 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) |
157 | */ |
158 | @Override |
159 | protected void createButtonsForButtonBar(Composite parent) { |
160 | createButton(parent, IDialogConstants.CANCEL_ID, |
161 | IDialogConstants.CANCEL_LABEL, false); |
162 | okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, |
163 | true); |
164 | validationOKButton(selectedDataSet); |
165 | } |
166 | |
167 | private void validationOKButton(Object selection) { |
168 | setErrorMessage(null); |
169 | okButton.setEnabled(true); |
170 | |
171 | if (buttonValidation && selection == null) { |
172 | okButton.setEnabled(false); |
173 | setErrorMessage("No Datasource selected!"); |
174 | } |
175 | |
176 | if (selection instanceof MemoryDAOFactory) { |
177 | MemoryDAOFactory memoryDAO = (MemoryDAOFactory) selection; |
178 | setTitle("Description:"); |
179 | setMessage(memoryDAO.getDescription()); |
180 | } |
181 | } |
182 | |
183 | protected void setAddButtonAction(SelectionListener listener){ |
184 | addButton.addSelectionListener(listener); |
185 | } |
186 | |
187 | protected void setRemoveButtonAction(SelectionListener listener){ |
188 | removeButton.addSelectionListener(listener); |
189 | } |
190 | |
191 | protected void setOpenButtonAction(SelectionListener listener){ |
192 | openButton.addSelectionListener(listener); |
193 | } |
194 | |
195 | /* (non-Javadoc) |
196 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#getInitialSize() |
197 | */ |
198 | @Override |
199 | protected Point getInitialSize() { |
200 | return new Point(400, 350); |
201 | } |
202 | |
203 | public Object getResult(){ |
204 | return selectedDataSet; |
205 | } |
206 | |
207 | protected void refresh(){ |
208 | viewer.refresh(); |
209 | } |
210 | } |