d4rK 3Mpr0R fr33 Kevin | Code :
- /* This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library 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
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- */
- // Benjamin Michotte <binny@baby-linux.net>
- package jcoincoin;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
- import java.beans.PropertyVetoException;
- import java.beans.VetoableChangeListener;
- import java.beans.beancontext.BeanContextChildSupport;
- import java.beans.beancontext.BeanContextServicesSupport;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.Reader;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.prefs.BackingStoreException;
- import java.util.prefs.NodeChangeEvent;
- import java.util.prefs.NodeChangeListener;
- import java.util.prefs.PreferenceChangeEvent;
- import java.util.prefs.PreferenceChangeListener;
- import java.util.prefs.Preferences;
- import javax.swing.JFrame;
- import javax.swing.SwingUtilities;
- import javax.swing.event.EventListenerList;
- import jcoincoin.gui.JCoinGui;
- import jcoincoin.utils.Options;
- import jcoincoin.utils.TribuneOptions;
- import beantest.BeanBuilder;
- import beantest.BeanPanel;
- import beantest.designer.DesignPanel;
- import beantest.model.ObjectHolder;
- import beantest.property.PropertyPane;
- /**
- * A JCoinCoin is an aggregation of tribunes the user wants to chat on.
- *
- * @author nraynaud
- * @author Benjamin Michotte
- * @version 0.1.0
- */
- public class JCoinCoin extends BeanContextServicesSupport {
- private static String revision = "";
- static {
- revision = getRevisionFromJar();
- if (revision == null) {
- revision = getRevisionFromSystem();
- }
- }
- private static String getRevisionFromJar() {
- try {
- String localFile = JCoinCoin.class.getProtectionDomain()
- .getCodeSource().getLocation().toString();
- localFile = localFile.concat("!/" );
- String tmpString = "jar:";
- String localJarFileString = tmpString.concat(localFile);
- java.net.URL localJarFileURL = new java.net.URL(localJarFileString);
- java.net.JarURLConnection localJarFile = (java.net.JarURLConnection) localJarFileURL
- .openConnection();
- java.util.jar.Manifest mf = localJarFile.getManifest();
- java.util.jar.Attributes attr = mf.getMainAttributes();
- return attr.getValue("Version" );
- } catch (Exception e) {
- return null;
- }
- }
- private static String getRevisionFromSystem() {
- try {
- Reader reader = new InputStreamReader(Runtime.getRuntime().exec(
- "svnversion ." ).getInputStream());
- StringBuffer buff = new StringBuffer();
- for (int c = reader.read(); c != -1; c = reader.read()) {
- char ch = (char) c;
- if (!Character.isISOControl(ch))
- buff.append(ch);
- }
- return buff.toString();
- } catch (IOException e) {
- return "";
- }
- }
- private static final String VERSION = "0.2.2 rev[" + revision + ']';
- transient private Options options;
- transient private final List tribunes = new ArrayList();
- public static final boolean DEBUG = false;
- transient private final EventListenerList listeners = new EventListenerList();
- private String userAgent = "JCoinCoin-Hiroshima (compatible; JC? " + "%v"
- + "; " + System.getProperty("os.name" ) + " )";
- private String cookiesFile = "";
- private transient Preferences preferences;
- private static Class boxed(Class c) {
- String name = c.getName();
- if (c.getName().equals("int" ))
- return Integer.class;
- if (c.getName().equals("boolean" ))
- return Boolean.class;
- if (c.getName().equals("long" ))
- return Long.class;
- if (c.getName().equals("float" ))
- return Float.class;
- if (c.getName().equals("double" ))
- return Double.class;
- if (c.getName().equals("byte" ))
- return Byte.class;
- if (c.getName().equals("char" ))
- return Character.class;
- if (c.getName().equals("short" ))
- return Short.class;
- return c;
- }
- private static boolean isPreferenceField(Field f) {
- int m = f.getModifiers();
- return !Modifier.isTransient(m) && !Modifier.isStatic(m)
- && !Modifier.isFinal(m);
- }
- /**
- * contructs the coincoin around the given preferences.
- *
- * @param prefs
- */
- public JCoinCoin(Preferences prefs) {
- readPreferences(prefs);
- }
- public JCoinCoin() {
- this(Preferences.userNodeForPackage(JCoinCoin.class));
- }
- /**
- * @param readConfig
- * read or not the configuration
- */
- public JCoinCoin(boolean readConfig) {
- if (readConfig)
- readPreferences(Preferences.userNodeForPackage(JCoinCoin.class));
- }
- public void readPreferences(Preferences prefs) {
- this.preferences = prefs;
- Field[] fields = getClass().getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- if (isPreferenceField(field)) {
- String valString = null;
- Object val = null;
- Method m = null;
- try {
- valString = prefs.get(field.getName(), String.valueOf(field
- .get(this)));
- try {
- m = boxed(field.getType()).getMethod("valueOf",
- new Class[] { String.class });
- assert Modifier.isStatic(m.getModifiers());
- val = m.invoke(null, new Object[] { valString });
- } catch (NoSuchMethodException e) {
- val = valString;
- }
- field.set(this, val);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new RuntimeException(e);
- }
- }
- }
- Preferences tribPrefs = prefs.node("tribunes" );
- String[] tribNames;
- try {
- tribNames = tribPrefs.childrenNames();
- } catch (BackingStoreException e) {
- // TODO Auto-generated catch block
- throw new RuntimeException(e);
- }
- for (int i = 0; i < tribNames.length; i++) {
- tribunes.add(new Tribune(tribPrefs.node(tribNames[i]), this));
- }
- tribPrefs.addNodeChangeListener(new NodeChangeListener() {
- public void childAdded(NodeChangeEvent evt) {
- String name = evt.getChild().name();
- // if already here, don't add.
- for (Iterator i = tribunes.iterator(); i.hasNext();) {
- Tribune trib = (Tribune) i.next();
- if (trib.getName().equals(name))
- return;
- }
- Tribune t = new Tribune(evt.getChild(), JCoinCoin.this);
- addTribune(t);
- }
- public void childRemoved(NodeChangeEvent evt) {
- // TODO : tribune removal
- }
- });
- prefs.addPreferenceChangeListener(new PreferenceChangeListener() {
- public void preferenceChange(PreferenceChangeEvent evt) {
- //TODO : implement this
- }
- });
- }
- /**
- * writes the contents into the given preferences node.
- *
- * @param prefs
- */
- public void writePreferences(Preferences prefs) {
- Field[] fields = getClass().getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- if (isPreferenceField(field)) {
- String valString = null;
- Object val = null;
- Method m = null;
- try {
- prefs.put(field.getName(), String.valueOf(field.get(this)));
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- throw new RuntimeException(e);
- }
- }
- }
- Preferences tribPrefs = prefs.node("tribunes" );
- for (Iterator i = tribunes.iterator(); i.hasNext();) {
- Tribune t = (Tribune) i.next();
- t.writePreferences(tribPrefs.node(t.getName()));
- }
- }
- public Tribune getTribune(int i) {
- return (Tribune) tribunes.get(i);
- }
- /**
- * @param name
- * @return
- */
- public Tribune getTribune(String name) {
- for (Iterator i = tribunes.iterator(); i.hasNext();) {
- Tribune t = (Tribune) i.next();
- if (t.getName().equals(name))
- return t;
- }
- return null;
- }
- public int getTribunesCount() {
- return tribunes.size();
- }
- public Tribune createTribune(String name) {
- Tribune t = new Tribune(this);
- addTribune(t);
- try {
- t.setName(name);
- } catch (PropertyVetoException e) {
- return null;
- }
- return t;
- }
- private void addTribune(Tribune t) {
- tribunes.add(t);
- t.addVetoableChangeListener(changeListener);
- t.addPropertyChangeListener(changeListener);
- fireTribuneAdded(t, tribunes.size() - 1);
- }
- public void addJCoinCoinListener(JCoinCoinListener l) {
- listeners.add(JCoinCoinListener.class, l);
- }
- public void removeJCoinCoinListener(JCoinCoinListener l) {
- listeners.remove(JCoinCoinListener.class, l);
- }
- public void fireTribuneAdded(Tribune tribune, int index) {
- Object[] listeners = this.listeners.getListenerList();
- for (int i = listeners.length - 2; i >= 0; i -= 2)
- if (listeners[i] == JCoinCoinListener.class)
- ((JCoinCoinListener) listeners[i + 1]).tribuneAdded(tribune,
- index);
- }
- private transient ChangeListener changeListener = new ChangeListener();
- public void savePreferencesOn(Preferences prefs) {
- }
- private class ChangeListener implements VetoableChangeListener,
- PropertyChangeListener {
- public void vetoableChange(PropertyChangeEvent evt)
- throws PropertyVetoException {
- if ("name".equals(evt.getPropertyName())) {
- String newName = (String) evt.getNewValue();
- for (int i = 0; i < tribunes.size(); i++)
- if (((Tribune) tribunes.get(i)).getName().equals(newName))
- throw new PropertyVetoException(
- "a tribune with this name already exists", evt);
- }
- }
- public void propertyChange(PropertyChangeEvent evt) {
- }
- }
- /**
- * @return
- */
- public String getUserAgent() {
- return userAgent;
- }
- /**
- * @return
- */
- public String getVersion() {
- return VERSION;
- }
- /**
- * @return the printable form of the userAgent ie with %v substitutiobn.
- */
- public String getPrintableUserAgent() {
- int i = userAgent.indexOf("%v" );
- if (i >= 0)
- return userAgent.substring(0, i) + VERSION
- + userAgent.substring(i + 2);
- return userAgent;
- }
- /**
- *
- */
- public void writePreferences() {
- writePreferences(preferences);
- }
- }
|
(c'est moinsmoins qui m'a dit de le faire) |