/plugins/Substance/trunk/substance-6.1/substance/src/tools/uidebug/RootPaneTitlePaneUiDebugger.java

# · Java · 444 lines · 409 code · 35 blank · 0 comment · 16 complexity · 0e230cea1ecad71ed07887e685b5419f MD5 · raw file

  1. package tools.uidebug;
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.ComponentOrientation;
  5. import java.awt.Container;
  6. import java.awt.FlowLayout;
  7. import java.awt.Dialog.ModalityType;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import javax.swing.JButton;
  14. import javax.swing.JCheckBoxMenuItem;
  15. import javax.swing.JComponent;
  16. import javax.swing.JDialog;
  17. import javax.swing.JMenu;
  18. import javax.swing.JMenuItem;
  19. import javax.swing.JPanel;
  20. import javax.swing.JPopupMenu;
  21. import javax.swing.JRootPane;
  22. import javax.swing.JScrollPane;
  23. import javax.swing.JTextArea;
  24. import javax.swing.SwingUtilities;
  25. import javax.swing.TransferHandler;
  26. import javax.swing.UIManager;
  27. import org.pushingpixels.lafwidget.LafWidgetAdapter;
  28. import org.pushingpixels.lafwidget.animation.AnimationConfigurationManager;
  29. import org.pushingpixels.lafwidget.animation.effects.GhostPaintingUtils;
  30. import org.pushingpixels.substance.api.ColorSchemeTransform;
  31. import org.pushingpixels.substance.api.ComponentState;
  32. import org.pushingpixels.substance.api.SubstanceColorScheme;
  33. import org.pushingpixels.substance.api.SubstanceLookAndFeel;
  34. import org.pushingpixels.substance.api.SubstanceSkin;
  35. import org.pushingpixels.substance.api.SubstanceConstants.FocusKind;
  36. import org.pushingpixels.substance.internal.utils.LazyResettableHashMap;
  37. import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
  38. public class RootPaneTitlePaneUiDebugger extends LafWidgetAdapter<JRootPane> {
  39. protected MouseListener substanceDebugUiListener;
  40. protected JComponent titlePane;
  41. @Override
  42. public boolean requiresCustomLafSupport() {
  43. return false;
  44. }
  45. @Override
  46. public void installUI() {
  47. SwingUtilities.invokeLater(new Runnable() {
  48. @Override
  49. public void run() {
  50. if (!(UIManager.getLookAndFeel() instanceof SubstanceLookAndFeel))
  51. return;
  52. titlePane = SubstanceLookAndFeel
  53. .getTitlePaneComponent(SwingUtilities
  54. .getWindowAncestor(jcomp));
  55. if (titlePane != null) {
  56. substanceDebugUiListener = new MouseAdapter() {
  57. @Override
  58. public void mousePressed(MouseEvent e) {
  59. process(e);
  60. }
  61. @Override
  62. public void mouseReleased(MouseEvent e) {
  63. process(e);
  64. }
  65. protected void process(MouseEvent e) {
  66. if (e.isPopupTrigger()) {
  67. JPopupMenu popup = new JPopupMenu();
  68. JMenu cbMenu = new JMenu("Color blindness");
  69. JMenuItem protanopiaCurrent = new JMenuItem(
  70. "Protanopia current");
  71. protanopiaCurrent
  72. .addActionListener(new SkinChanger(
  73. new ColorSchemeTransform() {
  74. public SubstanceColorScheme transform(
  75. SubstanceColorScheme scheme) {
  76. return new ProtanopiaColorScheme(
  77. scheme);
  78. }
  79. }, "Protanopia current"));
  80. cbMenu.add(protanopiaCurrent);
  81. JMenuItem deuteranopiaCurrent = new JMenuItem(
  82. "Deuteranopia current");
  83. deuteranopiaCurrent
  84. .addActionListener(new SkinChanger(
  85. new ColorSchemeTransform() {
  86. public SubstanceColorScheme transform(
  87. SubstanceColorScheme scheme) {
  88. return new DeuteranopiaColorScheme(
  89. scheme);
  90. }
  91. }, "Deuteranopia current"));
  92. cbMenu.add(deuteranopiaCurrent);
  93. JMenuItem tritanopiaCurrent = new JMenuItem(
  94. "Tritanopia current");
  95. tritanopiaCurrent
  96. .addActionListener(new SkinChanger(
  97. new ColorSchemeTransform() {
  98. public SubstanceColorScheme transform(
  99. SubstanceColorScheme scheme) {
  100. return new TritanopiaColorScheme(
  101. scheme);
  102. }
  103. }, "Tritanopia current"));
  104. cbMenu.add(tritanopiaCurrent);
  105. cbMenu.addSeparator();
  106. JMenuItem restoreOriginal = new JMenuItem(
  107. "Restore original");
  108. if (SubstanceLookAndFeel.getCurrentSkin(null)
  109. .getColorScheme(null,
  110. ComponentState.ENABLED) instanceof ColorBlindColorScheme) {
  111. restoreOriginal
  112. .addActionListener(new SkinChanger(
  113. new ColorSchemeTransform() {
  114. public SubstanceColorScheme transform(
  115. SubstanceColorScheme scheme) {
  116. if (scheme instanceof ColorBlindColorScheme)
  117. return ((ColorBlindColorScheme) scheme)
  118. .getOrigScheme();
  119. return scheme;
  120. }
  121. }, "Current"));
  122. } else {
  123. restoreOriginal.setEnabled(false);
  124. }
  125. cbMenu.add(restoreOriginal);
  126. popup.add(cbMenu);
  127. JMenu animMenu = new JMenu("Animation rate");
  128. JMenuItem debugNone = new JMenuItem("None");
  129. debugNone
  130. .addActionListener(new AnimationChanger(
  131. 0));
  132. animMenu.add(debugNone);
  133. JMenuItem debugAnim = new JMenuItem(
  134. "Debug rate (extra slow)");
  135. debugAnim
  136. .addActionListener(new AnimationChanger(
  137. 5000));
  138. animMenu.add(debugAnim);
  139. JMenuItem debugAnimFast = new JMenuItem(
  140. "Debug rate (faster)");
  141. debugAnimFast
  142. .addActionListener(new AnimationChanger(
  143. 2500));
  144. animMenu.add(debugAnimFast);
  145. JMenuItem debugSlow = new JMenuItem("Slow rate");
  146. debugSlow
  147. .addActionListener(new AnimationChanger(
  148. 1000));
  149. animMenu.add(debugSlow);
  150. JMenuItem debugRegular = new JMenuItem(
  151. "Regular rate");
  152. debugRegular
  153. .addActionListener(new AnimationChanger(
  154. 250));
  155. animMenu.add(debugRegular);
  156. JMenuItem debugFast = new JMenuItem("Fast rate");
  157. debugFast
  158. .addActionListener(new AnimationChanger(
  159. 100));
  160. animMenu.add(debugFast);
  161. popup.add(animMenu);
  162. JMenu focusMenu = new JMenu("Focus kind");
  163. for (FocusKind fKind : FocusKind.values()) {
  164. JMenuItem focusMenuItem = new JMenuItem(
  165. fKind.name().toLowerCase());
  166. focusMenuItem
  167. .addActionListener(new FocusKindChanger(
  168. fKind));
  169. focusMenu.add(focusMenuItem);
  170. }
  171. popup.add(focusMenu);
  172. JMenuItem dumpHierarchy = new JMenuItem(
  173. "Dump hierarchy");
  174. dumpHierarchy
  175. .addActionListener(new ActionListener() {
  176. public void actionPerformed(
  177. ActionEvent e) {
  178. dump(jcomp, 0);
  179. }
  180. });
  181. popup.add(dumpHierarchy);
  182. final JCheckBoxMenuItem ltrChange = new JCheckBoxMenuItem(
  183. "Is left-to-right");
  184. ltrChange.setSelected(jcomp
  185. .getComponentOrientation()
  186. .isLeftToRight());
  187. ltrChange
  188. .addActionListener(new ActionListener() {
  189. public void actionPerformed(
  190. ActionEvent e) {
  191. SwingUtilities
  192. .invokeLater(new Runnable() {
  193. public void run() {
  194. jcomp
  195. .applyComponentOrientation(ltrChange
  196. .isSelected() ? ComponentOrientation.LEFT_TO_RIGHT
  197. : ComponentOrientation.RIGHT_TO_LEFT);
  198. }
  199. });
  200. }
  201. });
  202. popup.add(ltrChange);
  203. final JCheckBoxMenuItem useThemedIcons = new JCheckBoxMenuItem(
  204. "Use themed icons");
  205. useThemedIcons
  206. .setSelected(SubstanceCoreUtilities
  207. .useThemedDefaultIcon(null));
  208. useThemedIcons
  209. .addActionListener(new ActionListener() {
  210. public void actionPerformed(
  211. ActionEvent e) {
  212. SwingUtilities
  213. .invokeLater(new Runnable() {
  214. public void run() {
  215. UIManager
  216. .put(
  217. SubstanceLookAndFeel.USE_THEMED_DEFAULT_ICONS,
  218. useThemedIcons
  219. .isSelected() ? Boolean.TRUE
  220. : null);
  221. jcomp.repaint();
  222. }
  223. });
  224. }
  225. });
  226. popup.add(useThemedIcons);
  227. final JCheckBoxMenuItem ghostDebugMode = new JCheckBoxMenuItem(
  228. "Ghost debug mode");
  229. ghostDebugMode
  230. .addActionListener(new ActionListener() {
  231. public void actionPerformed(
  232. ActionEvent e) {
  233. SwingUtilities
  234. .invokeLater(new Runnable() {
  235. public void run() {
  236. ghostDebugMode
  237. .setEnabled(false);
  238. GhostPaintingUtils.MAX_ICON_GHOSTING_ALPHA = 0.8f;
  239. GhostPaintingUtils.MIN_ICON_GHOSTING_ALPHA = 0.6f;
  240. GhostPaintingUtils.MAX_PRESS_GHOSTING_ALPHA = 0.8f;
  241. GhostPaintingUtils.MIN_PRESS_GHOSTING_ALPHA = 0.6f;
  242. GhostPaintingUtils.DECAY_FACTOR = 0.7f;
  243. }
  244. });
  245. }
  246. });
  247. popup.add(ghostDebugMode);
  248. JMenuItem showCacheStats = new JMenuItem(
  249. "Show cache stats");
  250. showCacheStats
  251. .addActionListener(new ActionListener() {
  252. @Override
  253. public void actionPerformed(
  254. ActionEvent e) {
  255. SwingUtilities
  256. .invokeLater(new Runnable() {
  257. public void run() {
  258. final JTextArea textArea = new JTextArea();
  259. java.util.List<String> stats = LazyResettableHashMap
  260. .getStats();
  261. if (stats != null) {
  262. for (String stat : stats) {
  263. textArea
  264. .append(stat
  265. + "\n");
  266. }
  267. }
  268. final JDialog dialog = new JDialog(
  269. SwingUtilities
  270. .getWindowAncestor(jcomp),
  271. ModalityType.APPLICATION_MODAL);
  272. dialog
  273. .setTitle("Substance cache stats");
  274. dialog
  275. .setLayout(new BorderLayout());
  276. dialog
  277. .add(
  278. new JScrollPane(
  279. textArea),
  280. BorderLayout.CENTER);
  281. JButton dismiss = new JButton(
  282. "Dismiss");
  283. dismiss
  284. .addActionListener(new ActionListener() {
  285. public void actionPerformed(
  286. ActionEvent e) {
  287. dialog
  288. .dispose();
  289. }
  290. });
  291. JButton copyToClipboard = new JButton(
  292. "Copy to clipboard");
  293. copyToClipboard
  294. .addActionListener(new ActionListener() {
  295. public void actionPerformed(
  296. ActionEvent e) {
  297. textArea
  298. .selectAll();
  299. TransferHandler
  300. .getCopyAction()
  301. .actionPerformed(
  302. new ActionEvent(
  303. textArea,
  304. ActionEvent.ACTION_PERFORMED,
  305. "Copy"));
  306. }
  307. });
  308. JPanel controls = new JPanel(
  309. new FlowLayout(
  310. FlowLayout.RIGHT));
  311. controls
  312. .add(copyToClipboard);
  313. controls
  314. .add(dismiss);
  315. dialog
  316. .add(
  317. controls,
  318. BorderLayout.SOUTH);
  319. dialog.setSize(
  320. 500,
  321. 400);
  322. dialog
  323. .setLocationRelativeTo(SwingUtilities
  324. .getRootPane(jcomp));
  325. dialog
  326. .setVisible(true);
  327. }
  328. });
  329. }
  330. });
  331. popup.add(showCacheStats);
  332. popup.show(titlePane, e.getX(), e.getY());
  333. }
  334. }
  335. };
  336. titlePane.addMouseListener(substanceDebugUiListener);
  337. }
  338. }
  339. });
  340. }
  341. @Override
  342. public void uninstallUI() {
  343. if (this.substanceDebugUiListener != null) {
  344. titlePane.removeMouseListener(this.substanceDebugUiListener);
  345. this.substanceDebugUiListener = null;
  346. }
  347. }
  348. protected static class SkinChanger implements ActionListener {
  349. protected ColorSchemeTransform transform;
  350. protected String name;
  351. public SkinChanger(ColorSchemeTransform transform, String name) {
  352. super();
  353. this.transform = transform;
  354. this.name = name;
  355. }
  356. public void actionPerformed(ActionEvent e) {
  357. SwingUtilities.invokeLater(new Runnable() {
  358. public void run() {
  359. SubstanceSkin newSkin = SubstanceLookAndFeel
  360. .getCurrentSkin(null).transform(transform, name);
  361. SubstanceLookAndFeel.setSkin(newSkin);
  362. }
  363. });
  364. }
  365. }
  366. protected static class AnimationChanger implements ActionListener {
  367. protected long newAnimationDuration;
  368. public AnimationChanger(long newAnimationDuration) {
  369. super();
  370. this.newAnimationDuration = newAnimationDuration;
  371. }
  372. public void actionPerformed(ActionEvent e) {
  373. SwingUtilities.invokeLater(new Runnable() {
  374. public void run() {
  375. AnimationConfigurationManager.getInstance()
  376. .setTimelineDuration(newAnimationDuration);
  377. }
  378. });
  379. }
  380. }
  381. protected static class FocusKindChanger implements ActionListener {
  382. protected FocusKind newFocusKind;
  383. public FocusKindChanger(FocusKind newFocusKind) {
  384. super();
  385. this.newFocusKind = newFocusKind;
  386. }
  387. public void actionPerformed(ActionEvent e) {
  388. SwingUtilities.invokeLater(new Runnable() {
  389. public void run() {
  390. UIManager
  391. .put(SubstanceLookAndFeel.FOCUS_KIND, newFocusKind);
  392. }
  393. });
  394. }
  395. }
  396. public static void dump(Component comp, int level) {
  397. StringBuffer sb = new StringBuffer();
  398. for (int i = 0; i < level; i++)
  399. sb.append(" ");
  400. sb.append(comp.toString());
  401. System.out.println(sb);
  402. if (comp instanceof Container) {
  403. Container cont = (Container) comp;
  404. for (int i = 0; i < cont.getComponentCount(); i++) {
  405. dump(cont.getComponent(i), level + 1);
  406. }
  407. }
  408. }
  409. }