implemented 1416503, automatic closing of braces
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / widget / ComboViewer.java
1 package com.quantum.view.widget;
2
3 import org.eclipse.jface.viewers.ContentViewer;
4 import org.eclipse.jface.viewers.ILabelProvider;
5 import org.eclipse.jface.viewers.ISelection;
6 import org.eclipse.jface.viewers.IStructuredContentProvider;
7 import org.eclipse.jface.viewers.IStructuredSelection;
8 import org.eclipse.jface.viewers.SelectionChangedEvent;
9 import org.eclipse.jface.viewers.StructuredSelection;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.widgets.Combo;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16
17 /**
18  * <p>A Combo viewer.
19  * 
20  * <p>In Eclipse version 3.0, a standard JFace ComboViewer class exists.
21  * 
22  * @author BC Holmes
23  */
24 public class ComboViewer extends ContentViewer {
25
26         private Combo combo;
27
28         private Object[] objects = new Object[0];
29
30         public ComboViewer(Composite parent) {
31                 this.combo = new Combo(parent, SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY);
32                 this.combo.addSelectionListener(new SelectionAdapter() {
33                         public void widgetSelected(SelectionEvent event) {
34                                 ComboViewer.this.fireSelectionChanged(new SelectionChangedEvent(
35                                                 ComboViewer.this, getSelection()) );
36                         }
37                 });
38         }
39
40         public Control getControl() {
41                 return this.combo;
42         }
43
44         public ISelection getSelection() {
45                 int index = this.combo.getSelectionIndex();
46
47                 return (this.objects == null || index >= this.objects.length || index < 0) 
48                                 ? new StructuredSelection()
49                                 : new StructuredSelection(this.objects[index]);
50         }
51
52         protected void inputChanged(Object input, Object oldInput) {
53                 super.inputChanged(input, oldInput);
54
55                 if (input == null) {
56                         this.objects = new Object[0];
57                 } else {
58                         this.objects = ((IStructuredContentProvider) getContentProvider()).getElements(input);
59                         for (int i = 0, length = this.objects == null ? 0 : this.objects.length; i < length; i++) {
60                                 String label = ((ILabelProvider) getLabelProvider()).getText(this.objects[i]);
61                                 this.combo.add(label);
62                         }
63                 }
64         }
65         public void refresh() {
66         }
67
68         public void setSelection(ISelection selection, boolean reveal) {
69                 if (selection != null && selection instanceof IStructuredSelection) {
70                         Object object = ((IStructuredSelection) selection).getFirstElement();
71                         for (int i = 0, length = this.objects == null ? 0 : this.objects.length; 
72                                         object != null && i < length; i++) {
73                                 if (this.objects[i].equals(object)) {
74                                         this.combo.select(i);
75                                 }
76                         }
77                 }
78         }
79 }