private static Editor createEditor() { EditorFactory editorFactory = EditorFactory.getInstance(); Document document = editorFactory.createDocument(""); EditorEx editor = (EditorEx) editorFactory.createEditor(document); reinitSettings(editor); return editor; }
private void releaseAllEditors() { for (final Editor editor : myEditors.values()) { if (!editor.isDisposed()) { EditorFactory.getInstance().releaseEditor(editor); } } myEditors.clear(); }
@Override protected void releaseResources() { super.releaseResources(); if (myPreview == null) return; EditorFactory.getInstance().releaseEditor(myPreview); myPreview = null; }
@Override public void removeNotify() { super.removeNotify(); EditorFactory.getInstance().releaseEditor(myEditor); disposeNonTextEditor(); }
public ImplementationViewComponent(PsiElement[] elements, final int index) { super(new BorderLayout()); final Project project = elements.length > 0 ? elements[0].getProject() : null; EditorFactory factory = EditorFactory.getInstance(); Document doc = factory.createDocument(""); doc.setReadOnly(true); myEditor = factory.createEditor(doc, project); ((EditorEx) myEditor).setBackgroundColor(EditorFragmentComponent.getBackgroundColor(myEditor)); final EditorSettings settings = myEditor.getSettings(); settings.setAdditionalLinesCount(1); settings.setAdditionalColumnsCount(1); settings.setLineMarkerAreaShown(false); settings.setIndentGuidesShown(false); settings.setLineNumbersShown(false); settings.setFoldingOutlineShown(false); myBinarySwitch = new CardLayout(); myViewingPanel = new JPanel(myBinarySwitch); myEditor.setBorder(null); ((EditorEx) myEditor).getScrollPane().setViewportBorder(JBScrollPane.createIndentBorder()); myViewingPanel.add(myEditor.getComponent(), TEXT_PAGE_KEY); myBinaryPanel = new JPanel(new BorderLayout()); myViewingPanel.add(myBinaryPanel, BINARY_PAGE_KEY); add(myViewingPanel, BorderLayout.CENTER); myToolbar = createToolbar(); myLocationLabel = new JLabel(); myCountLabel = new JLabel(); final JPanel header = new JPanel(new BorderLayout(2, 0)); header.setBorder( BorderFactory.createCompoundBorder( IdeBorderFactory.createBorder(SideBorder.BOTTOM), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5))); final JPanel toolbarPanel = new JPanel(new GridBagLayout()); final GridBagConstraints gc = new GridBagConstraints( GridBagConstraints.RELATIVE, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0); toolbarPanel.add(myToolbar.getComponent(), gc); setPreferredSize(new Dimension(600, 400)); update( elements, new PairFunction<PsiElement[], List<FileDescriptor>, Boolean>() { @Override public Boolean fun( final PsiElement[] psiElements, final List<FileDescriptor> fileDescriptors) { if (psiElements.length == 0) return false; myElements = psiElements; myIndex = index < myElements.length ? index : 0; PsiFile psiFile = getContainingFile(myElements[myIndex]); VirtualFile virtualFile = psiFile.getVirtualFile(); EditorHighlighter highlighter; if (virtualFile != null) highlighter = HighlighterFactory.createHighlighter(project, virtualFile); else { String fileName = psiFile.getName(); // some artificial psi file, lets do best we can highlighter = HighlighterFactory.createHighlighter(project, fileName); } ((EditorEx) myEditor).setHighlighter(highlighter); gc.fill = GridBagConstraints.HORIZONTAL; gc.weightx = 1; if (myElements.length > 1) { myFileChooser = new ComboBox( fileDescriptors.toArray(new FileDescriptor[fileDescriptors.size()]), 250); updateRenderer(project); myFileChooser.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int index = myFileChooser.getSelectedIndex(); if (myIndex != index) { myIndex = index; updateControls(); } } }); myLabel = new JLabel(); myLabel.setVisible(false); toolbarPanel.add(myFileChooser, gc); } else { myFileChooser = new ComboBox(); myFileChooser.setVisible(false); myCountLabel.setVisible(false); myLabel = new JLabel(); VirtualFile file = psiFile.getVirtualFile(); if (file != null) { myLabel.setIcon(getIconForFile(psiFile)); myLabel.setForeground( FileStatusManager.getInstance(project).getStatus(file).getColor()); myLabel.setText(file.getPresentableName()); myLabel.setBorder( new CompoundBorder( IdeBorderFactory.createRoundedBorder(), IdeBorderFactory.createEmptyBorder(0, 0, 0, 5))); } toolbarPanel.add(myLabel, gc); } gc.fill = GridBagConstraints.NONE; gc.weightx = 0; toolbarPanel.add(myCountLabel, gc); header.add(toolbarPanel, BorderLayout.CENTER); header.add(myLocationLabel, BorderLayout.EAST); add(header, BorderLayout.NORTH); updateControls(); return true; } }); }
private void recreateEditorsPanel() { myValuesPanel.removeAll(); myValuesPanel.setLayout(new CardLayout()); if (!myProject.isOpen()) return; JPanel valuesPanelComponent = new MyJPanel(new GridBagLayout()); myValuesPanel.add( new JBScrollPane(valuesPanelComponent) { @Override public void updateUI() { super.updateUI(); getViewport().setBackground(UIUtil.getPanelBackground()); } }, VALUES); myValuesPanel.add(myNoPropertySelectedPanel, NO_PROPERTY_SELECTED); List<PropertiesFile> propertiesFiles = myResourceBundle.getPropertiesFiles(); GridBagConstraints gc = new GridBagConstraints( 0, 0, 0, 0, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0); releaseAllEditors(); myTitledPanels.clear(); int y = 0; Editor previousEditor = null; Editor firstEditor = null; for (final PropertiesFile propertiesFile : propertiesFiles) { final Editor editor = createEditor(); final Editor oldEditor = myEditors.put(propertiesFile, editor); if (firstEditor == null) { firstEditor = editor; } if (previousEditor != null) { editor.putUserData( ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor); previousEditor.putUserData( ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, editor); } previousEditor = editor; if (oldEditor != null) { EditorFactory.getInstance().releaseEditor(oldEditor); } ((EditorEx) editor) .addFocusListener( new FocusChangeListener() { @Override public void focusGained(final Editor editor) { mySelectedEditor = editor; } @Override public void focusLost(final Editor eventEditor) { writeEditorPropertyValue(editor, propertiesFile, null); } }); gc.gridx = 0; gc.gridy = y++; gc.gridheight = 1; gc.gridwidth = GridBagConstraints.REMAINDER; gc.weightx = 1; gc.weighty = 1; gc.anchor = GridBagConstraints.CENTER; Locale locale = propertiesFile.getLocale(); List<String> names = new ArrayList<String>(); if (!Comparing.strEqual(locale.getDisplayLanguage(), null)) { names.add(locale.getDisplayLanguage()); } if (!Comparing.strEqual(locale.getDisplayCountry(), null)) { names.add(locale.getDisplayCountry()); } if (!Comparing.strEqual(locale.getDisplayVariant(), null)) { names.add(locale.getDisplayVariant()); } String title = propertiesFile.getName(); if (!names.isEmpty()) { title += " (" + StringUtil.join(names, "/") + ")"; } JComponent comp = new JPanel(new BorderLayout()) { @Override public Dimension getPreferredSize() { Insets insets = getBorder().getBorderInsets(this); return new Dimension(100, editor.getLineHeight() * 4 + insets.top + insets.bottom); } }; comp.add(editor.getComponent(), BorderLayout.CENTER); comp.setBorder(IdeBorderFactory.createTitledBorder(title, true)); myTitledPanels.put(propertiesFile, (JPanel) comp); valuesPanelComponent.add(comp, gc); } if (previousEditor != null) { previousEditor.putUserData( ChooseSubsequentPropertyValueEditorAction.NEXT_EDITOR_KEY, firstEditor); firstEditor.putUserData( ChooseSubsequentPropertyValueEditorAction.PREV_EDITOR_KEY, previousEditor); } gc.gridx = 0; gc.gridy = y; gc.gridheight = GridBagConstraints.REMAINDER; gc.gridwidth = GridBagConstraints.REMAINDER; gc.weightx = 10; gc.weighty = 1; valuesPanelComponent.add(new JPanel(), gc); selectionChanged(); myValuesPanel.repaint(); UIUtil.invokeAndWaitIfNeeded( new Runnable() { @Override public void run() { updateEditorsFromProperties(); } }); }
QuickEditHandler( Project project, @NotNull PsiFile injectedFile, final PsiFile origFile, Editor editor, QuickEditAction action) { myProject = project; myEditor = editor; myAction = action; myOrigDocument = editor.getDocument(); Place shreds = InjectedLanguageUtil.getShreds(injectedFile); FileType fileType = injectedFile.getFileType(); Language language = injectedFile.getLanguage(); PsiLanguageInjectionHost.Shred firstShred = ContainerUtil.getFirstItem(shreds); PsiFileFactory factory = PsiFileFactory.getInstance(project); String text = InjectedLanguageManager.getInstance(project).getUnescapedText(injectedFile); String newFileName = StringUtil.notNullize(language.getDisplayName(), "Injected") + " Fragment " + "(" + origFile.getName() + ":" + firstShred.getHost().getTextRange().getStartOffset() + ")" + "." + fileType.getDefaultExtension(); // preserve \r\n as it is done in MultiHostRegistrarImpl myNewFile = factory.createFileFromText(newFileName, language, text, true, false); myNewVirtualFile = ObjectUtils.assertNotNull((LightVirtualFile) myNewFile.getVirtualFile()); myNewVirtualFile.setOriginalFile(origFile.getVirtualFile()); assert myNewFile != null : "PSI file is null"; assert myNewFile.getTextLength() == myNewVirtualFile.getContent().length() : "PSI / Virtual file text mismatch"; myNewVirtualFile.setOriginalFile(origFile.getVirtualFile()); // suppress possible errors as in injected mode myNewFile.putUserData( InjectedLanguageUtil.FRANKENSTEIN_INJECTION, injectedFile.getUserData(InjectedLanguageUtil.FRANKENSTEIN_INJECTION)); myNewFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, shreds.getHostPointer()); myNewDocument = PsiDocumentManager.getInstance(project).getDocument(myNewFile); assert myNewDocument != null; EditorActionManager.getInstance() .setReadonlyFragmentModificationHandler(myNewDocument, new MyQuietHandler()); myOrigCreationStamp = myOrigDocument.getModificationStamp(); // store creation stamp for UNDO tracking myOrigDocument.addDocumentListener(this, this); myNewDocument.addDocumentListener(this, this); EditorFactory editorFactory = ObjectUtils.assertNotNull(EditorFactory.getInstance()); // not FileEditorManager listener because of RegExp checker and alike editorFactory.addEditorFactoryListener( new EditorFactoryAdapter() { int useCount; @Override public void editorCreated(@NotNull EditorFactoryEvent event) { if (event.getEditor().getDocument() != myNewDocument) return; useCount++; } @Override public void editorReleased(@NotNull EditorFactoryEvent event) { if (event.getEditor().getDocument() != myNewDocument) return; if (--useCount > 0) return; if (Boolean.TRUE.equals( myNewVirtualFile.getUserData(FileEditorManagerImpl.CLOSING_TO_REOPEN))) return; Disposer.dispose(QuickEditHandler.this); } }, this); if ("JAVA".equals(firstShred.getHost().getLanguage().getID())) { PsiLanguageInjectionHost.Shred lastShred = ContainerUtil.getLastItem(shreds); myAltFullRange = myOrigDocument.createRangeMarker( firstShred.getHostRangeMarker().getStartOffset(), lastShred.getHostRangeMarker().getEndOffset()); myAltFullRange.setGreedyToLeft(true); myAltFullRange.setGreedyToRight(true); initGuardedBlocks(shreds); myInjectedFile = null; } else { initMarkers(shreds); myAltFullRange = null; myInjectedFile = injectedFile; } }