Java Code Examples for com.intellij.openapi.fileEditor.FileEditorProvider

The following examples show how to use com.intellij.openapi.fileEditor.FileEditorProvider. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void testCsvTableEditorProviderIsAvailableAndHasCorrectNameAndPolicy() {
    FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(getProject(), myFixture.getFile().getVirtualFile());
    assertEquals(2, fileEditorProviders.length);
    assertInstanceOf(fileEditorProviders[1], CsvTableEditorProvider.class);

    FileEditorProvider fileEditorProvider = fileEditorProviders[1];
    assertEquals(CsvTableEditorProvider.EDITOR_TYPE_ID, fileEditorProvider.getEditorTypeId());
    assertEquals(FileEditorPolicy.PLACE_AFTER_DEFAULT_EDITOR, fileEditorProvider.getPolicy());
    assertEquals(true, fileEditorProvider.accept(getProject(), myFixture.getFile().getVirtualFile()));

    CsvEditorSettings csvEditorSettings = CsvEditorSettings.getInstance();
    csvEditorSettings.setEditorPrio(CsvEditorSettings.EditorPrio.TEXT_ONLY);
    assertEquals(FileEditorPolicy.PLACE_AFTER_DEFAULT_EDITOR, fileEditorProvider.getPolicy());
    assertEquals(false, fileEditorProvider.accept(getProject(), myFixture.getFile().getVirtualFile()));

    csvEditorSettings.setEditorPrio(CsvEditorSettings.EditorPrio.TABLE_FIRST);
    assertEquals(FileEditorPolicy.HIDE_DEFAULT_EDITOR, fileEditorProvider.getPolicy());
    assertEquals(true, fileEditorProvider.accept(getProject(), myFixture.getFile().getVirtualFile()));
}
 
Example 2
public void testWriteAndReadTableEditorState() {
    FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(getProject(), myFixture.getFile().getVirtualFile());
    FileEditorProvider fileEditorProvider = fileEditorProviders[1];

    CsvTableEditorState editorState = new CsvTableEditorState();
    editorState.setColumnWidths(new int[]{ 120, 32, 9});
    editorState.setRowLines(5);
    editorState.setShowInfoPanel(false);

    Element element = new Element("state");
    fileEditorProvider.writeState(editorState, getProject(), element);

    FileEditorState readState = fileEditorProvider.readState(element, getProject(), myFixture.getFile().getVirtualFile());

    assertInstanceOf(readState, CsvTableEditorState.class);

    CsvTableEditorState editorStateRead = (CsvTableEditorState)readState;
    assertTrue(Objects.deepEquals(editorState.getColumnWidths(), editorStateRead.getColumnWidths()));
    assertEquals(editorState.getRowLines(), editorStateRead.getRowLines());
    assertEquals(editorState.showInfoPanel(), editorStateRead.showInfoPanel());
}
 
Example 3
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static HistoryEntry createHeavy(@Nonnull Project project,
                                       @Nonnull VirtualFile file,
                                       @Nonnull FileEditorProvider[] providers,
                                       @Nonnull FileEditorState[] states,
                                       @Nonnull FileEditorProvider selectedProvider) {
  if (project.isDisposed()) return createLight(file, providers, states, selectedProvider);

  Disposable disposable = Disposable.newDisposable();
  VirtualFilePointer pointer = VirtualFilePointerManager.getInstance().create(file, disposable, null);

  HistoryEntry entry = new HistoryEntry(pointer, selectedProvider, disposable);
  for (int i = 0; i < providers.length; i++) {
    FileEditorProvider provider = providers[i];
    FileEditorState state = states[i];
    if (provider != null && state != null) {
      entry.putState(provider, state);
    }
  }
  return entry;
}
 
Example 4
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return element that was added to the <code>element</code>.
 * Returned element has tag {@link #TAG}. Never null.
 */
public Element writeExternal(Element element, Project project) {
  Element e = new Element(TAG);
  element.addContent(e);
  e.setAttribute(FILE_ATTR, myFilePointer.getUrl());

  for (final Map.Entry<FileEditorProvider, FileEditorState> entry : myProvider2State.entrySet()) {
    FileEditorProvider provider = entry.getKey();

    Element providerElement = new Element(PROVIDER_ELEMENT);
    if (provider.equals(mySelectedProvider)) {
      providerElement.setAttribute(SELECTED_ATTR_VALUE, Boolean.TRUE.toString());
    }
    providerElement.setAttribute(EDITOR_TYPE_ID_ATTR, provider.getEditorTypeId());
    Element stateElement = new Element(STATE_ELEMENT);
    providerElement.addContent(stateElement);
    provider.writeState(entry.getValue(), project, stateElement);

    e.addContent(providerElement);
  }

  return e;
}
 
Example 5
Source Project: consulo   Source File: ImplementationViewComponent.java    License: Apache License 2.0 6 votes vote down vote up
private void updateEditorText() {
  disposeNonTextEditor();

  final PsiElement elt = myElements[myIndex].getNavigationElement();
  Project project = elt.getProject();
  PsiFile psiFile = getContainingFile(elt);
  final VirtualFile vFile = psiFile.getVirtualFile();
  if (vFile == null) return;
  final FileEditorProvider[] providers = FileEditorProviderManager.getInstance().getProviders(project, vFile);
  for (FileEditorProvider provider : providers) {
    if (provider instanceof TextEditorProvider) {
      updateTextElement(elt);
      myBinarySwitch.show(myViewingPanel, TEXT_PAGE_KEY);
      break;
    }
    else if (provider.accept(project, vFile)) {
      myCurrentNonTextEditorProvider = provider;
      myNonTextEditor = myCurrentNonTextEditorProvider.createEditor(project, vFile);
      myBinaryPanel.removeAll();
      myBinaryPanel.add(myNonTextEditor.getComponent());
      myBinarySwitch.show(myViewingPanel, BINARY_PAGE_KEY);
      break;
    }
  }
}
 
Example 6
public void testCsvTableEditorCreatesInstanceOfCsvTableEditor() {
    FileEditorProvider[] fileEditorProviders = FileEditorProviderManager.getInstance().getProviders(getProject(), myFixture.getFile().getVirtualFile());
    FileEditorProvider fileEditorProvider = fileEditorProviders[1];

    FileEditor fileEditor = fileEditorProvider.createEditor(getProject(), myFixture.getFile().getVirtualFile());
    assertInstanceOf(fileEditor, CsvTableEditor.class);

    fileEditorProvider.disposeEditor(fileEditor);
}
 
Example 7
@Override
@Nonnull
public HistoryEntry currentStateAsHistoryEntry() {
  final FileEditor[] editors = getEditors();
  final FileEditorState[] states = new FileEditorState[editors.length];
  for (int j = 0; j < states.length; j++) {
    states[j] = editors[j].getState(FileEditorStateLevel.FULL);
    LOG.assertTrue(states[j] != null);
  }
  final int selectedProviderIndex = ArrayUtil.find(editors, getSelectedEditor());
  LOG.assertTrue(selectedProviderIndex != -1);
  final FileEditorProvider[] providers = getProviders();
  return HistoryEntry.createLight(getFile(), providers, states, providers[selectedProviderIndex]);
}
 
Example 8
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 5 votes vote down vote up
private HistoryEntry(@Nonnull VirtualFilePointer filePointer,
                     @Nullable FileEditorProvider selectedProvider,
                     @Nullable Disposable disposable) {
  myFilePointer = filePointer;
  mySelectedProvider = selectedProvider;
  myDisposable = disposable;
  myProvider2State = new HashMap<>();
}
 
Example 9
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static HistoryEntry createLight(@Nonnull VirtualFile file,
                                       @Nonnull FileEditorProvider[] providers,
                                       @Nonnull FileEditorState[] states,
                                       @Nonnull FileEditorProvider selectedProvider) {
  VirtualFilePointer pointer = new LightFilePointer(file);
  HistoryEntry entry = new HistoryEntry(pointer, selectedProvider, null);
  for (int i = 0; i < providers.length; i++) {
    entry.putState(providers[i], states[i]);
  }
  return entry;
}
 
Example 10
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static HistoryEntry createLight(@Nonnull Project project, @Nonnull Element e) throws InvalidDataException {
  EntryData entryData = parseEntry(project, e);

  VirtualFilePointer pointer = new LightFilePointer(entryData.url);
  HistoryEntry entry = new HistoryEntry(pointer, entryData.selectedProvider, null);
  for (Pair<FileEditorProvider, FileEditorState> state : entryData.providerStates) {
    entry.putState(state.first, state.second);
  }
  return entry;
}
 
Example 11
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static HistoryEntry createHeavy(@Nonnull Project project, @Nonnull Element e) throws InvalidDataException {
  if (project.isDisposed()) return createLight(project, e);

  EntryData entryData = parseEntry(project, e);

  Disposable disposable = Disposable.newDisposable();
  VirtualFilePointer pointer = VirtualFilePointerManager.getInstance().create(entryData.url, disposable, null);

  HistoryEntry entry = new HistoryEntry(pointer, entryData.selectedProvider, disposable);
  for (Pair<FileEditorProvider, FileEditorState> state : entryData.providerStates) {
    entry.putState(state.first, state.second);
  }
  return entry;
}
 
Example 12
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static EntryData parseEntry(@Nonnull Project project, @Nonnull Element e) throws InvalidDataException {
  if (!e.getName().equals(TAG)) {
    throw new IllegalArgumentException("unexpected tag: " + e);
  }

  String url = e.getAttributeValue(FILE_ATTR);
  List<Pair<FileEditorProvider, FileEditorState>> providerStates = new ArrayList<>();
  FileEditorProvider selectedProvider = null;

  VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url);

  for (Element _e : e.getChildren(PROVIDER_ELEMENT)) {
    String typeId = _e.getAttributeValue(EDITOR_TYPE_ID_ATTR);
    FileEditorProvider provider = FileEditorProviderManager.getInstance().getProvider(typeId);
    if (provider == null) {
      continue;
    }
    if (Boolean.valueOf(_e.getAttributeValue(SELECTED_ATTR_VALUE))) {
      selectedProvider = provider;
    }

    Element stateElement = _e.getChild(STATE_ELEMENT);
    if (stateElement == null) {
      throw new InvalidDataException();
    }

    if (file != null) {
      FileEditorState state = provider.readState(stateElement, project, file);
      providerStates.add(Pair.create(provider, state));
    }
  }

  return new EntryData(url, providerStates, selectedProvider);
}
 
Example 13
Source Project: consulo   Source File: HistoryEntry.java    License: Apache License 2.0 5 votes vote down vote up
public EntryData(@Nonnull String url,
                 @Nonnull List<Pair<FileEditorProvider, FileEditorState>> providerStates,
                 @Nullable FileEditorProvider selectedProvider) {
  this.url = url;
  this.providerStates = providerStates;
  this.selectedProvider = selectedProvider;
}
 
Example 14
Source Project: consulo   Source File: FileEditorProviderManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public FileEditorProvider[] getProviders(@Nonnull final Project project, @Nonnull final VirtualFile file) {
  // Collect all possible editors
  List<FileEditorProvider> sharedProviders = new ArrayList<>();
  boolean doNotShowTextEditor = false;
  for (final FileEditorProvider provider : myProviders) {
    ThrowableComputable<Boolean, RuntimeException> action = () -> {
      if (DumbService.isDumb(project) && !DumbService.isDumbAware(provider)) {
        return false;
      }
      return provider.accept(project, file);
    };
    if (AccessRule.read(action)) {
      sharedProviders.add(provider);
      doNotShowTextEditor |= provider.getPolicy() == FileEditorPolicy.HIDE_DEFAULT_EDITOR;
    }
  }

  // Throw out default editors provider if necessary
  if (doNotShowTextEditor) {
    ContainerUtil.retainAll(sharedProviders, provider -> !(provider instanceof TextEditorProvider));
  }

  // Sort editors according policies
  Collections.sort(sharedProviders, MyComparator.ourInstance);

  return sharedProviders.toArray(new FileEditorProvider[sharedProviders.size()]);
}
 
Example 15
Source Project: consulo   Source File: FileEditorProviderManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public FileEditorProvider getProvider(@Nonnull String editorTypeId) {
  for (FileEditorProvider provider : myProviders) {
    if (provider.getEditorTypeId().equals(editorTypeId)) {
      return provider;
    }
  }
  return null;
}
 
Example 16
Source Project: consulo   Source File: FileEditorProviderManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
private void registerProvider(@Nonnull FileEditorProvider provider) {
  String editorTypeId = provider.getEditorTypeId();
  if (getProvider(editorTypeId) != null) {
    throw new IllegalArgumentException("attempt to register provider with non unique editorTypeId: " + editorTypeId);
  }
  myProviders.add(provider);
}
 
Example 17
Source Project: consulo   Source File: FileEditorProviderManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
FileEditorProvider getSelectedFileEditorProvider(EditorHistoryManager editorHistoryManager, VirtualFile file, FileEditorProvider[] providers) {
  FileEditorProvider provider = editorHistoryManager.getSelectedProvider(file);
  if (provider != null || providers.length < 2) {
    return provider;
  }
  String id = mySelectedProviders.get(computeKey(providers));
  return id == null ? null : getProvider(id);
}
 
Example 18
Source Project: consulo   Source File: FileEditorProviderManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int compare(FileEditorProvider provider1, FileEditorProvider provider2) {
  final int i1 = provider1.getPolicy().ordinal();
  final int i2 = provider2.getPolicy().ordinal();
  if (i1 != i2) return i1 - i2;
  final double value = getWeight(provider1) - getWeight(provider2);
  return value > 0 ? 1 : value < 0 ? -1 : 0;
}
 
