1 package sourceforge.org.qmc2.options.editor.ui.dialogs;
2 
3 import org.eclipse.jface.dialogs.IMessageProvider;
4 import org.eclipse.jface.dialogs.TitleAreaDialog;
5 import org.eclipse.jface.viewers.ColumnLabelProvider;
6 import org.eclipse.jface.viewers.ISelection;
7 import org.eclipse.jface.viewers.IStructuredContentProvider;
8 import org.eclipse.jface.viewers.IStructuredSelection;
9 import org.eclipse.jface.viewers.TableViewer;
10 import org.eclipse.jface.viewers.TableViewerColumn;
11 import org.eclipse.jface.viewers.Viewer;
12 import org.eclipse.jface.window.Window;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ControlEvent;
15 import org.eclipse.swt.events.ControlListener;
16 import org.eclipse.swt.events.ModifyEvent;
17 import org.eclipse.swt.events.ModifyListener;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.layout.RowData;
23 import org.eclipse.swt.layout.RowLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30 
31 import sourceforge.org.qmc2.options.editor.model.Option;
32 import sourceforge.org.qmc2.options.editor.model.Section;
33 
34 public class AddSectionDialog extends TitleAreaDialog {
35 
36 	private Section section;
37 
38 	private static final String defaultMessage = "Create a new section in QMC2 template file";
39 
40 	private Text sectionNameText;
41 
42 	private Text descriptionText;
43 
44 	private TableViewer viewer;
45 
AddSectionDialog(Shell parentShell, Section section)46 	public AddSectionDialog(Shell parentShell, Section section) {
47 		super(parentShell);
48 		if (section != null) {
49 			this.section = section;
50 		} else {
51 			this.section = new Section(null);
52 		}
53 	}
54 
getSection()55 	public Section getSection() {
56 		return section;
57 	}
58 
59 	@Override
createDialogArea(Composite parent)60 	protected Control createDialogArea(Composite parent) {
61 
62 		Composite mainComposite = new Composite(
63 				(Composite) super.createDialogArea(parent), SWT.NONE);
64 		mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
65 		mainComposite.setLayout(new GridLayout(3, false));
66 
67 		Label sectionName = new Label(mainComposite, SWT.NONE);
68 		sectionName.setText("Name: ");
69 		sectionName.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
70 				1, 1));
71 
72 		sectionNameText = new Text(mainComposite, SWT.BORDER);
73 		sectionNameText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
74 				false, 2, 1));
75 		if (section.getName() != null) {
76 			sectionNameText.setText(section.getName());
77 		}
78 		sectionNameText.addModifyListener(new ModifyListener() {
79 
80 			@Override
81 			public void modifyText(ModifyEvent arg0) {
82 				validate();
83 			}
84 		});
85 
86 		Label description = new Label(mainComposite, SWT.NONE);
87 		description.setText("Description: ");
88 		description.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
89 				1, 1));
90 
91 		descriptionText = new Text(mainComposite, SWT.BORDER);
92 		descriptionText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
93 				false, 2, 1));
94 		if (section.getDescription("us") != null) {
95 			descriptionText.setText(section.getDescription("us"));
96 		}
97 		descriptionText.addModifyListener(new ModifyListener() {
98 
99 			@Override
100 			public void modifyText(ModifyEvent arg0) {
101 				validate();
102 			}
103 		});
104 
105 		createOptionsSection(mainComposite);
106 
107 		setTitle("Add Section");
108 		setMessage(defaultMessage);
109 
110 		return mainComposite;
111 	}
112 
createOptionsSection(Composite parent)113 	private void createOptionsSection(Composite parent) {
114 		viewer = new TableViewer(parent, SWT.V_SCROLL | SWT.H_SCROLL
115 				| SWT.MULTI | SWT.READ_ONLY | SWT.BORDER);
116 
117 		viewer.getTable().setLayoutData(
118 				new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
119 		viewer.getTable().setHeaderVisible(true);
120 		viewer.getTable().setLinesVisible(true);
121 		viewer.setContentProvider(new OptionContentProvider());
122 
123 		final TableViewerColumn optionNameColumn = new TableViewerColumn(
124 				viewer, SWT.NONE);
125 		optionNameColumn.getColumn().setMoveable(false);
126 		optionNameColumn.getColumn().setText("Name");
127 		optionNameColumn.setLabelProvider(new OptionNameLabelProvider());
128 
129 		final TableViewerColumn optionTypeColumn = new TableViewerColumn(
130 				viewer, SWT.NONE);
131 		optionTypeColumn.getColumn().setMoveable(false);
132 		optionTypeColumn.getColumn().setText("Type");
133 		optionTypeColumn.setLabelProvider(new OptionTypeLabelProvider());
134 
135 		final TableViewerColumn defaultValueColumn = new TableViewerColumn(
136 				viewer, SWT.NONE);
137 		defaultValueColumn.getColumn().setMoveable(false);
138 		defaultValueColumn.getColumn().setText("Default value");
139 		defaultValueColumn
140 				.setLabelProvider(new OptionDefaultValueLabelProvider());
141 
142 		final TableViewerColumn optionDescriptionColumn = new TableViewerColumn(
143 				viewer, SWT.NONE);
144 		optionDescriptionColumn.getColumn().setMoveable(false);
145 		optionDescriptionColumn.getColumn().setText("Description");
146 		optionDescriptionColumn
147 				.setLabelProvider(new OptionDescriptionLabelProvider());
148 
149 		viewer.getTable().addControlListener(new ControlListener() {
150 
151 			@Override
152 			public void controlResized(ControlEvent arg0) {
153 				optionNameColumn.getColumn().setWidth(
154 						viewer.getTable().getSize().x / 6);
155 				optionTypeColumn.getColumn().setWidth(
156 						viewer.getTable().getSize().x / 6);
157 				optionDescriptionColumn.getColumn().setWidth(
158 						viewer.getTable().getSize().x / 3);
159 				defaultValueColumn.getColumn().setWidth(
160 						viewer.getTable().getSize().x / 3 - 10);
161 			}
162 
163 			@Override
164 			public void controlMoved(ControlEvent arg0) {
165 				// do nothing
166 			}
167 		});
168 
169 		Composite buttonsComposite = new Composite(parent, SWT.NONE);
170 		buttonsComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false,
171 				false, 1, 1));
172 		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
173 		rowLayout.pack = false;
174 		buttonsComposite.setLayout(rowLayout);
175 
176 		Button add = new Button(buttonsComposite, SWT.PUSH);
177 		add.setText("Add Option...");
178 		add.setLayoutData(new RowData());
179 		add.addSelectionListener(new SelectionAdapter() {
180 			@Override
181 			public void widgetSelected(SelectionEvent e) {
182 				AddOptionDialog option = new AddOptionDialog(getShell(), null);
183 				if (option.open() == Window.OK) {
184 					section.addOption(option.getOption());
185 					viewer.refresh();
186 				}
187 			}
188 		});
189 
190 		Button remove = new Button(buttonsComposite, SWT.PUSH);
191 		remove.setText("Remove Option...");
192 		remove.setLayoutData(new RowData());
193 		remove.addSelectionListener(new SelectionAdapter() {
194 			@Override
195 			public void widgetSelected(SelectionEvent e) {
196 				ISelection selection = viewer.getSelection();
197 				if (selection instanceof IStructuredSelection) {
198 					IStructuredSelection sselection = (IStructuredSelection) selection;
199 					for (Object o : sselection.toArray()) {
200 						section.removeOption(((Option) o).getName());
201 					}
202 
203 					viewer.refresh();
204 				}
205 			}
206 		});
207 
208 		Button edit = new Button(buttonsComposite, SWT.PUSH);
209 		edit.setText("Edit Option...");
210 		edit.setLayoutData(new RowData());
211 		edit.setEnabled(false);
212 
213 		viewer.setInput(section);
214 	}
215 
validate()216 	private void validate() {
217 		String errorMessage = null;
218 		int errorStatus = IMessageProvider.NONE;
219 		if (sectionNameText.getText() == null
220 				|| sectionNameText.getText().trim().length() == 0) {
221 			errorMessage = "You must enter a valid section name";
222 			errorStatus = IMessageProvider.ERROR;
223 		} else if (sectionNameText.getText().contains(" ")) {
224 			errorMessage = "The section name must not contain whitespaces";
225 			errorStatus = IMessageProvider.ERROR;
226 		}
227 
228 		if (errorMessage == null
229 				&& (descriptionText.getText() == null || descriptionText
230 						.getText().trim().length() == 0)) {
231 			errorMessage = "You must enter a valid description";
232 			errorStatus = IMessageProvider.ERROR;
233 		}
234 
235 		setMessage(errorStatus == IMessageProvider.ERROR ? errorMessage
236 				: defaultMessage, errorStatus);
237 		if (getButton(OK) != null) {
238 			getButton(OK).setEnabled(errorStatus != IMessageProvider.ERROR);
239 		}
240 
241 	}
242 
243 	@Override
isResizable()244 	protected boolean isResizable() {
245 		return true;
246 	}
247 
248 	@Override
okPressed()249 	protected void okPressed() {
250 		section.setName(sectionNameText.getText());
251 		section.setDescription("us", descriptionText.getText());
252 		super.okPressed();
253 	}
254 
255 	private class OptionContentProvider implements IStructuredContentProvider {
256 
257 		@Override
dispose()258 		public void dispose() {
259 			// do nothing
260 		}
261 
262 		@Override
inputChanged(Viewer viewer, Object oldInput, Object newInput)263 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
264 			// do nothing
265 		}
266 
267 		@Override
getElements(Object inputElement)268 		public Object[] getElements(Object inputElement) {
269 			return section.getOptions().toArray(new Option[0]);
270 		}
271 	}
272 
273 	private class OptionNameLabelProvider extends ColumnLabelProvider {
274 		@Override
getText(Object element)275 		public String getText(Object element) {
276 			return ((Option) element).getName();
277 		}
278 	}
279 
280 	private class OptionTypeLabelProvider extends ColumnLabelProvider {
281 		@Override
getText(Object element)282 		public String getText(Object element) {
283 			return ((Option) element).getType();
284 		}
285 	}
286 
287 	private class OptionDescriptionLabelProvider extends ColumnLabelProvider {
288 		@Override
getText(Object element)289 		public String getText(Object element) {
290 			return ((Option) element).getDescription("us");
291 		}
292 	}
293 
294 	private class OptionDefaultValueLabelProvider extends ColumnLabelProvider {
295 		@Override
getText(Object element)296 		public String getText(Object element) {
297 			return ((Option) element).getDefaultValue();
298 		}
299 	}
300 }
301