p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / controller / PropertySheetController.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.controller;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.widgets.Tree;
15 import org.eclipse.swt.widgets.TreeItem;
16 import org.eclipse.ui.IActionBars;
17 import org.eclipse.ui.IPageLayout;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IViewReference;
20 import org.eclipse.ui.views.properties.PropertySheet;
21 import org.eclipse.ui.views.properties.PropertySheetPage;
22
23 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
24 import eu.etaxonomy.taxeditor.propertysheet.EditorPropertySheetEntry;
25
26 /**
27 * @author n.hoffmann
28 * @created 20.01.2009
29 * @version 1.0
30 */
31 public class PropertySheetController {
32 private static final Logger logger = Logger
33 .getLogger(PropertySheetController.class);
34
35 public static IViewPart getPropertySheet() {
36 for (IViewReference reference : GlobalController.getActivePage().getViewReferences()) {
37 if (reference.getId().equals(IPageLayout.ID_PROP_SHEET)) {
38 logger.warn(reference.getView(false).getSite().getPart().getTitle());
39 return reference.getView(false);
40 }
41 }
42 return null;
43 }
44
45 /**
46 * By default, property sheet has buttons in the toolbar for
47 * "Show advanced properties" and "Show categories".
48 * <p>
49 * This is confusing for the user, hence a method to remove them
50 * until such time as advanced properties or categories are implemented.
51 */
52 public static void hidePropertySheetToolbar() {
53 PropertySheet propertySheet = (PropertySheet) getPropertySheet();
54 IActionBars actionBars = propertySheet.getViewSite().getActionBars();
55 actionBars.getToolBarManager().removeAll();
56 actionBars.getMenuManager().removeAll();
57 }
58
59 /**
60 * The property sheet listener ensures only property sheets
61 * with data cause the Property Sheet to be updated.
62 */
63 public static void addPropertySheetInputListener() {
64 IViewPart propertySheet = getPropertySheet();
65 // propertySheet.get
66 PropertySheet ps = (PropertySheet) propertySheet;
67 // ps.addPartPropertyListener(listener)
68 // ps.addPropertyListener(l)
69 }
70
71 public static void setPropertySheetTree(Tree tree) {
72 TaxEditorPlugin.getDefault().setPropertySheetTree(tree);
73 }
74
75 public static Tree getPropertySheetTree() {
76 return TaxEditorPlugin.getDefault().getPropertySheetTree();
77 }
78
79 public static void setPropertySheetPage(PropertySheetPage page) {
80 TaxEditorPlugin.getDefault().setPropertySheetPage(page);
81 }
82
83 public static PropertySheetPage getPropertySheetPage() {
84 return TaxEditorPlugin.getDefault().getPropertySheetPage();
85 }
86
87 /**
88 * UiUtil.paintPropertySheetRow(P_DATEPUBLISHED, new Color(Display.getDefault(), WarningAnnotation.WARNING_RGB), true);
89 * UiUtil.unpaintPropertySheetRow(P_DATEPUBLISHED);
90 *
91 * @param id
92 * @param color
93 * @param doPaintChildren
94 */
95 public static void paintPropertySheetRow(String id, Color color, boolean doPaintChildren) {
96
97 // Catch null property sheet name
98 if (id == null) {
99 return;
100 }
101
102 // Catch uninit'ed property sheet tree
103 if (getPropertySheetTree() == null) {
104 return;
105 }
106
107 paintPropertySheetRow(id, color, doPaintChildren, getPropertySheetTree());
108 }
109
110 private static void paintPropertySheetRow(String id, Color color, boolean doPaintChildren, Object treeOrItem) {
111
112 // Init items w zero-length array
113 TreeItem[] items = new TreeItem[]{};
114
115 // Get child items depending to class
116 if (treeOrItem instanceof Tree) {
117 items = ((Tree) treeOrItem).getItems();
118 }
119 if (treeOrItem instanceof TreeItem) {
120 items = ((TreeItem) treeOrItem).getItems();
121 }
122
123 // If array hasn't been populated by the above, return
124 if (items.length == 0) {
125 return;
126 }
127
128 // Prop. sheet id's take the form "01:xxxx" for sorting - truncate
129 id = EditorPropertySheetEntry.truncateDisplayName(id);
130
131 // Iterate through child items
132 for (TreeItem item : items) {
133
134 // Item found, paint it
135 if (id.equals(item.getText())) {
136 paintItem(item, color, doPaintChildren);
137 return;
138 }
139
140 // Recursively search for item to paint in child items
141 if (item.getItemCount() > 0) {
142 paintPropertySheetRow(id, color, doPaintChildren, item);
143 }
144 }
145 }
146
147 public static void unpaintPropertySheetRow(String id) {
148
149 // Catch uninit'ed property sheet tree
150 if (getPropertySheetTree() == null) {
151 return;
152 }
153
154 // Get tree's background color to "unpaint"
155 Color color = getPropertySheetTree().getBackground();
156
157 paintPropertySheetRow(id, color, true);
158 }
159
160 /**
161 * Note: children are only painted if submenu has already been created, i.e. opened once.
162 *
163 * @param item
164 * @param color
165 * @param doPaintChildren
166 */
167 public static void paintItem(TreeItem item, Color color, boolean doPaintChildren) {
168
169 // Paint the item
170 item.setBackground(color);
171
172 // Recursively paint child items if requested
173 if (doPaintChildren) {
174 for (TreeItem childItem : item.getItems()) {
175 paintItem(childItem, color, doPaintChildren);
176 }
177 }
178 }
179
180 }