Example 19
Source Project: consulo   Source File: DockManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public Pair<FileEditor[], FileEditorProvider[]> createNewDockContainerFor(@Nonnull VirtualFile file, @Nonnull FileEditorManagerImpl fileEditorManager) {
  DockContainer container = getFactory(DockableEditorContainerFactory.TYPE).createContainer(null);
  register(container);

  final DockWindow window = createWindowFor(null, container);

  window.show(true);
  final EditorWindow editorWindow = ((DockableEditorTabbedContainer)container).getSplitters().getOrCreateCurrentWindow(file);
  final Pair<FileEditor[], FileEditorProvider[]> result = fileEditorManager.openFileImpl2(UIAccess.get(), editorWindow, file, true);
  container.add(EditorTabbedContainer.createDockableEditor(myProject, null, file, new Presentation(file.getName()), editorWindow), null);

  SwingUtilities.invokeLater(() -> window.myUiContainer.setPreferredSize(null));
  return result;
}
 
Example 20
Source Project: consulo   Source File: TestEditorTabGroup.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public Pair<FileEditor, FileEditorProvider> getOpenedEditor(){
  VirtualFile openedFile = getOpenedFile();
  if (openedFile == null) {
    return null;
  }

  return myOpenedTabs.get(openedFile);
}
 
Example 21
Source Project: consulo   Source File: TestEditorSplitter.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public FileEditor getFocusedFileEditor() {
  Pair<FileEditor, FileEditorProvider> openedEditor = getActiveTabGroup().getOpenedEditor();
  if(openedEditor == null)
    return null;

  return openedEditor.first;
}
 
Example 22
Source Project: consulo   Source File: TestEditorSplitter.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public FileEditorProvider getProviderFromFocused() {
  Pair<FileEditor, FileEditorProvider> openedEditor = getActiveTabGroup().getOpenedEditor();
  if(openedEditor == null)
    return null;

  return openedEditor.second;
}
 
Example 23
Source Project: consulo   Source File: StructureViewWrapperImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private FileEditor createTempFileEditor(@Nonnull VirtualFile file) {
  if (file.getLength() > PersistentFSConstants.getMaxIntellisenseFileSize()) return null;

  FileEditorProviderManager editorProviderManager = FileEditorProviderManager.getInstance();
  final FileEditorProvider[] providers = editorProviderManager.getProviders(myProject, file);
  return providers.length == 0 ? null : providers[0].createEditor(myProject, file);
}
 
Example 24
Source Project: consulo   Source File: WebEditorWithProviderComposite.java    License: Apache License 2.0 5 votes vote down vote up
public WebEditorWithProviderComposite(VirtualFile file, FileEditor[] editors, FileEditorProvider[] providers, FileEditorManagerEx fileEditorManager) {
  myFile = file;
  myEditors = editors;
  myProviders = providers;
  myFileEditorManager = fileEditorManager;

  myComponents = new Component[editors.length];
  for (int i = 0; i < editors.length; i++) {
    FileEditor editor = editors[i];

    myComponents[i] = editor.getUIComponent();
  }
}
 
Example 25
Source Project: consulo   Source File: WebPsiAwareFileEditorManagerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
protected EditorWithProviderComposite createEditorWithProviderComposite(@Nonnull VirtualFile file,
                                                                        @Nonnull FileEditor[] editors,
                                                                        @Nonnull FileEditorProvider[] providers,
                                                                        @Nonnull FileEditorManagerEx fileEditorManager) {
  return new WebEditorWithProviderComposite(file, editors, providers, fileEditorManager);
}
 
Example 26
@Nonnull
@Override
protected EditorWithProviderComposite createEditorWithProviderComposite(@Nonnull VirtualFile file,
                                                                        @Nonnull FileEditor[] editors,
                                                                        @Nonnull FileEditorProvider[] providers,
                                                                        @Nonnull FileEditorManagerEx fileEditorManager) {
  return new DesktopEditorWithProviderComposite(file, editors, providers, fileEditorManager);
}
 
Example 27
Source Project: consulo   Source File: BinaryEditorHolder.java    License: Apache License 2.0 4 votes vote down vote up
public BinaryEditorHolder(@Nonnull FileEditor editor, @Nonnull FileEditorProvider editorProvider) {
  myEditor = editor;
  myEditorProvider = editorProvider;
}
 
Example 28
Source Project: consulo   Source File: EditorWithProviderComposite.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
FileEditorProvider[] getProviders();
 
Example 29
Source Project: consulo   Source File: FileEditorManagerEx.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
public abstract Pair<FileEditor[], FileEditorProvider[]> getEditorsWithProviders(@Nonnull VirtualFile file);
 
Example 30
Source Project: consulo   Source File: FileEditorManagerEx.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
public abstract Pair<FileEditor[], FileEditorProvider[]> openFileWithProviders(@Nonnull VirtualFile file, boolean focusEditor, boolean searchForSplitter);