Java tutorial
/** * Copyright 2014 Microsoft Open Technologies Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.microsoftopentechnologies.intellij.forms; import com.intellij.ide.util.PropertiesComponent; import com.intellij.openapi.fileChooser.FileChooser; import com.intellij.openapi.fileChooser.FileChooserDescriptor; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.Consumer; import javax.swing.*; import java.awt.event.*; public class OpenSSLFinderForm extends JDialog { private JPanel contentPane; private JButton buttonOK; private JButton buttonCancel; private JTextField txtFile; private JButton btnBrowse; public OpenSSLFinderForm() { setContentPane(contentPane); setModal(true); getRootPane().setDefaultButton(buttonOK); buttonOK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onOK(); } }); buttonCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onCancel(); } }); // call onCancel() when cross is clicked setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { onCancel(); } }); // call onCancel() on ESCAPE contentPane.registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { onCancel(); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); btnBrowse.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false, false, false) { @Override public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) { try { return file.isDirectory() || file.getName().toLowerCase().contains("openssl"); } catch (Throwable t) { return super.isFileVisible(file, showHiddenFiles); } } @Override public boolean isFileSelectable(VirtualFile file) { return file.getName().toLowerCase().contains("openssl"); } }; fileChooserDescriptor.setTitle("Choose OpenSSL executable"); FileChooser.chooseFile(fileChooserDescriptor, null, null, new Consumer<VirtualFile>() { @Override public void consume(VirtualFile virtualFile) { if (virtualFile != null) txtFile.setText(virtualFile.getParent().getPath()); } }); } }); } private void onOK() { if (txtFile.getText() == null || txtFile.getText().isEmpty()) JOptionPane.showMessageDialog(this, "Must select the OpenSSL executable location.", "OpenSSL", JOptionPane.ERROR_MESSAGE); else { PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(); propertiesComponent.setValue("MSOpenSSLPath", txtFile.getText()); dispose(); } } private void onCancel() { // add your code here if necessary dispose(); } }