123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.swing;
-
- import com.dmdirc.Channel;
- import com.dmdirc.Main;
- import com.dmdirc.commandparser.commands.ChannelCommand;
- import com.dmdirc.commandparser.parsers.ChannelCommandParser;
- import com.dmdirc.commandparser.commands.Command;
- import com.dmdirc.commandparser.parsers.CommandParser;
- import com.dmdirc.commandparser.commands.ServerCommand;
- import com.dmdirc.parser.ChannelClientInfo;
- import com.dmdirc.ui.input.InputHandler;
- import com.dmdirc.ui.interfaces.ChannelWindow;
- import com.dmdirc.ui.swing.components.InputFrame;
- import com.dmdirc.ui.swing.dialogs.channelsetting.ChannelSettingsDialog;
- import static com.dmdirc.ui.swing.UIUtilities.SMALL_BORDER;
-
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.util.List;
-
- import javax.swing.JList;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JSplitPane;
- import javax.swing.ListSelectionModel;
- import javax.swing.SwingUtilities;
- import javax.swing.plaf.basic.BasicSplitPaneDivider;
- import javax.swing.plaf.basic.BasicSplitPaneUI;
-
- /**
- * The channel frame is the GUI component that represents a channel to the user.
- */
- public final class ChannelFrame extends InputFrame implements MouseListener,
- ActionListener, ChannelWindow {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 10;
-
- /** The nick list model used for this channel's nickname list. */
- private NicklistListModel nicklistModel;
-
- /** This channel's command parser. */
- private final ChannelCommandParser commandParser;
-
- /** Nick list. */
- private JList nickList;
-
- /** split pane. */
- private JSplitPane splitPane;
-
- /** popup menu item. */
- private JMenuItem settingsMI;
-
- /** The channel object that owns this frame. */
- private final Channel parent;
-
- /**
- * Creates a new instance of ChannelFrame. Sets up callbacks and handlers,
- * and default options for the form.
- * @param owner The Channel object that owns this frame
- */
- public ChannelFrame(final Channel owner) {
- super(owner);
-
- parent = owner;
-
- initComponents();
-
- nickList.setBackground(getConfigManager().getOptionColour("ui", "nicklistbackgroundcolour",
- getConfigManager().getOptionColour("ui", "backgroundcolour", Color.WHITE)));
- nickList.setForeground(getConfigManager().getOptionColour("ui", "nicklistforegroundcolour",
- getConfigManager().getOptionColour("ui", "foregroundcolour", Color.BLACK)));
-
- getConfigManager().addChangeListener("ui", "nicklistforegroundcolour", this);
- getConfigManager().addChangeListener("ui", "nicklistbackgroundcolour", this);
- getConfigManager().addChangeListener("nicklist", "altBackgroundColour", this);
-
- commandParser = new ChannelCommandParser(((Channel) getContainer()).
- getServer(), (Channel) getContainer());
-
- setInputHandler(new InputHandler(getInputField(), commandParser, this));
- }
-
- /**
- * Retrieves the command Parser for this command window.
- * @return This window's command Parser
- */
- public CommandParser getCommandParser() {
- return commandParser;
- }
-
- /** {@inheritDoc} */
- public void updateNames(final List<ChannelClientInfo> clients) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- nicklistModel.replace(clients);
- }
- });
- }
-
- /** {@inheritDoc} */
- public void updateNames() {
- nicklistModel.sort();
- }
-
- /** {@inheritDoc} */
- public void addName(final ChannelClientInfo client) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- nicklistModel.add(client);
- }
- });
- }
-
- /** {@inheritDoc} */
- public void removeName(final ChannelClientInfo client) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- nicklistModel.remove(client);
- }
- });
- }
-
- /**
- * Retrieves this channel frame's nicklist component.
- * @return This channel's nicklist
- */
- public JList getNickList() {
- return nickList;
- }
-
- /**
- * Initialises the compoents in this frame.
- */
- private void initComponents() {
- settingsMI = new JMenuItem("Settings");
- settingsMI.addActionListener(this);
- getPopup().addSeparator();
- getPopup().add(settingsMI);
- final JPanel panel = new JPanel(new BorderLayout());
-
- splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
-
- final JScrollPane nickScrollPane = new JScrollPane();
- nickList = new JList();
- nickList.setCellRenderer(new NicklistRenderer(parent.getConfigManager()));
- nickList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
-
- nickList.addMouseListener(this);
-
- splitPane.setBorder(null);
- final BasicSplitPaneDivider divider =
- ((BasicSplitPaneUI) splitPane.getUI()).getDivider();
- if (divider != null) {
- divider.setBorder(null);
- }
-
- nicklistModel = new NicklistListModel();
-
- nickList.setModel(nicklistModel);
- nickScrollPane.setViewportView(nickList);
-
- nickScrollPane.setMinimumSize(new Dimension(150, 10));
- getTextPane().setPreferredSize(new Dimension(((MainFrame) Main.getUI().
- getMainWindow()).getWidth(), 10));
-
- panel.add(getSearchBar(), BorderLayout.PAGE_START);
- panel.add(inputPanel, BorderLayout.PAGE_END);
-
- getContentPane().setLayout(new BorderLayout(SMALL_BORDER, SMALL_BORDER));
-
- getContentPane().add(splitPane, BorderLayout.CENTER);
- getContentPane().add(panel, BorderLayout.PAGE_END);
-
- splitPane.setLeftComponent(getTextPane());
- splitPane.setRightComponent(nickScrollPane);
-
- splitPane.setDividerLocation((double) 1);
- splitPane.setResizeWeight(1);
- splitPane.setDividerSize(SMALL_BORDER);
- splitPane.setContinuousLayout(true);
-
- pack();
- }
-
- /**
- * {@inheritDoc}.
- */
- public void actionPerformed(final ActionEvent actionEvent) {
- super.actionPerformed(actionEvent);
- if (commands.containsKey(actionEvent.getActionCommand())) {
- final Command command = commands.get(actionEvent.getActionCommand());
- for (Object nickname : nickList.getSelectedValues()) {
- if (command instanceof ChannelCommand) {
- ((ChannelCommand) commands.get(actionEvent.getActionCommand())).
- execute(this, getContainer().getServer(),
- (Channel) this.getContainer(), false,
- ((ChannelClientInfo) nickname).getNickname());
- } else if (command instanceof ServerCommand) {
- ((ServerCommand) commands.get(actionEvent.getActionCommand())).
- execute(this, getContainer().getServer(), false,
- ((ChannelClientInfo) nickname).getNickname());
- }
- }
- }
- if (actionEvent.getSource() == settingsMI) {
- ChannelSettingsDialog.getChannelSettingDialog((Channel) getContainer()).setVisible(true);
- }
- }
-
- /**
- * Returns the splitpane.
- * @return nicklist JSplitPane
- */
- public JSplitPane getSplitPane() {
- return splitPane;
- }
-
- /**
- * Checks for url's, channels and nicknames. {@inheritDoc}
- */
- public void mouseClicked(final MouseEvent mouseEvent) {
- processMouseEvent(mouseEvent);
- super.mouseClicked(mouseEvent);
- }
-
- /**
- * Not needed for this class. {@inheritDoc}
- */
- public void mousePressed(final MouseEvent mouseEvent) {
- processMouseEvent(mouseEvent);
- super.mousePressed(mouseEvent);
- }
-
- /**
- * Not needed for this class. {@inheritDoc}
- */
- public void mouseReleased(final MouseEvent mouseEvent) {
- processMouseEvent(mouseEvent);
- super.mouseReleased(mouseEvent);
- }
-
- /**
- * Processes every mouse button event to check for a popup trigger.
- *
- * @param e mouse event
- */
- public void processMouseEvent(final MouseEvent e) {
- if (e.getSource() == nickList && nickList.getMousePosition() != null) {
- boolean showMenu = false;
- showMenu = checkShowNicklistMenu();
- if (!showMenu) {
- showMenu = selectNickUnderCursor();
- }
- if (showMenu) {
- if (e.isPopupTrigger()) {
- final Point point = getMousePosition();
- popuplateNicklistPopup();
- nicklistPopup.show(this, (int) point.getX(), (int) point.getY());
- }
- } else {
- nickList.clearSelection();
- }
- }
- super.processMouseEvent(e);
- }
-
- /**
- *
- * Checks whether to show the nicklist menu.
- *
- * @return whether to show the nicklist menu
- */
- private boolean checkShowNicklistMenu() {
- boolean showMenu = false;
- for (int i = 0; i < nickList.getModel().getSize(); i++) {
- if (nickList.getCellBounds(i, i) != null
- && nickList.getMousePosition() != null
- && nickList.getCellBounds(i, i).contains(nickList.getMousePosition())
- && nickList.isSelectedIndex(i)) {
- showMenu = true;
- break;
- }
- }
- return showMenu;
- }
-
- /**
- * Selects the nick underneath the mouse.
- *
- * @return true if an item was selected
- */
- private boolean selectNickUnderCursor() {
- boolean suceeded = false;
- if (nickList.getMousePosition() != null) {
- for (int i = 0; i < nickList.getModel().getSize(); i++) {
- if (nickList.getCellBounds(i, i) != null
- && nickList.getMousePosition() != null
- && nickList.getCellBounds(i, i).contains(nickList.getMousePosition())) {
- nickList.setSelectedIndex(i);
- suceeded = true;
- break;
- }
- }
- }
- return suceeded;
- }
-
- /** {@inheritDoc} */
- @Override
- public void configChanged(final String domain, final String key,
- final String oldValue, final String newValue) {
- super.configChanged(domain, key, oldValue, newValue);
-
- if ("ui".equals(domain)) {
- if ("nicklistbackgroundcolour".equals(key) || "backgroundcolour".equals(key)) {
- nickList.setBackground(getConfigManager().getOptionColour("ui", "nicklistbackgroundcolour",
- getConfigManager().getOptionColour("ui", "backgroundcolour", Color.WHITE)));
- } else if ("nicklistforegroundcolour".equals(key) || "foregroundcolour".equals(key)) {
- nickList.setForeground(getConfigManager().getOptionColour("ui", "nicklistforegroundcolour",
- getConfigManager().getOptionColour("ui", "foregroundcolour", Color.BLACK)));
- }
- } else if ("nicklist".equals(domain) && "altBackgroundColour".equals(key)) {
- nickList.repaint();
- }
- }
- }
|