package chat; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.event.*; import java.io.*; import java.net.*; import lava.net.psyc.UNI; /** * **/ public class ConferencingWindow extends JFrame { /** * **/ private static int INPUT_BUFFER_LEN = 10; /** * **/ private TextListener listener = null; /** * **/ public Object identifier = null; /** * **/ public DefaultListModel members = new DefaultListModel(); /** * **/ private JComboBox _inputCombo; /** * **/ private ComboBoxEditor _inputEditor; /** * **/ private JTextPane _view; /** * **/ private JLabel _statusLine; /** * **/ private JPanel _viewPanel, _peoplePanel, _mainPanel; /** * **/ private JSplitPane _splitPane; /** * **/ public ConferencingWindow(UserServer userServer, TextListener listener, // String context, String title, boolean showPeople) { this.listener = listener; setTitle(title); setBackground(Properties.BG_COLOR); getContentPane().setLayout(new BorderLayout()); getContentPane().setBackground(Properties.BG_COLOR); getContentPane().setForeground(Properties.TEXT_COLOR); initViewPanel(Properties.ConferencingViewSize); initStatusLine(); _mainPanel = new JPanel(); _mainPanel.setLayout(new BorderLayout()); if(showPeople) { initPeoplePanel(userServer,context,// Properties.ConferencingPeopleSize); initSplitPane(); _mainPanel.add(_splitPane,BorderLayout.CENTER); } else { _mainPanel.add(_viewPanel,BorderLayout.CENTER); } // und jetzt den MainPanel darein getContentPane().add(_mainPanel,BorderLayout.CENTER); getContentPane().add(_statusLine,BorderLayout.SOUTH); pack(); setVisible(true); installInputListeners(); _inputEditor.getEditorComponent().requestFocus(); addWindowListener(new WindowAdapter() { /** * **/ public void windowClosing(WindowEvent we) { ConferencingWindow.this.listener.closedWindow(// ConferencingWindow.this); dispose(); } } ); } /** * **/ private void initViewPanel(Dimension size) { _viewPanel = new JPanel(); _viewPanel.setLayout(new BorderLayout()); _inputCombo = new JComboBox(); _inputCombo.setEditable(true); _inputEditor = _inputCombo.getEditor(); _view = new JTextPane(); _view.setBackground(Properties.BG_COLOR); _view.setForeground(Properties.TEXT_COLOR); _view.setEditable(false); JScrollPane scroller = new JScrollPane(_view); _viewPanel.add(scroller,BorderLayout.CENTER); _viewPanel.add(_inputCombo,BorderLayout.SOUTH); _viewPanel.setPreferredSize(size); // jetzt noch Style festlegen Style def = _view.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setFontFamily(def,"Helvetica"); StyleConstants.setFontSize(def,14); StyleConstants.setForeground(def,Properties.TEXT_COLOR); } /** * **/ private void initStatusLine() { _statusLine = new JLabel(); _statusLine.setForeground(Properties.STATUS_COLOR); _statusLine.setBackground(Properties.BG_COLOR); } /** * **/ private void initPeoplePanel(UserServer userServer, String context, // Dimension size) { _peoplePanel = new PeoplePanel(members,size,userServer,context); } /** * **/ private void installInputListeners() { // ein actionlistener auf das InputFeld _inputEditor.getEditorComponent().addKeyListener(new KeyAdapter() { /** * **/ // keyTyped doesn't work!! public void keyReleased(KeyEvent ke) { // return pressed ? if(ke.getKeyCode() == KeyEvent.VK_ENTER) { String input = (String)_inputEditor.getItem(); listener.gotText(ConferencingWindow.this,input); _inputCombo.insertItemAt(input,0); _inputEditor.setItem(""); // remove from end of history int size = _inputCombo.getItemCount(); if(size > INPUT_BUFFER_LEN) _inputCombo.removeItemAt(size - 1); } } } ); // ein itemlistener auf die liste _inputCombo.addItemListener(new ItemListener() { /** * **/ public void itemStateChanged(ItemEvent ie) { int index = _inputCombo.getSelectedIndex(); // -1 -> Input-Feld if(index != -1) _inputEditor.setItem(_inputCombo.getSelectedItem()); _inputEditor.getEditorComponent().requestFocus(); } } ); } /** * **/ private void initSplitPane() { _splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,_viewPanel,_peoplePanel); _splitPane.addComponentListener(new ComponentAdapter() { /** * **/ private int lastWidth = 0; /** * **/ public void componentResized(ComponentEvent ce) { Dimension d = _splitPane.getSize(); if(lastWidth != d.width) _splitPane.setDividerLocation(0.8); lastWidth = d.width; } } ); } /** * clear it, by the lynx **/ public void clear() { _view.setText(""); _statusLine.setText(""); } /** * **/ public void showText(String text) { //_view.setText(_view.getText()+text+"\n"); try { _view.getStyledDocument().insertString(// _view.getStyledDocument().getLength(),text + "\n",null); } catch(BadLocationException e) { System.out.println("whoups: " + e); } } /** * **/ public void showStatus(String text) { _statusLine.setText(text); } /** * **/ public static void main(String[] args) { ConferencingWindow c = new ConferencingWindow(// new UserServer() { /** * **/ public void friendSelected(String context, UNI user, int clickCount) { } } ,new TextListener() { /** * **/ public void closedWindow(ConferencingWindow window) { } /** * **/ public void gotText(ConferencingWindow window, String text) { window.showText(text); } } ,null,"Psyc-Chat V0.42",true); c.addWindowListener(new WindowAdapter() { /** * **/ public void windowClosing(WindowEvent we) { System.exit(0); } } ); } }