PageRenderTime 46ms CodeModel.GetById 23ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

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