Package net.sphene.goim.serverbrowser

Source Code of net.sphene.goim.serverbrowser.ServerBrowserPlugin

/*
* File    : ServerBrowserPlugin.java
* Created : 25.02.2006
* By      : kahless
*
* GOIM - Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul
*              (JabberId: kahless@sphene.net / Email: herbert.poul@gmail.com)
* 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.serverbrowser;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

import net.sphene.goim.rcp.beans.IGOIMPreferenceObject;
import net.sphene.libs.SpheneEvent;
import net.sphene.libs.SpheneListener;

import org.eclipse.ui.plugin.*;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.jivesoftware.smack.util.StringUtils;
import org.osgi.framework.BundleContext;

/**
* The main plugin class to be used in the desktop.
*/
public class ServerBrowserPlugin extends AbstractUIPlugin {

  //The shared instance.
  private static ServerBrowserPlugin plugin;
  private static Map<String,IGOIMPreferenceObject> parsedPreferenceObjects = new HashMap<String,IGOIMPreferenceObject>();
 
  /**
   * The constructor.
   */
  public ServerBrowserPlugin() {
    plugin = this;
  }

  /**
   * This method is called upon plug-in activation
   */
  public void start(BundleContext context) throws Exception {
    super.start(context);
  }

  /**
   * This method is called when the plug-in is stopped
   */
  public void stop(BundleContext context) throws Exception {
    super.stop(context);
    plugin = null;
  }

  /**
   * Returns the shared instance.
   */
  public static ServerBrowserPlugin getDefault() {
    return plugin;
  }

  /**
   * Returns an image descriptor for the image file at the given
   * plug-in relative path.
   *
   * @param path the path
   * @return the image descriptor
   */
  public static ImageDescriptor getImageDescriptor(String path) {
    return AbstractUIPlugin.imageDescriptorFromPlugin("net.sphene.goim.serverbrowser", path);
  }
 
 
 
 
 
 
 
 
  /**
   * Copied from GOIMPlugin.java
   */
  @SuppressWarnings("unchecked")
  public static synchronized <T extends IGOIMPreferenceObject> T getPreferenceObject(Class<T> cls) {
    String key = cls.getName();
    IGOIMPreferenceObject obj = parsedPreferenceObjects.get(key);
    if(obj != null) return (T)obj;
    IPreferenceStore preferenceStore = getDefault().getPreferenceStore();
    String str = preferenceStore.getString(key);
    if(!str.equals("")) {
      byte[] bytes = StringUtils.decodeBase64(str);
      ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
      try {
        ObjectInputStream in = new ObjectInputStream(inputStream);
        Object bobj = in.readObject();
        in.close();
        if(bobj instanceof IGOIMPreferenceObject) obj = (IGOIMPreferenceObject)bobj;
      } catch (IOException e) {
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }
    if(obj == null) {
      try {
        obj = cls.newInstance();
      } catch (InstantiationException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }
    parsedPreferenceObjects.put(key,obj);
    final Class saveCls = cls;
    obj.addChangeListener(new SpheneListener<SpheneEvent>(){
      public void handleEvent(SpheneEvent event) {
        //saveAccountList();
        savePreferenceObject(saveCls);
      }});
    return (T)obj;
  }
  public static synchronized void savePreferenceObject(Class<? extends IGOIMPreferenceObject> cls) {
    String key = cls.getName();
    IGOIMPreferenceObject obj = parsedPreferenceObjects.get(key);
    if(obj == null) return;
    IPreferenceStore preferenceStore = getDefault().getPreferenceStore();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      ObjectOutputStream out = new ObjectOutputStream(outputStream);
      out.writeObject(obj);
      out.flush();
      out.close();
      outputStream.close();
      String str = StringUtils.encodeBase64(outputStream.toByteArray());
      preferenceStore.setValue(key,str);
      System.out.println("Saved key: " + key + "  value: " + str);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}
TOP

Related Classes of net.sphene.goim.serverbrowser.ServerBrowserPlugin

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.