com.intellij.ide.util.projectWizard.ProjectJdkStep.java Source code

Java tutorial

Introduction

Here is the source code for com.intellij.ide.util.projectWizard.ProjectJdkStep.java

Source

/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * 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.
 */

/*
 * Created by IntelliJ IDEA.
 * User: Anna.Kozlova
 * Date: 16-Aug-2006
 * Time: 18:01:13
 */
package com.intellij.ide.util.projectWizard;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.intellij.ide.IdeBundle;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ui.configuration.ProjectSdksConfigurable;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.MultiLineLabelUI;

/**
 * @author Eugene Zhuravlev
 *         Date: Jan 21, 2004
 */
public class ProjectJdkStep extends ModuleWizardStep {
    private final WizardContext myContext;

    protected final ProjectSdksConfigurable myProjectSdksConfigurable;

    private final JComponent myJDKsComponent;

    public ProjectJdkStep(final WizardContext context) {
        myContext = context;
        myProjectSdksConfigurable = new ProjectSdksConfigurable(ProjectManager.getInstance().getDefaultProject());
        myProjectSdksConfigurable.reset();
        myJDKsComponent = myProjectSdksConfigurable.createComponent();
    }

    public JComponent getPreferredFocusedComponent() {
        return myJDKsComponent;
    }

    public String getHelpId() {
        return "reference.dialogs.new.project.fromScratch.sdk";
    }

    public JComponent getComponent() {
        final JLabel label = new JLabel(IdeBundle.message("prompt.please.select.project.jdk"));
        label.setUI(new MultiLineLabelUI());
        final JPanel panel = new JPanel(new GridBagLayout()) {
            public Dimension getPreferredSize() {
                return new Dimension(-1, 200);
            }
        };
        panel.add(label, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1, 0,
                GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        myJDKsComponent.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0));
        panel.add(myJDKsComponent, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1, 1.0,
                GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
        return panel;
    }

    public void updateDataModel() {

    }

    public Sdk getJdk() {
        return myProjectSdksConfigurable.getSelectedJdk();
    }

    public Icon getIcon() {
        return myContext.getStepIcon();
    }

    public boolean validate() throws ConfigurationException {
        final Sdk jdk = myProjectSdksConfigurable.getSelectedJdk();
        if (jdk == null && !ApplicationManager.getApplication().isUnitTestMode()) {
            int result = Messages.showOkCancelDialog(IdeBundle.message("prompt.confirm.project.no.jdk"),
                    IdeBundle.message("title.no.jdk.specified"), Messages.getWarningIcon());
            if (result != 0) {
                return false;
            }
        }
        myProjectSdksConfigurable.apply();
        return true;
    }

    @Override
    public String getName() {
        return "Project JDK";
    }

    public void disposeUIResources() {
        super.disposeUIResources();
        myProjectSdksConfigurable.disposeUIResources();
    }
}