1 package com.melloware.jukes.gui.view.editor;
2
3 import java.awt.Component;
4 import java.beans.PropertyChangeEvent;
5 import java.beans.PropertyChangeListener;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.swing.JComponent;
13 import javax.swing.JPanel;
14 import javax.swing.JScrollPane;
15 import javax.swing.SwingUtilities;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import com.jgoodies.uifextras.util.UIFactory;
21 import com.melloware.jukes.gui.tool.MainModule;
22 import com.melloware.jukes.gui.view.component.ComplexInternalFrame;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @SuppressWarnings("unchecked")
48 public final class EditorPanel
49 extends ComplexInternalFrame {
50
51 private static final Log LOG = LogFactory.getLog(EditorPanel.class);
52
53 private JScrollPane scrollPane;
54
55
56
57
58
59
60
61
62
63 private final List editors;
64
65
66
67
68
69
70 private final Map editorRegistry;
71
72
73
74
75
76
77 public EditorPanel(MainModule mainModule) {
78 super("Editor");
79 editorRegistry = new HashMap();
80 editors = new LinkedList();
81 setContent(buildContent());
82
83 mainModule.addPropertyChangeListener(MainModule.PROPERTYNAME_SELECTION, new SelectionChangeHandler());
84 }
85
86
87
88
89
90
91 public List getEditors() {
92 return this.editors;
93 }
94
95
96
97
98
99
100
101 public void setActiveEditor(Editor newEditor) {
102 setFrameIcon(newEditor.getIcon());
103 setTitle(newEditor.getTitle());
104 setToolBar(newEditor.getToolBar());
105 setHeaderToolBar(newEditor.getHeaderToolBar());
106 scrollPane.setViewportView((Component)newEditor);
107 }
108
109
110
111
112
113
114 public void addEditor(Editor editor) {
115 Class domainClass = editor.getDomainClass();
116 if (domainClass != null) {
117 registerEditor(domainClass, editor);
118 }
119
120 editors.add(editor);
121 }
122
123
124
125
126 public void clearEditors() {
127 editors.clear();
128 }
129
130
131
132
133
134
135 public void updateUI() {
136 super.updateUI();
137 if (editors == null) {
138 return;
139 }
140
141 for (Iterator i = editors.iterator(); i.hasNext();) {
142 JComponent viewer = (JComponent)i.next();
143
144
145 SwingUtilities.updateComponentTreeUI(viewer);
146 }
147 }
148
149
150
151
152
153
154 private Editor getActiveEditor() {
155 return (Editor)scrollPane.getViewport().getView();
156 }
157
158
159
160
161
162
163
164 private JComponent buildContent() {
165 return scrollPane = UIFactory.createStrippedScrollPane(new JPanel());
166 }
167
168
169
170
171
172
173
174
175 private Editor lookupEditor(Class domainClass) {
176 return (Editor)editorRegistry.get(domainClass);
177 }
178
179
180
181
182
183
184
185 private void registerEditor(Class domainClass, Editor editor) {
186 Object oldValue = editorRegistry.put(domainClass, editor);
187 if (oldValue != null) {
188 LOG.debug("Duplicate editor registered for " + domainClass);
189 }
190 }
191
192
193
194
195
196
197
198 private void updateActiveEditor(Object selection) {
199 if (selection == null) {
200 return;
201 }
202
203 getActiveEditor().deactivate();
204 Editor editor = lookupEditor(selection.getClass());
205 if (editor != null) {
206 editor.setModel(selection);
207 editor.activate();
208 setActiveEditor(editor);
209 }
210 }
211
212
213 private class SelectionChangeHandler
214 implements PropertyChangeListener {
215
216
217
218
219
220
221
222
223 public void propertyChange(PropertyChangeEvent evt) {
224 if (evt.getNewValue() == null) {
225
226 setActiveEditor((Editor)getEditors().get(0));
227 } else {
228 updateActiveEditor(evt.getNewValue());
229 }
230 }
231 }
232
233 }