bazaar4idea.ui.BzrSwitchDialog.java Source code

Java tutorial

Introduction

Here is the source code for bazaar4idea.ui.BzrSwitchDialog.java

Source

// Copyright 2009 Victor Iacoban
//
// 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 bazaar4idea.ui;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import bazaar4idea.command.BzrTagBranchCommand;
import bazaar4idea.data.BzrTagBranch;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.List;

public class BzrSwitchDialog extends DialogWrapper {

    private final Project project;

    private JPanel contentPanel;

    private JRadioButton branchOption;

    private JRadioButton revisionOption;

    private JTextField revisionTxt;

    private JCheckBox cleanCbx;

    private JComboBox branchSelector;

    private JRadioButton tagOption;

    private JComboBox tagSelector;

    private BzrRepositorySelectorComponent hgRepositorySelectorComponent;

    public BzrSwitchDialog(Project project) {
        super(project, false);
        this.project = project;
        hgRepositorySelectorComponent.setTitle("Select repository to switch");
        hgRepositorySelectorComponent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateRepository();
            }
        });

        ChangeListener changeListener = new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                update();
            }
        };
        branchOption.addChangeListener(changeListener);
        tagOption.addChangeListener(changeListener);
        revisionOption.addChangeListener(changeListener);

        setTitle("Switch working directory");
        init();
    }

    public void setRoots(Collection<VirtualFile> repos) {
        hgRepositorySelectorComponent.setRoots(repos);
        updateRepository();
    }

    public VirtualFile getRepository() {
        return hgRepositorySelectorComponent.getRepository();
    }

    public BzrTagBranch getTag() {
        return (BzrTagBranch) tagSelector.getSelectedItem();
    }

    public boolean isTagSelected() {
        return tagOption.isSelected();
    }

    public BzrTagBranch getBranch() {
        return (BzrTagBranch) branchSelector.getSelectedItem();
    }

    public boolean isBranchSelected() {
        return branchOption.isSelected();
    }

    public String getRevision() {
        return revisionTxt.getText();
    }

    public boolean isRevisionSelected() {
        return revisionOption.isSelected();
    }

    public boolean isRemoveLocalChanges() {
        return cleanCbx.isSelected();
    }

    private void update() {
        setOKActionEnabled(validateOptions());
        revisionTxt.setEnabled(revisionOption.isSelected());
        branchSelector.setEnabled(branchOption.isSelected());
        tagSelector.setEnabled(tagOption.isSelected());
    }

    private void updateRepository() {
        VirtualFile repo = hgRepositorySelectorComponent.getRepository();
        loadBranches(repo);
        loadTags(repo);
        update();
    }

    private void loadBranches(VirtualFile root) {
        List<BzrTagBranch> branches = new BzrTagBranchCommand(project, root).listBranches();
        branchSelector.setModel(new DefaultComboBoxModel(branches.toArray()));
    }

    private void loadTags(VirtualFile root) {
        List<BzrTagBranch> tags = new BzrTagBranchCommand(project, root).listTags();
        tagSelector.setModel(new DefaultComboBoxModel(tags.toArray()));
    }

    private boolean validateOptions() {
        return true;
    }

    protected JComponent createCenterPanel() {
        return contentPanel;
    }

    {
        // 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$$$() {
        contentPanel = new JPanel();
        contentPanel.setLayout(new GridLayoutManager(4, 2, new Insets(0, 0, 0, 0), -1, -1));
        final Spacer spacer1 = new Spacer();
        contentPanel.add(spacer1,
                new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1,
                        GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        final Spacer spacer2 = new Spacer();
        contentPanel.add(spacer2,
                new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        cleanCbx = new JCheckBox();
        cleanCbx.setText("Overwrite locally modified files (no backup)");
        cleanCbx.setMnemonic('O');
        cleanCbx.setDisplayedMnemonicIndex(0);
        contentPanel.add(cleanCbx,
                new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPanel.add(panel1,
                new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null,
                        null, 0, false));
        panel1.setBorder(BorderFactory.createTitledBorder("Switch to:"));
        branchOption = new JRadioButton();
        branchOption.setSelected(true);
        branchOption.setText("Branch");
        branchOption.setMnemonic('B');
        branchOption.setDisplayedMnemonicIndex(0);
        panel1.add(branchOption,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        branchSelector = new JComboBox();
        branchSelector.setEnabled(true);
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        branchSelector.setModel(defaultComboBoxModel1);
        panel1.add(branchSelector,
                new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0,
                        false));
        revisionOption = new JRadioButton();
        revisionOption.setSelected(false);
        revisionOption.setText("Revision");
        revisionOption.setMnemonic('R');
        revisionOption.setDisplayedMnemonicIndex(0);
        panel1.add(revisionOption,
                new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        revisionTxt = new JTextField();
        revisionTxt.setEnabled(false);
        revisionTxt.setText("");
        panel1.add(revisionTxt,
                new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null,
                        new Dimension(150, -1), null, 0, false));
        tagOption = new JRadioButton();
        tagOption.setText("Tag");
        tagOption.setMnemonic('T');
        tagOption.setDisplayedMnemonicIndex(0);
        panel1.add(tagOption,
                new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        tagSelector = new JComboBox();
        panel1.add(tagSelector,
                new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0,
                        false));
        hgRepositorySelectorComponent = new BzrRepositorySelectorComponent();
        contentPanel.add(hgRepositorySelectorComponent.$$$getRootComponent$$$(),
                new GridConstraints(0, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null,
                        null, 0, false));
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(revisionOption);
        buttonGroup.add(branchOption);
        buttonGroup.add(tagOption);
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return contentPanel;
    }
}