Example #1
0
 /** Overrides superclass method. Initializes editor component. */
 protected void initializeCloneableEditor(CloneableEditor editor) {
   DataObject obj = getDataObject();
   if (obj.isValid()) {
     org.openide.nodes.Node ourNode = obj.getNodeDelegate();
     editor.setActivatedNodes(new org.openide.nodes.Node[] {ourNode});
     editor.setIcon(ourNode.getIcon(java.beans.BeanInfo.ICON_COLOR_16x16));
     NodeListener nl = new DataNodeListener(editor);
     ourNode.addNodeListener(WeakListener.node(nl, ourNode));
     nodeL = nl;
   }
 }
/**
 * Implements children for basic source code patterns
 *
 * @author Petr Hrebejk, Jan Jancura
 */
public class PatternChildren extends ClassChildren {

  private boolean wri = true;

  private MethodElementListener methodListener = new MethodElementListener();
  private FieldElementListener fieldListener = new FieldElementListener();

  private PropertyChangeListener weakMethodListener =
      WeakListener.propertyChange(methodListener, null);
  // = new WeakListener.PropertyChange( methodListener );
  private PropertyChangeListener weakFieldListener =
      WeakListener.propertyChange(fieldListener, null);;
  // = new WeakListener.PropertyChange( fieldListener );

  static {
    Integer i =
        new Integer(
            PatternFilter.METHOD
                | PatternFilter.PROPERTY
                | PatternFilter.IDXPROPERTY
                | PatternFilter.EVENT_SET);
    propToFilter.put(ElementProperties.PROP_METHODS, i);
    i =
        new Integer(
            PatternFilter.FIELD
                | PatternFilter.PROPERTY
                | PatternFilter.IDXPROPERTY
                | PatternFilter.EVENT_SET);
    propToFilter.put(ElementProperties.PROP_FIELDS, i);
  }

  /** Object for finding patterns in class */
  private PatternAnalyser patternAnalyser;

  // Constructors -----------------------------------------------------------------------

  /**
   * Create pattern children. The children are initilay unfiltered.
   *
   * @param elemrent the atteached class. For this class we recognize the patterns
   */
  public PatternChildren(ClassElement classElement) {
    super(classElement);
    patternAnalyser = new PatternAnalyser(classElement);
  }

  public PatternChildren(ClassElement classElement, boolean isWritable) {
    this(classElement);
    wri = isWritable;
  }

  /**
   * Create pattern children. The children are initilay unfiltered.
   *
   * @param elemrent the atteached class. For this class we recognize the patterns
   */
  public PatternChildren(ElementNodeFactory factory, ClassElement classElement) {
    super(factory, classElement);
    patternAnalyser = new PatternAnalyser(classElement);
  }

  public PatternChildren(
      ElementNodeFactory factory, ClassElement classElement, boolean isWritable) {
    this(factory, classElement);
    wri = isWritable;
  }

  /*
      PatternChildren cloneChildren() {
        return.clone();
        System.out.println ( "CLONING CHILDREN" );
        return new PatternChildren( patternAnalyser.getClassElement() );
  }
      */

  // FilterCookie implementation --------------------------------------------------------

  /** Updates all the keys (elements) according to the current filter & ordering. */
  protected void refreshAllKeys() {
    cpl = new Collection[getOrder().length];
    refreshKeys(PatternFilter.ALL);
  }

  /** Updates all the keys with given filter. Overriden to provide package access tothis method. */
  protected void refreshKeys(int filter) {

    // Method is added or removed ve have to re-analyze the pattern abd to
    // registrate Children as listener

    reassignMethodListener();
    reassignFieldListener();
    patternAnalyser.analyzeAll();
    super.refreshKeys(filter);
    // Thread.dumpStack();
  }

  /**
   * @return The class of currently associated filter or null if no filter is associated with these
   *     children
   */
  public Class getFilterClass() {
    return PatternFilter.class;
  }

  /** Gets the pattern analyser which manages the patterns */
  PatternAnalyser getPatternAnalyser() {
    return patternAnalyser;
  }

  // Children.keys implementation -------------------------------------------------------

  /** Creates node for given key. */
  protected Node[] createNodes(Object key) {

    if (key instanceof IdxPropertyPattern)
      return new Node[] {new IdxPropertyPatternNode((IdxPropertyPattern) key, wri)};
    if (key instanceof PropertyPattern)
      return new Node[] {new PropertyPatternNode((PropertyPattern) key, wri)};
    if (key instanceof EventSetPattern)
      return new Node[] {new EventSetPatternNode((EventSetPattern) key, wri)};

    // Unknown pattern
    return super.createNodes(key);
  }

  // Utility methods --------------------------------------------------------------------

  protected Collection getKeysOfType(int elementType) {

    LinkedList keys = (LinkedList) super.getKeysOfType(elementType);
    LinkedList temp = null;

    if ((elementType & PatternFilter.PROPERTY) != 0) {
      temp = new LinkedList();
      temp.addAll(patternAnalyser.getPropertyPatterns());
      Collections.sort(temp, new PatternComparator());
      keys.addAll(temp);
      // keys.addAll( patternAnalyser.getPropertyPatterns() );
    }
    if ((elementType & PatternFilter.IDXPROPERTY) != 0) {
      temp = new LinkedList();
      temp.addAll(patternAnalyser.getIdxPropertyPatterns());
      Collections.sort(temp, new PatternComparator());
      keys.addAll(temp);
      // keys.addAll( patternAnalyser.getIdxPropertyPatterns() );
    }
    if ((elementType & PatternFilter.EVENT_SET) != 0) {
      temp = new LinkedList();
      temp.addAll(patternAnalyser.getEventSetPatterns());
      Collections.sort(temp, new PatternComparator());
      keys.addAll(temp);
      // keys.addAll( patternAnalyser.getEventSetPatterns() );
    }

    //    if ((filter == null) || filter.isSorted ())
    //      Collections.sort (keys, comparator);
    return keys;
  }

  /** Method for removing method listener */
  private void reassignMethodListener() {
    MethodElement[] methods = element.getMethods();
    for (int i = 0; i < methods.length; i++) {
      methods[i].removePropertyChangeListener(weakMethodListener);
      methods[i].addPropertyChangeListener(weakMethodListener);
    }
  }

  /** Method for removing field listener */
  private void reassignFieldListener() {
    FieldElement[] fields = element.getFields();
    for (int i = 0; i < fields.length; i++) {
      fields[i].removePropertyChangeListener(weakFieldListener);
      fields[i].addPropertyChangeListener(weakFieldListener);
    }
  }

  // Inner classes ----------------------------------------------------------------------

  /** The listener of method changes temporary used in PatternAnalyser to track changes in */
  final class MethodElementListener implements PropertyChangeListener {
    public void propertyChange(PropertyChangeEvent e) {
      refreshKeys(PatternFilter.ALL);
      // patternAnalyser.analyzeAll();
    }
  }

  /** The listener of method changes temporary used in PatternAnalyser to track changes in */
  final class FieldElementListener implements PropertyChangeListener {
    public void propertyChange(PropertyChangeEvent e) {
      refreshKeys(PatternFilter.ALL);
      // reassignFieldListener();
      // patternAnalyser.resolveFields();
    }
  }

  private static final class PatternComparator implements Comparator {

    public int compare(Object a, Object b) {
      return ((Pattern) a).getName().compareTo(((Pattern) b).getName());
    }

    public boolean equals(Object c) {
      return c instanceof PatternComparator;
    }
  }
}