1 | package de.uka.ipd.sdq.pcm.dialogs.exception; |
2 | |
3 | import java.util.ArrayList; |
4 | |
5 | import org.eclipse.emf.ecore.EReference; |
6 | import org.eclipse.emf.edit.provider.ComposedAdapterFactory; |
7 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
8 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
9 | import org.eclipse.emf.transaction.RecordingCommand; |
10 | import org.eclipse.emf.transaction.TransactionalEditingDomain; |
11 | import org.eclipse.emf.transaction.util.TransactionUtil; |
12 | import org.eclipse.jface.dialogs.IDialogConstants; |
13 | import org.eclipse.jface.dialogs.TitleAreaDialog; |
14 | import org.eclipse.jface.viewers.CellEditor; |
15 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
16 | import org.eclipse.jface.viewers.IStructuredSelection; |
17 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
18 | import org.eclipse.jface.viewers.TableViewer; |
19 | import org.eclipse.jface.viewers.TextCellEditor; |
20 | import org.eclipse.swt.SWT; |
21 | import org.eclipse.swt.events.ModifyEvent; |
22 | import org.eclipse.swt.events.ModifyListener; |
23 | import org.eclipse.swt.events.SelectionAdapter; |
24 | import org.eclipse.swt.graphics.Point; |
25 | import org.eclipse.swt.layout.GridData; |
26 | import org.eclipse.swt.layout.GridLayout; |
27 | import org.eclipse.swt.widgets.Composite; |
28 | import org.eclipse.swt.widgets.Control; |
29 | import org.eclipse.swt.widgets.Group; |
30 | import org.eclipse.swt.widgets.Shell; |
31 | import org.eclipse.swt.widgets.Table; |
32 | import org.eclipse.swt.widgets.TableColumn; |
33 | import org.eclipse.swt.widgets.Text; |
34 | import org.eclipse.swt.widgets.ToolBar; |
35 | import org.eclipse.swt.widgets.ToolItem; |
36 | |
37 | import de.uka.ipd.sdq.pcm.dialogs.DialogsImages; |
38 | import de.uka.ipd.sdq.dialogs.selection.FilteredItemsAdapterFactory; |
39 | import de.uka.ipd.sdq.pcm.repository.ExceptionType; |
40 | import de.uka.ipd.sdq.pcm.repository.Signature; |
41 | import de.uka.ipd.sdq.pcm.repository.provider.RepositoryItemProviderAdapterFactory; |
42 | import de.uka.ipd.sdq.pcmbench.ui.provider.PalladioItemProviderAdapterFactory; |
43 | |
44 | public class ExceptionsDialog extends TitleAreaDialog { |
45 | |
46 | private Signature signature; |
47 | private ExceptionType selecedExceptionType; |
48 | private Text messageField; |
49 | private SelectionAdapter deleteActionAdapter; |
50 | |
51 | public static final int ICON_COLUMN_INDEX = 0; |
52 | public static final int CONTEXT_COLUMN_INDEX = 1; |
53 | public static final int NAME_COLUMN_INDEX = 2; |
54 | |
55 | /** |
56 | * Columns of a table, which is used into ParameterEditDialog |
57 | */ |
58 | public final static String ICON_COLUMN = ""; |
59 | public final static String CONTEXT_COLUMN = "Context"; |
60 | public final static String NAME_COLUMN = "Name"; |
61 | |
62 | private final String TITLE_DIALOG = "Create/Edit a ExceptionType..."; |
63 | |
64 | // Set column names of Table |
65 | private static String[] columnNames = new String[] { ICON_COLUMN, |
66 | CONTEXT_COLUMN, NAME_COLUMN }; |
67 | |
68 | private TableViewer viewer; |
69 | |
70 | /** |
71 | * The transactional editing domain which is used to get the commands and |
72 | * alter the model |
73 | */ |
74 | protected TransactionalEditingDomain editingDomain = null; |
75 | |
76 | |
77 | public ExceptionsDialog(Shell parentShell, Signature signature) { |
78 | super(parentShell); |
79 | this.signature = signature; |
80 | this.deleteActionAdapter = new DeleteExctentionAction(signature); |
81 | this.editingDomain = TransactionUtil.getEditingDomain(signature); |
82 | |
83 | /** |
84 | * the result of combining the constants which are required |
85 | * to produce a typical application top level shell |
86 | */ |
87 | setShellStyle(SWT.RESIZE|SWT.TITLE|SWT.CLOSE |SWT.MIN|SWT.MAX); |
88 | } |
89 | |
90 | /* (non-Javadoc) |
91 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
92 | */ |
93 | @Override |
94 | protected void configureShell(Shell newShell) { |
95 | super.configureShell(newShell); |
96 | newShell.setText("Exceptions"); |
97 | } |
98 | |
99 | |
100 | /* (non-Javadoc) |
101 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
102 | */ |
103 | @Override |
104 | protected Control createDialogArea(Composite parent) { |
105 | |
106 | Composite area = (Composite) super.createDialogArea(parent); |
107 | |
108 | Composite container = new Composite(area, SWT.NONE); |
109 | container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
110 | container.setLayout(new GridLayout()); |
111 | |
112 | final Group exceptionsGroup = new Group(container, SWT.NONE); |
113 | final GridLayout gridLayout = new GridLayout(); |
114 | gridLayout.numColumns = 2; |
115 | exceptionsGroup.setLayout(gridLayout); |
116 | final GridData gd_group = new GridData(SWT.FILL, SWT.FILL, true, true); |
117 | gd_group.widthHint = 476; |
118 | exceptionsGroup.setLayoutData(gd_group); |
119 | exceptionsGroup.setText("Exeptions"); |
120 | |
121 | final Table table = new Table(exceptionsGroup, SWT.BORDER|SWT.FULL_SELECTION); |
122 | table.setLinesVisible(true); |
123 | table.setHeaderVisible(true); |
124 | final GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true); |
125 | gd_table.widthHint = 401; |
126 | table.setLayoutData(gd_table); |
127 | |
128 | // Definere the table columns |
129 | final TableColumn zeroColumn = new TableColumn(table, SWT.NONE); |
130 | zeroColumn.setResizable(false); |
131 | zeroColumn.setWidth(30); |
132 | |
133 | final TableColumn contextColumn = new TableColumn(table, SWT.NONE); |
134 | contextColumn.setWidth(100); |
135 | contextColumn.setText(CONTEXT_COLUMN); |
136 | |
137 | final TableColumn nameColumn = new TableColumn(table, SWT.NONE); |
138 | nameColumn.setWidth(140); |
139 | nameColumn.setText(NAME_COLUMN); |
140 | |
141 | /** set create TableViewer instance*/ |
142 | createTableViewer(table); |
143 | |
144 | final ToolBar toolBar = new ToolBar(exceptionsGroup, SWT.VERTICAL); |
145 | final GridData gd_toolBar = new GridData(SWT.FILL, SWT.FILL, false, true); |
146 | toolBar.setLayoutData(gd_toolBar); |
147 | |
148 | |
149 | ToolItem addItem = new ToolItem(toolBar, SWT.PUSH); |
150 | addItem.setImage(DialogsImages.imageRegistry.get(DialogsImages.ADD)); |
151 | addItem.addSelectionListener(new AddExceptionTypeAction(signature)); |
152 | |
153 | ToolItem deleteItem = new ToolItem(toolBar, SWT.PUSH); |
154 | deleteItem.setImage(DialogsImages.imageRegistry |
155 | .get(DialogsImages.DELETE)); |
156 | deleteItem.addSelectionListener(deleteActionAdapter); |
157 | |
158 | final Group exceptionMessageGroup = new Group(container, SWT.NONE); |
159 | exceptionMessageGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
160 | exceptionMessageGroup.setText("Exception Message"); |
161 | exceptionMessageGroup.setLayout(new GridLayout()); |
162 | |
163 | messageField = new Text(exceptionMessageGroup, SWT.BORDER | SWT.MULTI); |
164 | final GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false); |
165 | gd_text.heightHint = 60; |
166 | messageField.setLayoutData(gd_text); |
167 | messageField.addModifyListener(new ModifyListener(){ |
168 | |
169 | /* (non-Javadoc) |
170 | * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) |
171 | */ |
172 | public void modifyText(ModifyEvent e) { |
173 | setExceptionTypeMessage(messageField.getText()); |
174 | } |
175 | }); |
176 | |
177 | setTitle(TITLE_DIALOG); |
178 | |
179 | return container; |
180 | } |
181 | |
182 | /** Initialization of TableViewer */ |
183 | private void createTableViewer(Table table) { |
184 | |
185 | |
186 | ArrayList<Object> filterList = new ArrayList<Object>(); |
187 | filterList.add(ExceptionType.class); |
188 | ArrayList<EReference> additionalReferences = new ArrayList<EReference>(); |
189 | |
190 | viewer = new TableViewer(table); |
191 | viewer.setColumnProperties(columnNames); |
192 | |
193 | /** Create the cell editors for Name column */ |
194 | CellEditor[] editors = new CellEditor[columnNames.length]; |
195 | |
196 | editors[NAME_COLUMN_INDEX] = new TextCellEditor(table); |
197 | |
198 | /** Assign the cell editors to the viewer */ |
199 | viewer.setCellEditors(editors); |
200 | viewer.setCellModifier(new ExceptionsCellModifier(viewer, TransactionUtil |
201 | .getEditingDomain(signature))); |
202 | viewer.addSelectionChangedListener((ISelectionChangedListener) deleteActionAdapter); |
203 | |
204 | ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(); |
205 | adapterFactory |
206 | .addAdapterFactory(new RepositoryItemProviderAdapterFactory()); |
207 | viewer.setContentProvider(new AdapterFactoryContentProvider( |
208 | new FilteredItemsAdapterFactory(adapterFactory, filterList, |
209 | additionalReferences))); |
210 | viewer |
211 | .setLabelProvider(new AdapterFactoryLabelProvider( |
212 | new ExceptionsItemProviderAdapterFactory( |
213 | new PalladioItemProviderAdapterFactory( |
214 | adapterFactory)))); |
215 | viewer.addSelectionChangedListener(new ISelectionChangedListener() { |
216 | public void selectionChanged(SelectionChangedEvent event) { |
217 | IStructuredSelection sel = (IStructuredSelection) event |
218 | .getSelection(); |
219 | Object selection = (Object) sel.getFirstElement(); |
220 | selecedExceptionType = (ExceptionType) selection; |
221 | if (selecedExceptionType != null |
222 | && selecedExceptionType.getExceptionMessage() != null) |
223 | messageField.setText(selecedExceptionType |
224 | .getExceptionMessage()); |
225 | else |
226 | messageField.setText(""); |
227 | } |
228 | }); |
229 | viewer.setInput(signature); |
230 | } |
231 | |
232 | /* (non-Javadoc) |
233 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) |
234 | */ |
235 | @Override |
236 | protected void createButtonsForButtonBar(Composite parent) { |
237 | createButton(parent, IDialogConstants.CANCEL_ID, |
238 | IDialogConstants.CANCEL_LABEL, false); |
239 | createButton(parent, IDialogConstants.OK_ID, |
240 | IDialogConstants.OK_LABEL, true); |
241 | } |
242 | |
243 | /* (non-Javadoc) |
244 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#getInitialSize() |
245 | */ |
246 | @Override |
247 | protected Point getInitialSize() { |
248 | return new Point(500, 375); |
249 | } |
250 | |
251 | public static String[] getColumnNames() { |
252 | return columnNames; |
253 | } |
254 | |
255 | /** Set a message of ExceptionType*/ |
256 | private void setExceptionTypeMessage(final String msg){ |
257 | RecordingCommand recCommand = new RecordingCommand(editingDomain) { |
258 | @Override |
259 | protected void doExecute() { |
260 | selecedExceptionType.setExceptionMessage(msg); |
261 | } |
262 | }; |
263 | |
264 | recCommand.setDescription("Delete ..."); |
265 | editingDomain.getCommandStack().execute(recCommand); |
266 | } |
267 | |
268 | } |