1
/*******************************************************************************
2
* Copyright (C) 2003-2007, 2013, Guillaume Brocker
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
10
* Guillaume Brocker - Initial API and implementation
12
******************************************************************************/
14
package eclox.ui.editor.basic;
16
import java.util.HashSet;
17
import java.util.Iterator;
20
import eclox.core.doxyfiles.Setting;
21
import eclox.ui.editor.editors.AbstractEditor;
24
* Base implementation of for multi editors.
26
* @author Guillaume Brocker
28
public abstract class MultiEditor extends AbstractEditor {
31
* symbolic constant value for yes
33
private static final String YES = "YES";
36
* symbolic constant value for no
38
private static final String NO = "NO";
40
protected class State {
42
private Set selectedSettings = new HashSet();
43
private Set deselectedSettings = new HashSet();
45
State( String name ) {
49
void addSettingToSelect( Setting setting ) {
50
selectedSettings.add( setting );
51
deselectedSettings.remove( setting );
54
void addSettingToDeselect( Setting setting ) {
55
if( selectedSettings.contains(setting) == false ) {
56
deselectedSettings.add( setting );
64
boolean wantsSelection() {
65
boolean wanted = true;
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();
73
wanted = wanted && setting.getValue().equals(YES);
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();
81
wanted = wanted && setting.getValue().equals(NO);
91
i = selectedSettings.iterator();
92
while( i.hasNext() ) {
93
Setting setting = (Setting) i.next();
95
setting.setValue(YES);
98
i = deselectedSettings.iterator();
99
while( i.hasNext() ) {
100
Setting setting = (Setting) i.next();
102
setting.setValue(NO);
109
* the collection of managed states
111
protected State [] states;
114
* the state being selected
116
private State selection;
119
* a boolean telling if the editor is dirty
121
private boolean dirty = false;
124
* Creates a new multi editor instance
126
* @param states an array containing the name of the states to create
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]);
136
* Adds the given setting to a given state
138
* @param state the name of a state
139
* @param setting the setting to add to the given state
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);
148
states[i].addSettingToDeselect(setting);
155
* @see eclox.ui.editor.editors.IEditor#commit()
157
public void commit() {
158
// Commits the selected state.
159
if( selection != null ) {
167
* Retrieves the selected state of the editor
169
* @return a string representing the selected state, or null if none
171
public String getSelection() {
172
return (selection != null) ? selection.getName() : null;
176
* @see eclox.ui.editor.editors.IEditor#isDirty()
178
public boolean isDirty() {
183
* @see eclox.ui.editor.editors.IEditor#isStale()
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];
195
return wantedSelection != selection;
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.
202
* @see eclox.ui.editor.editors.IEditor#refresh()
204
public void refresh() {
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];
219
* Retrieves the selected state of the multi editor.
221
* @return a state or null when none
223
protected State getSelectionAsState() {
228
* Retrieves the state for the given name
230
* @name a string containing a state name
232
* @return the matching state or null when none
234
protected State getState( String name ) {
237
for( int i = 0; i != states.length; ++i ) {
238
if( states[i].getName() .equals(name) ) {
247
* Selectes the given state
249
* @param state a string containing a state name
251
protected void selectState( String state ) {
252
State candidate = getState(state);
253
if( candidate != null ) {
254
selection = candidate;