1 11 package org.eclipse.jface.viewers; 12 13 import java.text.MessageFormat ; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.jface.resource.ImageRegistry; 17 import org.eclipse.jface.resource.JFaceResources; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.FocusEvent; 20 import org.eclipse.swt.events.FocusListener; 21 import org.eclipse.swt.events.KeyAdapter; 22 import org.eclipse.swt.events.KeyEvent; 23 import org.eclipse.swt.events.SelectionAdapter; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.graphics.Color; 26 import org.eclipse.swt.graphics.Font; 27 import org.eclipse.swt.graphics.Point; 28 import org.eclipse.swt.graphics.Rectangle; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Label; 33 import org.eclipse.swt.widgets.Layout; 34 35 53 public abstract class DialogCellEditor extends CellEditor { 54 55 58 public static final String CELL_EDITOR_IMG_DOTS_BUTTON = "cell_editor_dots_button_image"; 60 63 private Composite editor; 64 65 68 private Control contents; 69 70 73 private Label defaultLabel; 74 75 78 private Button button; 79 80 84 private FocusListener buttonFocusListener; 85 86 89 private Object value = null; 90 91 static { 92 ImageRegistry reg = JFaceResources.getImageRegistry(); 93 reg.put(CELL_EDITOR_IMG_DOTS_BUTTON, ImageDescriptor.createFromFile( 94 DialogCellEditor.class, "images/dots_button.gif")); } 96 97 100 private class DialogCellLayout extends Layout { 101 public void layout(Composite editor, boolean force) { 102 Rectangle bounds = editor.getClientArea(); 103 Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, force); 104 if (contents != null) { 105 contents.setBounds(0, 0, bounds.width - size.x, bounds.height); 106 } 107 button.setBounds(bounds.width - size.x, 0, size.x, bounds.height); 108 } 109 110 public Point computeSize(Composite editor, int wHint, int hHint, 111 boolean force) { 112 if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) { 113 return new Point(wHint, hHint); 114 } 115 Point contentsSize = contents.computeSize(SWT.DEFAULT, SWT.DEFAULT, 116 force); 117 Point buttonSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, 118 force); 119 Point result = new Point(buttonSize.x, Math.max(contentsSize.y, 123 buttonSize.y)); 124 return result; 125 } 126 } 127 128 131 private static final int defaultStyle = SWT.NONE; 132 133 137 public DialogCellEditor() { 138 setStyle(defaultStyle); 139 } 140 141 148 protected DialogCellEditor(Composite parent) { 149 this(parent, defaultStyle); 150 } 151 152 161 protected DialogCellEditor(Composite parent, int style) { 162 super(parent, style); 163 } 164 165 176 protected Button createButton(Composite parent) { 177 Button result = new Button(parent, SWT.DOWN); 178 result.setText("..."); return result; 180 } 181 182 196 protected Control createContents(Composite cell) { 197 defaultLabel = new Label(cell, SWT.LEFT); 198 defaultLabel.setFont(cell.getFont()); 199 defaultLabel.setBackground(cell.getBackground()); 200 return defaultLabel; 201 } 202 203 206 protected Control createControl(Composite parent) { 207 208 Font font = parent.getFont(); 209 Color bg = parent.getBackground(); 210 211 editor = new Composite(parent, getStyle()); 212 editor.setFont(font); 213 editor.setBackground(bg); 214 editor.setLayout(new DialogCellLayout()); 215 216 contents = createContents(editor); 217 updateContents(value); 218 219 button = createButton(editor); 220 button.setFont(font); 221 222 button.addKeyListener(new KeyAdapter() { 223 226 public void keyReleased(KeyEvent e) { 227 if (e.character == '\u001b') { fireCancelEditor(); 229 } 230 } 231 }); 232 233 button.addFocusListener(getButtonFocusListener()); 234 235 button.addSelectionListener(new SelectionAdapter() { 236 239 public void widgetSelected(SelectionEvent event) { 240 button.removeFocusListener(getButtonFocusListener()); 243 244 Object newValue = openDialogBox(editor); 245 246 button.addFocusListener(getButtonFocusListener()); 248 249 if (newValue != null) { 250 boolean newValidState = isCorrect(newValue); 251 if (newValidState) { 252 markDirty(); 253 doSetValue(newValue); 254 } else { 255 setErrorMessage(MessageFormat.format(getErrorMessage(), 257 new Object [] { newValue.toString() })); 258 } 259 fireApplyEditorValue(); 260 } 261 } 262 }); 263 264 setValueValid(true); 265 266 return editor; 267 } 268 269 276 public void deactivate() { 277 if (button != null && !button.isDisposed()) { 278 button.removeFocusListener(getButtonFocusListener()); 279 } 280 281 super.deactivate(); 282 } 283 284 287 protected Object doGetValue() { 288 return value; 289 } 290 291 295 protected void doSetFocus() { 296 button.setFocus(); 297 298 button.addFocusListener(getButtonFocusListener()); 300 } 301 302 306 private FocusListener getButtonFocusListener() { 307 if (buttonFocusListener == null) { 308 buttonFocusListener = new FocusListener() { 309 310 313 public void focusGained(FocusEvent e) { 314 } 316 317 320 public void focusLost(FocusEvent e) { 321 DialogCellEditor.this.focusLost(); 322 } 323 }; 324 } 325 326 return buttonFocusListener; 327 } 328 329 332 protected void doSetValue(Object value) { 333 this.value = value; 334 updateContents(value); 335 } 336 337 342 protected Label getDefaultLabel() { 343 return defaultLabel; 344 } 345 346 361 protected abstract Object openDialogBox(Control cellEditorWindow); 362 363 377 protected void updateContents(Object value) { 378 if (defaultLabel == null) { 379 return; 380 } 381 382 String text = ""; if (value != null) { 384 text = value.toString(); 385 } 386 defaultLabel.setText(text); 387 } 388 } 389 | Popular Tags |