~ubuntu-branches/ubuntu/utopic/eclipse-eclox/utopic

« back to all changes in this revision

Viewing changes to eclox.ui/src/eclox/ui/editor/basic/ComboMultiEditor.java

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-07 20:33:10 UTC
  • Revision ID: package-import@ubuntu.com-20130707203310-a44yw80gqtc2s9ob
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2003-2007, 2013, Guillaume Brocker
 
3
 * 
 
4
 * All rights reserved. This program and the accompanying materials
 
5
 * are made available under the terms of the Eclipse Public License v1.0
 
6
 * which accompanies this distribution, and is available at
 
7
 * http://www.eclipse.org/legal/epl-v10.html
 
8
 *
 
9
 * Contributors:
 
10
 *     Guillaume Brocker - Initial API and implementation
 
11
 *
 
12
 ******************************************************************************/ 
 
13
 
 
14
package eclox.ui.editor.basic;
 
15
 
 
16
import org.eclipse.swt.SWT;
 
17
import org.eclipse.swt.events.SelectionEvent;
 
18
import org.eclipse.swt.events.SelectionListener;
 
19
import org.eclipse.swt.layout.FillLayout;
 
20
import org.eclipse.swt.widgets.Combo;
 
21
import org.eclipse.swt.widgets.Composite;
 
22
import org.eclipse.ui.forms.widgets.FormToolkit;
 
23
 
 
24
/**
 
25
 * Implements a multi editor that presents choices in a combo box.
 
26
 * 
 
27
 * @author Guillaume Brocker
 
28
 */
 
29
public class ComboMultiEditor extends MultiEditor {
 
30
        
 
31
        /**
 
32
         * Implements a selection listener that will handle selection changes in the combo control.
 
33
         */
 
34
        private class MySelectionListener implements SelectionListener {
 
35
 
 
36
                /**
 
37
                 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
 
38
                 */
 
39
                public void widgetDefaultSelected(SelectionEvent e) {
 
40
                        widgetSelected(e);
 
41
                }
 
42
 
 
43
                /**
 
44
                 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
 
45
                 */
 
46
                public void widgetSelected(SelectionEvent e) {
 
47
                        selectState( combo.getItem(combo.getSelectionIndex()) );
 
48
                        commit();
 
49
                }
 
50
                
 
51
        }
 
52
        /**
 
53
         * the combo control that is the representation of the editor
 
54
         */
 
55
        Combo combo;
 
56
        
 
57
        /**
 
58
         * Constructor
 
59
         * 
 
60
         * @param       states  an array of string representing all posible states
 
61
         */
 
62
        ComboMultiEditor( String [] states ) {
 
63
                super( states );
 
64
        }
 
65
 
 
66
        /**
 
67
         * @see eclox.ui.editor.editors.IEditor#createContent(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit)
 
68
         */
 
69
        public void createContent(Composite parent, FormToolkit formToolkit) {
 
70
                // Pre-condition
 
71
                assert combo != null;
 
72
                
 
73
                // Creates the combo control.
 
74
                combo = new Combo(parent, SWT.DROP_DOWN|SWT.READ_ONLY);
 
75
                combo.addSelectionListener( new MySelectionListener() );
 
76
                formToolkit.adapt(combo, true, true);
 
77
                
 
78
                // Fills the combo with the state names.
 
79
                for( int i = 0; i != states.length; ++i ) {
 
80
                        combo.add( states[i].getName() );
 
81
                }
 
82
                combo.setVisibleItemCount(states.length);
 
83
                
 
84
                // Installs a layout in the parent composite
 
85
                parent.setLayout(new FillLayout());
 
86
        }
 
87
 
 
88
        /**
 
89
         * @see eclox.ui.editor.editors.IEditor#dispose()
 
90
         */
 
91
        public void dispose() {
 
92
                combo = null;
 
93
        }
 
94
 
 
95
        /**
 
96
         * @see eclox.ui.editor.editors.IEditor#grabVerticalSpace()
 
97
         */
 
98
        public boolean grabVerticalSpace() {
 
99
                return false;
 
100
        }
 
101
        
 
102
        /**
 
103
         * @see eclox.ui.editor.basic.MultiEditor#refresh()
 
104
         */
 
105
        public void refresh() {
 
106
                // Pre-condition
 
107
                assert combo != null;
 
108
                
 
109
                super.refresh();
 
110
                
 
111
                // Selectes the string corresponding to the current selection
 
112
                State   selection = getSelectionAsState();
 
113
                if( selection != null ) {
 
114
                        combo.select( combo.indexOf(selection.getName()) );
 
115
                }
 
116
                else {
 
117
                        combo.deselectAll();
 
118
                }
 
119
        }
 
120
 
 
121
        /**
 
122
         * @see eclox.ui.editor.editors.IEditor#setEnabled(boolean)
 
123
         */
 
124
        public void setEnabled(boolean enabled) {
 
125
                // Pre-condition
 
126
                assert combo != null;
 
127
                
 
128
                combo.setEnabled(enabled);
 
129
        }
 
130
 
 
131
        /**
 
132
         * @see eclox.ui.editor.editors.IEditor#setFocus()
 
133
         */
 
134
        public void setFocus() {
 
135
                // Pre-condition
 
136
                assert combo != null;
 
137
                
 
138
                combo.setFocus();
 
139
        }
 
140
 
 
141
}