1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.jtreemap.ktreemap.example;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.net.URL;
38 import java.text.ParseException;
39 import java.util.LinkedHashMap;
40
41 import org.eclipse.swt.widgets.Combo;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Display;
44 import org.eclipse.swt.widgets.FileDialog;
45 import org.eclipse.swt.widgets.Group;
46 import org.eclipse.ui.part.*;
47 import org.eclipse.ui.plugin.AbstractUIPlugin;
48 import org.eclipse.core.runtime.FileLocator;
49 import org.eclipse.core.runtime.Path;
50 import org.eclipse.core.runtime.Platform;
51 import org.eclipse.jface.resource.ImageDescriptor;
52 import org.eclipse.jface.viewers.*;
53 import org.eclipse.swt.custom.SashForm;
54 import org.eclipse.swt.events.SelectionEvent;
55 import org.eclipse.swt.events.SelectionListener;
56 import org.eclipse.swt.graphics.Image;
57 import org.eclipse.swt.layout.FillLayout;
58 import org.eclipse.swt.layout.GridData;
59 import org.eclipse.swt.layout.GridLayout;
60 import org.eclipse.jface.action.*;
61 import org.eclipse.jface.dialogs.MessageDialog;
62 import org.eclipse.ui.*;
63 import org.eclipse.swt.widgets.Menu;
64 import org.eclipse.swt.SWT;
65 import net.sf.jtreemap.ktreemap.*;
66
67 /**
68 * This sample class demonstrates how to plug-in a new workbench view. The view
69 * shows data obtained from the model. The sample creates a dummy model on the
70 * fly, but a real implementation would connect to the model available either in
71 * this or another plug-in (e.g. the workspace). The view is connected to the
72 * model using a content provider.
73 * <p>
74 * The view uses a label provider to define how model objects should be
75 * presented in the view. Each view can present the same model objects using
76 * different labels and icons, if needed. Alternatively, a single label provider
77 * can be shared between views in order to ensure that objects of the same type
78 * are presented in the same way everywhere.
79 * <p>
80 */
81
82 public class KTreeMapView extends ViewPart {
83 private static final String ID_BUNDLE = "net.sf.jtreemap.ktreemap";
84
85 private TreeViewer viewer;
86 private DrillDownAdapter drillDownAdapter;
87 private Action openXmlAction;
88 private Action openTM3Action;
89 private Action selectionChangedAction;
90 private KTreeMap kTreeMap;
91 private LinkedHashMap<String, SplitStrategy> strategies = new LinkedHashMap<String, SplitStrategy>();
92 private LinkedHashMap<String, ITreeMapColorProvider> colorProviders = new LinkedHashMap<String, ITreeMapColorProvider>();
93 private Combo cmbStrategy;
94 private Combo cmbColorProvider;
95 private Combo cmbTM3Weight;
96 private Combo cmbTM3Value;
97 private Composite legend;
98 private XMLTreeMapProvider xmlProvider;
99 private TM3TreeMapProvider tm3Provider;
100 private BuilderTM3 builderTM3;
101 private Group grpTM3Params;
102
103
104
105
106
107
108
109
110 /**
111 * This is a callback that will allow us to create the viewer and initialize
112 * it.
113 */
114 @Override
115 public void createPartControl(Composite parent) {
116 SashForm sash = new SashForm(parent, SWT.HORIZONTAL);
117 sash.setLayout(new FillLayout());
118
119 TreeMapNode root = null;
120 try {
121 root = getDefaultRoot();
122 } catch (ParseException e) {
123 MessageDialog.openError(PlatformUI.getWorkbench().getDisplay()
124 .getActiveShell(), "Parse Error", e.getMessage());
125 e.printStackTrace();
126 }
127
128 createLeftComp(sash);
129 createKTreeMapComp(sash, root);
130 createRightComp(sash);
131
132 viewer.setInput(root);
133
134 sash.setWeights(new int[] { 15, 70, 15 });
135
136 makeActions();
137 hookContextMenu();
138 hookSelectionChangedAction();
139 contributeToActionBars();
140 }
141
142
143
144
145
146
147 @Override
148 public void dispose() {
149 super.dispose();
150
151 ResourceManager.dispose();
152 }
153
154 /**
155 * Passing the focus request to the viewer's control.
156 */
157 @Override
158 public void setFocus() {
159 viewer.getControl().setFocus();
160 }
161
162 void updateColorProvider(Composite comp) {
163 String key = this.cmbColorProvider.getText();
164 ITreeMapColorProvider cp = this.colorProviders.get(key);
165 this.kTreeMap.setColorProvider(cp);
166 if (legend != null) {
167 legend.dispose();
168 }
169 legend = cp.getLegend(comp, SWT.NONE);
170 legend.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
171 | GridData.VERTICAL_ALIGN_BEGINNING));
172 comp.layout();
173 }
174
175 void updateStrategy() {
176 String key = this.cmbStrategy.getText();
177 SplitStrategy strat = this.strategies.get(key);
178 this.kTreeMap.setStrategy(strat);
179 }
180
181 private void contributeToActionBars() {
182 IActionBars bars = getViewSite().getActionBars();
183 fillLocalPullDown(bars.getMenuManager());
184 fillLocalToolBar(bars.getToolBarManager());
185 }
186
187 private void createColorProviders(Composite comp) {
188 createColorProviders();
189
190 final Group grp = new Group(comp, SWT.NONE);
191 grp.setText("Color Provider");
192 grp.setLayout(new GridLayout());
193 grp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
194 | GridData.VERTICAL_ALIGN_BEGINNING));
195
196 this.cmbColorProvider = new Combo(grp, SWT.NONE);
197 this.cmbColorProvider.removeAll();
198 for (String key : this.colorProviders.keySet()) {
199 this.cmbColorProvider.add(key);
200 }
201 this.cmbColorProvider.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
202 | GridData.VERTICAL_ALIGN_BEGINNING));
203
204 this.cmbColorProvider.select(0);
205
206 this.cmbColorProvider.addSelectionListener(new SelectionListener() {
207 public void widgetDefaultSelected(SelectionEvent e) {
208
209 }
210
211 public void widgetSelected(SelectionEvent e) {
212 updateColorProvider(grp);
213 }
214 });
215
216
217 updateColorProvider(grp);
218 }
219
220 private void createColorProviders() {
221 this.colorProviders.put("HSB linear", new HSBTreeMapColorProvider(kTreeMap,
222 HSBTreeMapColorProvider.ColorDistributionTypes.Linear, Display
223 .getDefault().getSystemColor(SWT.COLOR_GREEN), Display.getDefault()
224 .getSystemColor(SWT.COLOR_RED)));
225 this.colorProviders.put("HSB log", new HSBTreeMapColorProvider(kTreeMap,
226 HSBTreeMapColorProvider.ColorDistributionTypes.Log, Display
227 .getDefault().getSystemColor(SWT.COLOR_GREEN), Display.getDefault()
228 .getSystemColor(SWT.COLOR_RED)));
229 this.colorProviders.put("HSB SquareRoot", new HSBTreeMapColorProvider(
230 kTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.SquareRoot,
231 Display.getDefault().getSystemColor(SWT.COLOR_GREEN), Display
232 .getDefault().getSystemColor(SWT.COLOR_RED)));
233 this.colorProviders.put("HSB CubicRoot", new HSBTreeMapColorProvider(
234 kTreeMap, HSBTreeMapColorProvider.ColorDistributionTypes.CubicRoot,
235 Display.getDefault().getSystemColor(SWT.COLOR_GREEN), Display
236 .getDefault().getSystemColor(SWT.COLOR_RED)));
237 this.colorProviders.put("HSB exp", new HSBTreeMapColorProvider(kTreeMap,
238 HSBTreeMapColorProvider.ColorDistributionTypes.Exp, Display
239 .getDefault().getSystemColor(SWT.COLOR_GREEN), Display.getDefault()
240 .getSystemColor(SWT.COLOR_RED)));
241 }
242
243 private void createKTreeMapComp(SashForm sash, TreeMapNode root) {
244 kTreeMap = new KTreeMap(sash, SWT.NONE, root);
245 kTreeMap.setTreeMapProvider(xmlProvider);
246 }
247
248 private void createLeftComp(SashForm sash) {
249 Composite comp = new Composite(sash, SWT.NONE);
250 GridLayout layout = new GridLayout();
251 layout.marginHeight = 2;
252 layout.marginWidth = 2;
253 comp.setLayout(layout);
254
255 viewer = new TreeViewer(comp, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
256 drillDownAdapter = new DrillDownAdapter(viewer);
257 viewer.setContentProvider(new ViewContentProvider());
258 viewer.setLabelProvider(new ViewLabelProvider());
259 viewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
260 }
261
262 private void createRightComp(SashForm sash) {
263 Composite comp = new Composite(sash, SWT.NONE);
264 GridLayout layout = new GridLayout();
265 layout.marginHeight = 2;
266 layout.marginWidth = 2;
267 comp.setLayout(layout);
268
269 createStrategies(comp);
270 createColorProviders(comp);
271 createTM3Params(comp);
272 }
273
274 private void createTM3Params(Composite comp) {
275 grpTM3Params = new Group(comp, SWT.NONE);
276 grpTM3Params.setText("TM3 params");
277 grpTM3Params.setLayout(new GridLayout());
278 grpTM3Params.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
279 | GridData.VERTICAL_ALIGN_BEGINNING));
280 grpTM3Params.setVisible(false);
281
282 this.cmbTM3Value = new Combo(grpTM3Params, SWT.NONE);
283 this.cmbTM3Value.setToolTipText("Select the value field");
284 this.cmbTM3Value.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
285 | GridData.VERTICAL_ALIGN_BEGINNING));
286 this.cmbTM3Value.addSelectionListener(new SelectionListener() {
287
288 public void widgetDefaultSelected(SelectionEvent e) {
289
290 }
291
292 public void widgetSelected(SelectionEvent e) {
293 Combo cmb = (Combo)e.getSource();
294 String field = cmb.getText();
295 TM3TreeMapProvider.setValueField(field);
296 createColorProviders();
297 updateColorProvider(cmbColorProvider.getParent());
298 kTreeMap.redraw();
299 }
300
301 });
302
303 this.cmbTM3Weight = new Combo(grpTM3Params, SWT.NONE);
304 this.cmbTM3Weight.setToolTipText("Select the weight field");
305 this.cmbTM3Weight.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
306 | GridData.VERTICAL_ALIGN_BEGINNING));
307 this.cmbTM3Weight.addSelectionListener(new SelectionListener() {
308 public void widgetDefaultSelected(SelectionEvent e) {
309
310 }
311
312 public void widgetSelected(SelectionEvent e) {
313 Combo cmb = (Combo)e.getSource();
314 String field = cmb.getText();
315 BuilderTM3.setFieldWeight(field);
316 builderTM3.setWeights();
317 kTreeMap.calculatePositions();
318 kTreeMap.redraw();
319 }
320
321 });
322
323 }
324
325 private void createStrategies(Composite comp) {
326 this.strategies.put("Squarified", new SplitSquarified());
327 this.strategies.put("Sorted Weight", new SplitBySortedWeight());
328 this.strategies.put("Weight", new SplitByWeight());
329 this.strategies.put("Slice", new SplitBySlice());
330 this.strategies.put("Equal Weight", new SplitByNumber());
331
332 Group grp = new Group(comp, SWT.NONE);
333 grp.setText("Strategy");
334 grp.setLayout(new GridLayout());
335 grp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
336 | GridData.VERTICAL_ALIGN_BEGINNING));
337
338 this.cmbStrategy = new Combo(grp, SWT.NONE);
339 this.cmbStrategy.removeAll();
340 for (String key : this.strategies.keySet()) {
341 this.cmbStrategy.add(key);
342 }
343 this.cmbStrategy.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
344 | GridData.VERTICAL_ALIGN_BEGINNING));
345
346 this.cmbStrategy.select(0);
347
348 this.cmbStrategy.addSelectionListener(new SelectionListener() {
349 public void widgetDefaultSelected(SelectionEvent e) {
350
351 }
352
353 public void widgetSelected(SelectionEvent e) {
354 updateStrategy();
355 }
356 });
357
358
359 updateStrategy();
360 }
361
362 private void fillContextMenu(IMenuManager manager) {
363 TreeMapNode orig = kTreeMap.getDisplayedRoot();
364
365 ITreeMapProvider provider = kTreeMap.getTreeMapProvider();
366
367 TreeMapNode cursor = orig;
368
369
370 String id = "separator";
371 manager.add(new Separator(id));
372
373
374 while (cursor.getParent() != null) {
375 TreeMapNode parent = cursor.getParent();
376 ZoomAction action = new ZoomAction(provider.getLabel(parent),
377 AbstractUIPlugin.imageDescriptorFromPlugin(ID_BUNDLE,
378 "icons/unzoom.gif"), parent);
379 manager.insertBefore(id, action);
380 cursor = parent;
381 id = action.getId();
382 }
383
384
385 cursor = orig;
386 while (cursor.getChild(kTreeMap.getCursorPosition()) != null) {
387 TreeMapNode child = cursor.getChild(kTreeMap.getCursorPosition());
388 if (!child.isLeaf()) {
389 ZoomAction action = new ZoomAction(provider.getLabel(child),
390 AbstractUIPlugin.imageDescriptorFromPlugin(ID_BUNDLE,
391 "icons/zoom.gif"), child);
392 manager.add(action);
393 }
394 cursor = child;
395 }
396 }
397
398 private void fillLocalPullDown(IMenuManager manager) {
399 manager.add(openXmlAction);
400 manager.add(openTM3Action);
401 }
402
403 private void fillLocalToolBar(IToolBarManager manager) {
404 manager.add(openXmlAction);
405 manager.add(openTM3Action);
406 manager.add(new Separator());
407 drillDownAdapter.addNavigationActions(manager);
408 }
409
410 private TreeMapNode getDefaultRoot() throws ParseException {
411 URL url = FileLocator.find(Platform.getBundle(ID_BUNDLE), new Path(
412 "/TreeMap.xml"), null);
413 try {
414 url = FileLocator.toFileURL(url);
415 } catch (IOException e) {
416 MessageDialog.openError(PlatformUI.getWorkbench().getDisplay()
417 .getActiveShell(), "Error", e.getMessage());
418 e.printStackTrace();
419 }
420 BuilderXML builder = new BuilderXML(new File(url.getPath()));
421
422 xmlProvider = new XMLTreeMapProvider();
423 tm3Provider = new TM3TreeMapProvider();
424
425 return builder.getRoot();
426 }
427
428 private void hookContextMenu() {
429 MenuManager menuMgr = new MenuManager("#PopupMenu");
430 menuMgr.setRemoveAllWhenShown(true);
431 menuMgr.addMenuListener(new IMenuListener() {
432 public void menuAboutToShow(IMenuManager manager) {
433 KTreeMapView.this.fillContextMenu(manager);
434 }
435 });
436 Menu menu = menuMgr.createContextMenu(kTreeMap);
437 kTreeMap.setMenu(menu);
438 }
439
440 private void hookSelectionChangedAction() {
441 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
442 public void selectionChanged(SelectionChangedEvent event) {
443 selectionChangedAction.run();
444 }
445 });
446 }
447
448 private void setTM3Fields() {
449 String[] numberFields = TM3Bean.getNumberFields();
450 String[] cmbValues = new String[numberFields.length + 1];
451 cmbValues[0] = "";
452 for (int i = 1; i < cmbValues.length; i++) {
453 cmbValues[i] = numberFields[i - 1];
454 }
455 this.cmbTM3Weight.removeAll();
456 this.cmbTM3Value.removeAll();
457 for (int i = 0; i < cmbValues.length; i++) {
458 String item = cmbValues[i];
459 this.cmbTM3Weight.add(item);
460 this.cmbTM3Value.add(item);
461 }
462
463 }
464
465 private void makeActions() {
466 openXmlAction = new OpenXMLAction();
467 openTM3Action = new OpenTM3Action();
468
469 selectionChangedAction = new Action() {
470 @Override
471 public void run() {
472 ISelection selection = viewer.getSelection();
473 Object obj = ((IStructuredSelection)selection).getFirstElement();
474 if (obj instanceof TreeMapNode) {
475 TreeMapNode dest = ((TreeMapNode)obj).getParent();
476 if (dest == null) {
477 return;
478 }
479
480 kTreeMap.zoom(dest);
481 kTreeMap.redraw();
482
483 }
484 }
485 };
486 }
487
488 class ViewContentProvider implements IStructuredContentProvider,
489 ITreeContentProvider {
490
491 public void dispose() {
492
493 public Object[] getChildren(Object parent) {
494 if (parent instanceof TreeMapNode) {
495 return ((TreeMapNode)parent).getChildren().toArray();
496 }
497 return new Object[0];
498 }
499
500 public Object[] getElements(Object parent) {
501 return getChildren(parent);
502 }
503
504 public Object getParent(Object child) {
505 if (child instanceof TreeMapNode) {
506 return ((TreeMapNode)child).getParent();
507 }
508 return null;
509 }
510
511 public boolean hasChildren(Object parent) {
512 if (parent instanceof TreeMapNode)
513 return !((TreeMapNode)parent).isLeaf();
514 return false;
515 }
516
517 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
518
519 }
520 }
521
522 class ViewLabelProvider extends LabelProvider {
523
524 @Override
525 public Image getImage(Object obj) {
526 return null;
527 }
528
529 @Override
530 public String getText(Object obj) {
531 if (obj instanceof TreeMapNode) {
532 TreeMapNode node = (TreeMapNode)obj;
533 ITreeMapProvider provider = KTreeMapView.this.kTreeMap
534 .getTreeMapProvider();
535 return provider.getLabel(node);
536 }
537 return obj.toString();
538 }
539 }
540
541 private class OpenXMLAction extends Action {
542 private final String[] EXTENTIONS = new String[] { "*.xml" };
543
544 /**
545 * Constructor
546 */
547 public OpenXMLAction() {
548 super("Open XML File");
549 setToolTipText(getText());
550 setId(getText());
551 }
552
553
554
555
556
557
558 @Override
559 public ImageDescriptor getImageDescriptor() {
560 return AbstractUIPlugin.imageDescriptorFromPlugin(ID_BUNDLE,
561 "icons/XMLFile.gif");
562 }
563
564
565
566
567
568
569 @Override
570 public void run() {
571 Display display = PlatformUI.getWorkbench().getDisplay();
572 final FileDialog dialog = new FileDialog(display.getActiveShell(),
573 SWT.OPEN);
574 dialog.setFilterExtensions(EXTENTIONS);
575 String path = dialog.open();
576
577 if (path != null) {
578 BuilderXML builder;
579 try {
580 builder = new BuilderXML(new File(path));
581 } catch (ParseException e) {
582 MessageDialog.openError(display.getActiveShell(), "Parse error", e
583 .getMessage());
584 return;
585 }
586 TreeMapNode root = builder.getRoot();
587 kTreeMap.setTreeMapProvider(xmlProvider);
588 kTreeMap.setRoot(root);
589 viewer.setInput(root);
590 createColorProviders();
591 updateColorProvider(cmbColorProvider.getParent());
592 grpTM3Params.setVisible(false);
593
594 }
595 }
596
597 }
598
599 private class OpenTM3Action extends Action {
600 private final String[] EXTENTIONS = new String[] { "*.tm3" };
601
602 /**
603 * Constructor
604 */
605 public OpenTM3Action() {
606 super("Open TM3 File");
607 setToolTipText(getText());
608 setId(getText());
609 }
610
611
612
613
614
615
616 @Override
617 public ImageDescriptor getImageDescriptor() {
618 return AbstractUIPlugin.imageDescriptorFromPlugin(ID_BUNDLE,
619 "icons/TM3File.gif");
620 }
621
622
623
624
625
626
627 @Override
628 public void run() {
629 Display display = PlatformUI.getWorkbench().getDisplay();
630 final FileDialog dialog = new FileDialog(display.getActiveShell(),
631 SWT.OPEN);
632 dialog.setFilterExtensions(EXTENTIONS);
633 String path = dialog.open();
634
635 if (path != null) {
636 try {
637 builderTM3 = new BuilderTM3(new File(path));
638 } catch (IOException e) {
639 MessageDialog.openError(display.getActiveShell(), "Parse error", e
640 .getMessage());
641 return;
642 }
643 TreeMapNode root = builderTM3.getRoot();
644 kTreeMap.setTreeMapProvider(tm3Provider);
645 kTreeMap.setRoot(root);
646 viewer.setInput(root);
647 createColorProviders();
648 updateColorProvider(cmbColorProvider.getParent());
649
650 setTM3Fields();
651 grpTM3Params.setVisible(true);
652 }
653 }
654
655 }
656
657 private class ZoomAction extends Action {
658 private TreeMapNode node;
659
660 /**
661 * Constructor
662 *
663 * @param text text of the action
664 * @param image image
665 * @param node destination TreeMapNode of the zoom
666 */
667 public ZoomAction(String text, ImageDescriptor image, TreeMapNode node) {
668 super(text, image);
669 this.node = node;
670 setId(text);
671 }
672
673
674
675
676
677
678 @Override
679 public boolean isEnabled() {
680 return true;
681 }
682
683
684
685
686
687
688 @Override
689 public void run() {
690 kTreeMap.zoom(this.node);
691 kTreeMap.redraw();
692 }
693 }
694 }
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710