org.codehaus.cargo.intellijidea.CargoContainerEditor.java Source code

Java tutorial

Introduction

Here is the source code for org.codehaus.cargo.intellijidea.CargoContainerEditor.java

Source

/*
 * ========================================================================
 *
 * Copyright 2004-2005 Vincent Massol.
 *
 * 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 org.codehaus.cargo.intellijidea;

import java.awt.Dimension;
import java.awt.Insets;
import java.io.File;

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

import com.intellij.j2ee.appServerIntegrations.ApplicationServerPersistentDataEditor;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;

/**
 * Editor for persistent configuration data ({@link CargoContainerModel}) for an
 * application server. E.g. home and configuration directories.
 *
 * @version $Id: CargoTask.jagva 391 2005-07-04 13:07:42Z vmassol $
 * @see CargoApplicationServerHelper#createConfigurable()
 */
public class CargoContainerEditor extends ApplicationServerPersistentDataEditor {
    /**
     * Panel that contains configuration controls..
     */
    private JPanel panel;
    /**
     * HomeDir textfield.
     */
    private TextFieldWithBrowseButton homeDir;

    /**
     * Constructor.
     */
    public CargoContainerEditor() {
        initChooser(homeDir, "Home Directory", "Select home directory");
    }

    /**
     * Init the chooser.
     *
     * @param field       textField
     * @param title       title
     * @param description description
     */
    private void initChooser(TextFieldWithBrowseButton field, String title, String description) {
        field.getTextField().setEditable(true);
        field.addBrowseFolderListener(title, description, null,
                new FileChooserDescriptor(false, true, false, false, false, false));
    }

    /**
     * Resets data. Transfers data from the passed object to the editor.
     *
     * @param data data
     */
    public void resetEditorFrom(Object data) {
        final CargoContainerModel cargoContainerModel = (CargoContainerModel) data;
        homeDir.setText(cargoContainerModel.getHomeDir());
    }

    /**
     * Transfers editor data to passed object.
     *
     * @param data persistent data
     * @throws ConfigurationException if something does not work
     */
    public void applyEditorTo(Object data) throws ConfigurationException {
        if (homeDir.getText() == null || homeDir.getText().length() == 0) {
            throw new ConfigurationException("You have to enter a valid home directory.");
        }
        final File home = new File(homeDir.getText()).getAbsoluteFile();
        checkIsDirectory(home);
        // more checks...?
        final CargoContainerModel cargoContainerModel = (CargoContainerModel) data;
        cargoContainerModel.setHomeDir(home.getAbsolutePath());
    }

    /**
     * Checks whether the file is an existing directory.
     *
     * @param file file
     * @throws ConfigurationException if the directory cannot be found
     */
    private void checkIsDirectory(File file) throws ConfigurationException {
        if (!file.isDirectory()) {
            throw new ConfigurationException("Can't find directory '" + ((String) file.getAbsolutePath()) + "'.");
        }
    }

    /**
     * Creates the editor.
     *
     * @return editor panel
     */
    public JComponent createEditor() {
        return panel;
    }

    /**
     * Clears used resources.
     */
    public void disposeEditor() {
    }

    /**
     * Creates a center panel.
     *
     * @return panel
     */
    protected JComponent createCenterPanel() {
        return panel;
    }

    {
        // 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!
     */
    private void $$$setupUI$$$() {
        panel = new JPanel();
        panel.setLayout(new GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
        final Spacer spacer1 = new Spacer();
        panel.add(spacer1, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null));
        final JLabel label1 = new JLabel();
        label1.setText("Home directory:");
        label1.setToolTipText("The container's home directory");
        panel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null));
        homeDir = new TextFieldWithBrowseButton();
        panel.add(homeDir,
                new GridConstraints(0, 1, 1, 2, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(200, -1), null));
    }
}