Skip to content

Commit

Permalink
Add the initial Online Session tab
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlettervariables committed Mar 11, 2020
1 parent 52e94d8 commit cc02a54
Show file tree
Hide file tree
Showing 7 changed files with 300 additions and 7 deletions.
3 changes: 3 additions & 0 deletions MekHQ/resources/mekhq/resources/CampaignGUI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,6 @@ tabOverview.toolTipText=A general overview of the unit, includes multiple breakd

bottomRating.DragoonsRating=<html><b>Dragoons Rating:</b> %s </html>
bottomRating.CampaignOpsRating=<html><b>Campaign Ops Rating:</b> %s </html>

# Online Tab
panOnline.TabConstraints.tabTitle=Online Session
16 changes: 16 additions & 0 deletions MekHQ/src/mekhq/gui/CampaignGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ public void showOverviewTab(boolean show) {
}
}

public void showOnlineTab(boolean show) {
if (show) {
addStandardTab(GuiTabType.ONLINE);
} else {
removeStandardTab(GuiTabType.ONLINE);
}
}

public void showGMToolsDialog() {
GMToolsDialog gmTools = new GMToolsDialog(getFrame(), this);
gmTools.setVisible(true);
Expand Down Expand Up @@ -308,6 +316,14 @@ private void initComponents() {
addStandardTab(GuiTabType.FINANCES);
addStandardTab(GuiTabType.OVERVIEW);

// If we're hosting a MekHQ session, or if we're connected to
// a MekHQ session, or if we've had remote campaigns previously,
// then we should display the ONLINE tab.
if (app.isHosting() || app.isRemote()
|| !getCampaignController().getRemoteCampaigns().isEmpty()) {
addStandardTab(GuiTabType.ONLINE);
}

initMain();
initTopButtons();
initStatusBar();
Expand Down
5 changes: 4 additions & 1 deletion MekHQ/src/mekhq/gui/GuiTabType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public enum GuiTabType {
MEKLAB(8, "panMekLab.TabConstraints.tabTitle"), //$NON-NLS-1$
FINANCES(9, "panFinances.TabConstraints.tabTitle"), //$NON-NLS-1$
OVERVIEW(10, "panOverview.TabConstraints.tabTitle"), //$NON-NLS-1$
CUSTOM(11, null);
ONLINE(11, "panOnline.TabConstraints.tabTitle"), //$NON-NLS-1$
CUSTOM(16, null);

private int defaultPos;
private String name;
Expand Down Expand Up @@ -88,6 +89,8 @@ public CampaignGuiTab createTab(CampaignGUI gui) {
return new FinancesTab(gui, name);
case OVERVIEW:
return new OverviewTab(gui, name);
case ONLINE:
return new OnlineTab(gui, name);
default:
return null;

Expand Down
150 changes: 150 additions & 0 deletions MekHQ/src/mekhq/gui/OnlineTab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2020 The MegaMek Team.
*
* This file is part of MekHQ.
*
* MekHQ 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 3 of the License, or
* (at your option) any later version.
*
* MekHQ 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 MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/
package mekhq.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import megamek.common.event.Subscribe;
import megamek.common.util.EncodeControl;
import mekhq.MekHQ;
import mekhq.campaign.CampaignController;
import mekhq.campaign.RemoteCampaign;
import mekhq.gui.model.OnlineCampaignsTableModel;
import mekhq.online.events.CampaignListUpdatedEvent;
import mekhq.preferences.PreferencesNode;

public final class OnlineTab extends CampaignGuiTab implements ActionListener {

private static final long serialVersionUID = 4133071018441878778L;

private ResourceBundle resourceMap;

private OnlineCampaignsTableModel hostCampaignTableModel;
private JTable hostCampaignTable;
private OnlineCampaignsTableModel campaignsTableModel;
private JTable campaignsTable;

OnlineTab(CampaignGUI gui, String name) {
super(gui, name);
MekHQ.registerHandler(this);
setUserPreferences();
}

private void setUserPreferences() {
PreferencesNode preferences = MekHQ.getPreferences().forClass(OnlineTab.class);

// TODO: manage preferences
}

private CampaignController getCampaignController() {
return getCampaignGui().getCampaignController();
}

@Override
public void initTab() {
resourceMap = ResourceBundle.getBundle("mekhq.resources.CampaignGUI", //$NON-NLS-1$
new EncodeControl());

setName("panelOnline"); //$NON-NLS-1$
setLayout(new GridBagLayout());

hostCampaignTableModel = new OnlineCampaignsTableModel(getHostCampaignAsList());
hostCampaignTable = new JTable(hostCampaignTableModel);

JScrollPane hostCampaignScrollPane = new JScrollPane(hostCampaignTable);
JPanel hostCampaignTablePanel = new JPanel(new GridLayout(0, 1));
hostCampaignTablePanel.setBorder(BorderFactory.createTitledBorder("Host Campaign"));
hostCampaignTablePanel.add(hostCampaignScrollPane);

campaignsTableModel = new OnlineCampaignsTableModel(getCampaignController().getRemoteCampaigns());
campaignsTable = new JTable(campaignsTableModel);

JScrollPane campaignsTableScrollPane = new JScrollPane(campaignsTable);
JPanel campaignsTablePanel = new JPanel(new GridLayout(0, 1));
campaignsTablePanel.setBorder(BorderFactory.createTitledBorder("Remote Campaigns"));
campaignsTablePanel.add(campaignsTableScrollPane);

JPanel topPanel = new JPanel(new GridLayout(0, 1));
if (!getCampaignController().isHost()) {
topPanel.add(hostCampaignTablePanel);
}
topPanel.add(campaignsTablePanel);

JPanel campaignDetailsPanel = new JPanel();

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel,
campaignDetailsPanel);
splitPane.setOneTouchExpandable(true);
splitPane.setResizeWeight(1.0);

GridBagConstraints gbc= new java.awt.GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 6;
gbc.fill = java.awt.GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
add(splitPane, gbc);
}

@Override
public void refreshAll() {
hostCampaignTableModel.setData(getHostCampaignAsList());
campaignsTableModel.setData(new ArrayList<>(getCampaignController().getRemoteCampaigns()));
}

private List<RemoteCampaign> getHostCampaignAsList() {
List<RemoteCampaign> list = new ArrayList<>();

list.add(new RemoteCampaign(getCampaignController().getHost(),
getCampaignController().getHostName(), getCampaignController().getHostDate(),
getCampaignController().getHostLocation(), getCampaignController().getHostIsGMMode()));

return list;
}

@Override
public GuiTabType tabType() {
return GuiTabType.ONLINE;
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

@Subscribe
public void handle(CampaignListUpdatedEvent e) {
refreshAll();
}
}
102 changes: 102 additions & 0 deletions MekHQ/src/mekhq/gui/model/OnlineCampaignsTableModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2020 The MegaMek Team.
*
* This file is part of MekHQ.
*
* MekHQ 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 3 of the License, or
* (at your option) any later version.
*
* MekHQ 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 MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/
package mekhq.gui.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import mekhq.campaign.RemoteCampaign;

public class OnlineCampaignsTableModel extends DataTableModel {

private static final long serialVersionUID = 5792881778901299604L;

public final static int COL_ID = 0;
public final static int COL_NAME = 1;
public final static int COL_DATE = 2;
public final static int COL_LOCATION = 3;
public final static int COL_IS_GM = 4;
public final static int COL_IS_ACTIVE = 5;
public final static int N_COL = 6;

private final DateTimeFormatter dateFormatter = ISODateTimeFormat.date();

public OnlineCampaignsTableModel() {
setData(Collections.emptyList());
}

public OnlineCampaignsTableModel(Collection<RemoteCampaign> remoteCampaigns) {
setData(new ArrayList<>(remoteCampaigns));
}

public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
return N_COL;
}

@Override
public String getColumnName(int column) {
switch(column) {
case COL_ID:
return "ID";
case COL_NAME:
return "Name";
case COL_DATE:
return "Date";
case COL_LOCATION:
return "Current Location";
case COL_IS_GM:
return "GM?";
case COL_IS_ACTIVE:
return "Active?";
default:
return "?";
}
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
RemoteCampaign row = (RemoteCampaign)data.get(rowIndex);
switch (columnIndex)
{
case COL_ID:
return row.getId();
case COL_NAME:
return row.getName();
case COL_DATE:
return dateFormatter.print(row.getDate());
case COL_LOCATION:
return row.getLocation().getName(row.getDate());
case COL_IS_GM:
return row.isGMMode() ? "Yes" : "No";
case COL_IS_ACTIVE:
return "Yes";
default:
return null;
}
}
}
17 changes: 11 additions & 6 deletions MekHQ/src/mekhq/online/MekHQClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ protected CampaignDetails getCampaignDetails() {
.setId(getCampaign().getId().toString())
.setName(getCampaign().getName())
.setDate(dateFormatter.print(getCampaign().getDateTime()))
.setLocation(getCampaign().getLocation().getCurrentSystem().getId()).build();
.setLocation(getCampaign().getLocation().getCurrentSystem().getId())
.setIsGMMode(getCampaign().isGM())
.build();
}

public void connect() {
Expand All @@ -108,6 +110,7 @@ public void connect() {
controller.setHostName(hostCampaign.getName());
controller.setHostDate(dateFormatter.parseDateTime(hostCampaign.getDate()));
controller.setHostLocation(hostCampaign.getLocation());
controller.setHostIsGMMode(hostCampaign.getIsGMMode());

knownCampaigns.add(controller.getHost());
for (CampaignDetails details : response.getCampaignsList()) {
Expand All @@ -119,7 +122,6 @@ public void connect() {
dateFormatter.parseDateTime(details.getDate()), details.getLocation(), details.getIsGMMode());
}


createMessageBus();

pingExecutor = Executors.newSingleThreadScheduledExecutor();
Expand Down Expand Up @@ -232,10 +234,7 @@ protected void handlePong(UUID id, Pong pong) {
dateFormatter.parseDateTime(campaign.getDate()), campaign.getLocation(), campaign.getIsGMMode());
}

// Only kick off a notification if we found any new campaigns
if (!knownCampaigns.equals(foundCampaigns)) {
MekHQ.triggerEvent(new CampaignListUpdatedEvent());
}
MekHQ.triggerEvent(new CampaignListUpdatedEvent());
}

protected void handleCampaignDateChanged(UUID hostId, CampaignDateChanged dateChanged) {
Expand All @@ -248,6 +247,8 @@ protected void handleCampaignDateChanged(UUID hostId, CampaignDateChanged dateCh
} else {
controller.setRemoteCampaignDate(campaignId, campaignDate);
}

MekHQ.triggerEvent(new CampaignListUpdatedEvent());
}

protected void handleGMModeChanged(UUID hostId, GMModeChanged gmModeChanged) {
Expand All @@ -259,6 +260,8 @@ protected void handleGMModeChanged(UUID hostId, GMModeChanged gmModeChanged) {
} else {
controller.setRemoteCampaignGMMode(campaignId, isGMMode);
}

MekHQ.triggerEvent(new CampaignListUpdatedEvent());
}

protected void handleLocationChanged(UUID hostId, LocationChanged locationChanged) {
Expand All @@ -271,6 +274,8 @@ protected void handleLocationChanged(UUID hostId, LocationChanged locationChange
} else {
controller.setRemoteCampaignLocation(campaignId, locationId);
}

MekHQ.triggerEvent(new CampaignListUpdatedEvent());
}

@Subscribe
Expand Down
Loading

0 comments on commit cc02a54

Please sign in to comment.