package net.sphene.goim.serverbrowser.views;
import net.sphene.goim.serverbrowser.models.GameServer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
public class ServerDetailsView extends ViewPart {
public static final String VIEW_ID = "net.sphene.goim.serverbrowser.serverdetails";
private Text txtServerName;
protected GameServer gameServer;
private Group serverInformation;
private Text txtServerAddress;
private Text txtMapName;
public ServerDetailsView() {
super();
}
@Override
public void createPartControl(Composite parent) {
parent.setLayout(new GridLayout(1,false));
serverInformation = new Group(parent,SWT.NULL);
serverInformation.setText("Server Information");
serverInformation.setLayout(new GridLayout(2,false));
serverInformation.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
txtServerName = createLabelAndText(serverInformation, "Server Name");
txtServerAddress = createLabelAndText(serverInformation,"Server Address");
txtMapName = createLabelAndText(serverInformation,"Map Name");
getSite().getPage().addSelectionListener(ServerBrowserView.VIEW_ID, new ISelectionListener() {
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
IStructuredSelection sel = (IStructuredSelection)selection;
if(sel.getFirstElement() instanceof GameServer) {
GameServer server = (GameServer)sel.getFirstElement();
ServerDetailsView.this.gameServer = server;
updateFields();
}
}
});
}
protected Text createLabelAndText(Composite parent, String label) {
Label lblLabel = new Label(parent,SWT.NULL);
lblLabel.setText(label);
Text txtText = new Text(parent,SWT.BORDER);
txtText.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false));
return txtText;
}
protected void updateFields() {
if(gameServer == null) {
serverInformation.setEnabled(false);
} else {
serverInformation.setEnabled(true);
if(gameServer.name != null)
txtServerName.setText(gameServer.name);
if(gameServer.address != null)
txtServerAddress.setText(gameServer.address.toString());
if(gameServer.mapname != null)
txtMapName.setText(gameServer.mapname);
}
}
@Override
public void setFocus() {
}
}