PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/azureus-4.7.0.2/org/gudy/azureus2/ui/swt/mainwindow/SystemWarningWindow.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 420 lines | 311 code | 85 blank | 24 comment | 32 complexity | 5f1b317d396c8098e3c83fb64b1e20dd MD5 | raw file
  1. /**
  2. * Created on Jan 4, 2010
  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 org.gudy.azureus2.ui.swt.mainwindow;
  19. import java.util.ArrayList;
  20. import org.eclipse.swt.SWT;
  21. import org.eclipse.swt.events.*;
  22. import org.eclipse.swt.graphics.*;
  23. import org.eclipse.swt.layout.*;
  24. import org.eclipse.swt.widgets.*;
  25. import org.gudy.azureus2.core3.internat.MessageText;
  26. import org.gudy.azureus2.core3.logging.LogAlert;
  27. import org.gudy.azureus2.core3.util.AERunnable;
  28. import org.gudy.azureus2.core3.util.Constants;
  29. import org.gudy.azureus2.core3.util.Debug;
  30. import org.gudy.azureus2.ui.swt.Alerts;
  31. import org.gudy.azureus2.ui.swt.Messages;
  32. import org.gudy.azureus2.ui.swt.Utils;
  33. import org.gudy.azureus2.ui.swt.shells.GCStringPrinter;
  34. import org.gudy.azureus2.ui.swt.shells.MessageBoxShell;
  35. import org.gudy.azureus2.ui.swt.shells.GCStringPrinter.URLInfo;
  36. import com.aelitis.azureus.ui.swt.imageloader.ImageLoader;
  37. import com.aelitis.azureus.ui.swt.utils.ColorCache;
  38. /**
  39. * @author TuxPaper
  40. * @created Jan 4, 2010
  41. *
  42. */
  43. public class SystemWarningWindow
  44. {
  45. private final static int WIDTH = 230;
  46. private final static int BORDER_X = 12;
  47. private final static int BORDER_Y0 = 10;
  48. private final static int BORDER_Y1 = 6;
  49. private final static int GAP_Y = 5;
  50. private final static int GAP_BUTTON_Y = 20;
  51. private final static int GAP_Y_TITLE_COUNT = 3;
  52. private final LogAlert logAlert;
  53. private final Point ptBottomRight;
  54. private final Shell parent;
  55. private Shell shell;
  56. private Image imgClose;
  57. private Rectangle boundsClose;
  58. private GCStringPrinter spText;
  59. private GCStringPrinter spTitle;
  60. private GCStringPrinter spCount;
  61. private Point sizeTitle;
  62. private Point sizeText;
  63. private Point sizeCount;
  64. private Font fontTitle;
  65. private Font fontCount;
  66. private int height;
  67. private Rectangle rectX;
  68. private int historyPosition;
  69. private String title;
  70. private String text;
  71. public static int numWarningWindowsOpen = 0;
  72. public SystemWarningWindow(LogAlert logAlert, Point ptBottomRight,
  73. Shell parent, int historyPosition) {
  74. this.logAlert = logAlert;
  75. this.ptBottomRight = ptBottomRight;
  76. this.parent = parent;
  77. this.historyPosition = historyPosition;
  78. String amb_key_suffix;
  79. switch (logAlert.entryType) {
  80. case LogAlert.AT_ERROR:
  81. amb_key_suffix = "error";
  82. break;
  83. case LogAlert.AT_INFORMATION:
  84. amb_key_suffix = "information";
  85. break;
  86. case LogAlert.AT_WARNING:
  87. amb_key_suffix = "warning";
  88. break;
  89. default:
  90. amb_key_suffix = null;
  91. break;
  92. }
  93. title = amb_key_suffix == null ? Constants.APP_NAME
  94. : MessageText.getString("AlertMessageBox." + amb_key_suffix);
  95. if (logAlert.text.startsWith("{")) {
  96. text = MessageText.expandValue(logAlert.text);
  97. } else {
  98. text = logAlert.text;
  99. }
  100. if (logAlert.err != null) {
  101. text += "\n" + Debug.getExceptionMessage(logAlert.err);
  102. }
  103. if (logAlert.details != null) {
  104. text += "\n<A HREF=\"details\">" + MessageText.getString("v3.MainWindow.button.viewdetails") + "</A>";
  105. }
  106. Utils.execSWTThread(new AERunnable() {
  107. public void runSupport() {
  108. openWindow();
  109. }
  110. });
  111. }
  112. protected void openWindow() {
  113. Display display = parent.getDisplay();
  114. //shell = new Shell(parent, SWT.TOOL | SWT.TITLE | SWT.CLOSE);
  115. //shell.setText("Warning (X of X)");
  116. shell = new Shell(parent, SWT.TOOL);
  117. shell.setLayout(new FormLayout());
  118. shell.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
  119. shell.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
  120. Menu menu = new Menu(shell);
  121. MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
  122. Messages.setLanguageText(menuItem, "MyTorrentsView.menu.thisColumn.toClipboard");
  123. menuItem.addSelectionListener(new SelectionListener() {
  124. public void widgetSelected(SelectionEvent e) {
  125. ClipboardCopy.copyToClipBoard(logAlert.text
  126. + (logAlert.details == null ? "" : "\n" + logAlert.details));
  127. }
  128. public void widgetDefaultSelected(SelectionEvent e) {
  129. }
  130. });
  131. shell.setMenu(menu);
  132. ImageLoader imageLoader = ImageLoader.getInstance();
  133. imgClose = imageLoader.getImage("image.systemwarning.closeitem");
  134. boundsClose = imgClose.getBounds();
  135. GC gc = new GC(shell);
  136. FontData[] fontdata = gc.getFont().getFontData();
  137. fontdata[0].setHeight(fontdata[0].getHeight() + 1);
  138. fontdata[0].setStyle(SWT.BOLD);
  139. fontTitle = new Font(display, fontdata);
  140. fontdata = gc.getFont().getFontData();
  141. fontdata[0].setHeight(fontdata[0].getHeight() - 1);
  142. fontCount = new Font(display, fontdata);
  143. shell.addDisposeListener(new DisposeListener() {
  144. public void widgetDisposed(DisposeEvent e) {
  145. Utils.disposeSWTObjects(new Object[] {
  146. fontTitle,
  147. fontCount,
  148. });
  149. numWarningWindowsOpen--;
  150. }
  151. });
  152. Rectangle printArea = new Rectangle(BORDER_X, 0, WIDTH - (BORDER_X * 2),
  153. 5000);
  154. spText = new GCStringPrinter(gc, text, printArea, true, false, SWT.WRAP);
  155. spText.setUrlColor(Colors.blues[Colors.FADED_DARKEST]);
  156. spText.calculateMetrics();
  157. gc.setFont(fontCount);
  158. String sCount = MessageText.getString("OpenTorrentWindow.xOfTotal",
  159. new String[] {
  160. "" + historyPosition + 1,
  161. "" + getWarningCount()
  162. });
  163. spCount = new GCStringPrinter(gc, sCount, printArea, true, false, SWT.WRAP);
  164. spCount.calculateMetrics();
  165. gc.setFont(fontTitle);
  166. spTitle = new GCStringPrinter(gc, title, printArea, true, false, SWT.WRAP);
  167. spTitle.calculateMetrics();
  168. gc.dispose();
  169. sizeText = spText.getCalculatedSize();
  170. sizeTitle = spTitle.getCalculatedSize();
  171. sizeCount = spCount.getCalculatedSize();
  172. FormData fd;
  173. Button btnDismiss = new Button(shell, SWT.PUSH);
  174. Messages.setLanguageText(btnDismiss, "Button.dismiss");
  175. final int btnHeight = btnDismiss.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
  176. Button btnPrev = new Button(shell, SWT.PUSH);
  177. btnPrev.setText("<");
  178. Button btnNext = new Button(shell, SWT.PUSH);
  179. btnNext.setText(">");
  180. fd = new FormData();
  181. fd.bottom = new FormAttachment(100, -BORDER_Y1);
  182. fd.right = new FormAttachment(100, -BORDER_X);
  183. btnNext.setLayoutData(fd);
  184. fd = new FormData();
  185. fd.bottom = new FormAttachment(100, -BORDER_Y1);
  186. fd.right = new FormAttachment(btnNext, -BORDER_X);
  187. btnPrev.setLayoutData(fd);
  188. fd = new FormData();
  189. fd.bottom = new FormAttachment(100, -BORDER_Y1);
  190. fd.right = new FormAttachment(btnPrev, -BORDER_X);
  191. btnDismiss.setLayoutData(fd);
  192. height = BORDER_Y0 + sizeTitle.y + GAP_Y + sizeText.y + GAP_Y_TITLE_COUNT
  193. + sizeCount.y + GAP_BUTTON_Y + btnHeight + BORDER_Y1;
  194. Rectangle area = shell.computeTrim(ptBottomRight.x - WIDTH, ptBottomRight.y
  195. - height, WIDTH, height);
  196. shell.setBounds(area);
  197. shell.setLocation(ptBottomRight.x - area.width, ptBottomRight.y
  198. - area.height - 2);
  199. rectX = new Rectangle(area.width - BORDER_X - boundsClose.width, BORDER_Y0,
  200. boundsClose.width, boundsClose.height);
  201. shell.addMouseMoveListener(new MouseMoveListener() {
  202. int lastCursor = SWT.CURSOR_ARROW;
  203. public void mouseMove(MouseEvent e) {
  204. if (shell == null || shell.isDisposed()) {
  205. return;
  206. }
  207. URLInfo hitUrl = spText.getHitUrl(e.x, e.y);
  208. int cursor = (rectX.contains(e.x, e.y)) || hitUrl != null
  209. ? SWT.CURSOR_HAND : SWT.CURSOR_ARROW;
  210. if (cursor != lastCursor) {
  211. lastCursor = cursor;
  212. shell.setCursor(e.display.getSystemCursor(cursor));
  213. }
  214. }
  215. });
  216. shell.addMouseListener(new MouseListener() {
  217. public void mouseUp(MouseEvent e) {
  218. if (shell == null || shell.isDisposed()) {
  219. return;
  220. }
  221. if (rectX.contains(e.x, e.y)) {
  222. shell.dispose();
  223. }
  224. URLInfo hitUrl = spText.getHitUrl(e.x, e.y);
  225. if (hitUrl != null) {
  226. if (hitUrl.url.equals("details")) {
  227. MessageBoxShell mb = new MessageBoxShell(Constants.APP_NAME,
  228. logAlert.details, new String[] {
  229. MessageText.getString("Button.ok")
  230. }, 0);
  231. mb.setUseTextBox(true);
  232. mb.setParent(Utils.findAnyShell());
  233. mb.open(null);
  234. } else {
  235. Utils.launch(hitUrl.url);
  236. }
  237. }
  238. }
  239. public void mouseDown(MouseEvent e) {
  240. }
  241. public void mouseDoubleClick(MouseEvent e) {
  242. }
  243. });
  244. shell.addPaintListener(new PaintListener() {
  245. public void paintControl(PaintEvent e) {
  246. e.gc.drawImage(imgClose, WIDTH - BORDER_X - boundsClose.width,
  247. BORDER_Y0);
  248. Rectangle printArea;
  249. printArea = new Rectangle(BORDER_X, BORDER_Y0 + sizeTitle.y + GAP_Y_TITLE_COUNT,
  250. WIDTH, 100);
  251. String sCount = MessageText.getString("OpenTorrentWindow.xOfTotal",
  252. new String[] {
  253. "" + (historyPosition + 1),
  254. "" + getWarningCount()
  255. });
  256. e.gc.setAlpha(180);
  257. Font lastFont = e.gc.getFont();
  258. e.gc.setFont(fontCount);
  259. spCount = new GCStringPrinter(e.gc, sCount, printArea, true, false,
  260. SWT.WRAP | SWT.TOP);
  261. spCount.printString();
  262. e.gc.setAlpha(255);
  263. sizeCount = spCount.getCalculatedSize();
  264. e.gc.setFont(lastFont);
  265. spText.printString(e.gc, new Rectangle(BORDER_X, BORDER_Y0
  266. + sizeTitle.y + GAP_Y_TITLE_COUNT + sizeCount.y + GAP_Y, WIDTH - BORDER_X
  267. - BORDER_X, 5000), SWT.WRAP | SWT.TOP);
  268. e.gc.setFont(fontTitle);
  269. e.gc.setForeground(ColorCache.getColor(e.gc.getDevice(), "#54728c"));
  270. spTitle.printString(e.gc, new Rectangle(BORDER_X, BORDER_Y0, WIDTH
  271. - BORDER_X - BORDER_X, 5000), SWT.WRAP | SWT.TOP);
  272. e.gc.setLineStyle(SWT.LINE_DOT);
  273. e.gc.setLineWidth(1);
  274. e.gc.setAlpha(180);
  275. e.gc.drawLine(BORDER_X, height - btnHeight - (GAP_BUTTON_Y / 2)
  276. - BORDER_Y1, WIDTH - BORDER_X, height - btnHeight
  277. - (GAP_BUTTON_Y / 2) - BORDER_Y1);
  278. }
  279. });
  280. shell.addTraverseListener(new TraverseListener() {
  281. public void keyTraversed(TraverseEvent e) {
  282. if (e.detail == SWT.TRAVERSE_ESCAPE) {
  283. shell.dispose();
  284. return;
  285. }
  286. }
  287. });
  288. btnPrev.addSelectionListener(new SelectionListener() {
  289. public void widgetSelected(SelectionEvent e) {
  290. ArrayList<LogAlert> alerts = Alerts.getUnviewedLogAlerts();
  291. int pos = historyPosition - 1;
  292. if (pos < 0 || pos >= alerts.size()) {
  293. return;
  294. }
  295. new SystemWarningWindow(alerts.get(pos), ptBottomRight, parent, pos);
  296. shell.dispose();
  297. }
  298. public void widgetDefaultSelected(SelectionEvent e) {
  299. }
  300. });
  301. btnPrev.setEnabled(historyPosition > 0);
  302. btnNext.addSelectionListener(new SelectionListener() {
  303. public void widgetSelected(SelectionEvent e) {
  304. ArrayList<LogAlert> alerts = Alerts.getUnviewedLogAlerts();
  305. int pos = historyPosition + 1;
  306. if (pos >= alerts.size()) {
  307. return;
  308. }
  309. new SystemWarningWindow(alerts.get(pos), ptBottomRight, parent, pos);
  310. shell.dispose();
  311. }
  312. public void widgetDefaultSelected(SelectionEvent e) {
  313. }
  314. });
  315. ArrayList<LogAlert> alerts = Alerts.getUnviewedLogAlerts();
  316. btnNext.setEnabled(alerts.size() != historyPosition + 1);
  317. btnDismiss.addSelectionListener(new SelectionListener() {
  318. public void widgetSelected(SelectionEvent e) {
  319. ArrayList<LogAlert> alerts = Alerts.getUnviewedLogAlerts();
  320. for (int i = 0; i < alerts.size() && i <= historyPosition; i++) {
  321. Alerts.markAlertAsViewed(alerts.get(i));
  322. }
  323. shell.dispose();
  324. }
  325. public void widgetDefaultSelected(SelectionEvent e) {
  326. }
  327. });
  328. shell.open();
  329. numWarningWindowsOpen++;
  330. }
  331. private int getWarningCount() {
  332. ArrayList<LogAlert> historyList = Alerts.getUnviewedLogAlerts();
  333. return historyList.size();
  334. }
  335. }