private void setUpChooseHtmlToShow() { FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor(StdFileTypes.HTML); fileChooserDescriptor.setRoots(ProjectRootManager.getInstance(project).getContentRoots()); htmlChooseFile.addBrowseFolderListener( "Choose file to show after translation is finished", null, project, fileChooserDescriptor); }
private void setUpChooseGenerateFilePath() { FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.getDirectoryChooserDescriptor( "directory where generated files will be stored"); fileChooserDescriptor.setRoots(ProjectRootManager.getInstance(project).getContentRoots()); generatedChooseFile.addBrowseFolderListener(null, null, project, fileChooserDescriptor); final JTextField textField = generatedChooseFile.getTextField(); textField .getDocument() .addDocumentListener( new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { onChange(); } @Override public void removeUpdate(DocumentEvent e) { onChange(); } @Override public void changedUpdate(DocumentEvent e) { onChange(); } private void onChange() { File file = new File(generatedChooseFile.getText()); if (!file.isDirectory()) { textField.setForeground(Color.RED); } else { textField.setForeground(Color.BLACK); } } }); }
@NotNull protected PsiElement[] invokeDialog(final Project project, final PsiDirectory directory) { final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor(); descriptor.setRoots(project.getBaseDir()); PsiDirectory currentDir = currentFile.getContainingDirectory(); PsiDirectory includeDir = currentDir.findSubdirectory("Includes"); VirtualFile selectDir = directory.getVirtualFile(); if (includeDir == null) { PsiDirectory parentDir = currentDir.getParentDirectory(); if (parentDir != null) { includeDir = parentDir.findSubdirectory("Includes"); } } if (includeDir != null) selectDir = includeDir.getVirtualFile(); final VirtualFile myFolder = FileChooser.chooseFile(descriptor, project, selectDir); if (myFolder != null) { PsiDirectory myDirectory = PsiManager.getInstance(project).findDirectory(myFolder); final MyInputValidator validator = new MyInputValidator(project, myDirectory); final String fileName = Messages.showInputDialog( project, getDialogPrompt(), getDialogTitle(), Messages.getQuestionIcon(), "", validator); final PsiElement[] elements = validator.getCreatedElements(); if (elements.length > 0) { ApplicationManager.getApplication() .invokeLater( new Runnable() { @Override public void run() { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { try { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); CaretModel caretModel = editor.getCaretModel(); int caretOffset = selectonModel.getSelectionStart(); EditorModificationUtil.deleteSelectedText(editor); PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); caretModel.moveToOffset(caretOffset); EditorModificationUtil.insertStringAtCaret( editor, "<% include " + fileName + " %>", true, false); PsiFile createdFile = (PsiFile) elements[0]; codeStyleManager.reformat(createdFile); psiDocumentManager.commitDocument( psiDocumentManager.getDocument(currentFile)); FileEditorManager.getInstance(project) .openFile(currentFile.getVirtualFile(), true); codeStyleManager.adjustLineIndent(currentFile, caretOffset); psiDocumentManager.commitDocument( psiDocumentManager.getDocument(currentFile)); } catch (Exception e) { e.printStackTrace(); // To change body of catch statement use // File | Settings | File Templates. } } }); } }); } return elements; } return PsiElement.EMPTY_ARRAY; }
@Override protected JComponent createCenterPanel() { final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createAllButJarContentsDescriptor(); calculateRoots(); final ArrayList<VirtualFile> list = new ArrayList<VirtualFile>(myRoots); final Comparator<VirtualFile> comparator = new Comparator<VirtualFile>() { @Override public int compare(VirtualFile o1, VirtualFile o2) { final boolean isDir1 = o1.isDirectory(); final boolean isDir2 = o2.isDirectory(); if (isDir1 != isDir2) return isDir1 ? -1 : 1; final String module1 = myModulesSet.get(o1); final String path1 = module1 != null ? module1 : o1.getPath(); final String module2 = myModulesSet.get(o2); final String path2 = module2 != null ? module2 : o2.getPath(); return path1.compareToIgnoreCase(path2); } }; descriptor.setRoots(list); myTree = new Tree(); myTree.setMinimumSize(new Dimension(200, 200)); myTree.setBorder(BORDER); myTree.setShowsRootHandles(true); myTree.setRootVisible(true); myTree.getExpandableItemsHandler().setEnabled(false); final MyCheckboxTreeCellRenderer cellRenderer = new MyCheckboxTreeCellRenderer( mySelectionManager, myModulesSet, myProject, myTree, myRoots); final FileSystemTreeImpl fileSystemTree = new FileSystemTreeImpl( myProject, descriptor, myTree, cellRenderer, null, new Convertor<TreePath, String>() { @Override public String convert(TreePath o) { final DefaultMutableTreeNode lastPathComponent = ((DefaultMutableTreeNode) o.getLastPathComponent()); final Object uo = lastPathComponent.getUserObject(); if (uo instanceof FileNodeDescriptor) { final VirtualFile file = ((FileNodeDescriptor) uo).getElement().getFile(); final String module = myModulesSet.get(file); if (module != null) return module; return file == null ? "" : file.getName(); } return o.toString(); } }); final AbstractTreeUi ui = fileSystemTree.getTreeBuilder().getUi(); ui.setNodeDescriptorComparator( new Comparator<NodeDescriptor>() { @Override public int compare(NodeDescriptor o1, NodeDescriptor o2) { if (o1 instanceof FileNodeDescriptor && o2 instanceof FileNodeDescriptor) { final VirtualFile f1 = ((FileNodeDescriptor) o1).getElement().getFile(); final VirtualFile f2 = ((FileNodeDescriptor) o2).getElement().getFile(); return comparator.compare(f1, f2); } return o1.getIndex() - o2.getIndex(); } }); myRoot = (DefaultMutableTreeNode) myTree.getModel().getRoot(); new ClickListener() { @Override public boolean onClick(@NotNull MouseEvent e, int clickCount) { int row = myTree.getRowForLocation(e.getX(), e.getY()); if (row < 0) return false; final Object o = myTree.getPathForRow(row).getLastPathComponent(); if (myRoot == o || getFile(o) == null) return false; Rectangle rowBounds = myTree.getRowBounds(row); cellRenderer.setBounds(rowBounds); Rectangle checkBounds = cellRenderer.myCheckbox.getBounds(); checkBounds.setLocation(rowBounds.getLocation()); if (checkBounds.height == 0) checkBounds.height = rowBounds.height; if (checkBounds.contains(e.getPoint())) { mySelectionManager.toggleSelection((DefaultMutableTreeNode) o); myTree.revalidate(); myTree.repaint(); } return true; } }.installOn(myTree); myTree.addKeyListener( new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_SPACE) { TreePath[] paths = myTree.getSelectionPaths(); if (paths == null) return; for (TreePath path : paths) { if (path == null) continue; final Object o = path.getLastPathComponent(); if (myRoot == o || getFile(o) == null) return; mySelectionManager.toggleSelection((DefaultMutableTreeNode) o); } myTree.revalidate(); myTree.repaint(); e.consume(); } } }); JBPanel panel = new JBPanel(new BorderLayout()); panel.add(new JBScrollPane(fileSystemTree.getTree()), BorderLayout.CENTER); mySelectedLabel = new JLabel(""); mySelectedLabel.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0)); panel.add(mySelectedLabel, BorderLayout.SOUTH); mySelectionManager.setSelectionChangeListener( new PlusMinus<VirtualFile>() { @Override public void plus(VirtualFile virtualFile) { mySelectedFiles.add(virtualFile); recalculateErrorText(); } private void recalculateErrorText() { checkEmpty(); if (mySelectionManager.canAddSelection()) { mySelectedLabel.setText(""); } else { mySelectedLabel.setText(CAN_NOT_ADD_TEXT); } mySelectedLabel.revalidate(); } @Override public void minus(VirtualFile virtualFile) { mySelectedFiles.remove(virtualFile); recalculateErrorText(); } }); return panel; }
/** @param contentEntryEditor : null means to clear the editor */ public void setContentEntryEditor(final ContentEntryEditor contentEntryEditor) { if (myContentEntryEditor != null && myContentEntryEditor.equals(contentEntryEditor)) { return; } if (myFileSystemTree != null) { Disposer.dispose(myFileSystemTree); myFileSystemTree = null; } if (myContentEntryEditor != null) { myContentEntryEditor.removeContentEntryEditorListener(myContentEntryEditorListener); myContentEntryEditor = null; } if (contentEntryEditor == null) { ((DefaultTreeModel) myTree.getModel()).setRoot(EMPTY_TREE_ROOT); myTreePanel.setVisible(false); if (myFileSystemTree != null) { Disposer.dispose(myFileSystemTree); } return; } myTreePanel.setVisible(true); myContentEntryEditor = contentEntryEditor; myContentEntryEditor.addContentEntryEditorListener(myContentEntryEditorListener); final ContentEntry entry = contentEntryEditor.getContentEntry(); assert entry != null : contentEntryEditor; final VirtualFile file = entry.getFile(); myDescriptor.setRoots(file); if (file == null) { final String path = VfsUtilCore.urlToPath(entry.getUrl()); myDescriptor.setTitle(FileUtil.toSystemDependentName(path)); } final Runnable init = new Runnable() { @Override public void run() { //noinspection ConstantConditions myFileSystemTree.updateTree(); myFileSystemTree.select(file, null); } }; myFileSystemTree = new FileSystemTreeImpl( myProject, myDescriptor, myTree, getContentEntryCellRenderer(), init, null) { @Override protected AbstractTreeBuilder createTreeBuilder( JTree tree, DefaultTreeModel treeModel, AbstractTreeStructure treeStructure, Comparator<NodeDescriptor> comparator, FileChooserDescriptor descriptor, final Runnable onInitialized) { return new MyFileTreeBuilder( tree, treeModel, treeStructure, comparator, descriptor, onInitialized); } }; myFileSystemTree.showHiddens(true); Disposer.register(myProject, myFileSystemTree); final NewFolderAction newFolderAction = new MyNewFolderAction(); final DefaultActionGroup mousePopupGroup = new DefaultActionGroup(); mousePopupGroup.add(myEditingActionsGroup); mousePopupGroup.addSeparator(); mousePopupGroup.add(newFolderAction); myFileSystemTree.registerMouseListener(mousePopupGroup); }