/edu/uncc/parsets/gui/MainWindow.java

https://code.google.com/p/parsets/ · Java · 327 lines · 240 code · 59 blank · 28 comment · 10 complexity · 0d96318521ce80ee953711a24ab7651c MD5 · raw file

  1. package edu.uncc.parsets.gui;
  2. import edu.uncc.parsets.parsets.SelectionChangeEvent;
  3. import java.awt.BorderLayout;
  4. import java.awt.Desktop;
  5. import java.awt.FileDialog;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.InputEvent;
  10. import java.awt.event.KeyEvent;
  11. import java.net.URI;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JCheckBoxMenuItem;
  14. import javax.swing.JFrame;
  15. import javax.swing.JMenu;
  16. import javax.swing.JMenuBar;
  17. import javax.swing.JMenuItem;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.KeyStroke;
  20. import edu.uncc.parsets.ParallelSets;
  21. import edu.uncc.parsets.data.LocalDB;
  22. import edu.uncc.parsets.parsets.ParSetsView;
  23. import edu.uncc.parsets.parsets.PopupPresenter;
  24. import edu.uncc.parsets.util.PSLogging;
  25. import edu.uncc.parsets.util.osabstraction.AbstractOS;
  26. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  27. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  28. * and others (see Authors.txt for full list)
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. *
  34. * * Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * * Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * * Neither the name of UNC Charlotte nor the names of its contributors
  40. * may be used to endorse or promote products derived from this software
  41. * without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  44. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  47. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  50. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  52. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  54. @SuppressWarnings("serial")
  55. public class MainWindow extends JFrame implements AbstractMainView {
  56. private static final int WINDOWHEIGHT = 600;
  57. private static final int WINDOWWIDTH = 900;
  58. public static final String WINDOWTITLE = ParallelSets.PROGRAMNAME + " V" + ParallelSets.VERSION;
  59. private static final String ICONFILE = "/support/parsets-512.gif";
  60. protected static final String MESSAGE = "Reinitializing the database will delete all datasets.";
  61. protected static final String TITLE = "Reinitialize DB";
  62. private Controller controller;
  63. private JMenuItem openDataSet;
  64. private JMenuItem editDataSet;
  65. private JMenuItem deleteDataSet;
  66. private static class PNGFileNameFilter extends CombinedFileNameFilter {
  67. @Override
  68. public String getDescription() {
  69. return "PNG Files";
  70. }
  71. @Override
  72. public String getExtension() {
  73. return ".png";
  74. }
  75. }
  76. private static class PDFFileNameFilter extends CombinedFileNameFilter {
  77. @Override
  78. public String getDescription() {
  79. return "PDF Files";
  80. }
  81. @Override
  82. public String getExtension() {
  83. return ".pdf";
  84. }
  85. }
  86. public MainWindow() {
  87. this(null);
  88. }
  89. public MainWindow(Controller controller) {
  90. super(WINDOWTITLE);
  91. setSize(WINDOWWIDTH, WINDOWHEIGHT);
  92. setIconImage(new ImageIcon(ICONFILE).getImage());
  93. setLayout(new BorderLayout());
  94. PSLogging.init(this, PSLogging.DEFAULTLOGLEVEL);
  95. Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  96. public void uncaughtException(Thread t, Throwable e) {
  97. PSLogging.logger.fatal("Uncaught exception, program terminating.", e);
  98. }
  99. });
  100. this.controller = controller;
  101. if(this.controller == null){
  102. this.controller = new Controller();
  103. this.controller.addPopupPresenter(new PopupPresenter(){
  104. @Override
  105. public JMenuItem getJMenuItem() {
  106. return new JMenuItem("Create Table");
  107. }
  108. @Override
  109. public void selectionChanged(SelectionChangeEvent event) {
  110. TableWindow tab = new TableWindow(event.getSelectedCategory(), event.isOnCategoryBar());
  111. }
  112. });
  113. }
  114. setJMenuBar(makeMenu(this.controller));
  115. SideBar sideBar = new SideBar(this, this.controller);
  116. add(sideBar, BorderLayout.WEST);
  117. ParSetsView parSetsView = new ParSetsView(this.controller);
  118. add(parSetsView, BorderLayout.CENTER);
  119. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  120. setVisible(true);
  121. }
  122. private JMenuBar makeMenu(final Controller controller) {
  123. JMenuBar menuBar = new JMenuBar();
  124. JMenu dataset = new JMenu("Data Set");
  125. openDataSet = new JMenuItem("Open");
  126. openDataSet.setEnabled(false);
  127. openDataSet.addActionListener(new ActionListener() {
  128. @Override
  129. public void actionPerformed(ActionEvent e) {
  130. controller.dbTab.openSelectedDataSet();
  131. }
  132. });
  133. dataset.add(openDataSet);
  134. JMenuItem closeDataSet = new JMenuItem("Close");
  135. closeDataSet.setEnabled(false);
  136. closeDataSet.setAccelerator(KeyStroke.getKeyStroke('W', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
  137. dataset.add(closeDataSet);
  138. editDataSet = new JMenuItem("Edit");
  139. editDataSet.setEnabled(false);
  140. dataset.add(editDataSet);
  141. deleteDataSet = new JMenuItem("Delete");
  142. deleteDataSet.setEnabled(false);
  143. deleteDataSet.addActionListener(new ActionListener() {
  144. @Override
  145. public void actionPerformed(ActionEvent e) {
  146. controller.dbTab.deleteSelectedDataSet();
  147. }
  148. });
  149. deleteDataSet.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
  150. dataset.add(deleteDataSet);
  151. dataset.addSeparator();
  152. JMenuItem importcsv = new JMenuItem("Import CSV File");
  153. importcsv.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
  154. importcsv.addActionListener(new ActionListener() {
  155. @Override
  156. public void actionPerformed(ActionEvent e) {
  157. new DataWizard();
  158. }
  159. });
  160. dataset.add(importcsv);
  161. JMenuItem reinit = new JMenuItem("Reinitialize DB");
  162. reinit.addActionListener(new ActionListener() {
  163. @Override
  164. public void actionPerformed(ActionEvent e) {
  165. int choice = JOptionPane.showConfirmDialog(MainWindow.this, MESSAGE,
  166. TITLE, JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
  167. if (choice == 0) {
  168. LocalDB.getDefaultDB().initializeDB();
  169. }
  170. }
  171. });
  172. dataset.add(reinit);
  173. dataset.addSeparator();
  174. JMenu export = new JMenu("Save Screenshot ...");
  175. dataset.add(export);
  176. JMenuItem savepng = new JMenuItem("as PNG");
  177. savepng.setAccelerator(KeyStroke.getKeyStroke('P', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | InputEvent.ALT_MASK));
  178. savepng.addActionListener(new ActionListener() {
  179. @Override
  180. public void actionPerformed(ActionEvent e) {
  181. String fileName = AbstractOS.getCurrentOS().showDialog(MainWindow.this, new PNGFileNameFilter(), FileDialog.SAVE);
  182. if (fileName != null) {
  183. controller.parSetsView.takePNGScreenShot(fileName);
  184. }
  185. }
  186. });
  187. export.add(savepng);
  188. JMenuItem savepdf = new JMenuItem("as PDF");
  189. savepdf.setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | InputEvent.ALT_MASK));
  190. savepdf.addActionListener(new ActionListener() {
  191. @Override
  192. public void actionPerformed(ActionEvent e) {
  193. String fileName = AbstractOS.getCurrentOS().showDialog(MainWindow.this, new PDFFileNameFilter(), FileDialog.SAVE);
  194. if (fileName != null) {
  195. controller.parSetsView.takePDFScreenShot(fileName);
  196. }
  197. }
  198. });
  199. export.add(savepdf);
  200. if (!AbstractOS.getCurrentOS().isMacOSX()) {
  201. dataset.addSeparator();
  202. JMenuItem quit = new JMenuItem("Quit");
  203. quit.setAccelerator(KeyStroke.getKeyStroke('Q', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
  204. quit.addActionListener(new ActionListener() {
  205. @Override
  206. public void actionPerformed(ActionEvent e) {
  207. setVisible(false);
  208. dispose();
  209. }
  210. });
  211. dataset.add(quit);
  212. }
  213. menuBar.add(dataset);
  214. JMenu view = new JMenu("View");
  215. final JMenuItem tooltips = new JCheckBoxMenuItem("Show Tooltips", true);
  216. tooltips.addActionListener(new ActionListener() {
  217. @Override
  218. public void actionPerformed(ActionEvent e) {
  219. controller.parSetsView.setShowTooltips(tooltips.isSelected());
  220. }
  221. });
  222. view.add(tooltips);
  223. final JCheckBoxMenuItem strong = new JCheckBoxMenuItem("Stronger Highlights", true);
  224. strong.addActionListener(new ActionListener() {
  225. @Override
  226. public void actionPerformed(ActionEvent e) {
  227. controller.parSetsView.setStrongerSelection(strong.isSelected());
  228. }
  229. });
  230. view.add(strong);
  231. final JCheckBoxMenuItem antialiasing = new JCheckBoxMenuItem("Anti-Aliasing", true);
  232. antialiasing.addActionListener(new ActionListener() {
  233. @Override
  234. public void actionPerformed(ActionEvent e) {
  235. controller.parSetsView.setAntiAlias(antialiasing.isSelected());
  236. }
  237. });
  238. view.add(antialiasing);
  239. menuBar.add(view);
  240. JMenu help = new JMenu("Help");
  241. if (!AbstractOS.getCurrentOS().isMacOSX()) {
  242. JMenuItem about = new JMenuItem("About Parallel Sets");
  243. help.add(about);
  244. }
  245. JMenuItem website = new JMenuItem("Visit Website");
  246. website.addActionListener(new ActionListener() {
  247. @Override
  248. public void actionPerformed(ActionEvent event) {
  249. try {
  250. Desktop.getDesktop().browse(new URI(ParallelSets.WEBSITE));
  251. } catch (Exception e) {
  252. PSLogging.logger.error("Could not open website", e);
  253. }
  254. }
  255. });
  256. help.add(website);
  257. menuBar.add(help);
  258. return menuBar;
  259. }
  260. public void setDSMenuItemsEnabled(boolean enabled) {
  261. openDataSet.setEnabled(enabled);
  262. editDataSet.setEnabled(enabled);
  263. deleteDataSet.setEnabled(enabled);
  264. }
  265. public Controller getController() {
  266. return controller;
  267. }
  268. }