-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dialog and action for creating new autoconfig files
- Loading branch information
Showing
6 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/de/gebit/intellij/autoconfig/create/CreateAutoconfigFileAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package de.gebit.intellij.autoconfig.create; | ||
|
||
import com.intellij.ide.IdeView; | ||
import com.intellij.ide.actions.CreateInDirectoryActionBase; | ||
import com.intellij.ide.actions.OpenFileAction; | ||
import com.intellij.openapi.actionSystem.ActionManager; | ||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.LangDataKeys; | ||
import com.intellij.openapi.application.WriteAction; | ||
import com.intellij.openapi.diagnostic.Logger; | ||
import com.intellij.openapi.editor.Document; | ||
import com.intellij.openapi.fileEditor.FileDocumentManager; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.psi.PsiFile; | ||
import de.gebit.intellij.autoconfig.UpdateHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
/** | ||
* An action to create/open an Autoconfig file for a specific handler/plugin | ||
*/ | ||
public class CreateAutoconfigFileAction extends CreateInDirectoryActionBase { | ||
private static final @NotNull Logger LOGGER = Logger.getInstance(CreateAutoconfigFileAction.class); | ||
|
||
@Override | ||
public void actionPerformed(@NotNull AnActionEvent e) { | ||
Project project = e.getProject(); | ||
if (project != null) { | ||
CreateAutoconfigFileDialog autoconfigFileDialog = new CreateAutoconfigFileDialog(); | ||
if (!autoconfigFileDialog.showAndGet()) { | ||
// the dialog has been canceled | ||
return; | ||
} | ||
// the update handler selected in the dialogs form | ||
Optional<UpdateHandler<?>> optionalUpdateHandler = autoconfigFileDialog.getSelectedHandler(); | ||
if (optionalUpdateHandler.isEmpty()) { | ||
return; | ||
} | ||
UpdateHandler<?> updateHandler = optionalUpdateHandler.get(); | ||
VirtualFile projectFile = project.getProjectFile(); | ||
if (projectFile == null) { | ||
LOGGER.warn("No project file? This is unusual. We can't automatically create a new Autoconfig file then. Please create it yourself."); | ||
return; | ||
} | ||
try { | ||
VirtualFile updateHandlerFile = WriteAction.compute(() -> { | ||
VirtualFile autoconfigDirectory = projectFile.getParent().findChild("autoconfig"); | ||
if (autoconfigDirectory == null) { | ||
// no ".idea/autoconfig" directory found. We're going to create it. | ||
autoconfigDirectory = projectFile.getParent().createChildDirectory(this, "autoconfig"); | ||
} | ||
return autoconfigDirectory.findOrCreateChildData(this, updateHandler.getFileName()); | ||
}); | ||
if (updateHandlerFile != null) { | ||
// we open the file | ||
OpenFileAction.openFile(updateHandlerFile, project); | ||
Document document = FileDocumentManager.getInstance().getDocument(updateHandlerFile); | ||
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document); | ||
IdeView ideView = e.getData(LangDataKeys.IDE_VIEW); | ||
if (psiFile != null && ideView != null) { | ||
// and then focus the project/file view on the file | ||
ideView.selectElement(psiFile); | ||
} | ||
ActionManager actionManager = ActionManager.getInstance(); | ||
AnAction codeCompletion = actionManager.getAction("CodeCompletion"); | ||
// and then we invoke code-completion on the possibly newly created opened file. | ||
// now=false because otherwise it might get executed before other IDE actions are done, | ||
// which would result in it losing its focus right away | ||
actionManager.tryToExecute(codeCompletion, null, null, null, false); | ||
} | ||
} catch (IOException ex) { | ||
LOGGER.error("Couldn't create Autoconfig file!", ex); | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/de/gebit/intellij/autoconfig/create/CreateAutoconfigFileDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.gebit.intellij.autoconfig.create; | ||
|
||
import com.intellij.openapi.ui.DialogWrapper; | ||
import de.gebit.intellij.autoconfig.AutoconfigStartup; | ||
import de.gebit.intellij.autoconfig.UpdateHandler; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.swing.*; | ||
import java.util.Optional; | ||
|
||
/** | ||
* DialogWrapper for {@link CreateAutoconfigFileForm}, creating the form with all available configuration updaters. | ||
*/ | ||
public class CreateAutoconfigFileDialog extends DialogWrapper { | ||
|
||
private CreateAutoconfigFileForm autoconfigFileForm; | ||
|
||
protected CreateAutoconfigFileDialog() { | ||
super(false); | ||
init(); | ||
setTitle("Create Autoconfig File"); | ||
} | ||
|
||
@Override | ||
protected @Nullable JComponent createCenterPanel() { | ||
autoconfigFileForm = new CreateAutoconfigFileForm(AutoconfigStartup.EP_NAME.getExtensionList()); | ||
return autoconfigFileForm.getForm(); | ||
} | ||
|
||
public Optional<UpdateHandler<?>> getSelectedHandler() { | ||
return autoconfigFileForm.getSelectedHandler(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/de/gebit/intellij/autoconfig/create/CreateAutoconfigFileForm.form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.gebit.intellij.autoconfig.create.CreateAutoconfigFileForm"> | ||
<grid id="27dc6" binding="form" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<xy x="20" y="20" width="500" height="400"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="d216" class="javax.swing.JComboBox" binding="handlerSelection"> | ||
<constraints> | ||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
</component> | ||
<component id="42ea1" class="javax.swing.JLabel"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<text value="Select which Plugin to create the Autoconfig File for"/> | ||
</properties> | ||
</component> | ||
<vspacer id="56603"> | ||
<constraints> | ||
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
</vspacer> | ||
</children> | ||
</grid> | ||
</form> |
74 changes: 74 additions & 0 deletions
74
src/main/java/de/gebit/intellij/autoconfig/create/CreateAutoconfigFileForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package de.gebit.intellij.autoconfig.create; | ||
|
||
import com.intellij.uiDesigner.core.GridConstraints; | ||
import com.intellij.uiDesigner.core.GridLayoutManager; | ||
import com.intellij.uiDesigner.core.Spacer; | ||
import de.gebit.intellij.autoconfig.UpdateHandler; | ||
import lombok.Getter; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Form displaying a dropdown with {@link UpdateHandler}s to choose from | ||
*/ | ||
public class CreateAutoconfigFileForm { | ||
|
||
private JComboBox<HandlerComboBoxWrapper> handlerSelection; | ||
@Getter | ||
private JPanel form; | ||
|
||
public CreateAutoconfigFileForm(List<UpdateHandler<?>> handlers) { | ||
ComboBoxModel<HandlerComboBoxWrapper> comboBoxModel = new DefaultComboBoxModel<HandlerComboBoxWrapper>(handlers.stream().map(HandlerComboBoxWrapper::new).toArray(HandlerComboBoxWrapper[]::new)); | ||
handlerSelection.setModel(comboBoxModel); | ||
} | ||
|
||
{ | ||
// GUI initializer generated by IntelliJ IDEA GUI Designer | ||
// >>> IMPORTANT!! <<< | ||
// DO NOT EDIT OR ADD ANY CODE HERE! | ||
$$$setupUI$$$(); | ||
} | ||
|
||
/** | ||
* Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR call it in your code! | ||
* | ||
* @noinspection ALL | ||
*/ | ||
private void $$$setupUI$$$() { | ||
form = new JPanel(); | ||
form.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1)); | ||
handlerSelection = new JComboBox(); | ||
form.add(handlerSelection, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); | ||
final JLabel label1 = new JLabel(); | ||
label1.setText("Select which Plugin to create the Autoconfig File for"); | ||
form.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); | ||
final Spacer spacer1 = new Spacer(); | ||
form.add(spacer1, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); | ||
} | ||
|
||
/** | ||
* @noinspection ALL | ||
*/ | ||
public JComponent $$$getRootComponent$$$() { | ||
return form; | ||
} | ||
|
||
private record HandlerComboBoxWrapper(UpdateHandler<?> handler) { | ||
@Override | ||
public String toString() { | ||
return handler.getUpdaterName(); | ||
} | ||
} | ||
|
||
public Optional<UpdateHandler<?>> getSelectedHandler() { | ||
if (handlerSelection.getSelectedItem() instanceof HandlerComboBoxWrapper handlerComboBoxWrapper) { | ||
return Optional.of(handlerComboBoxWrapper.handler()); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters