⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fingerprintconfigcontentprovider.java

📁 dump3 morpheus 0.2.9 src
💻 JAVA
字号:
/**
 * DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
 * Copyright 2005 Alexander Gr&auml;sser<BR>
 * All Rights Reserved, http://dump3.sourceforge.net/<BR>
 * <BR>
 * This file is part of DuMP3.<BR>
 * <BR>
 * DuMP3 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.<BR>
 * <BR>
 * DuMP3 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.<BR>
 * <BR>
 * You should have received a copy of the GNU General Public License along with DuMP3; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.za.grasser.duplicate.gui;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.za.grasser.duplicate.fingerprint.AbstractFingerprint;
import net.za.grasser.duplicate.fingerprint.configure.ConfigFactory;
import net.za.grasser.duplicate.fingerprint.configure.FingerprintConfig;
import net.za.grasser.duplicate.gui.beans.ConfigProperty;
import net.za.grasser.duplicate.gui.beans.ConfigPropertyComparator;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 * This class ...
 * 
 * @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
 * @version $Revision: 1.12 $
 */
public class FingerprintConfigContentProvider implements IStructuredContentProvider {
  /**
   * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
   */
  @SuppressWarnings("unchecked")
  public Object[] getElements(final Object element) {
    if (element == null) {
      return new Object[0];
    }
    String config = null;
    if (element instanceof Class) {
      config = ConfigFactory.getConfig((Class< ? extends AbstractFingerprint>)element).getClass().getName();
    }
    if (element instanceof FingerprintConfig) {
      config = ((FingerprintConfig)element).getClass().getName();
    }
    if (config == null) {
      return new Object[0];
    }
    final String beanInfo = config + "BeanInfo";
    try {
      final Class< ? extends FingerprintConfig> fc = (Class< ? extends FingerprintConfig>)Class.forName(config);
      final Class< ? extends SimpleBeanInfo> c = (Class< ? extends SimpleBeanInfo>)Class.forName(beanInfo);
      final SimpleBeanInfo info = c.newInstance();
      final BeanInfo[] more = info.getAdditionalBeanInfo();
      final List<ConfigProperty> full = new ArrayList<ConfigProperty>();
      for (final PropertyDescriptor lDescriptor : info.getPropertyDescriptors()) {
        full.add(new ConfigProperty(lDescriptor, fc));
      }
      for (final BeanInfo bi : more) {
        for (final PropertyDescriptor lDescriptor : bi.getPropertyDescriptors()) {
          full.add(new ConfigProperty(lDescriptor, fc));
        }
      }
      Collections.sort(full, new ConfigPropertyComparator());
      return full.toArray();
    } catch (final ClassNotFoundException e) {
      // this is the case where the BeanInfo class does not exist
      try {
        final Class< ? extends FingerprintConfig> fc = (Class< ? extends FingerprintConfig>)Class.forName(config);
        final Map<String, Method> getters = new HashMap<String, Method>();
        final Map<String, Method> setters = new HashMap<String, Method>();
        for (final Method lMethod : fc.getMethods()) {
          if (lMethod.getName().startsWith("get") || lMethod.getName().startsWith("is") || lMethod.getName().startsWith("has")) {
            getters.put(lMethod.getName(), lMethod);
          }
          if (lMethod.getName().startsWith("set")) {
            setters.put(lMethod.getName(), lMethod);
          }
        }
        final List<ConfigProperty> full = new ArrayList<ConfigProperty>();
        for (final String lKey : getters.keySet()) {
          String prop = lKey;
          if (prop.startsWith("get") || prop.startsWith("has")) {
            prop = prop.substring(3);
          } else {
            // if (prop.startsWith("is"))
            prop = prop.substring(2);
          }
          PropertyDescriptor md = null;
          // check for bean property
          if (setters.containsKey("set" + prop)) {
            md = new PropertyDescriptor(prop.substring(0, 1).toLowerCase() + prop.substring(1), fc);
            md.setBound(true);
            md.setConstrained(true);
          }
          // check for list contrained property
          if (getters.containsKey(lKey + 's') && md != null) {
            // found a list
            final Method ref = fc.getMethod("getInstance", new Class[]{});
            final FingerprintConfig conf = (FingerprintConfig)ref.invoke(null, new Object[]{});
            final Method m = getters.get(lKey + 's');
            md.setValue("enumerationValues", m.invoke(conf, new Object[]{}));
          }
          if (md != null) {
            full.add(new ConfigProperty(md, fc));
          }
        }
        Collections.sort(full, new ConfigPropertyComparator());
        return full.toArray();
      } catch (final ClassNotFoundException cnf) {
        cnf.printStackTrace();
      } catch (final IntrospectionException ex) {
        ex.printStackTrace();
      } catch (final NoSuchMethodException nsm) {
        nsm.printStackTrace();
      } catch (final IllegalAccessException ia) {
        ia.printStackTrace();
      } catch (final InvocationTargetException ite) {
        ite.printStackTrace();
      }
    } catch (final InstantiationException e) {
      e.printStackTrace();
    } catch (final IllegalAccessException e) {
      e.printStackTrace();
    } catch (final Throwable t) {
      t.printStackTrace();
    }
    return new Object[0];
  }

  /**
   * @see org.eclipse.jface.viewers.IContentProvider#dispose()
   */
  public void dispose() {
    // TODO Auto-generated method stub
  }

  /**
   * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
   */
  public void inputChanged(final Viewer pArg0, final Object pArg1, final Object pArg2) {
    // TODO Auto-generated method stub
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -