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

« back to all changes in this revision

Viewing changes to eclox.ui/src/eclox/ui/editor/basic/MultiEditor.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 java.util.HashSet;
 
17
import java.util.Iterator;
 
18
import java.util.Set;
 
19
 
 
20
import eclox.core.doxyfiles.Setting;
 
21
import eclox.ui.editor.editors.AbstractEditor;
 
22
 
 
23
/**
 
24
 * Base implementation of for multi editors.
 
25
 * 
 
26
 * @author Guillaume Brocker
 
27
 */
 
28
public abstract class MultiEditor extends AbstractEditor {
 
29
        
 
30
        /**
 
31
         * symbolic constant value for yes
 
32
         */
 
33
        private static final String YES = "YES";
 
34
        
 
35
        /**
 
36
         * symbolic constant value for no
 
37
         */
 
38
        private static final String NO = "NO";
 
39
        
 
40
        protected class State { 
 
41
                private String  name;
 
42
                private Set             selectedSettings = new HashSet();
 
43
                private Set             deselectedSettings = new HashSet();
 
44
                
 
45
                State( String name ) {
 
46
                        this.name = name;
 
47
                }
 
48
                
 
49
                void addSettingToSelect( Setting setting ) {
 
50
                        selectedSettings.add( setting );
 
51
                        deselectedSettings.remove( setting );
 
52
                }
 
53
                
 
54
                void addSettingToDeselect( Setting setting ) {
 
55
                        if( selectedSettings.contains(setting) == false ) {
 
56
                                deselectedSettings.add( setting );
 
57
                        }
 
58
                }
 
59
                
 
60
                String getName() {
 
61
                        return name;
 
62
                }
 
63
                
 
64
                boolean wantsSelection() {
 
65
                        boolean         wanted = true;
 
66
                        Iterator        i;
 
67
                        
 
68
                        // Updates the selection according to the value of settings owned by the state.
 
69
                        i = selectedSettings.iterator();
 
70
                        while( i.hasNext() ) {
 
71
                                Setting setting = (Setting) i.next();
 
72
                                
 
73
                                wanted = wanted && setting.getValue().equals(YES);
 
74
                        }
 
75
                        
 
76
                        // Updates the selection according to the value of settings owned by the state.
 
77
                        i = deselectedSettings.iterator();
 
78
                        while( i.hasNext() ) {
 
79
                                Setting setting = (Setting) i.next();
 
80
                                
 
81
                                wanted = wanted && setting.getValue().equals(NO);
 
82
                        }
 
83
                        
 
84
                        // Job's done.
 
85
                        return wanted;
 
86
                }
 
87
                
 
88
                void commit() {
 
89
                        Iterator i;
 
90
                        
 
91
                        i = selectedSettings.iterator();
 
92
                        while( i.hasNext() ) {
 
93
                                Setting setting = (Setting) i.next();
 
94
                                
 
95
                                setting.setValue(YES);
 
96
                        }
 
97
 
 
98
                        i = deselectedSettings.iterator();
 
99
                        while( i.hasNext() ) {
 
100
                                Setting setting = (Setting) i.next();
 
101
                                
 
102
                                setting.setValue(NO);
 
103
                        }
 
104
                }
 
105
                
 
106
        }
 
107
        
 
108
        /**
 
109
         * the collection of managed states
 
110
         */
 
111
        protected State [] states;
 
112
        
 
113
        /**
 
114
         * the state being selected
 
115
         */
 
116
        private State selection;
 
117
        
 
118
        /**
 
119
         * a boolean telling if the editor is dirty
 
120
         */
 
121
        private boolean dirty = false;
 
122
        
 
123
        /**
 
124
         * Creates a new multi editor instance
 
125
         * 
 
126
         * @param states        an array containing the name of the states to create
 
127
         */
 
128
        public MultiEditor( String [] states ) {
 
129
                this.states = new State[states.length];
 
130
                for( int i = 0; i != states.length; ++i ) {
 
131
                        this.states[i] = new State(states[i]);
 
132
                }
 
133
        }
 
134
        
 
135
        /**
 
136
         * Adds the given setting to a given state
 
137
         * 
 
138
         * @param       state   the name of a state
 
139
         * @param       setting the setting to add to the given state
 
140
         */
 
141
        public void addSetting( String state, Setting setting ) {
 
142
                if( setting != null ) {
 
143
                        for( int i = 0; i != states.length; ++i ) {
 
144
                                if( states[i].name.equals(state) ) {
 
145
                                        states[i].addSettingToSelect(setting);
 
146
                                }
 
147
                                else {
 
148
                                        states[i].addSettingToDeselect(setting);
 
149
                                }
 
150
                        }
 
151
                }
 
152
        }       
 
153
        
 
154
        /**
 
155
         * @see eclox.ui.editor.editors.IEditor#commit()
 
156
         */
 
157
        public void commit() {
 
158
                // Commits the selected state.
 
159
                if( selection != null ) {
 
160
                        selection.commit();
 
161
                }
 
162
                dirty = false;
 
163
                fireEditorChanged();
 
164
        }
 
165
        
 
166
        /**
 
167
         * Retrieves the selected state of the editor
 
168
         * 
 
169
         * @return      a string representing the selected state, or null if none
 
170
         */
 
171
        public String getSelection() {
 
172
                return (selection != null) ? selection.getName() : null;
 
173
        }
 
174
        
 
175
        /**
 
176
         * @see eclox.ui.editor.editors.IEditor#isDirty()
 
177
         */
 
178
        public boolean isDirty() {
 
179
                return dirty;
 
180
        }
 
181
 
 
182
        /**
 
183
         * @see eclox.ui.editor.editors.IEditor#isStale()
 
184
         */
 
185
        public boolean isStale() {
 
186
                // Looks for the state that wants the selection.
 
187
                State   wantedSelection = null;
 
188
                for( int i = 0; i != states.length; ++i ) {
 
189
                        if( states[i].wantsSelection() ) {
 
190
                                wantedSelection = states[i];
 
191
                                break;
 
192
                        }
 
193
                }
 
194
                
 
195
                return wantedSelection != selection;
 
196
        }
 
197
 
 
198
        /**
 
199
         * Sub-classes must call this method first, in order to refresh the current state
 
200
         * and then refresh the user interface controls, according to the current state.
 
201
         * 
 
202
         * @see eclox.ui.editor.editors.IEditor#refresh()
 
203
         */
 
204
        public void refresh() {
 
205
                selection = null;
 
206
                dirty = false;
 
207
                
 
208
                // Searches the states that wants to be selected
 
209
                for( int i = 0; i != states.length; ++i ) {
 
210
                        if( states[i].wantsSelection() ) {
 
211
                                selection = states[i];
 
212
                                break;
 
213
                        }
 
214
                }
 
215
                fireEditorChanged();
 
216
        }
 
217
        
 
218
        /**
 
219
         * Retrieves the selected state of the multi editor.
 
220
         * 
 
221
         * @return      a state or null when none
 
222
         */
 
223
        protected State getSelectionAsState() {
 
224
                return selection;
 
225
        }
 
226
        
 
227
        /**
 
228
         * Retrieves the state for the given name
 
229
         * 
 
230
         * @name        a string containing a state name
 
231
         * 
 
232
         * @return      the matching state or null when none
 
233
         */
 
234
        protected State getState( String name ) {
 
235
                State   found = null;
 
236
                
 
237
                for( int i = 0; i != states.length; ++i ) {
 
238
                        if( states[i].getName() .equals(name) ) {
 
239
                                found = states[i];
 
240
                                break;
 
241
                        }
 
242
                }
 
243
                return found;
 
244
        }
 
245
 
 
246
        /**
 
247
         * Selectes the given state
 
248
         * 
 
249
         * @param       state   a string containing a state name
 
250
         */
 
251
        protected void selectState( String state ) {
 
252
                State   candidate = getState(state);            
 
253
                if( candidate != null ) {
 
254
                        selection = candidate;
 
255
                        dirty = true;
 
256
                        fireEditorChanged();
 
257
                }
 
258
        }
 
259
 
 
260
}