36c87d22124f06124e6a4939de94011bb8870bd5
[public/netxms.git] / webui / webapp / Core / src / org / netxms / ui / eclipse / console / ApplicationWorkbenchWindowAdvisor.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 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.console;
20
21 import org.eclipse.core.commands.common.NotDefinedException;
22 import org.eclipse.jface.bindings.BindingManager;
23 import org.eclipse.rap.rwt.RWT;
24 import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.CBanner;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.application.ActionBarAdvisor;
35 import org.eclipse.ui.application.IActionBarConfigurer;
36 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
37 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
38 import org.eclipse.ui.internal.keys.BindingService;
39 import org.eclipse.ui.keys.IBindingService;
40 import org.netxms.client.NXCSession;
41 import org.netxms.client.objects.AbstractObject;
42 import org.netxms.client.objects.Dashboard;
43 import org.netxms.ui.eclipse.console.resources.RegionalSettings;
44 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
45 import org.netxms.ui.eclipse.tools.MessageDialogHelper;
46
47 /**
48 * Workbench window advisor
49 */
50 @SuppressWarnings("restriction")
51 public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor
52 {
53 /**
54 * @param configurer
55 */
56 public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer)
57 {
58 super(configurer);
59 }
60
61 /* (non-Javadoc)
62 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
63 */
64 public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer)
65 {
66 return new ApplicationActionBarAdvisor(configurer);
67 }
68
69 /* (non-Javadoc)
70 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#preWindowOpen()
71 */
72 @Override
73 public void preWindowOpen()
74 {
75 RegionalSettings.updateFromPreferences();
76
77 IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
78 configurer.setShowPerspectiveBar(true);
79 configurer.setShowStatusLine(false);
80 configurer.setTitle(Messages.get().ApplicationWorkbenchWindowAdvisor_AppTitle);
81 configurer.setShellStyle(SWT.NO_TRIM);
82 }
83
84 /* (non-Javadoc)
85 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowCreate()
86 */
87 @Override
88 public void postWindowCreate()
89 {
90 super.postWindowCreate();
91
92 NXCSession session = (NXCSession)ConsoleSharedData.getSession();
93
94 // Changes the page title at runtime
95 JavaScriptExecutor executor = RWT.getClient().getService(JavaScriptExecutor.class);
96 if (executor != null)
97 {
98 StringBuilder js = new StringBuilder();
99 js.append("document.title = "); //$NON-NLS-1$
100 js.append("\"");
101 js.append(Messages.get().ApplicationWorkbenchWindowAdvisor_AppTitle);
102 js.append(" - [");
103 js.append(session.getUserName());
104 js.append("@");
105 js.append(session.getServerAddress());
106 js.append("]");
107 js.append("\"");
108 executor.execute(js.toString());
109 }
110
111 BindingService service = (BindingService)getWindowConfigurer().getWindow().getWorkbench().getService(IBindingService.class);
112 BindingManager bindingManager = service.getBindingManager();
113 try
114 {
115 bindingManager.setActiveScheme(service.getScheme("org.netxms.ui.eclipse.defaultKeyBinding")); //$NON-NLS-1$
116 }
117 catch(NotDefinedException e)
118 {
119 e.printStackTrace();
120 }
121
122 final Shell shell = getWindowConfigurer().getWindow().getShell();
123 shell.setMaximized(true);
124
125 for(Control ctrl : shell.getChildren())
126 {
127 ctrl.setData(RWT.CUSTOM_VARIANT, "gray"); //$NON-NLS-1$
128 if (ctrl instanceof CBanner)
129 {
130 for(Control cc : ((CBanner)ctrl).getChildren())
131 cc.setData(RWT.CUSTOM_VARIANT, "gray"); //$NON-NLS-1$
132 }
133 else if (ctrl.getClass().getName().equals("org.eclipse.swt.widgets.Composite")) //$NON-NLS-1$
134 {
135 for(Control cc : ((Composite)ctrl).getChildren())
136 cc.setData(RWT.CUSTOM_VARIANT, "gray"); //$NON-NLS-1$
137 }
138 }
139
140 Menu menuBar = shell.getMenuBar();
141 if (menuBar != null)
142 menuBar.setData(RWT.CUSTOM_VARIANT, "menuBar"); //$NON-NLS-1$
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowOpen()
147 */
148 @Override
149 public void postWindowOpen()
150 {
151 String dashboardId = RWT.getRequest().getParameter("dashboard"); //$NON-NLS-1$
152 if (dashboardId != null)
153 showDashboard(dashboardId);
154 }
155
156 /**
157 * Show dashboard
158 *
159 * @param dashboardId
160 */
161 private void showDashboard(String dashboardId)
162 {
163 NXCSession session = (NXCSession)ConsoleSharedData.getSession();
164
165 long objectId;
166 try
167 {
168 objectId = Long.parseLong(dashboardId);
169 }
170 catch(NumberFormatException e)
171 {
172 AbstractObject object = session.findObjectByName(dashboardId);
173 if ((object == null) || !(object instanceof Dashboard))
174 {
175 MessageDialogHelper.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Error, String.format(Messages.get().ApplicationWorkbenchWindowAdvisor_CannotOpenDashboard, dashboardId));
176 return;
177 }
178 objectId = object.getObjectId();
179 }
180
181 Dashboard dashboard = (Dashboard)session.findObjectById(objectId, Dashboard.class);
182 if (dashboard == null)
183 {
184 MessageDialogHelper.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Error, String.format(Messages.get().ApplicationWorkbenchWindowAdvisor_CannotOpenDashboard, dashboardId));
185 return;
186 }
187
188 IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();
189 try
190 {
191 IViewPart view = page.showView("org.netxms.ui.eclipse.dashboard.views.DashboardView", Long.toString(objectId), IWorkbenchPage.VIEW_ACTIVATE); //$NON-NLS-1$
192 page.setPartState(page.getReference(view), IWorkbenchPage.STATE_MAXIMIZED);
193 }
194 catch(PartInitException e)
195 {
196 MessageDialogHelper.openError(null, Messages.get().ApplicationWorkbenchWindowAdvisor_Error, String.format(Messages.get().ApplicationWorkbenchWindowAdvisor_CannotOpenDashboardType2, dashboardId, e.getLocalizedMessage()));
197 }
198 }
199
200 /* (non-Javadoc)
201 * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#postWindowClose()
202 */
203 @Override
204 public void postWindowClose()
205 {
206 super.postWindowClose();
207 JavaScriptExecutor executor = RWT.getClient().getService(JavaScriptExecutor.class);
208 if (executor != null)
209 executor.execute("location.reload(true);");
210 }
211 }