📄 collaborationpanel.java
字号:
/* * File: CollaborationPanel.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.p2p;import mpi.eudico.client.annotator.ElanLocale;import mpi.eudico.client.annotator.ElanLocaleListener;import java.awt.Component;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.Serializable;import java.util.EventObject;import java.util.logging.Logger;import javax.swing.AbstractCellEditor;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.SwingConstants;import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableCellEditor;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableModel;/** * A panel that show information on and show UI elements to interact with * a p2p/collaborative annotation session. * @author Han Sloetjes */public class CollaborationPanel extends JComponent implements ActionListener, TableModelListener, ElanLocaleListener { private static Logger logger = Logger.getLogger(CollaborationPanel.class.getName()); private JLabel sessionNameLabel; private JLabel chairNameLabel; private JLabel docNameLabel; private JLabel sessionValueLabel; private JLabel chairValueLabel; private JLabel docValueLabel; private JTable sessionTable; private DefaultTableModel sessionTableModel; private JButton requestControlButton; private JButton byeButton; private String controllingParticipantMail; private String localParticipantName; private String localParticipantMail; private ElanP2P elanP2P; /** * Creates a new CollaborationPanel instance. */ public CollaborationPanel() { initPanel(); populate(this); } /** * Creates a new CollaborationPanel instance. The ElanP2P object * handles the communication between this panel and other Elan objects * and the P2P server. * @param elanP2P the ElanP2P object */ public CollaborationPanel(ElanP2P elanP2P) { this.elanP2P = elanP2P; initPanel(); // populate(this); } private void initPanel() { setLayout(new GridBagLayout()); Insets insets = new Insets(3, 5, 3, 5); sessionNameLabel = new JLabel(); chairNameLabel = new JLabel(); docNameLabel = new JLabel(); sessionValueLabel = new JLabel(); chairValueLabel = new JLabel(); docValueLabel = new JLabel(); sessionTableModel = new CollaborativeTableModel(); //sessionTableModel.addTableModelListener(this); sessionTable = new JTable(sessionTableModel); JScrollPane tableScrollPane = new JScrollPane(sessionTable); requestControlButton = new JButton(); requestControlButton.setActionCommand("control"); requestControlButton.addActionListener(this); byeButton = new JButton(); byeButton.addActionListener(this); byeButton.setActionCommand("bye"); // add to gridlayout GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.NONE; gbc.insets = insets; add(sessionNameLabel, gbc); gbc.gridx = 1; add(sessionValueLabel, gbc); gbc.gridx = 0; gbc.gridy = 1; add(chairNameLabel, gbc); gbc.gridx = 1; add(chairValueLabel, gbc); gbc.gridx = 0; gbc.gridy = 2; add(docNameLabel, gbc); gbc.gridx = 1; add(docValueLabel, gbc); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; add(tableScrollPane, gbc); gbc.gridy = 4; gbc.fill = GridBagConstraints.NONE; gbc.gridwidth = 2; gbc.weightx = 0.0; gbc.weighty = 0.0; add(requestControlButton, gbc); gbc.gridy = 5; gbc.anchor = GridBagConstraints.NORTHEAST; add(byeButton, gbc); updateLocale(); sessionTable.getColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Control")).setMaxWidth(60); //RadioButtonCellEditor controlEditor = new RadioButtonCellEditor(); //sessionTable.getColumn(ElanLocale.getString("P2P.CollaborationPanel.Header.Control")).setCellEditor(controlEditor); sessionTable.getColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Control")).setCellRenderer(new RadioButtonTableCellRenderer()); } /** * Leave a collaborative annotation session. */ private void logOff() { logger.info("Signing off..."); elanP2P.leaveSession(); for (int i = sessionTableModel.getRowCount() - 1; i >= 0; i--) { sessionTableModel.removeRow(i); } final String empty = ""; setSessionName(empty); setChairName(empty); setSharedDocumentName(empty); } private void requestControl() { if (localParticipantMail == null) { return; } if (localParticipantMail.equals(controllingParticipantMail)) { return; } elanP2P.requestControl(); setControllingParticipant(localParticipantMail); logger.info("Setting controlling participant to: " + localParticipantMail); } /** * Returns the email address of the local participant. * @return the email address of the local participant */ public String getLocalParticipantMail() { return localParticipantMail; } /** * Returns the name of the local participant. * @return the name of the local participant */ public String getLocalParticipantName() { return localParticipantName; } /** * Sets the email address of the local participant. * @param localParticipantMail the email address of the local participant */ public void setLocalParticipantMail(String localParticipantMail) { this.localParticipantMail = localParticipantMail; } /** * Sets the name of the local participant. * @param localParticipantName the name of the local participant */ public void setLocalParticipantName(String localParticipantName) { this.localParticipantName = localParticipantName; } /** * Sets the name of the p2p session. * @param name the name of the session */ public void setSessionName(String name) { if (name != null) { sessionValueLabel.setText(name); } } /** * Returns the name of the current session. * @return the name of the current session */ public String getSessionName() { return sessionValueLabel.getText(); } /** * Sets the name of the chairman (m/w) of the session. * @param name the name of the chairman (m/w) */ public void setChairName(String name) { if (name != null) { chairValueLabel.setText(name); } } /** * Returns the name of the chairman (m/w) of the session, i.e. * the publisher. * @return the name of the chairman (m/w) */ public String getChairName() { return chairValueLabel.getText(); } /** * Sets the name of the shared document of the session. * @param name the name of the shared document */ public void setSharedDocumentName(String name) { if (name != null) { docValueLabel.setText(name); } } /** * Returns the name of the shared document of the session. * @return the name of the shared document */ public String getSharedDocumentName() { return docValueLabel.getText(); } /** * Adds a participant to the table.<br> * For the time being only a name and email need to be supplied. * Only the name should be unique, for now. * The new participant initially does not have the control. * Eventually a unique id should probably be passed. * @param name the name of the participant * @param email the email address */ public void addParticipant(String name, String email) { if (name == null) { logger.warning( "Illegal Argument: A participant without a name is not allowed"); throw new IllegalArgumentException( "A participant without a name is not allowed"); } if (email == null) { logger.warning( "Illegal Argument: A participant without a valid email address is not allowed"); throw new IllegalArgumentException( "A participant without a valid email address is not allowed"); } int nameColumn = sessionTableModel.findColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Participant")); int emailColumn = sessionTableModel.findColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Mail")); for (int i = 0; i < sessionTableModel.getRowCount(); i++) { String nm = (String) sessionTableModel.getValueAt(i, nameColumn); if (name.equals(nm)) { logger.warning( "Illegal Argument: A participant with this name already exists"); throw new IllegalArgumentException( "A participant with this name already exists"); } String mail = (String) sessionTableModel.getValueAt(i, emailColumn); if (email.equals(mail)) { logger.warning( "Illegal Argument: A participant with this email address already exists"); throw new IllegalArgumentException( "A participant with this email address already exists"); } } sessionTableModel.addRow(new String[] { "false", name, email }); } /** * Removes a participant from the table.<br> * @param name the name of the participant * @param email the email address of the participant */ public void removeParticipant(String email) { int column = sessionTableModel.findColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Mail")); for (int i = 0; i < sessionTableModel.getRowCount(); i++) { String n = (String) sessionTableModel.getValueAt(i, column); if (email.equals(n)) { sessionTableModel.removeRow(i); if (controllingParticipantMail.equals(email)) { controllingParticipantMail = null; // should we give one of the remaining participants control? } return; } } } /** * Sets the participant that has control. * @param mail the email address of the participant that has gained control */ public void setControllingParticipant(String mail) { int mailColumn = sessionTableModel.findColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Mail")); int controlColumn = sessionTableModel.findColumn(ElanLocale.getString( "P2P.CollaborationPanel.Header.Control"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -