Package view

Source Code of view.MessagesTable

package view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;


import edu.stanford.ejalbert.BrowserLauncher;
import edu.stanford.ejalbert.exception.BrowserLaunchingInitializingException;
import edu.stanford.ejalbert.exception.UnsupportedOperatingSystemException;


public class MessagesTable implements TableModel 
{
  public JFrame frame;
  public TableModelReceived modelReceived;
  public TableModelSend modelSend;
  public JTable tableReceived;
  public JTable tableSend;
  private AbstractAction openUrlAction;
  private JPopupMenu popupmenu;
  private String url; 
 
  public MessagesTable() {    
    JTabbedPane tabbedPane = new JTabbedPane();
    frame = new JFrame();
      String[] headerReceivedTable = { "Time", "From", "Message" };
      String[] headerSendTable = { "Time", "To", "Message" };
      frame.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );         
      modelReceived = new TableModelReceived(headerReceivedTable);
      modelSend = new TableModelSend(headerSendTable);
      tableReceived = new JTable(modelReceived);     
      tableSend = new JTable(modelSend);
      TableColumn col = tableReceived.getColumnModel().getColumn(0);
      col.setMinWidth(150); col.setMaxWidth(150);
      col = tableReceived.getColumnModel().getColumn(1);
      col.setMinWidth(110);col.setMaxWidth(110);
     
      col = tableSend.getColumnModel().getColumn(0);
      col.setMinWidth(150); col.setMaxWidth(150);
      col = tableSend.getColumnModel().getColumn(1);
      col.setMinWidth(150);col.setMaxWidth(150);
     
      tableReceived.setDefaultRenderer(Object.class, new ItalicCellRenderer());
     
      tabbedPane.add("Received messages",new JScrollPane(tableReceived));
      tabbedPane.add("Send messages",new JScrollPane(tableSend));
      frame.add(tabbedPane);
           
      frame.pack();
      //center frame
      Dimension frameSize = new Dimension(700, 300)
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int top = (screenSize.height - frameSize.height) / 2;
    int left = (screenSize.width - frameSize.width) / 2;
    frame.setSize(frameSize);
    frame.setLocation(left, top);
      frame.setVisible(false);

      tableReceived.getActionMap().put("copy", new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
            markAndCopy();
          }
      });
     
      popupmenu = new JPopupMenu();      
      popupmenu.add( new AbstractAction("copy") {
          public void actionPerformed( ActionEvent e ) {           
            markAndCopy();
          }
        } );    
           
      openUrlAction = new AbstractAction("open Url") {
          public void actionPerformed( ActionEvent e ) {           
            if((!url.startsWith("http://")) && (!url.startsWith("ftp://"))){
              url = "http://" + url;
            }
            try {
        BrowserLauncher launcher = new BrowserLauncher();
        launcher.setNewWindowPolicy(true);
        launcher.openURLinBrowser(url);       
      } catch (BrowserLaunchingInitializingException e1) {       
        e1.printStackTrace();
      } catch (UnsupportedOperatingSystemException e1) {       
        e1.printStackTrace();
      }
          }
        };     
      popupmenu.add(openUrlAction);
      tableReceived.addMouseListener(new PopupMenuMouseListener(popupmenu, tableReceived, this));     
  }
 
  public void addUrlOpener(String link){   
    openUrlAction.setEnabled(true);
    url = link; 
  }
  public void removeUrlOpener(){   
    openUrlAction.setEnabled(false);
  }
 
  public void changed(){   
    modelSend.fireTableDataChanged();
    modelReceived.fireTableDataChanged();   
  }

  @Override
  public Class<?> getColumnClass(int arg0) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public int getColumnCount() {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public String getColumnName(int arg0) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public int getRowCount() {
    // TODO Auto-generated method stub
    return 0;
  }

  @Override
  public Object getValueAt(int arg0, int arg1) {
    return null;
  }

  @Override
  public boolean isCellEditable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public void removeTableModelListener(TableModelListener arg0) {
    // TODO Auto-generated method stub
   
  }

  @Override
  public void setValueAt(Object arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub
   
  }




  @Override
  public void addTableModelListener(TableModelListener arg0) {
    // TODO Auto-generated method stub
   
  }

  private void markAndCopy() {
    int col = tableReceived.getSelectedColumn();
    int row = tableReceived.getSelectionModel().getLeadSelectionIndex();
    if (row == -1) {
        row = tableReceived.getSelectedRow();
    }
    if ((row >= 0) && (col >= 0)) {
        Object cellValue = tableReceived.getValueAt(row, col);
        if (cellValue != null) {
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(cellValue.toString()), null);
        }
    }
  }
 
  public String markAndGet() {
    Object cellValue = null;
    int col = tableReceived.getSelectedColumn();
    int row = tableReceived.getSelectionModel().getLeadSelectionIndex();
    if (row == -1) {
        row = tableReceived.getSelectedRow();
    }
    if ((row >= 0) && (col >= 0)) {
        cellValue = tableReceived.getValueAt(row, col);       
    }
    return cellValue.toString();
  }
}
TOP

Related Classes of view.MessagesTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.