| 1 | package de.uka.ipd.sdq.pcmbench.tabs.operations; |
| 2 | |
| 3 | import java.text.MessageFormat; |
| 4 | |
| 5 | import org.eclipse.jface.viewers.DialogCellEditor; |
| 6 | import org.eclipse.swt.SWT; |
| 7 | import org.eclipse.swt.events.FocusEvent; |
| 8 | import org.eclipse.swt.events.FocusListener; |
| 9 | import org.eclipse.swt.events.KeyAdapter; |
| 10 | import org.eclipse.swt.events.KeyEvent; |
| 11 | import org.eclipse.swt.events.SelectionAdapter; |
| 12 | import org.eclipse.swt.events.SelectionEvent; |
| 13 | import org.eclipse.swt.events.SelectionListener; |
| 14 | import org.eclipse.swt.graphics.Color; |
| 15 | import org.eclipse.swt.graphics.Font; |
| 16 | import org.eclipse.swt.widgets.Button; |
| 17 | import org.eclipse.swt.widgets.Composite; |
| 18 | import org.eclipse.swt.widgets.Control; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * @author Roman Andrej |
| 23 | * |
| 24 | */ |
| 25 | public abstract class TypeDialogCellEditor extends DialogCellEditor { |
| 26 | |
| 27 | /** The editor control. */ |
| 28 | private Composite editor; |
| 29 | |
| 30 | /** The current contents. */ |
| 31 | private Control contents; |
| 32 | |
| 33 | /** The button. */ |
| 34 | private Button selButton; |
| 35 | private Button delButton; |
| 36 | |
| 37 | /** |
| 38 | * Listens for 'focusLost' events and fires the 'apply' event as long |
| 39 | * as the focus wasn't lost because the dialog was opened. |
| 40 | */ |
| 41 | private FocusListener buttonFocusListener; |
| 42 | |
| 43 | /** |
| 44 | * The value of this cell editor; initially <code>null</code>. |
| 45 | */ |
| 46 | private Object value = null; |
| 47 | |
| 48 | |
| 49 | /* @See org.eclipse.jface.viewers.DialogCellEditor#DialogCellEditor(org.eclipse.swt.widgets.Control |
| 50 | * parent) |
| 51 | */ |
| 52 | public TypeDialogCellEditor(Composite composite, |
| 53 | SelectionListener cellValueListener) { |
| 54 | super(composite, SWT.DEL); |
| 55 | |
| 56 | if (delButton != null) { |
| 57 | delButton.addSelectionListener(cellValueListener); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* (non-Javadoc) |
| 62 | * @see org.eclipse.jface.viewers.DialogCellEditor#createControl(org.eclipse.swt.widgets.Composite) |
| 63 | */ |
| 64 | @Override |
| 65 | protected Control createControl(Composite parent) { |
| 66 | |
| 67 | Font font = parent.getFont(); |
| 68 | Color bg = parent.getBackground(); |
| 69 | |
| 70 | editor = new Composite(parent, getStyle()); |
| 71 | editor.setFont(font); |
| 72 | editor.setBackground(bg); |
| 73 | |
| 74 | contents = createContents(editor); |
| 75 | updateContents(value); |
| 76 | |
| 77 | delButton = new Button(editor, SWT.DOWN); |
| 78 | delButton.setText("X"); |
| 79 | delButton.setFont(font); |
| 80 | |
| 81 | selButton = createButton(editor); |
| 82 | selButton.setFont(font); |
| 83 | editor.setLayout(new DialogCellLayout(contents, selButton, delButton)); |
| 84 | selButton.addKeyListener(new KeyAdapter() { |
| 85 | |
| 86 | /* (non-Javadoc) |
| 87 | * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent) |
| 88 | */ |
| 89 | @Override |
| 90 | public void keyReleased(KeyEvent e) { |
| 91 | if (e.character == '\u001b') { // Escape |
| 92 | fireCancelEditor(); |
| 93 | } |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | selButton.addFocusListener(getButtonFocusListener()); |
| 98 | selButton.addSelectionListener(new SelectionAdapter() { |
| 99 | |
| 100 | /* (non-Javadoc) |
| 101 | * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
| 102 | */ |
| 103 | @Override |
| 104 | public void widgetSelected(SelectionEvent event) { |
| 105 | // Remove the button's focus listener since it's guaranteed |
| 106 | // to lose focus when the dialog opens |
| 107 | selButton.removeFocusListener(getButtonFocusListener()); |
| 108 | |
| 109 | Object newValue = openDialogBox(editor); |
| 110 | |
| 111 | // Re-add the listener once the dialog closes |
| 112 | selButton.addFocusListener(getButtonFocusListener()); |
| 113 | |
| 114 | if (newValue != null) { |
| 115 | boolean newValidState = isCorrect(newValue); |
| 116 | if (newValidState) { |
| 117 | markDirty(); |
| 118 | doSetValue(newValue); |
| 119 | } else { |
| 120 | // try to insert the current value into the error |
| 121 | // message. |
| 122 | setErrorMessage(MessageFormat.format(getErrorMessage(), |
| 123 | new Object[] { newValue.toString() })); |
| 124 | } |
| 125 | fireApplyEditorValue(); |
| 126 | } |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | setValueValid(true); |
| 131 | |
| 132 | return editor; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Return a listener for button focus. |
| 138 | * |
| 139 | * @return FocusListener |
| 140 | */ |
| 141 | private FocusListener getButtonFocusListener() { |
| 142 | if (buttonFocusListener == null) { |
| 143 | buttonFocusListener = new FocusListener() { |
| 144 | |
| 145 | /* (non-Javadoc) |
| 146 | * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent) |
| 147 | */ |
| 148 | public void focusGained(FocusEvent e) { |
| 149 | // Do nothing |
| 150 | } |
| 151 | |
| 152 | /* (non-Javadoc) |
| 153 | * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent) |
| 154 | */ |
| 155 | public void focusLost(FocusEvent e) { |
| 156 | TypeDialogCellEditor.this.focusLost(); |
| 157 | } |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | return buttonFocusListener; |
| 162 | } |
| 163 | |
| 164 | /* (non-Javadoc) |
| 165 | * @see org.eclipse.jface.viewers.DialogCellEditor#doSetFocus() |
| 166 | */ |
| 167 | @Override |
| 168 | protected void doSetFocus() { |
| 169 | selButton.setFocus(); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | protected void doSetValue(Object value) { |
| 174 | super.doSetValue(value); |
| 175 | } |
| 176 | } |