Package net.sphene.goim.rcp.ui.preferences

Source Code of net.sphene.goim.rcp.ui.preferences.GOIMPreferenceGameGUI

/*
* 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 net.sphene.goim.rcp.GOIMPlugin;
import net.sphene.goim.rcp.beans.GOIMAbstractListEvent;
import net.sphene.goim.rcp.beans.GOIMGameItem;
import net.sphene.goim.rcp.beans.GOIMGameList;
import net.sphene.goim.rcp.extensionpoints.GameExtensionPoint.GameExtensionProxy;
import net.sphene.goim.rcp.extensionpoints.game.GameUtils;
import net.sphene.goim.rcp.preferences.PreferenceConstants;
import net.sphene.goim.rcp.ui.actions.GOIMActionHelper;
import net.sphene.libs.SpheneEvent;
import net.sphene.libs.SpheneListener;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
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.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class GOIMPreferenceGameGUI extends Composite {
  private Group groupGames = null;
  private Table gamesTable = null;

  //private List<GameExtensionProxy> games;

  private GOIMGameList gameList;

  private SpheneListener<SpheneEvent> changeListener;
  private Composite compositeButtons = null;
  private Button buttonCreate = null;
  private Button buttonEdit = null;
  private Button buttonRemove = null;
  private Button buttonAutoDetect = null;
  private Text txtAfter;
  private Text txtBefore;

  public GOIMPreferenceGameGUI(Composite parent, int style) {
    super(parent, style);
    gameList = GOIMPlugin.getPreferenceObject(GOIMGameList.class);
    initialize();
    fillWithData();
  }

  public void fillWithData() {
    //games = GameExtensionPoint.getGameExtensions();
   
    for(GOIMGameItem item : gameList) {
      createTableItem(item);
    }
    packGameTableCols();
    gameList.addChangeListener(changeListener = new SpheneListener<SpheneEvent>() {
      public void handleEvent(SpheneEvent sevent) {
        GOIMAbstractListEvent event = (GOIMAbstractListEvent)sevent;
        if(event.type == GOIMAbstractListEvent.TYPE_ADD) {
          createTableItem((GOIMGameItem)event.source);
          gamesTable.setSelection(gamesTable.getItemCount()-1);
          packGameTableCols();
        } else {
          TableItem item = null;
          for(TableItem tmpitem : gamesTable.getItems()) {
            if(tmpitem.getData() == event.source)
              item = tmpitem;
          }
          if(item == null) return;
          if(event.type == GOIMAbstractListEvent.TYPE_DEL) {
            item.dispose();
          } else if(event.type == GOIMAbstractListEvent.TYPE_EDIT) {
            updateTableItem((GOIMGameItem)item.getData(),item);
            packGameTableCols();
          }
        }
      } });
    IPreferenceStore store = GOIMPlugin.getDefault().getPreferenceStore();
    txtBefore.setText(store.getString(PreferenceConstants.P_GAME_EXECUTE_COMMAND_BEFORE));
    txtAfter.setText(store.getString(PreferenceConstants.P_GAME_EXECUTE_COMMAND_AFTER));
    addListener(SWT.Dispose,new Listener(){
      public void handleEvent(Event event) {
        gameList.removeChangeListener(changeListener);
      }});
  }
  private void createTableItem(GOIMGameItem item) {
    TableItem tableItem = new TableItem(gamesTable, SWT.NULL);
    updateTableItem(item,tableItem);
  }
  private void updateTableItem(GOIMGameItem item, TableItem tableItem) {
    GameExtensionProxy proxy = item.retrieveExtensionProxy(true);
    if(proxy == null) { tableItem.dispose(); return; }
    tableItem.setImage(proxy.getIcon().createImage());
    tableItem.setText(new String[] { "",proxy.name, item.version, item.path });
//    TableEditor editor = (TableEditor)tableItem.getData("editor");
//    if(editor == null) {
//      editor = new TableEditor(gamesTable);
//      editor.minimumWidth = 16;
//      editor.minimumHeight = 16;
//      Label icon = new Label(gamesTable,SWT.NONE);
//      icon.setImage(proxy.getIcon().createImage());
//      editor.setColumn(0);
//      editor.setItem(tableItem);
//      editor.setEditor(icon);
//      //editor.setEditor(icon,tableItem,1);
//      tableItem.setData("editor",editor);
//      icon.setSize(16,16);
//      icon.setFocus();
//      System.out.println("Created editor...");
//    } else {
//      Label icon = (Label)editor.getEditor();
//      icon.getImage().dispose();
//      icon.setImage(proxy.getIcon().createImage());
//    }
    tableItem.setData(item);
  }
  private void packGameTableCols() {
    TableColumn[] cols = gamesTable.getColumns();
    cols[1].pack();
    cols[2].pack();
    cols[3].pack();
  }

  private void initialize() {
    this.setLayout(new GridLayout());
    createGroupGames();
    setSize(new org.eclipse.swt.graphics.Point(300, 321));
  }

  /**
   * This method initializes groupGames
   *
   */
  private void createGroupGames() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 2;
    GridData gridData = new org.eclipse.swt.layout.GridData();
    gridData.grabExcessHorizontalSpace = true;
    gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData.grabExcessVerticalSpace = true;
    groupGames = new Group(this, SWT.NONE);
    groupGames.setText("Games");
    groupGames.setLayoutData(gridData);
    groupGames.setLayout(gridLayout);
    createExecuteCommands();
    createGamesTable();
    createCompositeButtons();
  }

  private void createExecuteCommands() {
    Group executeCommands = new Group(groupGames, SWT.NONE);
    executeCommands.setText("Execute Commands");
    executeCommands.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false,2,1));
    executeCommands.setLayout(new GridLayout(2,false));
   
    Link linkToHelp = new Link(executeCommands, SWT.NONE);
    GridData linkToHelpData = new GridData(SWT.FILL,SWT.CENTER,true,false,2,1);
    linkToHelpData.widthHint = 300;
    linkToHelp.setLayoutData(linkToHelpData);
    linkToHelp.setText("This options allow you to specify command executed before/after a game is launched. See <A>GOIM Help</A> for more information (like variable names to be used) - See 'ExecuteCommands'");
   
    linkToHelp.addListener(SWT.Selection,new Listener() {
      public void handleEvent(Event event) {
        GOIMActionHelper.openBrowser(GOIMPlugin.goimSiteURLhelp);
      }
    });
   
    Label lblBefore = new Label(executeCommands, SWT.NONE);
    lblBefore.setText("Execute Before Game Launch: ");
   
    txtBefore = new Text(executeCommands, SWT.BORDER);
    txtBefore.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false));
    txtBefore.addListener(SWT.Modify,new Listener() {
      public void handleEvent(Event event) {
        IPreferenceStore store = GOIMPlugin.getDefault().getPreferenceStore();
        store.setValue(PreferenceConstants.P_GAME_EXECUTE_COMMAND_BEFORE,txtBefore.getText());
      }
    });
   
   
    Label lblAfter = new Label(executeCommands, SWT.NONE);
    lblAfter.setText("Execute After Game Terminates:");
   
    txtAfter = new Text(executeCommands, SWT.BORDER);
    txtAfter.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,false));
    txtAfter.addListener(SWT.Modify,new Listener() {
      public void handleEvent(Event event) {
        IPreferenceStore store = GOIMPlugin.getDefault().getPreferenceStore();
        store.setValue(PreferenceConstants.P_GAME_EXECUTE_COMMAND_AFTER,txtAfter.getText());
      }
    });
  }
  /**
   * This method initializes gamesTable
   *
   */
  private void createGamesTable() {
    GridData gridData1 = new org.eclipse.swt.layout.GridData();
    gridData1.grabExcessHorizontalSpace = true;
    gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData1.grabExcessVerticalSpace = true;
    gamesTable = new Table(groupGames, SWT.FULL_SELECTION);
    gamesTable.setHeaderVisible(true);
    gamesTable.setLayoutData(gridData1);
    gamesTable.setLinesVisible(true);
//    gamesTable
    TableColumn tableColumnGameIcon = new TableColumn(gamesTable, SWT.NONE);
    tableColumnGameIcon.setWidth(20);
//        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
//          public void widgetSelected(
//              org.eclipse.swt.events.SelectionEvent e) {
//            updateEditGame();
//          }
//        });
    TableColumn tableColumnGame = new TableColumn(gamesTable, SWT.NONE);
    tableColumnGame.setWidth(60);
    tableColumnGame.setText("Game");
    TableColumn tableColumnVersion = new TableColumn(gamesTable, SWT.NONE);
    tableColumnVersion.setWidth(60);
    tableColumnVersion.setText("Version");
    TableColumn tableColumnPath = new TableColumn(gamesTable, SWT.NONE);
    tableColumnPath.setWidth(60);
    tableColumnPath.setText("Path");
  }

  /**
   * This method initializes compositeButtons 
   *
   */
  private void createCompositeButtons() {
    GridData gridData2 = new GridData();
    gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.BEGINNING;
    RowLayout rowLayout = new RowLayout();
    rowLayout.type = org.eclipse.swt.SWT.VERTICAL;
    rowLayout.fill = true;
    compositeButtons = new Composite(groupGames, SWT.NONE);
    compositeButtons.setLayout(rowLayout);
    compositeButtons.setLayoutData(gridData2);
    buttonCreate = new Button(compositeButtons, SWT.NONE);
    buttonCreate.setText("Add Game");
    buttonCreate
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
          public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            GOIMPreferenceGameGUIEditGame.createNewShell(getShell(),(GOIMGameItem)null,gameList);
          }
        });
    buttonEdit = new Button(compositeButtons, SWT.NONE);
    buttonEdit.setText("Edit Game");
    buttonEdit.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
      public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
        GOIMGameItem gameItem = getCurrentSelectedGameItem();
        if(gameItem == null) return;
        GOIMPreferenceGameGUIEditGame.createNewShell(getShell(),gameItem,gameList);
      }
    });
    buttonRemove = new Button(compositeButtons, SWT.NONE);
    buttonRemove.setText("Remove Game");
    buttonAutoDetect = new Button(compositeButtons, SWT.NONE);
    buttonAutoDetect.setText("Autodetect");
    buttonAutoDetect
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
          public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            GameUtils.autodetectGames(gameList);
          }
        });
    buttonRemove
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
          public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
            GOIMGameItem gameItem = getCurrentSelectedGameItem();
            if(gameItem == null) return;
            gameList.remove(gameItem);
          }
        });
  }
 
  public GOIMGameItem getCurrentSelectedGameItem() {
    TableItem[] items = gamesTable.getSelection();
    if(items == null || items.length < 1) return null;
    TableItem tableItem = items[0];
    return (GOIMGameItem)tableItem.getData();
  }

} // @jve:decl-index=0:visual-constraint="10,10"
TOP

Related Classes of net.sphene.goim.rcp.ui.preferences.GOIMPreferenceGameGUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.