How to open a custom swing dialog when a custom file in project view double clicked?

Answered

Hi,

I made a custom file type.

Now, Double click on the file in project view, There shows a default file editor.

How to display a swing dialog that designed by myself instend of default editor?

Can anyone give some guidance?

Thanks.

 

 

0
1 comment

plugin.xml:

<extensions defaultExtensionNs="com.intellij">
<fileType name="GCI file" implementationClass="biz.lifang.gci.filetype.GCIFileType" fieldName="INSTANCE"
language="gci" extensions="gci"/>
<fileEditorProvider implementation="biz.lifang.gci.filetype.GCIEditorProvider"/>
</extensions>

GCIFileType.java:

package biz.lifang.gci.filetype;

import com.intellij.openapi.fileTypes.LanguageFileType;
import icons.SdkIcons;
import org.jetbrains.annotations.*;

import javax.swing.*;

public class GCIFileType extends LanguageFileType {
public static final GCIFileType INSTANCE = new GCIFileType();

private GCIFileType() {
super(GCILanguage.INSTANCE);
}

@NotNull
@Override
public String getName() {
return "GCI file";
}

@NotNull
@Override
public String getDescription() {
return "GCI language file";
}

@NotNull
@Override
public String getDefaultExtension() {
return "gci";
}

@Nullable
@Override
public Icon getIcon() {
return SdkIcons.Sdk_default_icon;
}
}

GCILanguage.java

package biz.lifang.gci.filetype;

import com.intellij.lang.Language;

public class GCILanguage extends Language {
public static final GCILanguage INSTANCE = new GCILanguage();

private GCILanguage() {
super("gci");
}
}

 

GCIEditorProvider.java:

package biz.lifang.gci.filetype;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorPolicy;
import com.intellij.openapi.fileEditor.FileEditorProvider;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.DumbAware;

/**
* https://intellij-support.jetbrains.com/hc/en-us/community/posts/206137969-How-I-can-handle-double-click-the-file-in-project-tree-
* You'll need to provide an implementation of com.intellij.openapi.fileEditor.FileEditorProvider
* interface, and open your external utility from its createEditor() method.
* There are lots of places in IntelliJ IDEA from where files can be opened,
* and double-click in project view is just one of them. Providing a FileEditorProvider
* implementation will cover them all.
*
* https://intellij-support.jetbrains.com/hc/en-us/community/posts/206758405-How-to-register-new-FileEditorProvider-in-system-
* https://intellij-support.jetbrains.com/hc/en-us/community/posts/206117539-FileEditorProvider-question
*/
public class GCIEditorProvider implements FileEditorProvider,DumbAware {
@NonNls
private static final String EDITOR_TYPE_ID = "gci";

@Override
public boolean accept(@NotNull Project project, @NotNull VirtualFile file) {
return file.getExtension().equals("gci");
}

@NotNull
@Override
public FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file) {
return new GCIEditorImpl(project,file);
}

@Override
public void disposeEditor(@NotNull FileEditor editor) {
Disposer.dispose(editor);
}

@NotNull
@Override
public String getEditorTypeId() {
return EDITOR_TYPE_ID;
}

@NotNull
@Override
public FileEditorPolicy getPolicy() {
return FileEditorPolicy.HIDE_DEFAULT_EDITOR;
}
}

GCIEditorImpl.java:

package biz.lifang.gci.filetype;

import biz.lifang.gci.ui.GCIBaseDialog;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorLocation;
import com.intellij.openapi.fileEditor.FileEditorState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.beans.PropertyChangeListener;
import java.util.HashMap;

public class GCIEditorImpl extends UserDataHolderBase implements FileEditor {
private static final String NAME = "GCIEditor";
private VirtualFile myFile;
private Project myProject;

public GCIEditorImpl(VirtualFile file) {
myFile = file;
}

public GCIEditorImpl(Project project, VirtualFile file) {
myFile = file;
myProject = project;
}

@NotNull
@Override
public JComponent getComponent() {


GCIBaseDialog dialog = new GCIBaseDialog(this.myProject,new HashMap<>());
return dialog.getContentPanel();

// JLabel label = new JLabel(
// "Binary file " + myFile.getPath() + " is too large (" + (myFile.getLength()) + ")");
// label.setHorizontalAlignment(SwingConstants.CENTER);
// return label;
}

@Nullable
@Override
public JComponent getPreferredFocusedComponent() {
return null;
}

@NotNull
@Override
public String getName() {
return NAME;
}

@Override
public void setState(@NotNull FileEditorState state) {
}

@Override
public boolean isModified() {
return false;
}

@Override
public boolean isValid() {
return myFile.isValid();
}

@Override
public void addPropertyChangeListener(@NotNull PropertyChangeListener listener) {

}

@Override
public void removePropertyChangeListener(@NotNull PropertyChangeListener listener) {

}

@Nullable
@Override
public FileEditorLocation getCurrentLocation() {
return null;
}

@Override
public void dispose() {

}

@Nullable
@Override
public <T> T getUserData(@NotNull Key<T> key) {
return null;
}

@Override
public <T> void putUserData(@NotNull Key<T> key, @Nullable T value) {

}
}

GCIBaseDialog.java is created by ideallij, right click on projectview, new -> swing ui designer ->GUI form:

package biz.lifang.gci.ui;
import biz.lifang.gci.utils.Comm;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;

public class GCIBaseDialog extends DialogWrapper{
private JCheckBox adminModuleCheckBox;
private JCheckBox xchxModuleCheckBox;
private JCheckBox gzhhModuleCheckBox;
private JCheckBox appModuleCheckBox;
private JLabel logoLabel;
private JTextField logoTextField;
private JButton logoBrowseButton;
private JTextField localHostnameTextField;
private JTextField localUsernameTextField;
private JTextField localPasswordTextField;
private JTextField localDatabaseTextField;
private JTextField proHostnameTextField;
private JTextField proUsernameTextField;
private JTextField proPasswordTextField;
private JTextField proDatabaseTextField;
private JTextField localBaseUrlTextField;
private JTextField proBaseUrlTextField;
private JLabel localDatabaseLabel;
private JLabel proDatabaseLabel;
private JLabel testDatabaseLabel;
private JTextField testHostnameTextField;
private JTextField testUsernameTextField;
private JTextField testPasswordTextField;
private JTextField testDatabaseTextField;
private JTextField testBaseUrlTextField;
private JLabel testBaseUrl;
private JLabel proBaseUrl;
private JLabel localBaseUrl;
private JLabel moduleLabel;
private JPanel rootPanel;
private JLabel tipsLabel;

protected Project project;

public GCIBaseDialog(Project p) {
super(true);
this.project = p;
init();
setTitle("生成框架代码");
}

@Override
protected ValidationInfo doValidate(){
if(localBaseUrlTextField.getText().trim().isEmpty()){
return new ValidationInfo("本地base_url 必须填写", localBaseUrlTextField);
}
return null;
}

@Override
protected void doOKAction(){
Messages.showMessageDialog(this.project, "Ok button Clicked", "Title", Messages.getInformationIcon());
}

@Nullable
@Override
protected JComponent createCenterPanel() {
return rootPanel;
}
}


 

0

Please sign in to leave a comment.