1 package sourceforge.org.qmc2.options.editor.ui;
2 
3 import org.eclipse.core.commands.ExecutionException;
4 import org.eclipse.core.commands.operations.IUndoableOperation;
5 import org.eclipse.core.runtime.NullProgressMonitor;
6 import org.eclipse.jface.viewers.CellEditor;
7 import org.eclipse.jface.viewers.EditingSupport;
8 import org.eclipse.jface.viewers.TextCellEditor;
9 import org.eclipse.swt.widgets.Composite;
10 
11 import sourceforge.org.qmc2.options.editor.model.DescriptableItem;
12 import sourceforge.org.qmc2.options.editor.ui.operations.EditOperation;
13 
14 public class QMC2EditingSupport extends EditingSupport {
15 
16 	private final String lang;
17 
18 	private final CellEditor editor;
19 
20 	private final QMC2Editor qmc2Editor;
21 
QMC2EditingSupport(QMC2Editor editor, String language)22 	public QMC2EditingSupport(QMC2Editor editor, String language) {
23 		super(editor.getViewer());
24 		this.lang = language;
25 		this.qmc2Editor = editor;
26 		this.editor = new TextCellEditor((Composite) editor.getViewer()
27 				.getControl());
28 
29 	}
30 
31 	@Override
canEdit(Object item)32 	protected boolean canEdit(Object item) {
33 		return item instanceof DescriptableItem;
34 	}
35 
36 	@Override
getCellEditor(Object item)37 	protected CellEditor getCellEditor(Object item) {
38 		return editor;
39 	}
40 
41 	@Override
getValue(Object item)42 	protected Object getValue(Object item) {
43 		String value = null;
44 		if (item instanceof DescriptableItem) {
45 			value = ((DescriptableItem) item).getDescription(lang);
46 		}
47 
48 		return value == null ? "" : value;
49 	}
50 
51 	@Override
setValue(Object item, Object value)52 	protected void setValue(Object item, Object value) {
53 		if (item instanceof DescriptableItem) {
54 			String oldValue = ((DescriptableItem) item).getDescription(lang);
55 			if ((value != null && oldValue != null && !oldValue.equals(value
56 					.toString()))
57 					|| (value != null && oldValue == null)
58 					|| (value == null && oldValue != null)) {
59 				IUndoableOperation operation = new EditOperation(qmc2Editor,
60 						(DescriptableItem) item, lang, value.toString());
61 				try {
62 					qmc2Editor.getOperationHistory().execute(operation,
63 							new NullProgressMonitor(), null);
64 				} catch (ExecutionException e) {
65 					// TODO Auto-generated catch block
66 					e.printStackTrace();
67 				}
68 
69 			}
70 
71 		}
72 		qmc2Editor.updateApplicationMenuBar();
73 
74 	}
75 }
76