PageRenderTime 83ms CodeModel.GetById 72ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 1ms

/bundles/plugins-trunk/SideKick/sidekick/ModeOptionPaneController.java

#
Java | 95 lines | 75 code | 13 blank | 7 comment | 14 complexity | 8b33f7f51f4f9f8c1bc7cc50168b274e MD5 | raw file
 1package sidekick;
 2
 3import java.awt.Component;
 4import java.util.HashMap;
 5import java.util.HashSet;
 6import java.util.Iterator;
 7import java.util.Set;
 8
 9import javax.swing.JComponent;
10
11
12public class ModeOptionPaneController implements ModeOptionPane {
13
14	public interface ModeOptionPaneDelegate {
15		// Returns the UI component of the option pane
16		JComponent getUIComponent();
17		// Update the given properties from the UI
18		void updatePropsFromUI(Object props);
19		// Update the UI from the given properties
20		void updateUIFromProps(Object props);
21		// Create a new, initialized properties object for the specified mode
22		Object createModeProps(String mode);
23		// Save (commit) mode properties
24		void saveModeProps(String mode, Object props);
25		// Reset mode properties to the default settings
26		void resetModeProps(String mode);
27		// Returns whether or not the specified mode has mode-specific properties
28		boolean hasModeProps(String mode);
29	}
30
31	private HashMap<String, Object> modeProps;
32	private Set<String> useDefaults;	// Modes that use default settings
33	private Object props;	// Properties of current mode
34	private String mode;	// Currently selected mode
35	ModeOptionPaneDelegate delegate;		// The delegate controlled by this controller.
36
37	public ModeOptionPaneController(ModeOptionPaneDelegate mop) {
38		modeProps = new HashMap<String, Object>();
39		useDefaults = new HashSet<String>();
40		delegate = mop;
41	}
42
43	public void modeSelected(String mode) {
44		if (this.mode != null)
45			delegate.updatePropsFromUI(props);
46		this.mode = mode;
47		props = modeProps.get(mode);
48		if (props == null) {
49			props = delegate.createModeProps(mode);
50			if ((mode != null) && (! delegate.hasModeProps(mode)))
51				useDefaults.add(mode);
52			modeProps.put(mode, props);
53		}
54		delegate.updateUIFromProps(props);
55		setEnabled(delegate.getUIComponent(), ! useDefaults.contains(mode));
56	}
57
58	public void setUseDefaults(boolean b) {
59		if (b)
60			useDefaults.add(mode);
61		else
62			useDefaults.remove(mode);
63		setEnabled(delegate.getUIComponent(), ! b);
64	}
65
66	public void setEnabled(JComponent c, boolean enabled) {
67		c.setEnabled(enabled);
68		Component [] children = c.getComponents();
69		for (int i = 0; i < children.length; i++)
70			if (children[i] instanceof JComponent)
71				setEnabled((JComponent) children[i], enabled);
72	}
73
74	public void save() {
75		delegate.updatePropsFromUI(props);
76		Iterator<String> modes = modeProps.keySet().iterator();
77		while (modes.hasNext()) {
78			String m = modes.next();
79			if (useDefaults.contains(m))
80				delegate.resetModeProps(m);
81			else
82				delegate.saveModeProps(m, modeProps.get(m));
83		}
84	}
85
86	public void cancel() {
87		modeProps.clear();
88	}
89
90	public boolean getUseDefaults(String mode) {
91		if (modeProps.get(mode) != null)
92			return useDefaults.contains(mode);
93		return (! delegate.hasModeProps(mode));
94	}
95}