PageRenderTime 61ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/azureus-4.7.0.2/com/aelitis/azureus/ui/swt/views/TopBarView.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 433 lines | 337 code | 44 blank | 52 comment | 80 complexity | 5c3a5463cd29c054c8f0df569e4e0a46 MD5 | raw file
  1. /**
  2. * Created on Jun 27, 2008
  3. *
  4. * Copyright 2008 Vuze, Inc. All rights reserved.
  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; version 2 of the License only.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. package com.aelitis.azureus.ui.swt.views;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.eclipse.swt.SWT;
  22. import org.eclipse.swt.events.*;
  23. import org.eclipse.swt.graphics.Point;
  24. import org.eclipse.swt.graphics.Rectangle;
  25. import org.eclipse.swt.graphics.Transform;
  26. import org.eclipse.swt.layout.*;
  27. import org.eclipse.swt.widgets.*;
  28. import org.gudy.azureus2.core3.config.COConfigurationManager;
  29. import org.gudy.azureus2.core3.internat.MessageText;
  30. import org.gudy.azureus2.core3.util.AERunnable;
  31. import org.gudy.azureus2.core3.util.Debug;
  32. import org.gudy.azureus2.ui.swt.Utils;
  33. import org.gudy.azureus2.ui.swt.plugins.UISWTInstance;
  34. import org.gudy.azureus2.ui.swt.plugins.UISWTViewEvent;
  35. import org.gudy.azureus2.ui.swt.pluginsimpl.*;
  36. import org.gudy.azureus2.ui.swt.views.stats.VivaldiView;
  37. import com.aelitis.azureus.core.AzureusCore;
  38. import com.aelitis.azureus.core.AzureusCoreFactory;
  39. import com.aelitis.azureus.core.AzureusCoreRunningListener;
  40. import com.aelitis.azureus.ui.UIFunctionsManager;
  41. import com.aelitis.azureus.ui.common.updater.UIUpdatable;
  42. import com.aelitis.azureus.ui.skin.SkinConstants;
  43. import com.aelitis.azureus.ui.swt.UIFunctionsManagerSWT;
  44. import com.aelitis.azureus.ui.swt.skin.*;
  45. import com.aelitis.azureus.ui.swt.skin.SWTSkinButtonUtility.ButtonListenerAdapter;
  46. import com.aelitis.azureus.ui.swt.views.skin.SkinView;
  47. /**
  48. * @author TuxPaper
  49. * @created Jun 27, 2008
  50. *
  51. */
  52. public class TopBarView
  53. extends SkinView
  54. {
  55. private List<UISWTViewCore> topbarViews = new ArrayList<UISWTViewCore>();
  56. private UISWTViewCore activeTopBar;
  57. private SWTSkin skin;
  58. private org.eclipse.swt.widgets.List listPlugins;
  59. private Composite cPluginArea;
  60. private static boolean registeredCoreSubViews = false;
  61. public Object skinObjectInitialShow(SWTSkinObject skinObject, Object params) {
  62. this.skin = skinObject.getSkin();
  63. skin.addListener("topbar-plugins", new SWTSkinObjectListener() {
  64. public Object eventOccured(SWTSkinObject skinObject, int eventType,
  65. Object params) {
  66. if (eventType == SWTSkinObjectListener.EVENT_SHOW) {
  67. skin.removeListener("topbar-plugins", this);
  68. // building needs UISWTInstance, which needs core.
  69. AzureusCoreFactory.addCoreRunningListener(new AzureusCoreRunningListener() {
  70. public void azureusCoreRunning(AzureusCore core) {
  71. Utils.execSWTThreadLater(0, new AERunnable() {
  72. public void runSupport() {
  73. buildTopBarViews();
  74. }
  75. });
  76. }
  77. });
  78. }
  79. return null;
  80. }
  81. });
  82. // trigger autobuild
  83. skin.getSkinObject("topbar-area-plugin");
  84. return null;
  85. }
  86. /**
  87. * @param skinObject
  88. *
  89. * @since 3.0.1.1
  90. */
  91. public void buildTopBarViews() {
  92. // TODO actually use plugins..
  93. SWTSkinObject skinObject = skin.getSkinObject("topbar-plugins");
  94. if (skinObject == null) {
  95. return;
  96. }
  97. try {
  98. cPluginArea = (Composite) skinObject.getControl();
  99. final UIUpdatable updatable = new UIUpdatable() {
  100. public void updateUI() {
  101. Object[] views = topbarViews.toArray();
  102. for (int i = 0; i < views.length; i++) {
  103. try {
  104. UISWTViewCore view = (UISWTViewCore) views[i];
  105. if (view.getComposite().isVisible()) {
  106. view.triggerEvent(UISWTViewEvent.TYPE_REFRESH, null);
  107. }
  108. } catch (Exception e) {
  109. Debug.out(e);
  110. }
  111. }
  112. }
  113. public String getUpdateUIName() {
  114. return "TopBar";
  115. }
  116. };
  117. try {
  118. UIFunctionsManager.getUIFunctions().getUIUpdater().addUpdater(updatable);
  119. } catch (Exception e) {
  120. Debug.out(e);
  121. }
  122. skinObject.getControl().addDisposeListener(new DisposeListener() {
  123. public void widgetDisposed(DisposeEvent e) {
  124. try {
  125. UIFunctionsManager.getUIFunctions().getUIUpdater().removeUpdater(
  126. updatable);
  127. } catch (Exception ex) {
  128. Debug.out(ex);
  129. }
  130. Object[] views = topbarViews.toArray();
  131. topbarViews.clear();
  132. for (int i = 0; i < views.length; i++) {
  133. UISWTViewCore view = (UISWTViewCore) views[i];
  134. if (view != null) {
  135. view.triggerEvent(UISWTViewEvent.TYPE_DESTROY, null);
  136. }
  137. }
  138. }
  139. });
  140. SWTSkinObject soPrev = skin.getSkinObject("topbar-plugin-prev");
  141. if (soPrev != null) {
  142. SWTSkinButtonUtility btnPrev = new SWTSkinButtonUtility(soPrev);
  143. btnPrev.addSelectionListener(new ButtonListenerAdapter() {
  144. public void pressed(SWTSkinButtonUtility buttonUtility,
  145. SWTSkinObject skinObject, int stateMask) {
  146. //System.out.println("prev click " + activeTopBar + " ; "
  147. // + topbarViews.size());
  148. if (activeTopBar == null || topbarViews.size() <= 1) {
  149. return;
  150. }
  151. int i = topbarViews.indexOf(activeTopBar) - 1;
  152. if (i < 0) {
  153. i = topbarViews.size() - 1;
  154. }
  155. activateTopBar((UISWTViewCore) topbarViews.get(i));
  156. }
  157. });
  158. }
  159. SWTSkinObject soNext = skin.getSkinObject("topbar-plugin-next");
  160. if (soNext != null) {
  161. SWTSkinButtonUtility btnNext = new SWTSkinButtonUtility(soNext);
  162. btnNext.addSelectionListener(new ButtonListenerAdapter() {
  163. public void pressed(SWTSkinButtonUtility buttonUtility,
  164. SWTSkinObject skinObject, int stateMask) {
  165. //System.out.println("next click");
  166. if (activeTopBar == null || topbarViews.size() <= 1) {
  167. return;
  168. }
  169. int i = topbarViews.indexOf(activeTopBar) + 1;
  170. if (i >= topbarViews.size()) {
  171. i = 0;
  172. }
  173. activateTopBar((UISWTViewCore) topbarViews.get(i));
  174. }
  175. });
  176. }
  177. SWTSkinObject soTitle = skin.getSkinObject("topbar-plugin-title");
  178. if (soTitle != null) {
  179. final Composite cTitle = (Composite) soTitle.getControl();
  180. cTitle.addPaintListener(new PaintListener() {
  181. public void paintControl(PaintEvent e) {
  182. e.gc.setAdvanced(true);
  183. //Font font = new Font(e.gc.getDevice(), "Sans", 8, SWT.NORMAL);
  184. //e.gc.setFont(font);
  185. if (e.gc.getAdvanced() && activeTopBar != null) {
  186. try {
  187. e.gc.setTextAntialias(SWT.ON);
  188. } catch (Exception ex) {
  189. // Ignore ERROR_NO_GRAPHICS_LIBRARY error or any others
  190. }
  191. try {
  192. Transform transform = new Transform(e.gc.getDevice());
  193. transform.rotate(270);
  194. e.gc.setTransform(transform);
  195. String s = activeTopBar.getFullTitle();
  196. Point size = e.gc.textExtent(s);
  197. e.gc.drawText(s, -size.x, 0, true);
  198. //e.gc.drawText(s, 0,0, true);
  199. transform.dispose();
  200. } catch (Exception ex) {
  201. // setTransform can trhow a ERROR_NO_GRAPHICS_LIBRARY error
  202. // no use trying to draw.. it would look weird
  203. }
  204. //font.dispose();
  205. }
  206. }
  207. });
  208. }
  209. SWTSkinObject soList = skin.getSkinObject("topbar-plugin-list");
  210. if (soList != null) {
  211. final Composite cList = (Composite) soList.getControl();
  212. listPlugins = new org.eclipse.swt.widgets.List(cList, SWT.None);
  213. listPlugins.setLayoutData(Utils.getFilledFormData());
  214. listPlugins.setBackground(cList.getBackground());
  215. listPlugins.setForeground(cList.getForeground());
  216. listPlugins.addSelectionListener(new SelectionListener() {
  217. public void widgetSelected(SelectionEvent e) {
  218. int i = listPlugins.getSelectionIndex();
  219. if (i >= 0 && i < topbarViews.size()) {
  220. activateTopBar((UISWTViewCore) topbarViews.get(i));
  221. COConfigurationManager.setParameter("topbar.viewindex", i);
  222. }
  223. }
  224. public void widgetDefaultSelected(SelectionEvent e) {
  225. }
  226. });
  227. }
  228. skinObject = skin.getSkinObject(SkinConstants.VIEWID_PLUGINBAR);
  229. if (skinObject != null) {
  230. Listener l = new Listener() {
  231. private int mouseDownAt = 0;
  232. public void handleEvent(Event event) {
  233. Composite c = (Composite) event.widget;
  234. if (event.type == SWT.MouseDown) {
  235. Rectangle clientArea = c.getClientArea();
  236. if (event.y > clientArea.height - 10) {
  237. mouseDownAt = event.y;
  238. }
  239. } else if (event.type == SWT.MouseUp && mouseDownAt > 0) {
  240. int diff = event.y - mouseDownAt;
  241. mouseDownAt = 0;
  242. FormData formData = (FormData) c.getLayoutData();
  243. formData.height += diff;
  244. if (formData.height < 50) {
  245. formData.height = 50;
  246. } else {
  247. Rectangle clientArea = c.getShell().getClientArea();
  248. int max = clientArea.height - 350;
  249. if (formData.height > max) {
  250. formData.height = max;
  251. }
  252. }
  253. COConfigurationManager.setParameter("v3.topbar.height",
  254. formData.height);
  255. Utils.relayout(c);
  256. } else if (event.type == SWT.MouseMove) {
  257. Rectangle clientArea = c.getClientArea();
  258. boolean draggable = (event.y > clientArea.height - 10);
  259. c.setCursor(draggable ? c.getDisplay().getSystemCursor(
  260. SWT.CURSOR_SIZENS) : null);
  261. } else if (event.type == SWT.MouseExit) {
  262. c.setCursor(null);
  263. }
  264. }
  265. };
  266. Control control = skinObject.getControl();
  267. control.addListener(SWT.MouseDown, l);
  268. control.addListener(SWT.MouseUp, l);
  269. control.addListener(SWT.MouseMove, l);
  270. control.addListener(SWT.MouseExit, l);
  271. skinObject.addListener(new SWTSkinObjectListener() {
  272. public Object eventOccured(SWTSkinObject skinObject, int eventType,
  273. Object params) {
  274. if (eventType == EVENT_SHOW) {
  275. int h = COConfigurationManager.getIntParameter("v3.topbar.height");
  276. Control control = skinObject.getControl();
  277. FormData formData = (FormData) control.getLayoutData();
  278. formData.height = h;
  279. control.setLayoutData(formData);
  280. Utils.relayout(control);
  281. }
  282. return null;
  283. }
  284. });
  285. }
  286. int toActiveView = COConfigurationManager.getIntParameter(
  287. "topbar.viewindex", 0);
  288. int viewIndex = toActiveView;
  289. UISWTInstanceImpl uiSWTinstance = (UISWTInstanceImpl) UIFunctionsManagerSWT.getUIFunctionsSWT().getUISWTInstance();
  290. if (uiSWTinstance != null && !registeredCoreSubViews ) {
  291. uiSWTinstance.addView(UISWTInstance.VIEW_TOPBAR, "ViewDownSpeedGraph",
  292. new ViewDownSpeedGraph());
  293. uiSWTinstance.addView(UISWTInstance.VIEW_TOPBAR, "ViewUpSpeedGraph",
  294. new ViewUpSpeedGraph());
  295. uiSWTinstance.addView(UISWTInstance.VIEW_TOPBAR, "ViewDownSpeedGraph",
  296. new VivaldiView(false));
  297. registeredCoreSubViews = true;
  298. }
  299. UISWTViewEventListenerHolder[] pluginViews = uiSWTinstance.getViewListeners(UISWTInstance.VIEW_TOPBAR);
  300. for (UISWTViewEventListenerHolder l : pluginViews) {
  301. if (l != null) {
  302. try {
  303. UISWTViewImpl view = new UISWTViewImpl(UISWTInstance.VIEW_TOPBAR,
  304. l.getViewID(), l, null);
  305. addTopBarView(view, cPluginArea);
  306. if (toActiveView-- == 0) {
  307. activateTopBar(view);
  308. if (listPlugins != null) {
  309. listPlugins.setSelection(viewIndex);
  310. }
  311. }
  312. } catch (Exception e) {
  313. e.printStackTrace();
  314. // skip, plugin probably specifically asked to not be added
  315. }
  316. }
  317. }
  318. if (toActiveView >= 0 && topbarViews.size() > 0) {
  319. activeTopBar = topbarViews.get(0);
  320. activeTopBar.getComposite().setVisible(true);
  321. }
  322. skinObject.getControl().getParent().layout(true);
  323. } catch (Exception e) {
  324. Debug.out(e);
  325. }
  326. }
  327. /**
  328. * @param view
  329. *
  330. * @since 3.0.1.1
  331. */
  332. protected void activateTopBar(UISWTViewCore view) {
  333. if (activeTopBar != null) {
  334. Composite c = activeTopBar.getComposite();
  335. while (c.getParent() != cPluginArea) {
  336. c = c.getParent();
  337. }
  338. c.setVisible(false);
  339. }
  340. activeTopBar = view;
  341. Composite c = activeTopBar.getComposite();
  342. while (c.getParent() != cPluginArea) {
  343. c = c.getParent();
  344. }
  345. c.setVisible(true);
  346. SWTSkinObject soTitle = skin.getSkinObject("topbar-plugin-title");
  347. //System.out.println("Hello" + soTitle);
  348. if (soTitle != null) {
  349. soTitle.getControl().redraw();
  350. }
  351. }
  352. /**
  353. * @param view
  354. *
  355. * @since 3.0.1.1
  356. */
  357. private void addTopBarView(UISWTViewCore view, Composite composite) {
  358. Composite parent = new Composite(composite, SWT.None);
  359. parent.setLayoutData(Utils.getFilledFormData());
  360. parent.setLayout(new FormLayout());
  361. view.initialize(parent);
  362. parent.setVisible(false);
  363. Control[] children = parent.getChildren();
  364. for (int i = 0; i < children.length; i++) {
  365. Control control = children[i];
  366. Object ld = control.getLayoutData();
  367. boolean useGridLayout = ld != null && (ld instanceof GridData);
  368. if (useGridLayout) {
  369. GridLayout gridLayout = new GridLayout();
  370. gridLayout.horizontalSpacing = 0;
  371. gridLayout.marginHeight = 0;
  372. gridLayout.marginWidth = 0;
  373. gridLayout.verticalSpacing = 0;
  374. parent.setLayout(gridLayout);
  375. break;
  376. } else if (ld == null) {
  377. control.setLayoutData(Utils.getFilledFormData());
  378. }
  379. }
  380. topbarViews.add(view);
  381. if (listPlugins != null) {
  382. String s = view.getFullTitle();
  383. if (MessageText.keyExists(s)) {
  384. s = MessageText.getString(s);
  385. }
  386. listPlugins.add(s);
  387. }
  388. }
  389. }