diff --git a/src/urChatBasic/base/IRCChannelBase.java b/src/urChatBasic/base/IRCChannelBase.java index de0dc9c..9518711 100644 --- a/src/urChatBasic/base/IRCChannelBase.java +++ b/src/urChatBasic/base/IRCChannelBase.java @@ -14,6 +14,7 @@ import urChatBasic.frontend.LineFormatter.ClickableText; import urChatBasic.frontend.components.FontPanel; import urChatBasic.frontend.dialogs.FontDialog; +import urChatBasic.frontend.panels.ConnectionPanel; import urChatBasic.frontend.panels.InterfacePanel; import urChatBasic.frontend.utils.URColour; import urChatBasic.frontend.UserGUI; @@ -108,14 +109,14 @@ public class IRCChannelBase extends JPanel // getters & setters - public JTextField getUserTextBox() + public JTextField getUserTextBox () { return clientTextBox; } - public IRCServerBase getServer() + public IRCServerBase getServer () { - if(this instanceof IRCServer) + if (this instanceof IRCServer) { return (IRCServerBase) this; } @@ -124,55 +125,55 @@ public IRCServerBase getServer() } @Override - public String getName() + public String getName () { return this.channelName; } @Override - public void setName(String newName) + public void setName (String newName) { channelName = newName; } - public void hideEventTicker() + public void hideEventTicker () { eventTickerShown = false; tickerPanel.setVisible(false); bottomPanel.setPreferredSize(clientTextBox.getPreferredSize()); } - public void hideUsersList() + public void hideUsersList () { usersListShown = false; toggleUsersList(usersListShown); } - public void showUsersList() + public void showUsersList () { usersListShown = true; toggleUsersList(usersListShown); } - protected IRCChannelBase(String channelName) + protected IRCChannelBase (String channelName) { this.channelName = channelName; initChannel(); } - protected IRCChannelBase(IRCServerBase server, String channelName) + protected IRCChannelBase (IRCServerBase server, String channelName) { this.channelName = channelName; setServer(server); initChannel(); } - public void setServer(IRCServerBase server) + public void setServer (IRCServerBase server) { this.server = server; } - private void initChannel() + private void initChannel () { channelTextArea.setEditable(false); @@ -181,21 +182,21 @@ private void initChannel() String nodeName = getServer().getName() != null ? getServer().getName() : channelName; markerName = getServer().getName() != null ? getServer().getName() + "-" + channelName : channelName; - if(nodeName.equals(channelName)) + if (nodeName.equals(channelName)) setSettingsPath(URProfilesUtil.getActiveFavouritesPath().node(nodeName)); else setSettingsPath(URProfilesUtil.getActiveFavouritesPath().node(nodeName).node(channelName)); fontDialog = new FontDialog(channelName, gui.getStyle(), channelPrefs); - lineFormatter = new LineFormatter(getFontPanel().getStyle(), channelTextArea , channelScroll, getServer(), channelPrefs); + lineFormatter = new LineFormatter(getFontPanel().getStyle(), channelTextArea, channelScroll, getServer(), channelPrefs); } else { markerName = channelName; setSettingsPath(URProfilesUtil.getActiveFavouritesPath().node(channelName)); fontDialog = new FontDialog(channelName, gui.getStyle(), channelPrefs); - lineFormatter = new LineFormatter(getFontPanel().getStyle() , channelTextArea, channelScroll, null, channelPrefs); + lineFormatter = new LineFormatter(getFontPanel().getStyle(), channelTextArea, channelScroll, null, channelPrefs); } // Add Logging Marker @@ -221,7 +222,7 @@ private void initChannel() myActions = new IRCActions(this); - if(((InterfacePanel) gui.interfacePanel).isLoadChannelLogsEnabled()) + if (((InterfacePanel) gui.interfacePanel).isLoadChannelLogsEnabled()) loadChannelHistory(); } @@ -234,7 +235,7 @@ public void actionPerformed (ActionEvent arg0) { String nodeName = getServer().getName() != null ? getServer().getName() : channelName; - if(nodeName.equals(channelName)) + if (nodeName.equals(channelName)) setSettingsPath(URProfilesUtil.getActiveFavouritesPath().node(nodeName)); else setSettingsPath(URProfilesUtil.getActiveFavouritesPath().node(nodeName).node(channelName)); @@ -250,7 +251,7 @@ public void rejoin () public void setSettingsPath (Preferences settingsPath) { channelPrefs = settingsPath; - if(getFontPanel() != null) + if (getFontPanel() != null) { getFontPanel().setSettingsPath(settingsPath); setFont(getFontPanel().getFont()); @@ -262,12 +263,12 @@ public Preferences getSettingsPath () return channelPrefs; } - public void createChannelPopUp() + public void createChannelPopUp () { myMenu = new ChannelPopUp(); } - protected void setupMainPanel() + protected void setupMainPanel () { mainPanel.setLayout(new BorderLayout()); setupMainTextArea(); @@ -280,8 +281,7 @@ protected void setupMainPanel() mainResizer.setOneTouchExpandable(true); // This should be set to where the minimum size of the userScroller would end up - mainResizer.setDividerLocation( - gui.getWidth() - (userScroller.getPreferredSize().width + mainResizer.getDividerSize())); + mainResizer.setDividerLocation(gui.getWidth() - (userScroller.getPreferredSize().width + mainResizer.getDividerSize())); // Left most panel (channelScroll pane), gets the extra space when resizing the window mainResizer.setResizeWeight(1); @@ -294,15 +294,14 @@ protected void setupMainPanel() mainPanel.add(bottomPanel, BorderLayout.SOUTH); } - public FontPanel getFontPanel() + public FontPanel getFontPanel () { return fontDialog != null && fontDialog.getFontPanel() != null ? fontDialog.getFontPanel() : null; } - protected void setupMainTextArea() + protected void setupMainTextArea () { - channelScroll.setPreferredSize( - new Dimension(Constants.MAIN_WIDTH - usersListWidth, Constants.MAIN_HEIGHT - BOTTOM_HEIGHT)); + channelScroll.setPreferredSize(new Dimension(Constants.MAIN_WIDTH - usersListWidth, Constants.MAIN_HEIGHT - BOTTOM_HEIGHT)); channelScroll.setLocation(0, 0); channelScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); channelTextArea.addMouseListener(new ChannelClickListener()); @@ -317,7 +316,7 @@ protected void setupMainTextArea() // resetLineFormatter(); } - protected void setupUsersList() + protected void setupUsersList () { usersList.setFont(getFontPanel().getFont()); usersList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); @@ -329,7 +328,7 @@ protected void setupUsersList() } - protected void setupBottomPanel() + protected void setupBottomPanel () { setupTickerPanel(); @@ -347,7 +346,7 @@ protected void setupBottomPanel() clientTextBox.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET); } - private void setupTickerPanel() + private void setupTickerPanel () { tickerPanel.setFont(getFontPanel().getFont()); tickerPanel.setPreferredSize(labelSize); @@ -365,7 +364,7 @@ public class IRCAlert extends JLabel private static final long serialVersionUID = 1L; AlertType type; - IRCAlert(AlertType type) + IRCAlert (AlertType type) { this.type = type; } @@ -374,7 +373,7 @@ public class IRCAlert extends JLabel public class ChannelClickListener extends MouseInputAdapter { - public void mouseClicked(MouseEvent e) + public void mouseClicked (MouseEvent e) { StyledDocument doc = (StyledDocument) channelTextArea.getDocument(); Element ele = doc.getCharacterElement(channelTextArea.viewToModel2D((e.getPoint()))); @@ -395,7 +394,7 @@ public void mouseClicked(MouseEvent e) class ChannelMovementListener extends MouseAdapter { - public void mouseMoved(MouseEvent e) + public void mouseMoved (MouseEvent e) { StyledDocument doc = (StyledDocument) channelTextArea.getDocument(); Element wordElement = doc.getCharacterElement(channelTextArea.viewToModel2D((e.getPoint()))); @@ -411,7 +410,7 @@ public void mouseMoved(MouseEvent e) } } - public void createEvent(String eventText) + public void createEvent (String eventText) { if (((InterfacePanel) gui.interfacePanel).isJoinsQuitsMainEnabled() && !(this instanceof IRCPrivate)) printText(eventText, Constants.EVENT_USER); @@ -423,14 +422,13 @@ public void createEvent(String eventText) int tempX; if (!(eventLabels.isEmpty())) { - if (eventLabels.get(eventLabels.size() - 1).getPreferredSize().width - + eventLabels.get(eventLabels.size() - 1).getX() + EVENT_BUFFER < super.getWidth()) + if (eventLabels.get(eventLabels.size() - 1).getPreferredSize().width + eventLabels.get(eventLabels.size() - 1).getX() + + EVENT_BUFFER < super.getWidth()) { tempX = super.getWidth() + EVENT_BUFFER; } else { - tempX = eventLabels.get(eventLabels.size() - 1).getPreferredSize().width - + eventLabels.get(eventLabels.size() - 1).getX() + EVENT_BUFFER; + tempX = eventLabels.get(eventLabels.size() - 1).getPreferredSize().width + eventLabels.get(eventLabels.size() - 1).getX() + EVENT_BUFFER; } } else { @@ -454,7 +452,7 @@ public void createEvent(String eventText) } } - public void callForAttention() + public void callForAttention () { myActions.callForAttention(); } @@ -464,27 +462,32 @@ public String getMarker () return markerName; } - class Message { + class Message + { private Optional date = Optional.empty(); private String line; private String fromUser; - public Message(String line, String fromUser) { + public Message (String line, String fromUser) + { this.line = line; this.fromUser = fromUser; } - public Message(Date date, String line, String fromUser) { + public Message (Date date, String line, String fromUser) + { this.date = Optional.of(date); this.line = line; this.fromUser = fromUser; } - public String getLine () { + public String getLine () + { return line; } - public String getUser () { + public String getUser () + { return fromUser; } @@ -496,39 +499,43 @@ public Optional getDate () // TODO: Change this to accept IRCUser instead // TODO: Overload method with date object - public void printText(String line, String fromUser) + public void printText (String line, String fromUser) { - try { + try + { messageQueue.put(new Message(line, fromUser)); - if(!messageQueueInProgress) + if (!messageQueueInProgress) handleMessageQueue(); - } catch (InterruptedException e) { + } catch (InterruptedException e) + { Constants.LOGGER.warn(e.getLocalizedMessage(), e); } } - public void printText(Date messageDate, String message, String fromUser) + public void printText (Date messageDate, String message, String fromUser) { - try { + try + { messageQueue.put(new Message(messageDate, message, fromUser)); - if(!messageQueueInProgress) + if (!messageQueueInProgress) handleMessageQueue(); - } catch (InterruptedException e) { + } catch (InterruptedException e) + { Constants.LOGGER.warn(e.getLocalizedMessage(), e); } } - public boolean messageQueueFull() + public boolean messageQueueFull () { return (messageQueue.remainingCapacity() == 0); } - public boolean messageQueueWorking() + public boolean messageQueueWorking () { return (!messageQueue.isEmpty() || messageQueueInProgress); } @@ -642,8 +649,8 @@ public void run () } /** - * First checks to make sure the user hasn't set it manually for this channel. usersListShown is - * only set by the pop up menu, so unless you've changed it, it won't care about the global setting + * First checks to make sure the user hasn't set it manually for this channel. usersListShown is only set by the pop up menu, so unless you've changed it, + * it won't care about the global setting * * @param showIt */ @@ -653,16 +660,15 @@ public void toggleUsersList (boolean showIt) { // userScroller.setVisible(showIt); if (showIt) - mainResizer.setDividerLocation( - gui.getWidth() - (userScroller.getPreferredSize().width + mainResizer.getDividerSize())); + mainResizer.setDividerLocation(gui.getWidth() - (userScroller.getPreferredSize().width + mainResizer.getDividerSize())); else mainResizer.setDividerLocation(gui.getWidth()); } } /** - * First checks to make sure the user hasn't set it manually for this channel. eventTickerShown is - * only set by the pop up menu, so unless you've changed it, it won't care about the global setting + * First checks to make sure the user hasn't set it manually for this channel. eventTickerShown is only set by the pop up menu, so unless you've changed it, + * it won't care about the global setting * * @param showIt */ @@ -684,19 +690,20 @@ public void toggleEventTicker (boolean showIt) * @param channelName * @return IRCChannel */ - public IRCUser getCreatedUser(String userName) { - synchronized(usersMap) + public IRCUser getCreatedUser (String userName) + { + synchronized (usersMap) { return usersMap.get(userName.toLowerCase()); } } - public void disableFocus() + public void disableFocus () { channelTextArea.setFocusable(false); } - public void enableFocus() + public void enableFocus () { channelTextArea.setFocusable(true); } @@ -704,19 +711,24 @@ public void enableFocus() // Adds users to the list in the users array[] public void addToUsersList (final String[] users) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - synchronized(usersMap) + SwingUtilities.invokeLater(new Runnable() + { + public void run () + { + synchronized (usersMap) { - if (users.length >= 0 && null != getServer()) { - for (int x = 0; x < users.length; x++) { + if (users.length >= 0 && null != getServer()) + { + for (int x = 0; x < users.length; x++) + { String tempUserName = users[x]; if (users[x].startsWith(":")) tempUserName = tempUserName.substring(1); IRCUser newUser = getServer().getIRCUser(tempUserName); - if (null != newUser) { + if (null != newUser) + { usersMap.put(newUser.getName().toLowerCase(), newUser); if (users.length == 1) createEvent("++ " + newUser + " has joined."); @@ -732,7 +744,7 @@ public void run() { public void addToUsersList (final IRCUser newUser) { - synchronized(usersMap) + synchronized (usersMap) { usersMap.put(newUser.getName().toLowerCase(), newUser); createEvent("++ " + newUser + " has joined."); @@ -743,12 +755,12 @@ public void addToUsersList (final IRCUser newUser) } // Adds a single user, good for when a user joins the channel - public void addToUsersList(final String user) + public void addToUsersList (final String user) { - addToUsersList(new String[]{user}); + addToUsersList(new String[] {user}); } - public String getChannelTopic(String channelName) + public String getChannelTopic (String channelName) { return getChannelTopic(); } @@ -758,11 +770,11 @@ public String getChannelTopic(String channelName) * * @param user **/ - public void removeFromUsersList(final String user) + public void removeFromUsersList (final String user) { SwingUtilities.invokeLater(new Runnable() { - public void run() + public void run () { String thisUser = user; if (user.startsWith(":")) @@ -770,7 +782,7 @@ public void run() usersMap.remove(thisUser.toLowerCase()); - if(usersListModel.hasUser(user)) + if (usersListModel.hasUser(user)) { usersListModel.removeUser(thisUser); createEvent("-- " + thisUser + " has quit."); @@ -781,29 +793,29 @@ public void run() } /** Clear the users list */ - public void clearUsersList() + public void clearUsersList () { usersMap.clear(); } - public String getChannelTopic() + public String getChannelTopic () { return channelTopic; } - public void setChannelTopic(String channelTopic) + public void setChannelTopic (String channelTopic) { this.channelTopic = channelTopic; this.createEvent(channelTopic); } /** Rename user by removing old name and inserting new name. */ - public void renameUser(final String oldUserName, final String newUserName) + public void renameUser (final String oldUserName, final String newUserName) { SwingUtilities.invokeLater(new Runnable() { - public void run() + public void run () { IRCUser tempUser = getCreatedUser(oldUserName); if (tempUser != null) @@ -823,7 +835,7 @@ class WrapEditorKit extends StyledEditorKit private static final long serialVersionUID = 980393121518733188L; ViewFactory defaultFactory = new WrapColumnFactory(); - public ViewFactory getViewFactory() + public ViewFactory getViewFactory () { return defaultFactory; } @@ -832,7 +844,7 @@ public ViewFactory getViewFactory() class WrapColumnFactory implements ViewFactory { - public View create(Element elem) + public View create (Element elem) { String kind = elem.getName(); @@ -850,12 +862,12 @@ public View create(Element elem) class WrapLabelView extends LabelView { - public WrapLabelView(Element elem) + public WrapLabelView (Element elem) { super(elem); } - public float getMinimumSpan(int axis) + public float getMinimumSpan (int axis) { return switch (axis) { @@ -881,7 +893,7 @@ public class ChannelPopUp extends JPopupMenu public JMenuItem addAsFavouriteItem; JMenuItem chooseFont; - public ChannelPopUp() + public ChannelPopUp () { nameItem = new JMenuItem(IRCChannelBase.this.getName()); add(nameItem); @@ -913,16 +925,15 @@ public ChannelPopUp() } @Override - public void show(Component arg0, int arg1, int arg2) + public void show (Component arg0, int arg1, int arg2) { - // TODO: Favourites handling to be done elsewhere - // if (gui.isFavourite(IRCChannelBase.this)) - // { - // ((ChannelPopUp) IRCChannelBase.this.myMenu).addAsFavouriteItem.setText("Remove as Favourite"); - // } else - // { - // ((ChannelPopUp) IRCChannelBase.this.myMenu).addAsFavouriteItem.setText("Add as Favourite"); - // } + if ((gui.getConnectionPanel() instanceof ConnectionPanel connectionPanel) && connectionPanel.isFavourite(IRCChannelBase.this)) + { + ((ChannelPopUp) IRCChannelBase.this.myMenu).addAsFavouriteItem.setText("Remove as Favourite"); + } else + { + ((ChannelPopUp) IRCChannelBase.this.myMenu).addAsFavouriteItem.setText("Add as Favourite"); + } super.show(arg0, arg1, arg2); } @@ -932,26 +943,25 @@ private class AddAsFavourite implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { - // TODO: Favourites handling to be done elsewhere - // if (null != getServer()) - // { - // if (!gui.isFavourite(IRCChannelBase.this)) - // { - // gui.addFavourite(getServer().getName(), getName()); - // } else - // { - // gui.removeFavourite(getServer().getName(), getName()); - // } - // } + if (null != getServer() && (gui.getConnectionPanel() instanceof ConnectionPanel connectionPanel)) + { + if (!connectionPanel.isFavourite(IRCChannelBase.this)) + { + connectionPanel.addFavourite(getServer().getName(), getName()); + } else + { + connectionPanel.removeFavourite(getServer().getName(), getName()); + } + } } } private class LoadChannelHistory implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { loadChannelHistory(); } @@ -959,19 +969,21 @@ public void actionPerformed(ActionEvent arg0) private void loadChannelHistory () { - Thread fileReadingThread = new Thread(() -> { + Thread fileReadingThread = new Thread( () -> { String logFilePath = URLogger.getLogFilePath(getMarker()); String line = ""; - try (BufferedReader br = new BufferedReader (new InputStreamReader (new ReverseLineInputStream(new File(logFilePath))))) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(new ReverseLineInputStream(new File(logFilePath))))) + { int maxCount = ((InterfacePanel) gui.interfacePanel).getLimitChannelLinesCount(); Constants.LOGGER.info("Loading channel history, max line count [" + maxCount + "]"); int loadCount = 0; - while ((line = br.readLine()) != null && loadCount < maxCount) { - if(messageQueueFull()) + while ((line = br.readLine()) != null && loadCount < maxCount) + { + if (messageQueueFull()) Thread.sleep(10); Map parsedLine = parseLogLineFull(line); - if(parsedLine.size() != 0) + if (parsedLine.size() != 0) { printText((Date) parsedLine.get("DATE"), parsedLine.get("MESSAGE").toString(), parsedLine.get("USER").toString()); loadCount++; @@ -980,7 +992,8 @@ private void loadChannelHistory () } catch (NullPointerException npe) { Constants.LOGGER.info("Log File doesn't yet exist."); - } catch (Exception e) { + } catch (Exception e) + { e.printStackTrace(); } }); @@ -990,7 +1003,7 @@ private void loadChannelHistory () private class QuitItem implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { if (null != getServer()) { @@ -1008,7 +1021,7 @@ public void actionPerformed(ActionEvent arg0) private class ToggleHideTickerListItem implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { eventTickerShown = !tickerPanel.isVisible(); toggleEventTicker(!tickerPanel.isVisible()); @@ -1024,11 +1037,10 @@ public void actionPerformed(ActionEvent arg0) private class ToggleHideUsersListItem implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { - if (mainResizer.getDividerLocation() <= gui.getWidth() - - (userScroller.getPreferredSize().width + mainResizer.getDividerSize())) + if (mainResizer.getDividerLocation() <= gui.getWidth() - (userScroller.getPreferredSize().width + mainResizer.getDividerSize())) { usersListShown = false; } else @@ -1043,7 +1055,7 @@ private class ChooseFont implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { fontDialog.setVisible(true); } @@ -1052,7 +1064,7 @@ public void actionPerformed(ActionEvent arg0) private class SendTextListener implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { if (!getUserTextBox().getText().trim().isEmpty()) { @@ -1064,7 +1076,7 @@ public void actionPerformed(ActionEvent arg0) } } - public void sendClientText(String line, String source) + public void sendClientText (String line, String source) { if (null != getServer() && getServer().isConnected()) { @@ -1072,25 +1084,25 @@ public void sendClientText(String line, String source) } } - public JTextPane getChannelTextPane() + public JTextPane getChannelTextPane () { return channelTextArea; } - public LineFormatter getLineFormatter() + public LineFormatter getLineFormatter () { return lineFormatter; } @Override - public void setFont(Font f) + public void setFont (Font f) { if (fontDialog != null) { super.setFont(fontDialog.getFontPanel().getFont()); SwingUtilities.invokeLater(new Runnable() { - public void run() + public void run () { // lineFormatter.setFont(fontDialog.getFontPanel().getFont()); lineFormatter.setStyle(fontDialog.getFontPanel().getStyle()); @@ -1105,7 +1117,7 @@ public void run() private class SaveFontListener implements ActionListener { @Override - public void actionPerformed(ActionEvent arg0) + public void actionPerformed (ActionEvent arg0) { // fontDialog.saveFont(fontDialog.getFont()); fontDialog.getFontPanel().setFont(fontDialog.getFontPanel().getStyle(), true); @@ -1124,21 +1136,20 @@ public void closeChannel () repaint(); } - public boolean userIsTyping() + public boolean userIsTyping () { return !clientTextBox.getText().isEmpty(); } /** - * Tab Auto Complete between where the Caret is positioned and the index of the last space. - * Therefore it must be the last word of the sentence. + * Tab Auto Complete between where the Caret is positioned and the index of the last space. Therefore it must be the last word of the sentence. * * @author Matt * */ private class ChannelKeyListener implements KeyListener { - public void keyPressed(KeyEvent e) + public void keyPressed (KeyEvent e) { // When the user presses tab if (e.getKeyCode() == KeyEvent.VK_TAB) @@ -1152,10 +1163,8 @@ public void keyPressed(KeyEvent e) // get text between last space and caretPosition for (String word : clientTextBox.getText().split(" ")) { - if (startingCharacters == null - || (clientTextBox.getText().indexOf(word) < clientTextBox.getCaretPosition() - && clientTextBox.getText().indexOf(word) > clientTextBox.getText() - .indexOf(startingCharacters))) + if (startingCharacters == null || (clientTextBox.getText().indexOf(word) < clientTextBox.getCaretPosition() + && clientTextBox.getText().indexOf(word) > clientTextBox.getText().indexOf(startingCharacters))) { startingCharacters = word; } @@ -1166,16 +1175,14 @@ public void keyPressed(KeyEvent e) // ")+1, clientTextBox.getCaretPosition()); } else { - startingCharacters = - clientTextBox.getText().toLowerCase().substring(0, clientTextBox.getCaretPosition()); + startingCharacters = clientTextBox.getText().toLowerCase().substring(0, clientTextBox.getCaretPosition()); } } // If usersArray and clientText isn't empty. - if (!usersMap.isEmpty() && clientTextBox.getText().length() > 0) { - usersMap.values().stream() - .filter(user -> user.getName().toLowerCase().replace("@", "") - .startsWith(startingCharacters.toLowerCase())) + if (!usersMap.isEmpty() && clientTextBox.getText().length() > 0) + { + usersMap.values().stream().filter(user -> user.getName().toLowerCase().replace("@", "").startsWith(startingCharacters.toLowerCase())) .forEach(user -> autoCompleteNames.add(user.getName())); // If the matches arean't already in autoCompleteNames @@ -1208,14 +1215,12 @@ public void keyPressed(KeyEvent e) { // TODO: this should only replace the text closest to the caret // position. - clientTextBox.setText( - clientTextBox.getText().replace(lastUserToComplete + ": ", nextUser + ": ")); + clientTextBox.setText(clientTextBox.getText().replace(lastUserToComplete + ": ", nextUser + ": ")); completionLength += 2; } else { String textAfterCaret = clientTextBox.getText().substring(clientTextBox.getCaretPosition()); - clientTextBox.setText(clientTextBox.getText().substring(0, - (clientTextBox.getCaretPosition() - startingCharacters.length())) + clientTextBox.setText(clientTextBox.getText().substring(0, (clientTextBox.getCaretPosition() - startingCharacters.length())) + (nextUser + ": ") + textAfterCaret); } clientTextBox.setCaretPosition((currentCaretPosition + completionLength)); @@ -1227,8 +1232,7 @@ public void keyPressed(KeyEvent e) int nextTextInt = 0; switch (e.getKeyCode()) { - case KeyEvent.VK_UP -> - { + case KeyEvent.VK_UP -> { if (!userHistory.isEmpty()) { nextTextInt = userHistory.indexOf(clientTextBox.getText()) - 1; @@ -1238,8 +1242,7 @@ public void keyPressed(KeyEvent e) clientTextBox.setText(userHistory.get(nextTextInt)); } } - case KeyEvent.VK_DOWN -> - { + case KeyEvent.VK_DOWN -> { if (!userHistory.isEmpty()) { nextTextInt = userHistory.indexOf(clientTextBox.getText()) + 1; @@ -1250,8 +1253,7 @@ public void keyPressed(KeyEvent e) } } case KeyEvent.VK_ESCAPE -> clientTextBox.setText(""); - default -> - { + default -> { if (lastUserToComplete != null) lastUserToComplete = null; if (startingCharacters != null) @@ -1263,21 +1265,21 @@ public void keyPressed(KeyEvent e) } } - public void keyTyped(KeyEvent ke) + public void keyTyped (KeyEvent ke) {} - public void keyReleased(KeyEvent ke) + public void keyReleased (KeyEvent ke) {} } // Repaints the window, delayed by EVENT_DELAY private class TickerAction implements ActionListener { - public void actionPerformed(ActionEvent event) + public void actionPerformed (ActionEvent event) { SwingUtilities.invokeLater(new Runnable() { - public void run() + public void run () { if (IRCChannelBase.this.tickerPanel.isVisible()) { @@ -1319,12 +1321,12 @@ public void run() private class TickerListener extends MouseInputAdapter { - public void mouseEntered(MouseEvent e) + public void mouseEntered (MouseEvent e) { eventTickerTimer.setDelay(((InterfacePanel) gui.interfacePanel).getEventTickerDelay() * 10); } - public void mouseExited(MouseEvent e) + public void mouseExited (MouseEvent e) { eventTickerTimer.setDelay(((InterfacePanel) gui.interfacePanel).getEventTickerDelay()); } @@ -1338,7 +1340,7 @@ public void mouseExited(MouseEvent e) */ class UsersMouseListener extends MouseInputAdapter { - public void mouseClicked(MouseEvent e) + public void mouseClicked (MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { diff --git a/src/urChatBasic/frontend/IRCServer.java b/src/urChatBasic/frontend/IRCServer.java index 7663772..b4087e5 100644 --- a/src/urChatBasic/frontend/IRCServer.java +++ b/src/urChatBasic/frontend/IRCServer.java @@ -91,7 +91,7 @@ public IRCServer (String serverName, String nick, String login, String password, icon = new ImageIcon(imgPath); } catch (IOException e) { - Constants.LOGGER.warn( "COULD NOT LOAD Server.png " + e.getLocalizedMessage()); + Constants.LOGGER.warn("COULD NOT LOAD Server.png " + e.getLocalizedMessage()); } } @@ -118,7 +118,6 @@ public void nickservRequestAuthentication () public void saslCompleteAuthentication () { sendClientText("CAP END", getName()); - // TODO: gui.connectFavourites(this); reconnectChannels(); } @@ -336,7 +335,7 @@ public void actionPerformed (ActionEvent arg0) { if (IRCServer.this.isConnected()) { - Constants.LOGGER.info( "send quit message"); + Constants.LOGGER.info("send quit message"); // Send the /quit message, which disconnects and remove the gui elements sendClientText("/quit Goodbye cruel world", getName()); } else @@ -388,7 +387,7 @@ public void connect (String[] autoConnectChannels) } catch (Exception e) { - Constants.LOGGER.error( "Failed to create backend! " + e.getLocalizedMessage()); + Constants.LOGGER.error("Failed to create backend! " + e.getLocalizedMessage()); } new Thread(serverConnection).start(); @@ -513,9 +512,10 @@ public void quitChannel (IRCChannelBase ircChannel) boolean tabExists = Arrays.stream(gui.tabbedPane.getComponents()).anyMatch(channel -> channel.equals(ircChannel)); - try { - if (tabExists && gui.tabbedPane.getSelectedComponent().equals(ircChannel)) - gui.tabbedPane.setSelectedComponent(gui.previousSelectedTab); + try + { + if (tabExists && gui.tabbedPane.getSelectedComponent().equals(ircChannel)) + gui.tabbedPane.setSelectedComponent(gui.previousSelectedTab); } catch (IllegalArgumentException iae) { Constants.LOGGER.debug("Previous Selected tab doesn't exist, unable to revert selection"); @@ -558,42 +558,42 @@ public void addToCreatedChannels (String channelName, boolean asPrivate) boolean iconsShown = (boolean) URPanels.getKeyComponentValue(Constants.KEY_SHOW_TAB_ICON); - SwingUtilities.invokeLater( - new Runnable() { + SwingUtilities.invokeLater(new Runnable() + { - @Override - public void run () + @Override + public void run () + { + IRCChannelBase tempChannel = getCreatedChannel(channelName, asPrivate); + int newIndex = gui.tabbedPane.indexOfComponent(gui.currentSelectedTab) + 1; + gui.tabbedPane.insertTab(channelName, iconsShown ? tempChannel.icon : null, tempChannel, null, + gui.tabbedPane.indexOfComponent(gui.currentSelectedTab) + 1); + + // gui.tabbedPane.addTab(channelName, tempChannel.icon, tempChannel); + Component currentTab = gui.tabbedPane.getSelectedComponent(); + if (currentTab instanceof IRCChannelBase) { - IRCChannelBase tempChannel = getCreatedChannel(channelName, asPrivate); - int newIndex = gui.tabbedPane.indexOfComponent(gui.currentSelectedTab) + 1; - gui.tabbedPane.insertTab(channelName, iconsShown ? tempChannel.icon : null, tempChannel, null, gui.tabbedPane.indexOfComponent(gui.currentSelectedTab) + 1); - - // gui.tabbedPane.addTab(channelName, tempChannel.icon, tempChannel); - Component currentTab = gui.tabbedPane.getSelectedComponent(); - if (currentTab instanceof IRCChannelBase) + if (!((IRCChannelBase) currentTab).userIsTyping()) + { + gui.tabbedPane.setSelectedIndex(newIndex); + tempChannel.getUserTextBox().requestFocus(); + } else + { + tempChannel.callForAttention(); + } + } else if (currentTab instanceof IRCServer) + { + if (clientTextBox.getText().isEmpty()) { - if (!((IRCChannelBase) currentTab).userIsTyping()) - { - gui.tabbedPane.setSelectedIndex(newIndex); - tempChannel.getUserTextBox().requestFocus(); - } else - { - tempChannel.callForAttention(); - } - } else if (currentTab instanceof IRCServer) + gui.tabbedPane.setSelectedIndex(newIndex); + tempChannel.getUserTextBox().requestFocus(); + } else { - if (clientTextBox.getText().isEmpty()) - { - gui.tabbedPane.setSelectedIndex(newIndex); - tempChannel.getUserTextBox().requestFocus(); - } else - { - tempChannel.callForAttention(); - } + tempChannel.callForAttention(); } } } - ); + }); } } diff --git a/src/urChatBasic/frontend/UserGUI.java b/src/urChatBasic/frontend/UserGUI.java index d71dd5c..997a239 100644 --- a/src/urChatBasic/frontend/UserGUI.java +++ b/src/urChatBasic/frontend/UserGUI.java @@ -180,7 +180,7 @@ public UROptionsPanel getProfilePanel () public UROptionsPanel getConnectionPanel () { - return interfacePanel; + return connectionPanel; } @@ -334,7 +334,8 @@ public void updatePreviewTextArea () // previewTextArea.setFont(clientFontPanel.getFont()); if (previewLineFormatter == null) { - previewLineFormatter = new LineFormatter(clientFontPanel.getStyle(), previewTextArea, previewTextScroll, null, URProfilesUtil.getActiveProfilePath()); + previewLineFormatter = + new LineFormatter(clientFontPanel.getStyle(), previewTextArea, previewTextScroll, null, URProfilesUtil.getActiveProfilePath()); URProfilesUtil.addListener(EventType.CHANGE, e -> { previewLineFormatter.setSettingsPath(URProfilesUtil.getActiveProfilePath()); diff --git a/src/urChatBasic/frontend/panels/ConnectionPanel.java b/src/urChatBasic/frontend/panels/ConnectionPanel.java index 02c6139..a225666 100644 --- a/src/urChatBasic/frontend/panels/ConnectionPanel.java +++ b/src/urChatBasic/frontend/panels/ConnectionPanel.java @@ -42,7 +42,8 @@ import urChatBasic.frontend.dialogs.MessageDialog; import urChatBasic.frontend.utils.URPanels; -public class ConnectionPanel extends UROptionsPanel { +public class ConnectionPanel extends UROptionsPanel +{ public static final String PANEL_DISPLAY_NAME = "Connection"; // Identification @@ -88,8 +89,8 @@ public ConnectionPanel (MainOptionsPanel optionsPanel, Optional preferr } /** - * Adds all the components to the panel, with the related preference Keys for that component. - * i.e showEventTicker is set via the KEY_EVENT_TICKER_ACTIVE key ('show event ticker') + * Adds all the components to the panel, with the related preference Keys for that component. i.e showEventTicker is set via the KEY_EVENT_TICKER_ACTIVE key + * ('show event ticker') */ private void setupConnectionPanel () { @@ -126,29 +127,22 @@ private void setupConnectionPanel () SpringLayout connectionLayout = (SpringLayout) this.getLayout(); // Puts the connect button further below Channel - connectionLayout.putConstraint(SpringLayout.NORTH, connectButton, TOP_SPACING * TOP_SPACING, SpringLayout.SOUTH, - firstChannelTextField); - connectionLayout.putConstraint(SpringLayout.WEST, connectButton, LEFT_ALIGNED, SpringLayout.WEST, - firstChannelTextField); + connectionLayout.putConstraint(SpringLayout.NORTH, connectButton, TOP_SPACING * TOP_SPACING, SpringLayout.SOUTH, firstChannelTextField); + connectionLayout.putConstraint(SpringLayout.WEST, connectButton, LEFT_ALIGNED, SpringLayout.WEST, firstChannelTextField); // Aligns the autoConnectToFavourites checkbox to the label of userNameTextField connectionLayout.putConstraint(SpringLayout.NORTH, autoConnectToFavourites, TOP_ALIGNED, SpringLayout.NORTH, - URPanels.getLabelForComponent(this,userNameTextField)); - connectionLayout.putConstraint(SpringLayout.WEST, autoConnectToFavourites, LEFT_SPACING, SpringLayout.EAST, - proxyTypeChoice); + URPanels.getLabelForComponent(this, userNameTextField)); + connectionLayout.putConstraint(SpringLayout.WEST, autoConnectToFavourites, LEFT_SPACING, SpringLayout.EAST, proxyTypeChoice); URPanels.addKeyAssociation(this, autoConnectToFavourites, Constants.KEY_AUTO_CONNECT_FAVOURITES); // Puts the Favourites box inline with the autoConnectToFavourites check box and the connect button - connectionLayout.putConstraint(SpringLayout.NORTH, favouritesScroller, TOP_SPACING, SpringLayout.SOUTH, - autoConnectToFavourites); - connectionLayout.putConstraint(SpringLayout.WEST, favouritesScroller, LEFT_ALIGNED, SpringLayout.WEST, - autoConnectToFavourites); - connectionLayout.putConstraint(SpringLayout.EAST, favouritesScroller, LEFT_ALIGNED, SpringLayout.EAST, - autoConnectToFavourites); - connectionLayout.putConstraint(SpringLayout.SOUTH, favouritesScroller, TOP_SPACING, SpringLayout.SOUTH, - connectButton); + connectionLayout.putConstraint(SpringLayout.NORTH, favouritesScroller, TOP_SPACING, SpringLayout.SOUTH, autoConnectToFavourites); + connectionLayout.putConstraint(SpringLayout.WEST, favouritesScroller, LEFT_ALIGNED, SpringLayout.WEST, autoConnectToFavourites); + connectionLayout.putConstraint(SpringLayout.EAST, favouritesScroller, LEFT_ALIGNED, SpringLayout.EAST, autoConnectToFavourites); + connectionLayout.putConstraint(SpringLayout.SOUTH, favouritesScroller, TOP_SPACING, SpringLayout.SOUTH, connectButton); URProfilesUtil.addListener(EventType.CHANGE, e -> { loadFavouritesList(); @@ -169,7 +163,7 @@ public Boolean isFavourite (IRCChannelBase channel) for (Object tempItem : favouritesListModel.toArray()) { castItem = (FavouritesItem) tempItem; - if (castItem.favChannel.equals(channel.getName()) && castItem.favServer.equals(channel.getServer())) + if (castItem.favChannel.equals(channel.getName()) && castItem.favServer.equals(channel.getServer().toString())) { return true; } @@ -200,8 +194,7 @@ public void loadFavouritesList () } /** - * Create an element in the favourites list. Contains a constructor plus a pop up menu for the - * element. + * Create an element in the favourites list. Contains a constructor plus a pop up menu for the element. * * @author Matt * @param String server @@ -278,7 +271,7 @@ private class ShowFontDialog implements ActionListener @Override public void actionPerformed (ActionEvent arg0) { - if(favFontDialog == null) + if (favFontDialog == null) { favFontDialog = new FontDialog(favChannel, DriverGUI.gui.getStyle(), settingsPath); // favFontDialog.addSaveListener(new SaveChannelFontListener()); @@ -406,8 +399,7 @@ public void actionPerformed (ActionEvent e) public void connectFavourites (IRCServerBase server) { if (servernameTextField.getText().trim().equals(server.getName())) - server.sendClientText("/join " + firstChannelTextField.getText().trim(), - servernameTextField.getText().trim()); + server.sendClientText("/join " + firstChannelTextField.getText().trim(), servernameTextField.getText().trim()); if (autoConnectToFavourites.isSelected()) { @@ -434,47 +426,43 @@ private class ConnectPressed implements ActionListener @Override public void actionPerformed (ActionEvent arg0) { - if (!authenticationTypeChoice.getSelectedItem().equals(CapabilityTypes.NONE.getType()) && passwordTextField.getPassword().length == 0 ) + if (!authenticationTypeChoice.getSelectedItem().equals(CapabilityTypes.NONE.getType()) && passwordTextField.getPassword().length == 0) { - MessageDialog dialog = new MessageDialog( - "Password field is empty and is required for your chosen authentication method.", "Warning", + MessageDialog dialog = new MessageDialog("Password field is empty and is required for your chosen authentication method.", "Warning", JOptionPane.WARNING_MESSAGE); dialog.setVisible(true); - } else if (!proxyTypeChoice.getSelectedItem().equals(ProxyTypes.NONE.getType()) && (proxyHostNameTextField.getText().isBlank() || - proxyPortTextField.getText().isBlank()) ) + } else if (!proxyTypeChoice.getSelectedItem().equals(ProxyTypes.NONE.getType()) + && (proxyHostNameTextField.getText().isBlank() || proxyPortTextField.getText().isBlank())) { - MessageDialog dialog = new MessageDialog( - "Hostname or Port field is empty which is required for your chosen Proxy method.", "Warning", - JOptionPane.WARNING_MESSAGE); + MessageDialog dialog = new MessageDialog("Hostname or Port field is empty which is required for your chosen Proxy method.", "Warning", + JOptionPane.WARNING_MESSAGE); dialog.setVisible(true); } else { // DriverGUI.gui.addToCreatedServers(servernameTextField.getText().trim()); IRCServerBase newServer = new IRCServer(servernameTextField.getText().trim(), userNameTextField.getText().trim(), - realNameTextField.getText().trim(), new String(passwordTextField.getPassword()), - serverPortTextField.getText().trim(), serverTLSCheckBox.isSelected(), - proxyHostNameTextField.getText(), proxyPortTextField.getText(), proxyTypeChoice.getSelectedItem(), authenticationTypeChoice.getSelectedItem()); + realNameTextField.getText().trim(), new String(passwordTextField.getPassword()), serverPortTextField.getText().trim(), + serverTLSCheckBox.isSelected(), proxyHostNameTextField.getText(), proxyPortTextField.getText(), proxyTypeChoice.getSelectedItem(), + authenticationTypeChoice.getSelectedItem()); // TODO: Revisit when considering adding support for multiple servers // if (autoConnectToFavourites.isSelected()) // { - // FavouritesItem castItem; - // for (Object tempItem : favouritesListModel.toArray()) - // { - // castItem = (FavouritesItem) tempItem; - // DriverGUI.gui.addToCreatedServers(castItem.favServer); - // } + // FavouritesItem castItem; + // for (Object tempItem : favouritesListModel.toArray()) + // { + // castItem = (FavouritesItem) tempItem; + // DriverGUI.gui.addToCreatedServers(castItem.favServer); + // } // } List favouriteChannels = new ArrayList<>(); - if(autoConnectToFavourites.isSelected()) - favouriteChannels = Collections.list(favouritesListModel.elements()) - .stream() - .map(FavouritesItem::getChannelName) - .collect(Collectors.toList()); + if (autoConnectToFavourites.isSelected()) + favouriteChannels = + Collections.list(favouritesListModel.elements()).stream().map(FavouritesItem::getChannelName).collect(Collectors.toList()); - if(!firstChannelTextField.getText().isBlank()) + if (!firstChannelTextField.getText().isBlank()) favouriteChannels.add(firstChannelTextField.getText()); newServer.connect(favouriteChannels.stream().toArray(size -> new String[size]));