eaa7892047799c422768c485e7b4a9b6d7abe11a
[public/netxms.git] / src / java / netxms-eclipse / ObjectView / src / org / netxms / ui / eclipse / objectview / objecttabs / ObjectTab.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.objectview.objecttabs;
20
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.custom.CTabFolder;
26 import org.eclipse.swt.custom.CTabItem;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.FillLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.ui.IPluginContribution;
31 import org.eclipse.ui.part.ViewPart;
32 import org.eclipse.ui.plugin.AbstractUIPlugin;
33 import org.netxms.client.objects.AbstractObject;
34
35 /**
36 * Abstract object tab class
37 */
38 public abstract class ObjectTab implements IPluginContribution
39 {
40 protected String pluginId;
41 protected String id;
42
43 private ViewPart viewPart;
44 private CTabFolder tabFolder;
45 private CTabItem tabItem;
46 private Composite clientArea;
47 private AbstractObject object;
48 private String name;
49 private int order;
50 private ImageDescriptor icon;
51 private Image tabImage;
52
53 /**
54 * Read configuration before widget creation
55 *
56 * @param ce Eclipse registry configuration element
57 */
58 public void configure(IConfigurationElement ce, ViewPart viewPart)
59 {
60 this.viewPart = viewPart;
61
62 pluginId = ce.getContributor().getName();
63 id = ce.getAttribute("id"); //$NON-NLS-1$
64 if (id == null)
65 id = "no_id"; //$NON-NLS-1$
66
67 name = ce.getAttribute("name"); //$NON-NLS-1$
68 if (name == null)
69 name = "<noname>"; //$NON-NLS-1$
70
71 try
72 {
73 order = Integer.parseInt(ce.getAttribute("order"), 10); //$NON-NLS-1$
74 }
75 catch(NumberFormatException e)
76 {
77 order = Integer.MAX_VALUE;
78 }
79
80 String path = ce.getAttribute("icon"); //$NON-NLS-1$
81 if (path != null)
82 {
83 icon = AbstractUIPlugin.imageDescriptorFromPlugin(ce.getContributor().getName(), path);
84 }
85 }
86
87 /**
88 * Create object tab. Intended to be called only by TabbedObjectView.
89 *
90 * @param tabFolder Parent tab folder
91 * @param ce Configuration element
92 */
93 public void create(final CTabFolder tabFolder)
94 {
95 this.tabFolder = tabFolder;
96 clientArea = new Composite(tabFolder, SWT.NONE);
97 clientArea.setLayout(new FillLayout());
98 createTabContent(clientArea);
99 clientArea.setVisible(false);
100 }
101
102 /**
103 * Create tab's content.
104 *
105 * @param parent Parent composite
106 */
107 protected abstract void createTabContent(Composite parent);
108
109 /**
110 * Called by framework when tab is selected.
111 */
112 public void selected()
113 {
114 clientArea.moveAbove(null);
115
116 // sometimes recursive activation can happen when tab selection changed - it's OK here
117 try
118 {
119 clientArea.setFocus();
120 }
121 catch(RuntimeException e)
122 {
123 }
124 }
125
126 /**
127 * Test if tab should be shown for given NetXMS object. Default implementation always returns true.
128 *
129 * @param object Object to test
130 * @return Should return true if tab must be shown for given object
131 */
132 public boolean showForObject(final AbstractObject object)
133 {
134 return true;
135 }
136
137 /**
138 * Called by parent view to inform tab that currently selected object was changed.
139 *
140 * @param object New object to display
141 */
142 public abstract void objectChanged(AbstractObject object);
143
144 /**
145 * Change current object. Intended to be called only by parent view.
146 *
147 * @param object New object to display
148 */
149 public void changeObject(AbstractObject object)
150 {
151 this.object = object;
152 objectChanged(object);
153 }
154
155 /**
156 * Called by parent view to inform tab that update notification for
157 * current object was received from server. Default implementation do nothing.
158 * This method always called in UI thread.
159 *
160 * @param object new instance of current object
161 */
162 public void currentObjectUpdated(AbstractObject object)
163 {
164 }
165
166 /**
167 * Show tab
168 */
169 public void show()
170 {
171 if (tabItem == null)
172 {
173 // Find insertion point
174 int index = tabFolder.getItemCount();
175 for(int i = 0; i < tabFolder.getItemCount(); i++)
176 {
177 ObjectTab tab = (ObjectTab)tabFolder.getItem(i).getData();
178 if (tab.getOrder() > order)
179 {
180 index = i;
181 break;
182 }
183 }
184
185 tabItem = new CTabItem(tabFolder, SWT.NONE, index);
186 tabItem.setText(name);
187 if (icon != null)
188 {
189 tabImage = icon.createImage();
190 tabItem.setImage(tabImage);
191 }
192 tabItem.setControl(clientArea);
193 tabItem.setData(this);
194 clientArea.setVisible(true);
195 }
196 }
197
198 /**
199 * @return the name
200 */
201 public String getName()
202 {
203 return name;
204 }
205
206 /**
207 * Hide tab
208 */
209 public void hide()
210 {
211 if (tabItem != null)
212 {
213 tabItem.setControl(null);
214 // sometimes recursive activation can happen when we hide unneeded tabs - it's OK here
215 try
216 {
217 tabItem.dispose();
218 }
219 catch(RuntimeException e)
220 {
221 }
222 tabItem = null;
223 if (tabImage != null)
224 {
225 tabImage.dispose();
226 tabImage = null;
227 }
228 clientArea.setVisible(false);
229 }
230 }
231
232 /**
233 * Check if this tab is currently visible
234 *
235 * @return
236 */
237 public final boolean isVisible()
238 {
239 return tabItem != null;
240 }
241
242 /**
243 * Check if this tab is currently active
244 *
245 * @return
246 */
247 public final boolean isActive()
248 {
249 return tabFolder.getSelection() == tabItem;
250 }
251
252 /**
253 * Get currently selected object.
254 *
255 * @return Currently selected object
256 */
257 public AbstractObject getObject()
258 {
259 return object;
260 }
261
262 /**
263 * @return the order
264 */
265 public int getOrder()
266 {
267 return order;
268 }
269
270 /**
271 * @return the viewPart
272 */
273 public ViewPart getViewPart()
274 {
275 return viewPart;
276 }
277
278 /**
279 * Disposes the tab.
280 *
281 * This is the last method called on the ObjectTab. There is no guarantee that createTabContent() has been called,
282 * so the tab controls may never have been created.
283 *
284 * Within this method a tab may release any resources, fonts, images, etc. held by this tab. It is also very
285 * important to deregister all listeners from the workbench.
286 *
287 * Clients should not call this method (the tab manager calls this method at appropriate times).
288 */
289 public void dispose()
290 {
291 if (tabImage != null)
292 {
293 tabImage.dispose();
294 tabImage = null;
295 }
296 }
297
298 /**
299 * @return the clientArea
300 */
301 protected Composite getClientArea()
302 {
303 return clientArea;
304 }
305
306 /**
307 * Called by tab manager when user press refresh button in tab view.
308 * Default implementation do nothing.
309 */
310 public void refresh()
311 {
312 }
313
314 /**
315 * Get selection provider on tab, if any. If there are no selection provider
316 * on tab, must return null. Default implementation returns null.
317 *
318 * @return selection provider on tab
319 */
320 public ISelectionProvider getSelectionProvider()
321 {
322 return null;
323 }
324
325 /* (non-Javadoc)
326 * @see org.eclipse.ui.IPluginContribution#getLocalId()
327 */
328 @Override
329 public String getLocalId()
330 {
331 return id;
332 }
333
334 /* (non-Javadoc)
335 * @see org.eclipse.ui.IPluginContribution#getPluginId()
336 */
337 @Override
338 public String getPluginId()
339 {
340 return pluginId;
341 }
342 }