/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.rcp.ui.preferences;
import java.util.ArrayList;
import java.util.List;
import net.sphene.goim.rcp.beans.GOIMGameItem;
import net.sphene.goim.rcp.beans.GOIMGameList;
import net.sphene.goim.rcp.beans.GOIMGameItem.GOIMGameExecuteCommands;
import net.sphene.goim.rcp.extensionpoints.GameAdapter;
import net.sphene.goim.rcp.extensionpoints.GameExtensionPoint;
import net.sphene.goim.rcp.extensionpoints.IGame;
import net.sphene.goim.rcp.extensionpoints.GameExtensionPoint.GameExtensionProxy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Table;
public class GOIMPreferenceGameGUIEditGame extends Composite {
private Group groupEditGame;
private Label labelGame;
private Label labelVersion;
private Text textVersion;
private Label labelPath;
private Combo comboGame;
private Composite compositeChooseExe;
private Text textPath;
private Button buttonChooseExe;
private Composite buttonComposite;
private Button buttonSave;
private List<GameExtensionProxy> games;
private Button buttonCancel = null;
private GOIMGameItem gameItem;
private GOIMGameList gameList;
protected Listener closeListener = new Listener() {
public void handleEvent(Event event) {
GOIMPreferenceGameGUIEditGame.this.dispose();
}
};
private Table executeCommandsTable = null;
private Button executeCommandsCustomize;
private Button delCommand;
private Button addCommand;
private Label defaultCommand;
protected GOIMPreferenceGameGUIEditGame(Composite parent, int style) {
super(parent,style);
initialize();
}
public GOIMPreferenceGameGUIEditGame(Composite parent, int style, GOIMGameItem gameItem, GOIMGameList gameList) {
super(parent, style);
initialize();
this.gameItem = gameItem;
this.gameList = gameList;
fillWithData();
}
private void initialize() {
this.setLayout(new FillLayout());
setSize(new Point(300, 270));
createGroupEditGame();
}
protected void fillWithData() {
games = GameExtensionPoint.getGameExtensions();
for(GameExtensionProxy game : games) {
comboGame.add(game.name);
}
comboGame.select(0);
if(gameItem != null) {
comboGame.select(games.indexOf(gameItem.retrieveExtensionProxy()));
textVersion.setText(gameItem.version == null ? "" : gameItem.version);
textPath.setText(gameItem.path);
executeCommandsCustomize.setSelection(gameItem.customizedExecute);
if(gameItem.executeCommands != null) {
for(GOIMGameExecuteCommands cmd : gameItem.executeCommands) {
TableItem item = new TableItem(executeCommandsTable,SWT.NULL);
item.setText(getGOIMGameExecuteCommandToString(cmd));
item.setData(cmd.clone());
}
}
}
updateDefaultCommand();
updateExecuteCommands();
}
/**
* This method initializes groupEditGame
*
*/
private void createGroupEditGame() {
GridData gridData11 = new org.eclipse.swt.layout.GridData();
gridData11.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData11.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData10 = new org.eclipse.swt.layout.GridData();
gridData10.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData10.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData7 = new org.eclipse.swt.layout.GridData();
gridData7.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData7.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData6 = new org.eclipse.swt.layout.GridData();
gridData6.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData6.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData5 = new org.eclipse.swt.layout.GridData();
gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData5.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridData gridData4 = new org.eclipse.swt.layout.GridData();
gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData4.grabExcessHorizontalSpace = true;
gridData4.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = false;
groupEditGame = new Group(this, SWT.NONE);
groupEditGame.setText("Edit Game");
groupEditGame.setLayout(gridLayout);
labelGame = new Label(groupEditGame, SWT.NONE);
labelGame.setText("Game");
labelGame.setLayoutData(gridData4);
createComboGame();
labelVersion = new Label(groupEditGame, SWT.NONE);
labelVersion.setText("Version (Optional)");
labelVersion.setLayoutData(gridData6);
textVersion = new Text(groupEditGame, SWT.BORDER);
textVersion.setLayoutData(gridData5);
labelPath = new Label(groupEditGame, SWT.NONE);
labelPath.setText("Path To Executable");
labelPath.setLayoutData(gridData7);
createCompositeChooseExe();
createExecuteCommandsTable();
createButtonComposite();
}
/**
* This method initializes comboGame
*
*/
private void createComboGame() {
GridData gridData3 = new org.eclipse.swt.layout.GridData();
gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData3.grabExcessHorizontalSpace = true;
gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
comboGame = new Combo(groupEditGame, SWT.READ_ONLY);
comboGame.setLayoutData(gridData3);
comboGame.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
updateDefaultCommand();
}
});
}
/**
* This method initializes compositeChooseExe
*
*/
private void createCompositeChooseExe() {
GridData gridData = new GridData();
gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
GridData gridData9 = new org.eclipse.swt.layout.GridData();
gridData9.grabExcessHorizontalSpace = true;
gridData9.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData9.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
GridData gridData8 = new org.eclipse.swt.layout.GridData();
gridData8.grabExcessHorizontalSpace = true;
gridData8.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData8.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData8.widthHint = 200;
GridLayout gridLayout1 = new GridLayout();
gridLayout1.numColumns = 2;
gridLayout1.marginWidth = 0;
gridLayout1.marginHeight = 0;
gridLayout1.horizontalSpacing = 5;
compositeChooseExe = new Composite(groupEditGame, SWT.NONE);
compositeChooseExe.setLayout(gridLayout1);
compositeChooseExe.setLayoutData(gridData9);
textPath = new Text(compositeChooseExe, SWT.BORDER);
textPath.setLayoutData(gridData8);
buttonChooseExe = new Button(compositeChooseExe, SWT.NONE);
buttonChooseExe.setText("...");
buttonChooseExe.setLayoutData(gridData);
buttonChooseExe
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(
org.eclipse.swt.events.SelectionEvent e) {
FileDialog fileDialog = new FileDialog(getShell(),
SWT.OPEN);
if (comboGame.getSelectionIndex() > -1) {
String name = games.get(comboGame
.getSelectionIndex()).exename.get(getOSType());
if(name != null)
fileDialog.setFileName(name);
if ("win32".equals(getOSType())) {
// fileDialog.setFilterNames(new
// String[]{"Executable"});
fileDialog
.setFilterExtensions(new String[] { "*.exe" });
}
}
// fileDialog.setFilterExtensions()
String fileName = fileDialog.open();
if (fileName != null)
textPath.setText(fileName);
}
});
}
public String getOSType() {
String name = System.getProperty("os.name").toLowerCase();
if(name.startsWith("windows"))
return "win32";
else if(name.startsWith("linux"))
return "linux";
return "unknown";
}
/**
* This method initializes buttonComposite
*
*/
private void createButtonComposite() {
RowLayout rowLayout = new RowLayout();
rowLayout.fill = false;
rowLayout.justify = true;
GridData gridData12 = new org.eclipse.swt.layout.GridData();
gridData12.grabExcessHorizontalSpace = true;
gridData12.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData12.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
gridData12.horizontalSpan = 3;
buttonComposite = new Composite(groupEditGame, SWT.NONE);
buttonComposite.setLayoutData(gridData12);
buttonComposite.setLayout(rowLayout);
buttonSave = new Button(buttonComposite, SWT.NONE);
buttonSave.setText("Save");
buttonSave.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
boolean createNew = false;
if(gameItem == null) {
gameItem = new GOIMGameItem();
createNew = true;
}
gameItem.gameId = games.get(comboGame.getSelectionIndex()).id;
gameItem.version = textVersion.getText();
gameItem.path = textPath.getText();
gameItem.customizedExecute = executeCommandsCustomize.getSelection();
TableItem[] items = executeCommandsTable.getItems();
List<GOIMGameExecuteCommands> cmds = new ArrayList<GOIMGameExecuteCommands>(items.length);
for(TableItem item : items) {
cmds.add((GOIMGameExecuteCommands)item.getData());
}
gameItem.executeCommands = cmds;
if(createNew)
gameList.add(gameItem);
else
gameItem.fireChangeEvent();
closeListener.handleEvent(null);
}
});
buttonCancel = new Button(buttonComposite, SWT.NONE);
buttonCancel.setText("Cancel");
buttonCancel
.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
closeListener.handleEvent(null);
}
});
}
public static GOIMPreferenceGameGUIEditGame createNewShell(Shell parent,GOIMGameItem gameItem, GOIMGameList gameList) {
final Shell shell = new Shell(parent,SWT.APPLICATION_MODAL | SWT.SHELL_TRIM);
shell.setText("Edit Game");
shell.setLayout(new FillLayout());
GOIMPreferenceGameGUIEditGame editGame = new GOIMPreferenceGameGUIEditGame(shell,SWT.NULL, gameItem, gameList);
editGame.closeListener = new Listener() {
public void handleEvent(Event event) {
shell.close();
}
};
shell.pack();
shell.open();
return editGame;
}
/**
* This method initializes executeCommandsTable
*
*/
private void createExecuteCommandsTable() {
Group executeCommands = new Group(groupEditGame,SWT.NONE);
executeCommands.setText("Execute Commands");
executeCommands.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
executeCommands.setLayout(new GridLayout(1,false));
executeCommandsCustomize = new Button(executeCommands, SWT.CHECK);
executeCommandsCustomize.setText("Use Customized Commands");
defaultCommand = new Label(executeCommands, SWT.NONE);
defaultCommand.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false));
executeCommandsTable = new Table(executeCommands, SWT.FULL_SELECTION | SWT.SINGLE);
executeCommandsTable.setHeaderVisible(true);
executeCommandsTable.setLinesVisible(true);
GridData executeCommandsTableData = new GridData(SWT.FILL,SWT.FILL,true,true);
executeCommandsTableData.heightHint = 50;
executeCommandsTable.setLayoutData(executeCommandsTableData);
TableColumn cmd = new TableColumn(executeCommandsTable,SWT.NULL);
cmd.setText("Command");
cmd.setWidth(150);
TableColumn type = new TableColumn(executeCommandsTable,SWT.NULL);
type.setText("Type");
type.setWidth(70);
TableColumn wait = new TableColumn(executeCommandsTable,SWT.NULL);
wait.setText("Wait");
wait.setWidth(50);
final TableEditor cmdEditor = new TableEditor(executeCommandsTable);
final TableEditor typeEditor = new TableEditor(executeCommandsTable);
final TableEditor waitEditor = new TableEditor(executeCommandsTable);
cmdEditor.horizontalAlignment =
typeEditor.horizontalAlignment =
waitEditor.horizontalAlignment = SWT.LEFT;
cmdEditor.grabHorizontal =
typeEditor.grabHorizontal =
waitEditor.grabHorizontal = true;
Composite executeCommandsOptions = new Composite(executeCommands,SWT.NULL);
executeCommandsOptions.setLayoutData(new GridData(SWT.CENTER,SWT.TOP,true,false));
executeCommandsOptions.setLayout(new GridLayout(2,true));
addCommand = new Button(executeCommandsOptions,SWT.PUSH);
addCommand.setText("Add Command");
delCommand = new Button(executeCommandsOptions,SWT.PUSH);
delCommand.setText("Delete Command");
executeCommandsCustomize.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
updateExecuteCommands();
}
});
addCommand.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
TableItem item = new TableItem(executeCommandsTable,SWT.NULL);
GOIMGameExecuteCommands cmd = new GOIMGameExecuteCommands();
item.setData(cmd);
item.setText(getGOIMGameExecuteCommandToString(cmd));
executeCommandsTable.setFocus();
executeCommandsTable.setSelection(new TableItem[] { item });
}
});
delCommand.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
TableItem[] items = executeCommandsTable.getSelection();
if(items.length > 0) {
items[0].dispose();
}
}
});
executeCommandsTable.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
TableItem[] items = executeCommandsTable.getSelection();
if(items.length < 1) return;
final TableItem item = items[0];
if(cmdEditor.getEditor() != null) {
cmdEditor.getEditor().dispose();
typeEditor.getEditor().dispose();
waitEditor.getEditor().dispose();
}
final GOIMGameExecuteCommands cmd = (GOIMGameExecuteCommands)item.getData();
final Text command = new Text(executeCommandsTable,SWT.NONE);
command.setText(cmd.command);
command.selectAll();
command.setFocus();
cmdEditor.setEditor(command, item, 0);
final Combo type = new Combo(executeCommandsTable,SWT.DROP_DOWN | SWT.READ_ONLY);
type.add(GOIMGameExecuteCommands.ExecuteCommandType.BOTH.toString());
type.add(GOIMGameExecuteCommands.ExecuteCommandType.CONNECT.toString());
type.add(GOIMGameExecuteCommands.ExecuteCommandType.LAUNCH.toString());
if(cmd.commandType == GOIMGameExecuteCommands.ExecuteCommandType.BOTH) {
type.select(0);
} else if(cmd.commandType == GOIMGameExecuteCommands.ExecuteCommandType.CONNECT) {
type.select(1);
} else if(cmd.commandType == GOIMGameExecuteCommands.ExecuteCommandType.LAUNCH) {
type.select(2);
}
typeEditor.setEditor(type, item, 1);
final Button wait = new Button(executeCommandsTable,SWT.CHECK);
wait.setText("Wait");
wait.setToolTipText("Wait until command exits.");
wait.setSelection(cmd.waitforterminate);
waitEditor.setEditor(wait, item, 2);
command.addListener(SWT.Modify,new Listener() {
public void handleEvent(Event event) {
cmdEditor.getItem().setText(0,command.getText());
cmd.command = command.getText();
}
});
type.addListener(SWT.Modify,new Listener() {
public void handleEvent(Event event) {
GOIMGameExecuteCommands.ExecuteCommandType cmdtype = null;
switch(type.getSelectionIndex()) {
case 0:
cmdtype = GOIMGameExecuteCommands.ExecuteCommandType.BOTH;
break;
case 1:
cmdtype = GOIMGameExecuteCommands.ExecuteCommandType.CONNECT;
break;
case 2:
cmdtype = GOIMGameExecuteCommands.ExecuteCommandType.LAUNCH;
break;
}
typeEditor.getItem().setText(1,cmdtype.toString());
cmd.commandType = cmdtype;
}
});
wait.addListener(SWT.Selection,new Listener() {
public void handleEvent(Event event) {
typeEditor.getItem().setText(2,Boolean.toString(wait.getSelection()));
cmd.waitforterminate = wait.getSelection();
}
});
}
});
}
protected String[] getGOIMGameExecuteCommandToString(GOIMGameExecuteCommands cmd) {
return new String[] { cmd.command, cmd.commandType.toString(), Boolean.toString(cmd.waitforterminate) };
}
private void updateExecuteCommands() {
boolean customize = executeCommandsCustomize.getSelection();
executeCommandsTable.setEnabled(customize);
addCommand.setEnabled(customize);
delCommand.setEnabled(customize);
}
private void updateDefaultCommand() {
String defaultCommand = "";
if(comboGame.getSelectionIndex() > -1) {
GameExtensionProxy proxy = games.get(comboGame.getSelectionIndex());
if(proxy != null) {
IGame delegate = proxy.getDelegate();
if(delegate instanceof GameAdapter) {
defaultCommand = "Default Connect Command: " + proxy.getDefaultConnectCommand();
}
}
}
this.defaultCommand.setText(defaultCommand);
}
}