1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.xsanalyzer.ui.support.mydoggy;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import javax.swing.JComponent;
30 import javax.swing.JPanel;
31 import javax.swing.SwingUtilities;
32
33 import org.noos.xing.mydoggy.AggregationPosition;
34 import org.noos.xing.mydoggy.Content;
35 import org.noos.xing.mydoggy.ContentManager;
36 import org.noos.xing.mydoggy.ContentManagerUIListener;
37 import org.noos.xing.mydoggy.Dockable;
38 import org.noos.xing.mydoggy.MultiSplitConstraint;
39 import org.noos.xing.mydoggy.MultiSplitContentManagerUI;
40 import org.noos.xing.mydoggy.TabbedContentManagerUI;
41 import org.noos.xing.mydoggy.ToolWindow;
42 import org.noos.xing.mydoggy.event.ContentManagerUIEvent;
43 import org.noos.xing.mydoggy.plaf.MyDoggyToolWindowManager;
44 import org.noos.xing.mydoggy.plaf.ui.cmp.ExtendedTableLayout;
45 import org.noos.xing.mydoggy.plaf.ui.content.MyDoggyMultiSplitContentManagerUI;
46 import org.springframework.richclient.application.ApplicationWindow;
47 import org.springframework.richclient.application.PageComponent;
48 import org.springframework.richclient.application.PageDescriptor;
49 import org.springframework.richclient.application.PageLayoutBuilder;
50 import org.springframework.richclient.application.ViewDescriptor;
51 import org.springframework.richclient.application.support.AbstractApplicationPage;
52 import org.springframework.util.Assert;
53
54
55
56
57
58
59
60
61
62 public class MyDoggyApplicationPage extends AbstractApplicationPage implements PageLayoutBuilder {
63
64 private JPanel rootComponent;
65
66 private final MyDoggyToolWindowManager toolWindowManager;
67
68 private final ContentManager contentManager;
69
70 private final MultiSplitContentManagerUI contentManagerUI;
71
72 private final Map<String, Entry<Dockable, PageComponent>> contentAndPageComponentById;
73
74 MyDoggyApplicationPage(ApplicationWindow window, PageDescriptor pageDescriptor) {
75 super(window, pageDescriptor);
76 contentAndPageComponentById = new HashMap<String, Entry<Dockable, PageComponent>>();
77 toolWindowManager = new MyDoggyToolWindowManager();
78 contentManager = toolWindowManager.getContentManager();
79 contentManagerUI = new MyDoggyMultiSplitContentManagerUI();
80 contentManager.setContentManagerUI(contentManagerUI);
81
82 assert SwingUtilities.isEventDispatchThread();
83
84 contentManagerUI.setTabPlacement(TabbedContentManagerUI.TabPlacement.TOP);
85 contentManagerUI.setShowAlwaysTab(true);
86 contentManagerUI.setCloseable(true);
87 contentManagerUI.setDetachable(true);
88
89
90 contentManagerUI.addContentManagerUIListener(new MyDoggyContentListener());
91 }
92
93
94
95
96
97
98
99 @Override
100 protected void doAddPageComponent(PageComponent pageComponent) {
101 Assert.notNull(pageComponent);
102
103
104 MultiSplitConstraint constraint;
105 final Iterator<Entry<Dockable, PageComponent>> iterator = contentAndPageComponentById.values().iterator();
106 if (iterator.hasNext()) {
107 Dockable dock = iterator.next().getKey();
108 if (dock instanceof Content) {
109 constraint = new MultiSplitConstraint((Content) dock);
110 }
111 else {
112 constraint = new MultiSplitConstraint(AggregationPosition.DEFAULT);
113 }
114 }
115 else {
116 constraint = new MultiSplitConstraint(AggregationPosition.DEFAULT);
117 }
118 final Dockable dockable;
119
120 ViewDescriptor viewDescriptor = getViewDescriptor(pageComponent.getId());
121
122 if (viewDescriptor instanceof MyDoggyToolWindowViewDescriptor) {
123 final MyDoggyToolWindowViewDescriptor toolPageComponent = (MyDoggyToolWindowViewDescriptor) viewDescriptor;
124 ToolWindow tw = toolWindowManager.registerToolWindow(pageComponent.getId(), pageComponent.getDisplayName(), pageComponent.getIcon(), pageComponent.getControl(), toolPageComponent.getAnchor());
125
126 tw.setType(toolPageComponent.getType());
127 tw.setAvailable(toolPageComponent.isAvailable());
128 tw.setActive(toolPageComponent.isActive());
129 dockable = tw;
130 }
131 else {
132
133 Content c = contentManager.addContent(pageComponent.getId(), pageComponent.getDisplayName(),
134 pageComponent.getIcon(), pageComponent.getControl(), pageComponent.getDisplayName(),
135 constraint);
136 dockable = c;
137 }
138 Assert.notNull(dockable);
139 Assert.isTrue(dockable.getId().equals(pageComponent.getId()));
140 contentAndPageComponentById.put(pageComponent.getId(), new DoggyEntry<Dockable, PageComponent>(dockable, pageComponent));
141
142
143
144
145
146 pageComponent.getControl();
147 }
148
149
150
151
152
153
154
155 @Override
156 protected void doRemovePageComponent(PageComponent pageComponent) {
157 Entry<Dockable, PageComponent> e = contentAndPageComponentById.remove(pageComponent.getId());
158 Assert.notNull(e);
159 Dockable dock = e.getKey();
160 final boolean ret;
161 if (dock instanceof Content) {
162 ret = contentManager.removeContent((Content) dock);
163 }
164 else {
165 toolWindowManager.unregisterToolWindow(dock.getId());
166 ret = true;
167 }
168 Assert.isTrue(ret);
169 }
170
171
172
173
174
175
176
177 @Override
178 protected boolean giveFocusTo(PageComponent pageComponent) {
179 return pageComponent.getControl().requestFocusInWindow();
180 }
181
182 @Override
183 public void setActiveComponent(PageComponent pageComponent) {
184 if (pageComponent != null) {
185
186 Content c = getContent(pageComponent.getId());
187 if (c != null) {
188
189 }
190 setActive(pageComponent.getId());
191 }
192 super.setActiveComponent(pageComponent);
193 }
194
195
196
197
198
199
200 @Override
201 protected JComponent createControl() {
202
203
204 Assert.isNull(rootComponent, "Should not being initialized twice.");
205 rootComponent = new JPanel();
206 rootComponent.setLayout(new ExtendedTableLayout(new double[][] { { 0, -1, 0}, { 0, -1, 0}}));
207 rootComponent.add(toolWindowManager, "1,1,");
208
209 return rootComponent;
210 }
211
212
213
214
215
216
217 @Override
218 public void addView(String viewDescriptorId) {
219
220
221 showView(viewDescriptorId);
222 }
223
224
225 private Content getContent(String id) {
226 return contentManager.getContent(id);
227 }
228
229 private PageComponent getPageComponent(String id) {
230 Entry<Dockable, PageComponent> entry = contentAndPageComponentById.get(id);
231 if (entry == null) { return null; }
232
233 return entry.getValue();
234 }
235
236 void buildInitialLayout() {
237
238 getPageDescriptor().buildInitialLayout(this);
239 }
240
241
242
243
244
245
246 private boolean setActive(final String id) {
247 Assert.isTrue(SwingUtilities.isEventDispatchThread());
248
249 Content c = getContent(id);
250 if (c != null) {
251 c.setSelected(true);
252 return true;
253 }
254 return false;
255 }
256
257 @Override
258 public boolean close() {
259 try {
260 saveLayout();
261 }
262 catch (IOException ex) {
263 logger.warn("IO Error while saving layout!", ex);
264 }
265 return super.close();
266 }
267
268 boolean loadLayout() throws IOException {
269 File file = getLayoutFile();
270 if (file != null && file.canRead()) {
271 Assert.isTrue(SwingUtilities.isEventDispatchThread());
272 Assert.isTrue(rootComponent.isVisible());
273
274 FileInputStream inputStream = new FileInputStream(file);
275
276
277
278
279
280
281
282
283
284
285
286 inputStream.close();
287
288
289 logger.info("Loaded " + contentManager.getContentCount() + " view(s).");
290 return true;
291 }
292 return false;
293 }
294
295 void saveLayout() throws IOException {
296 File file = getLayoutFile();
297 if (file != null) {
298 if (!file.exists()) {
299 file.createNewFile();
300 }
301 if (file.canWrite()) {
302 FileOutputStream output = new FileOutputStream(file);
303 toolWindowManager.getPersistenceDelegate().save(output);
304
305 output.close();
306
307 logger.info("Saved " + contentManager.getContentCount() + " view(s).");
308 }
309 }
310 }
311
312 private File getLayoutFile() throws IOException {
313 PageDescriptor pageDescriptor = getPageDescriptor();
314
315 if (pageDescriptor instanceof MyDoggyPageDescriptor) {
316 return ((MyDoggyPageDescriptor) pageDescriptor).getInitialLayout().getFile();
317 }
318 else {
319 throw new IOException("Couldn't return the layout file. Wrong PageDescriptorType:" + pageDescriptor.getClass());
320 }
321 }
322
323
324
325
326 private class MyDoggyContentListener implements ContentManagerUIListener {
327
328 public boolean contentUIRemoving(ContentManagerUIEvent cmEvent) {
329 Content content = cmEvent.getContentUI().getContent();
330 Assert.notNull(content);
331 PageComponent pc = getPageComponent(content.getId());
332 Assert.notNull(pc);
333 close(pc);
334
335
336
337 return false;
338 }
339
340 public void contentUIDetached(ContentManagerUIEvent cmEvent) {
341 }
342 }
343
344 private static class DoggyEntry<K, V> implements Map.Entry<K, V> {
345
346 private final K key;
347
348 private V innerValue;
349
350 DoggyEntry(K key, V value) {
351 this.key = key;
352 this.innerValue = value;
353 }
354
355 public K getKey() {
356 return key;
357 }
358
359 public V getValue() {
360 return innerValue;
361 }
362
363 public V setValue(V value) {
364 V old = this.innerValue;
365 this.innerValue = value;
366 return old;
367 }
368 }
369 }