From a0de2863e9db949b5f3f4c53db566a5a05004409 Mon Sep 17 00:00:00 2001 From: Sergey Ponomarev Date: Sun, 2 Aug 2026 17:05:26 +0300 Subject: [PATCH] ChatRoom: initialize fields in declaration where possible --- .../org/jivesoftware/spark/ui/ChatRoom.java | 69 ++++++------------- .../spark/ui/rooms/ChatRoomImpl.java | 29 +------- .../spark/ui/rooms/GroupChatRoom.java | 4 -- 3 files changed, 24 insertions(+), 78 deletions(-) diff --git a/core/src/main/java/org/jivesoftware/spark/ui/ChatRoom.java b/core/src/main/java/org/jivesoftware/spark/ui/ChatRoom.java index abc675476..820604e0f 100644 --- a/core/src/main/java/org/jivesoftware/spark/ui/ChatRoom.java +++ b/core/src/main/java/org/jivesoftware/spark/ui/ChatRoom.java @@ -62,6 +62,7 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import static java.awt.GridBagConstraints.*; +import static org.apache.commons.lang3.StringUtils.isBlank; /** * The base implementation of all ChatRoom conversations. You would implement @@ -69,42 +70,40 @@ import static java.awt.GridBagConstraints.*; */ public abstract class ChatRoom extends BackgroundPanel implements ActionListener, StanzaListener, DocumentListener, ConnectionListener, FocusListener, ContextMenuListener, ChatFrameToFrontListener { private final LocalPreferences pref = SettingsManager.getLocalPreferences(); + private final JPanel chatPanel = new JPanel(new GridBagLayout()); + private final JSplitPane splitPane = new JSplitPane(); + private final JSplitPane verticalSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT); - private final JPanel chatPanel; - private final JSplitPane splitPane; - private final JSplitPane verticalSplit; + private final JLabel notificationLabel = new JLabel(); + private final TranscriptWindow transcriptWindow = UIComponentRegistry.createTranscriptWindow(); + private final ChatAreaSendField chatAreaButton = new ChatAreaSendField(); + private final ChatToolBar toolbar = new ChatToolBar(); + private final JScrollPane textScroller = new JScrollPane(transcriptWindow); + private final JPanel bottomPanel = new JPanel(); - private final JLabel notificationLabel; - private final TranscriptWindow transcriptWindow; - private final ChatAreaSendField chatAreaButton; - private final ChatToolBar toolbar; - private final JScrollPane textScroller; - private final JPanel bottomPanel; - - private final JPanel editorWrapperBar; - private final JPanel editorBarRight; - private final JPanel editorBarLeft; - private final JPanel chatWindowPanel; + private final JPanel editorWrapperBar = new JPanel(new BorderLayout()); + private final JPanel editorBarRight = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 1)); + private final JPanel editorBarLeft = new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 1)); + private final JPanel chatWindowPanel = new JPanel(); private int unreadMessageCount; private boolean mousePressed; private final CopyOnWriteArrayList closingListeners = new CopyOnWriteArrayList<>(); + private final CopyOnWriteArrayList messageListeners = new CopyOnWriteArrayList<>(); + private final CopyOnWriteArrayList fileDropListeners = new CopyOnWriteArrayList<>(); private ChatRoomTransferHandler transferHandler; - private final List packetIDList; - private final CopyOnWriteArrayList messageListeners; - private final List transcript; - private final CopyOnWriteArrayList fileDropListeners; + private final List packetIDList = new ArrayList<>(); + private final List transcript = new ArrayList<>(); private final MouseAdapter transcriptWindowMouseListener; private final KeyAdapter chatEditorKeyListener; private ChatFrame _chatFrame; private final RolloverButton _alwaysOnTopItem; - private boolean _isAlwaysOnTopActive; // Chat state private TimerTask typingTimerTask; @@ -112,30 +111,15 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener private ChatState lastNotificationSent; private final long pauseTimePeriod = 2000; private final long inactiveTimePeriod = 120000; - protected long lastActivity; + protected long lastActivity = System.currentTimeMillis(); // set the last activity to be right now /** Flag to show is the room is stale i.e. it was last active more than defaultChatLengthTimeout (15 min) */ boolean stale; private static final Color COLOR_BOTTOM_PANEL_BORDER = new Color(197, 213, 230); protected ChatRoom() { - chatPanel = new JPanel(new GridBagLayout()); - transcriptWindow = UIComponentRegistry.createTranscriptWindow(); - splitPane = new JSplitPane(); - packetIDList = new ArrayList<>(); - notificationLabel = new JLabel(); - toolbar = new ChatToolBar(); - bottomPanel = new JPanel(); - - messageListeners = new CopyOnWriteArrayList<>(); - transcript = new ArrayList<>(); - - editorWrapperBar = new JPanel(new BorderLayout()); - editorBarLeft = new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 1)); - editorBarRight = new JPanel(new FlowLayout(FlowLayout.RIGHT, 1, 1)); editorWrapperBar.add(editorBarLeft, BorderLayout.WEST); editorWrapperBar.add(editorBarRight, BorderLayout.EAST); - fileDropListeners = new CopyOnWriteArrayList<>(); transcriptWindowMouseListener = new MouseAdapter() { @Override @@ -161,10 +145,8 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener transcriptWindow.addMouseListener(transcriptWindowMouseListener); - chatAreaButton = new ChatAreaSendField(); chatAreaButton.setVisible(true); chatAreaButton.setEnabled(true); - textScroller = new JScrollPane(transcriptWindow); textScroller.setBackground(transcriptWindow.getBackground()); textScroller.getViewport().setBackground(Color.white); transcriptWindow.setBackground(Color.white); @@ -177,7 +159,6 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener splitPane.setBorder(null); splitPane.setOneTouchExpandable(false); // Add Vertical Split Pane - verticalSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT); add(verticalSplit, new GridBagConstraints(0, 1, 1, 1, 1, 1, CENTER, BOTH, new Insets(0, 0, 0, 0), 0, 0)); verticalSplit.setBorder(null); @@ -185,7 +166,6 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener verticalSplit.setTopComponent(splitPane); textScroller.setAutoscrolls(true); - // For the first 5*150ms we wait for transcript to load and move // ScrollPane to max position if size of ScrollPane changed textScroller.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { @@ -227,7 +207,6 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener textScroller.getVerticalScrollBar().setBlockIncrement(200); textScroller.getVerticalScrollBar().setUnitIncrement(20); - chatWindowPanel = new JPanel(); chatWindowPanel.setLayout(new GridBagLayout()); chatWindowPanel.add(textScroller, new GridBagConstraints(0, 10, 1, 1, 1, 1, WEST, BOTH, new Insets(0, 0, 0, 0), 0, 0)); chatWindowPanel.setOpaque(false); @@ -293,20 +272,16 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener } }); - _isAlwaysOnTopActive = pref.isChatWindowAlwaysOnTop(); - _alwaysOnTopItem = UIComponentRegistry.getButtonFactory().createAlwaysOnTop(_isAlwaysOnTopActive); - + _alwaysOnTopItem = UIComponentRegistry.getButtonFactory().createAlwaysOnTop(pref.isChatWindowAlwaysOnTop()); _alwaysOnTopItem.addActionListener(actionEvent -> { - if (!_isAlwaysOnTopActive) { + if (!pref.isChatWindowAlwaysOnTop()) { pref.setChatWindowAlwaysOnTop(true); _chatFrame.setWindowAlwaysOnTop(true); - _isAlwaysOnTopActive = true; _alwaysOnTopItem.setIcon(SparkRes.getImageIcon(SparkRes.Icon.FRAME_ALWAYS_ON_TOP_ACTIVE)); } else { pref.setChatWindowAlwaysOnTop(false); _chatFrame.setWindowAlwaysOnTop(false); - _isAlwaysOnTopActive = false; _alwaysOnTopItem.setIcon(SparkRes.getImageIcon(SparkRes.Icon.FRAME_ALWAYS_ON_TOP_DEACTIVE)); } }); @@ -399,7 +374,7 @@ public abstract class ChatRoom extends BackgroundPanel implements ActionListener private void handleNickNameCompletion() throws ChatRoomNotFoundException { // Search for a name that starts with the same word as the last word in the chat input editor. final String text = getChatInputEditor().getText(); - if (text == null || text.isEmpty()) { + if (isBlank(text)) { return; } diff --git a/core/src/main/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java b/core/src/main/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java index 5b460276f..1d49230f8 100644 --- a/core/src/main/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java +++ b/core/src/main/java/org/jivesoftware/spark/ui/rooms/ChatRoomImpl.java @@ -58,6 +58,7 @@ import java.text.DateFormat; import java.util.Date; import java.util.concurrent.CopyOnWriteArrayList; +import static org.apache.commons.lang3.StringUtils.isBlank; import static org.apache.commons.lang3.Strings.CS; import static org.jivesoftware.spark.util.StringUtils.replaceMe; @@ -165,7 +166,6 @@ public class ChatRoomImpl extends ChatRoom { addToRosterButton.addActionListener(this); } - lastActivity = System.currentTimeMillis(); Log.debug("Loaded chat room impl: " + title); } @@ -222,7 +222,7 @@ public class ChatRoomImpl extends ChatRoom { // Remove control characters text = text.replaceAll("[\\u0001-\\u0008\\u000B-\\u001F]", ""); // If the body is empty, just return and do nothing - if (!ModelUtil.hasLength(text)) { + if (isBlank(text)) { return; } MessageBuilder messageBuilder = StanzaBuilder.buildMessage() @@ -236,7 +236,6 @@ public class ChatRoomImpl extends ChatRoom { // Fire Message Filters SparkManager.getChatManager().filterOutgoingMessage(this, messageBuilder); - // Fire Global Filters Message message = messageBuilder.build(); SparkManager.getChatManager().fireGlobalMessageSentListeners(this, message); @@ -296,7 +295,6 @@ public class ChatRoomImpl extends ChatRoom { private void displaySendMessage( Message message ) { lastActivity = System.currentTimeMillis(); - try { getTranscriptWindow().insertMessage( getNickname(), message, ChatManager.TO_COLOR); getChatInputEditor().selectAll(); @@ -308,7 +306,6 @@ public class ChatRoomImpl extends ChatRoom { catch (Exception ex) { Log.error( "Error sending message", ex); } - // Notify users that message has been sent fireMessageSent(message); @@ -324,29 +321,16 @@ public class ChatRoomImpl extends ChatRoom { return roomname; } - @Override public Icon getTabIcon() { return tabIcon; } - public void setTabIcon(Icon icon) { - this.tabIcon = icon; - } - @Override public String getTabTitle() { return tabTitle; } - public void setTabTitle(String tabTitle) { - this.tabTitle = tabTitle; - } - - public void setRoomTitle(String roomTitle) { - this.roomTitle = roomTitle; - } - @Override public String getRoomTitle() { return roomTitle; @@ -379,8 +363,6 @@ public class ChatRoomImpl extends ChatRoom { /** * Returns the users full jid (ex. macbeth@jivesoftware.com/spark) or null if user is offline. - * - * @return the users Full JID. */ public EntityFullJid getJidOnline() { presence = PresenceManager.getPresence(getParticipantJID()); @@ -639,13 +621,6 @@ public class ChatRoomImpl extends ChatRoom { } } - /** - * Returns the current presence of the client this room was created for. - */ - public Presence getPresence() { - return presence; - } - @Override public void authenticated( XMPPConnection xmppConnection, boolean b ) { diff --git a/core/src/main/java/org/jivesoftware/spark/ui/rooms/GroupChatRoom.java b/core/src/main/java/org/jivesoftware/spark/ui/rooms/GroupChatRoom.java index 9e9de24b4..8b41fc17b 100644 --- a/core/src/main/java/org/jivesoftware/spark/ui/rooms/GroupChatRoom.java +++ b/core/src/main/java/org/jivesoftware/spark/ui/rooms/GroupChatRoom.java @@ -229,9 +229,6 @@ public class GroupChatRoom extends ChatRoom { } }); - // set the last activity to be right now - lastActivity = System.currentTimeMillis(); - final GroupChatRoomTransferHandler transferHandler = new GroupChatRoomTransferHandler(this); getTranscriptWindow().setTransferHandler(transferHandler); // Adds the Settings and Subject Button to the right Toolbar @@ -982,7 +979,6 @@ public class GroupChatRoom extends ChatRoom { if (!chatStatEnabled || !SparkManager.getConnection().isConnected()) { return; } - // XEP-0085: SHOULD NOT send 'gone' in a MUC. if (state == ChatState.gone) { return;