src/org/gel/mauve/gui/AnalysisDisplayWindow.java

Go to the documentation of this file.
00001 package org.gel.mauve.gui;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.CardLayout;
00005 import java.awt.Font;
00006 import java.awt.GridLayout;
00007 // these are needed to grab the users display screen size
00008 import java.awt.Toolkit;
00009 import java.awt.event.ActionEvent;
00010 import java.awt.event.ActionListener;
00011 import java.io.File;
00012 import java.io.PrintStream;
00013 import java.util.HashMap;
00014 
00015 import javax.swing.BorderFactory;
00016 import javax.swing.BoxLayout;
00017 import javax.swing.JButton;
00018 import javax.swing.JComponent;
00019 import javax.swing.JFileChooser;
00020 import javax.swing.JFrame;
00021 import javax.swing.JOptionPane;
00022 import javax.swing.JPanel;
00023 import javax.swing.JTable;
00024 import javax.swing.JTextArea;
00025 import javax.swing.JScrollPane;
00026 import javax.swing.border.BevelBorder;
00027 import javax.swing.table.DefaultTableModel;
00028 import javax.swing.table.TableModel;
00029 
00030 //import org.gel.mauve.assembly.ScoreAssembly.ChangeCards;
00031 
00032 public class AnalysisDisplayWindow extends JFrame {
00033         
00034         private static int xMax = Toolkit.getDefaultToolkit().getScreenSize().width;
00035         
00036         private int contentCount;
00037         
00038         private JPanel topBar;
00039         
00040         private JButton saveBtn;
00041         
00042         private JPanel content;
00043         
00044         private JPanel botBar;
00045         
00046         private JFrame frame;
00047         
00048         private Font font;
00049         
00050         private HashMap<String,JComponent> components;
00051         
00052         private String name;
00053         
00054         private int width;
00055         
00056         private int height;
00057         
00058         private CardLayout cardMngr;
00059         
00060         private ContentManager cc;
00061         
00062         public AnalysisDisplayWindow(String name, int width, int height) {
00063                 contentCount = 0;
00064                 font = new Font ("monospaced", Font.PLAIN, 12);
00065                 frame = new JFrame(name);
00066                 
00067                 setLayout(new BorderLayout());
00068                 this.name = name;
00069                 this.width = width;
00070                 this.height = height;
00071                 frame.setSize(this.width, this.height);
00072                 frame.setLocation(xMax-this.width, 0);
00073                 components = new HashMap<String,JComponent>();
00074                 cc = new ContentManager();
00075                 topBar = new JPanel();
00076                 BoxLayout tmp = new BoxLayout(topBar, BoxLayout.X_AXIS);
00077                 topBar.setLayout(tmp);
00078                 saveBtn = new JButton("Save");
00079                 topBar.add(saveBtn);
00080                 topBar.setSize(100, 100);
00081                 saveBtn.addActionListener(cc);
00082                 GridLayout tmp1 = new GridLayout(1,0);
00083                 tmp1.setHgap(10);
00084                 botBar = new JPanel(tmp1,true);
00085                 content = new JPanel(new BorderLayout());
00086                 topBar.setBorder(BorderFactory.createEmptyBorder(2,1,4,1));
00087                 botBar.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
00088                 cardMngr = new CardLayout();
00089                 content.setLayout(cardMngr);
00090                 content.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
00091                 JPanel tempPanel = new JPanel(new BorderLayout());
00092                 tempPanel.add(topBar, BorderLayout.NORTH);
00093                 tempPanel.add(botBar,BorderLayout.SOUTH);
00094                 tempPanel.add(content, BorderLayout.CENTER);
00095                 tempPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
00096                 frame.getContentPane().add(tempPanel, BorderLayout.CENTER);
00097                 
00098         }
00099         
00100         public void showWindow(){
00101                 if (components.size() == 0)
00102                         throw new RuntimeException("Can't call showWindow() unless there are Panels to show");
00103                 
00104                 if (botBar.getComponentCount() <= 1){
00105                         botBar.setVisible(false);
00106                 } else {
00107                         botBar.setVisible(true);
00108                 }
00109                 if (!frame.isVisible()) {
00110                         frame.setLocation(xMax-this.width, 0);
00111                         frame.setSize(width, height);   
00112                 }
00113                 frame.setVisible(true);
00114         }
00115         
00116         public void closeWindow(){
00117                 frame.setVisible(false);
00118         }
00119 
00128         public JTextArea addContentPanel(String cmd, String desc, boolean setTop){
00129                 // create button, add to bottom bar, and add a listener
00130                 JButton butn = new JButton(cmd);
00131                 botBar.add(butn);
00132                 butn.addActionListener(cc);
00133                 JTextArea ta = new JTextArea(25, 25); 
00134                 components.put(cmd, ta);
00135                 ta.setFont(font);
00136                 ta.setEditable(false);
00137                 cc.addCard(cmd, desc);
00138                 if (setTop){
00139                         cardMngr.show(content, desc);
00140                 }
00141                 JScrollPane jsp = new JScrollPane(ta);
00142                 content.add(desc, jsp);
00143                 contentCount++;
00144                 return ta;
00145         }
00146         
00155         public DefaultTableModel addContentTable(String cmd, String desc, boolean setTop){
00156                 DefaultTableModel data = new DefaultTableModel();
00157                 JButton butn = new JButton(cmd);
00158                 botBar.add(butn);
00159                 butn.addActionListener(cc);
00160                 cc.addCard(cmd, desc);
00161                 if (setTop){
00162                         cardMngr.show(content, desc);
00163                 }
00164                 JTable table = new JTable(data);
00165                 components.put(cmd, table);
00166                 JScrollPane jsp = new JScrollPane(table);
00167                 table.setFillsViewportHeight(true);
00168                 table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
00169                 content.add(desc, jsp);
00170                 return data;
00171         }
00172         
00173         
00174         private class ContentManager implements ActionListener {
00175                 
00176                 HashMap<String,String> btmBarMap;
00177                 
00178                 HashMap<String,String> topBarMap;
00179                 
00180                 String lastCmd;
00181                 
00182                 JPanel savePanel;
00183                 
00184                 
00185                 public ContentManager(){
00186                         savePanel = new JPanel();
00187                         savePanel.setSize(200, 100);
00188                         btmBarMap = new HashMap<String,String>();
00189                         topBarMap = new HashMap<String,String>();
00190                 }
00191                 
00192                 public void addCard(String cmd, String action){
00193                         if (lastCmd == null)
00194                                 lastCmd = cmd;
00195                         btmBarMap.put(cmd, action);
00196                 }
00197                 
00198                 public void actionPerformed (ActionEvent e) {
00199                         String cmd = e.getActionCommand();
00200                         if (cmd.equalsIgnoreCase("Save")){
00201                                 try {
00202                                         printText();
00203                                 } catch (Exception exc){
00204                                         
00205                                 }
00206                         } else if (btmBarMap.containsKey(cmd)){
00207                                 String action = btmBarMap.get(cmd);
00208                                 lastCmd = cmd;
00209                                 cardMngr.show(content, action);
00210                         } else {
00211                                 System.err.println("Illegal Action!");
00212                         }
00213                 }
00214                 
00215                 private void printText() throws Exception{
00216                         File file = null;
00217                         int option = -1;
00218                         boolean selectFile = true;
00219 
00220                         JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
00221                         while(selectFile){
00222                                 selectFile = false;
00223                                 option = fc.showSaveDialog(frame);
00224                                 if (option == JFileChooser.APPROVE_OPTION){
00225                                         file = fc.getSelectedFile();
00226                                         if (file.exists()){
00227                                                 int result = JOptionPane.showConfirmDialog(frame,
00228                                                                                 "The file " + file + " already exists.  Overwrite?", 
00229                                                                                 "File exists", JOptionPane.YES_NO_OPTION);
00230                                                 selectFile = result == JOptionPane.NO_OPTION;
00231                                         } else {
00232                                                 file.createNewFile();
00233                                                 selectFile = false;
00234                                         }
00235                                 } 
00236                         }
00237                         if (option != JFileChooser.CANCEL_OPTION){
00238                                 System.err.println("Writing "+frame.getTitle()+" - "+lastCmd+ " to " + file.getAbsolutePath());
00239 
00240                                 PrintStream out = (new PrintStream(file));
00241                                 JComponent toPrint = components.get(lastCmd);
00242                                 if (toPrint instanceof JTextArea){
00243                                         out.print(((JTextArea)toPrint).getText());
00244                                 } else if (toPrint instanceof JTable) {
00245                                         DefaultTableModel data = (DefaultTableModel) 
00246                                                                                         ((JTable) toPrint).getModel();
00247                                         int numCol = data.getColumnCount();
00248                                         int numRow = data.getRowCount();
00249                                         out.print(data.getColumnName(0));
00250                                         for (int colI = 1 ; colI < numCol; colI++){
00251                                                 out.print("\t"+data.getColumnName(colI));
00252                                         }
00253                                         out.print("\n");
00254                                         for (int rowI = 0; rowI < numRow; rowI++){
00255                                                 out.print(data.getValueAt(rowI, 0).toString());
00256                                                 for (int colI = 0 ; colI < numCol; colI++){
00257                                                         out.print("\t"+data.getValueAt(rowI, colI).
00258                                                                                                                         toString());
00259                                                 }
00260                                                 out.print("\n");
00261                                         }
00262                                 }
00263                                 out.flush();
00264                                 out.close();
00265                         }
00266                                 
00267                 
00268                 }
00269                 
00270                 public String getLastCommand(){
00271                         return lastCmd;
00272                 }
00273                 
00274         }
00275         
00276         
00277         
00278 }

Generated on Mon Aug 19 06:03:41 2013 for Mauve by doxygen 1.3.6