|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package org.jboss.tools.intellij.openshift.ui.project; |
| 12 | + |
| 13 | +import com.intellij.openapi.actionSystem.ActionManager; |
| 14 | +import com.intellij.openapi.actionSystem.AnAction; |
| 15 | +import com.intellij.openapi.actionSystem.CommonShortcuts; |
| 16 | +import com.intellij.openapi.actionSystem.IdeActions; |
| 17 | +import com.intellij.openapi.project.DumbAwareAction; |
| 18 | +import com.intellij.openapi.project.Project; |
| 19 | +import com.intellij.openapi.ui.ComponentValidator; |
| 20 | +import com.intellij.openapi.ui.DialogWrapper; |
| 21 | +import com.intellij.openapi.ui.ValidationInfo; |
| 22 | +import com.intellij.openapi.util.text.StringUtil; |
| 23 | +import com.intellij.ui.PopupBorder; |
| 24 | +import com.intellij.ui.components.JBLabel; |
| 25 | +import com.intellij.ui.components.JBTextField; |
| 26 | +import com.intellij.util.ui.JBUI; |
| 27 | +import net.miginfocom.swing.MigLayout; |
| 28 | +import org.jetbrains.annotations.Nullable; |
| 29 | + |
| 30 | +import javax.swing.JComponent; |
| 31 | +import javax.swing.JLabel; |
| 32 | +import javax.swing.JPanel; |
| 33 | +import javax.swing.JRootPane; |
| 34 | +import javax.swing.RootPaneContainer; |
| 35 | +import javax.swing.SwingConstants; |
| 36 | +import java.awt.Point; |
| 37 | +import java.awt.Window; |
| 38 | +import java.util.Collection; |
| 39 | +import java.util.function.Supplier; |
| 40 | + |
| 41 | +import static org.jboss.tools.intellij.openshift.ui.SwingUtils.locationOrMouseLocation; |
| 42 | + |
| 43 | +public class CreateNewProjectDialog extends DialogWrapper { |
| 44 | + |
| 45 | + private static final String WIDTH = "300"; |
| 46 | + private final Collection<String> allProjects; |
| 47 | + private final Point location; |
| 48 | + private final Project project; |
| 49 | + private JBTextField newProjectTextField; |
| 50 | + |
| 51 | + private String newProject; |
| 52 | + |
| 53 | + public CreateNewProjectDialog( |
| 54 | + @Nullable Project project, |
| 55 | + Collection<String> allProjects, |
| 56 | + Point location) { |
| 57 | + super(project, false); |
| 58 | + this.project = project; |
| 59 | + this.allProjects = allProjects; |
| 60 | + this.location = location; |
| 61 | + init(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void init() { |
| 66 | + super.init(); |
| 67 | + Window dialogWindow = getPeer().getWindow(); |
| 68 | + JRootPane rootPane = ((RootPaneContainer) dialogWindow).getRootPane(); |
| 69 | + registerShortcuts(rootPane); |
| 70 | + setOKButtonText("Create"); |
| 71 | + setBorders(rootPane); |
| 72 | + setLocation(location); |
| 73 | + setTitle("Create New Project"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void setLocation(Point location) { |
| 78 | + super.setLocation(locationOrMouseLocation(location)); |
| 79 | + } |
| 80 | + |
| 81 | + private void registerShortcuts(JRootPane rootPane) { |
| 82 | + AnAction escape = ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_ESCAPE); |
| 83 | + DumbAwareAction.create(e -> closeImmediately()) |
| 84 | + .registerCustomShortcutSet( |
| 85 | + escape == null ? CommonShortcuts.ESCAPE : escape.getShortcutSet(), |
| 86 | + rootPane, |
| 87 | + myDisposable); |
| 88 | + } |
| 89 | + |
| 90 | + private void setBorders(JRootPane rootPane) { |
| 91 | + rootPane.setBorder(PopupBorder.Factory.create(true, true)); |
| 92 | + rootPane.setWindowDecorationStyle(JRootPane.NONE); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected @Nullable JComponent createCenterPanel() { |
| 97 | + JComponent panel = new JPanel(new MigLayout( |
| 98 | + "flowx, ins 0, gap 0, fillx, filly, hidemode 3", |
| 99 | + "[left]10[" + WIDTH +",fill]")); |
| 100 | + JLabel newActiveProjectLabel = new JBLabel("New project:", SwingConstants.LEFT); |
| 101 | + newActiveProjectLabel.setBorder(JBUI.Borders.empty(10, 0)); |
| 102 | + panel.add(newActiveProjectLabel, "left, bottom"); |
| 103 | + this.newProjectTextField = new JBTextField(); |
| 104 | + newProjectTextField.selectAll(); |
| 105 | + panel.add(newProjectTextField, "pushx, growx, wrap"); |
| 106 | + ComponentValidator activeProjectValidator = new ComponentValidator(myDisposable) |
| 107 | + .withValidator(new ActiveProjectValidator()) |
| 108 | + .installOn(newProjectTextField) |
| 109 | + .andRegisterOnDocumentListener(newProjectTextField); |
| 110 | + activeProjectValidator.revalidate(); |
| 111 | + return panel; |
| 112 | + } |
| 113 | + |
| 114 | + private void closeImmediately() { |
| 115 | + if (isVisible()) { |
| 116 | + doCancelAction(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected void doOKAction() { |
| 122 | + super.doOKAction(); |
| 123 | + this.newProject = newProjectTextField.getText(); |
| 124 | + } |
| 125 | + |
| 126 | + public String getNewProject() { |
| 127 | + return newProject; |
| 128 | + } |
| 129 | + |
| 130 | + private class ActiveProjectValidator implements Supplier<ValidationInfo> { |
| 131 | + |
| 132 | + @Override |
| 133 | + public ValidationInfo get() { |
| 134 | + String activeProject = newProjectTextField.getText(); |
| 135 | + ValidationInfo validation = getValidationInfo(activeProject); |
| 136 | + // update OK button |
| 137 | + setOKActionEnabled(validation.okEnabled); |
| 138 | + return validation; |
| 139 | + } |
| 140 | + |
| 141 | + private ValidationInfo getValidationInfo(String newProject) { |
| 142 | + ValidationInfo validation = new ValidationInfo("").withOKEnabled(); |
| 143 | + if (StringUtil.isEmptyOrSpaces(newProject)) { |
| 144 | + validation = new ValidationInfo("Provide project name").forComponent(newProjectTextField).asWarning(); |
| 145 | + } else if (allProjects.contains(newProject)) { |
| 146 | + validation = new ValidationInfo("Already exists, choose new name").forComponent(newProjectTextField).asWarning(); |
| 147 | + } |
| 148 | + return validation; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments