summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/mergeinfo/SelectBranchAction.java
blob: 10839c3b384dd2c40edf9876c4b9415ecc935ede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * 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.
 */
package org.jetbrains.idea.svn.mergeinfo;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Getter;
import com.intellij.openapi.vcs.changes.committed.DecoratorManager;
import com.intellij.openapi.vcs.changes.committed.LabeledComboBoxAction;
import com.intellij.util.Consumer;
import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.dialogs.WCInfoWithBranches;

import javax.swing.*;

public class SelectBranchAction extends LabeledComboBoxAction implements SelectRootListener, Getter<WCInfoWithBranches.Branch> {
  private static final Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.mergeinfo.SelectBranchAction");
  private final DecoratorManager myManager;
  private WCInfoWithBranches mySelectedRoot;
  public static final DefaultComboBoxModel EMPTY = new DefaultComboBoxModel();
  private final Consumer<String> mySelectionListener;

  public SelectBranchAction(final DecoratorManager manager, final Consumer<String> selectionListener) {
    super(SvnBundle.message("committed.changes.action.merge.highlighting.select.branch"));
    myManager = manager;
    mySelectionListener = selectionListener;
  }

  protected void selectionChanged(final Object selection) {
    mySelectionListener.consume(((WCInfoWithBranches.Branch) selection).getUrl());
    myManager.repaintTree();
  }

  public void selectionChanged(final WCInfoWithBranches wcInfoWithBranches) {
    final boolean valueChanges = (wcInfoWithBranches != null) && (!wcInfoWithBranches.equals(mySelectedRoot));
    mySelectedRoot = wcInfoWithBranches;

    if (valueChanges) {
      final ComboBoxModel model = createModel();
      setModel(model);
      myManager.repaintTree();
    }
  }

  public void force(final WCInfoWithBranches info) {
    mySelectedRoot = info;

    if (info == null) {
      setModel(EMPTY);
    } else {

      final WCInfoWithBranches.Branch selected = (WCInfoWithBranches.Branch) getSelected();
      final ComboBoxModel model = createModel();
      setModel(model);

      if (selected != null) {
        boolean selectedSet = false;
        for (int i = 0; i < model.getSize(); i++) {
          final WCInfoWithBranches.Branch element = (WCInfoWithBranches.Branch) model.getElementAt(i);
          if (selected.equals(element)) {
            model.setSelectedItem(element);
            selectedSet = true;
          }
        }
        if (! selectedSet) {
          model.setSelectedItem(model.getElementAt(0));
        }
      } else {
        if (model.getSize() > 0) {
          model.setSelectedItem(model.getElementAt(0));
        }
      }
    }

    myManager.repaintTree();
  }

  protected ComboBoxModel createModel() {
    if (mySelectedRoot == null) {
      return EMPTY;
    }

    final DefaultComboBoxModel model = new DefaultComboBoxModel(mySelectedRoot.getBranches().toArray());
    if (model.getSize() > 0) {
      selectionChanged(model.getElementAt(0));
    }
    return model;
  }

  public void enable(final boolean value) {
    myManager.repaintTree();
  }

  public WCInfoWithBranches.Branch get() {
    return (WCInfoWithBranches.Branch) getSelected();
  }
}