示例#1
0
 private VirtualFileSystemEntry applyEvent(@NotNull VFileEvent event) {
   try {
     if (event instanceof VFileCreateEvent) {
       final VFileCreateEvent createEvent = (VFileCreateEvent) event;
       return executeCreateChild(createEvent.getParent(), createEvent.getChildName());
     } else if (event instanceof VFileDeleteEvent) {
       final VFileDeleteEvent deleteEvent = (VFileDeleteEvent) event;
       executeDelete(deleteEvent.getFile());
     } else if (event instanceof VFileContentChangeEvent) {
       final VFileContentChangeEvent contentUpdateEvent = (VFileContentChangeEvent) event;
       executeTouch(
           contentUpdateEvent.getFile(),
           contentUpdateEvent.isFromRefresh(),
           contentUpdateEvent.getModificationStamp());
     } else if (event instanceof VFileCopyEvent) {
       final VFileCopyEvent copyEvent = (VFileCopyEvent) event;
       return executeCreateChild(copyEvent.getNewParent(), copyEvent.getNewChildName());
     } else if (event instanceof VFileMoveEvent) {
       final VFileMoveEvent moveEvent = (VFileMoveEvent) event;
       executeMove(moveEvent.getFile(), moveEvent.getNewParent());
     } else if (event instanceof VFilePropertyChangeEvent) {
       final VFilePropertyChangeEvent propertyChangeEvent = (VFilePropertyChangeEvent) event;
       if (VirtualFile.PROP_NAME.equals(propertyChangeEvent.getPropertyName())) {
         executeRename(propertyChangeEvent.getFile(), (String) propertyChangeEvent.getNewValue());
       } else if (VirtualFile.PROP_WRITABLE.equals(propertyChangeEvent.getPropertyName())) {
         executeSetWritable(
             propertyChangeEvent.getFile(),
             ((Boolean) propertyChangeEvent.getNewValue()).booleanValue());
       } else if (VirtualFile.PROP_HIDDEN.equals(propertyChangeEvent.getPropertyName())) {
         executeSetHidden(
             propertyChangeEvent.getFile(),
             ((Boolean) propertyChangeEvent.getNewValue()).booleanValue());
       } else if (VirtualFile.PROP_SYMLINK_TARGET.equals(propertyChangeEvent.getPropertyName())) {
         executeSetTarget(
             propertyChangeEvent.getFile(), (String) propertyChangeEvent.getNewValue());
       }
     }
   } catch (Exception e) {
     // Exception applying single event should not prevent other events from applying.
     LOG.error(e);
   }
   return null;
 }
  @Override
  public void before(@NotNull final List<? extends VFileEvent> events) {
    List<FilePointerPartNode> toFireEvents = new ArrayList<FilePointerPartNode>();
    List<FilePointerPartNode> toUpdateUrl = new ArrayList<FilePointerPartNode>();
    VirtualFilePointer[] toFirePointers;

    synchronized (this) {
      incModificationCount();
      for (VFileEvent event : events) {
        if (event instanceof VFileDeleteEvent) {
          final VFileDeleteEvent deleteEvent = (VFileDeleteEvent) event;
          addPointersUnder(deleteEvent.getFile(), false, "", toFireEvents);

        } else if (event instanceof VFileCreateEvent) {
          final VFileCreateEvent createEvent = (VFileCreateEvent) event;
          addPointersUnder(createEvent.getParent(), true, createEvent.getChildName(), toFireEvents);
        } else if (event instanceof VFileCopyEvent) {
          final VFileCopyEvent copyEvent = (VFileCopyEvent) event;
          addPointersUnder(
              copyEvent.getNewParent(), true, copyEvent.getFile().getName(), toFireEvents);
        } else if (event instanceof VFileMoveEvent) {
          final VFileMoveEvent moveEvent = (VFileMoveEvent) event;
          VirtualFile eventFile = moveEvent.getFile();
          addPointersUnder(moveEvent.getNewParent(), true, eventFile.getName(), toFireEvents);

          List<FilePointerPartNode> nodes = new ArrayList<FilePointerPartNode>();
          addPointersUnder(eventFile, false, "", nodes);
          for (FilePointerPartNode pair : nodes) {
            VirtualFile file = pair.leaf.getFile();
            if (file != null) {
              toUpdateUrl.add(pair);
            }
          }
        } else if (event instanceof VFilePropertyChangeEvent) {
          final VFilePropertyChangeEvent change = (VFilePropertyChangeEvent) event;
          if (VirtualFile.PROP_NAME.equals(change.getPropertyName())) {
            VirtualFile eventFile = change.getFile();
            VirtualFile parent = eventFile.getParent(); // e.g. for LightVirtualFiles
            addPointersUnder(parent, true, change.getNewValue().toString(), toFireEvents);

            List<FilePointerPartNode> nodes = new ArrayList<FilePointerPartNode>();
            addPointersUnder(eventFile, false, "", nodes);
            for (FilePointerPartNode pair : nodes) {
              VirtualFile file = pair.leaf.getFile();
              if (file != null) {
                toUpdateUrl.add(pair);
              }
            }
          }
        }
      }

      myEvents = new ArrayList<EventDescriptor>();
      toFirePointers = toPointers(toFireEvents);
      for (final VirtualFilePointerListener listener : myPointers.keySet()) {
        if (listener == null) continue;
        List<VirtualFilePointer> filtered =
            ContainerUtil.filter(
                toFirePointers,
                new Condition<VirtualFilePointer>() {
                  @Override
                  public boolean value(VirtualFilePointer pointer) {
                    return ((VirtualFilePointerImpl) pointer).getListener() == listener;
                  }
                });
        if (!filtered.isEmpty()) {
          EventDescriptor event =
              new EventDescriptor(
                  listener, filtered.toArray(new VirtualFilePointer[filtered.size()]));
          myEvents.add(event);
        }
      }
    }

    for (EventDescriptor descriptor : myEvents) {
      descriptor.fireBefore();
    }

    if (!toFireEvents.isEmpty()) {
      myBus.syncPublisher(VirtualFilePointerListener.TOPIC).beforeValidityChanged(toFirePointers);
    }

    myPointersToFire = toFireEvents;
    myPointersToUpdateUrl = toUpdateUrl;
  }