summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnMergeInfoRootPanelManual.java
blob: d4b48281f7ca95f514716f95e29f379b88b26d2a (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * 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.history;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.FixedSizeButton;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.NullableFunction;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.branchConfig.SvnBranchMapperManager;
import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.SvnUtil;
import org.jetbrains.idea.svn.branchConfig.SelectBranchPopup;
import org.jetbrains.idea.svn.branchConfig.SvnBranchConfigurationNew;
import org.jetbrains.idea.svn.dialogs.WCInfoWithBranches;
import org.jetbrains.idea.svn.integrate.IntegratedSelectedOptionsDialog;
import org.jetbrains.idea.svn.integrate.WorkingCopyInfo;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.util.SVNPathUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class SvnMergeInfoRootPanelManual {
  private JCheckBox myInclude;
  private JLabel myHereLabel;
  private JLabel myThereLabel;
  private TextFieldWithBrowseButton myBranchField;
  private FixedSizeButton myFixedSelectLocal;
  private JPanel myContentPanel;
  private JTextArea myUrlText;
  private JTextArea myLocalArea;
  private JTextArea myMixedRevisions;

  private final Project myProject;
  private final NullableFunction<WCInfoWithBranches, WCInfoWithBranches> myRefresher;
  private final Runnable myListener;
  private boolean myOnlyOneRoot;
  private WCInfoWithBranches myInfo;
  private final Map<String, String> myBranchToLocal;
  private WCInfoWithBranches.Branch mySelectedBranch;

  public SvnMergeInfoRootPanelManual(final Project project, final NullableFunction<WCInfoWithBranches, WCInfoWithBranches> refresher,
                                     final Runnable listener,
                                     final boolean onlyOneRoot,
                                     final WCInfoWithBranches info) {
    myOnlyOneRoot = onlyOneRoot;
    myInfo = info;
    myProject = project;
    myRefresher = refresher;
    myListener = listener;
    myBranchToLocal = new HashMap<String, String>();

    init();
    myInclude.setVisible(! onlyOneRoot);
    initWithData();
  }

  private void initWithData() {
    myInclude.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent e) {
        myListener.run();
      }
    });

    final String path = myInfo.getPath();
    final String urlString = myInfo.getUrl().toString();
    myUrlText.setText(urlString);

    myFixedSelectLocal.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent e) {
        if (mySelectedBranch != null) {
          final Pair<WorkingCopyInfo,SVNURL> info =
            IntegratedSelectedOptionsDialog.selectWorkingCopy(myProject, myInfo.getUrl(), mySelectedBranch.getUrl(), false, null,
                                                              null);
          if (info != null) {
            final String local = info.getFirst().getLocalPath();
            calculateBranchPathByBranch(mySelectedBranch.getUrl(), local);
          }
          myListener.run();
        }
      }
    });

    myBranchField.getTextField().setEditable(false);
    myBranchField.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent e) {
        final VirtualFile vf = SvnUtil.getVirtualFile(myInfo.getPath());
        if (vf != null) {
          SelectBranchPopup.show(myProject, vf, new SelectBranchPopup.BranchSelectedCallback() {
            public void branchSelected(final Project project, final SvnBranchConfigurationNew configuration, final String url, final long revision) {
              refreshSelectedBranch(url);
              calculateBranchPathByBranch(mySelectedBranch.getUrl(), null);
              myListener.run();
            }
          }, SvnBundle.message("select.branch.popup.general.title"));
        }
      }
    });

    if (myInfo.getBranches().isEmpty()) {
      calculateBranchPathByBranch(null, null);
    } else {
      final WCInfoWithBranches.Branch branch = myInfo.getBranches().get(0);
      refreshSelectedBranch(branch.getUrl());
      calculateBranchPathByBranch(mySelectedBranch.getUrl(), null);
    }
  }

  private void init() {
    myContentPanel = new JPanel(new GridBagLayout()) {
      @Override
      public void setBounds(final Rectangle r) {
        super.setBounds(r);
      }
    };
    myContentPanel.setMinimumSize(new Dimension(200, 100));

    final GridBagConstraints gb =
      new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0);

    myInclude = new JCheckBox();
    gb.fill = GridBagConstraints.NONE;
    gb.weightx = 0;
    myContentPanel.add(myInclude, gb);

    // newline
    myHereLabel = new JLabel("From:");
    ++ gb.gridy;
    gb.gridx = 0;
    myContentPanel.add(myHereLabel, gb);

    myUrlText = new JTextArea();
    myUrlText.setLineWrap(true);
    myUrlText.setBackground(UIUtil.getLabelBackground());
    myUrlText.setWrapStyleWord(true);
    gb.weightx = 1;
    ++ gb.gridx;
    gb.gridwidth = 2;
    gb.fill = GridBagConstraints.HORIZONTAL;
    myContentPanel.add(myUrlText, gb);

    // newline
    gb.fill = GridBagConstraints.NONE;
    myThereLabel = new JLabel("To:");
    gb.weightx = 0;
    gb.gridwidth = 1;
    ++ gb.gridy;
    gb.gridx = 0;
    myContentPanel.add(myThereLabel, gb);

    myBranchField = new TextFieldWithBrowseButton();
    gb.weightx = 1;
    ++ gb.gridx;
    gb.gridwidth = 2;
    gb.fill = GridBagConstraints.HORIZONTAL;
    myContentPanel.add(myBranchField, gb);

    // newline
    gb.gridx = 1;
    ++ gb.gridy;
    gb.gridwidth = 1;
    myLocalArea = new JTextArea();
    myLocalArea.setBackground(UIUtil.getLabelBackground());
    myLocalArea.setLineWrap(true);
    myLocalArea.setWrapStyleWord(true);
    myContentPanel.add(myLocalArea, gb);

    ++ gb.gridx;
    gb.weightx = 0;
    gb.fill = GridBagConstraints.NONE;
    myFixedSelectLocal = new FixedSizeButton(20);
    myContentPanel.add(myFixedSelectLocal, gb);

    ++ gb.gridy;
    gb.gridx = 0;
    gb.gridwidth = 2;
    myMixedRevisions = new JTextArea("Mixed Revision Working Copy");
    myMixedRevisions.setForeground(Color.red);
    myMixedRevisions.setBackground(myContentPanel.getBackground());
    myContentPanel.add(myMixedRevisions, gb);

    myMixedRevisions.setVisible(false);
  }

  public void setMixedRevisions(final boolean value) {
    myMixedRevisions.setVisible(value);
  }

  @Nullable
  private String getLocal(@NotNull final String url, @Nullable final String localPath) {
    final Set<String> paths = SvnBranchMapperManager.getInstance().get(url);
    if (paths != null && (! paths.isEmpty())) {
      if (localPath != null) {
        // check whether it is still actual
        for (String path : paths) {
          if (path.equals(localPath)) {
            return path;
          }
        }
      } else {
        final java.util.List<String> list = new ArrayList<String>(paths);
        Collections.sort(list);
        return list.get(0);
      }
    }
    return null;
  }

  // always assign to local area here
  private void calculateBranchPathByBranch(final String url, final String localPath) {
    final String local = url == null ? null : getLocal(url, localPath == null ? myBranchToLocal.get(url) : localPath);
    if (local == null) {
      myLocalArea.setForeground(Color.red);
      myLocalArea.setText(SvnBundle.message("tab.repository.merge.panel.root.panel.select.local"));
    } else {
      myLocalArea.setForeground(UIUtil.getInactiveTextColor());
      myLocalArea.setText(local);
      myBranchToLocal.put(url, local);
    }
  }

  // always assign to selected branch here
  private void refreshSelectedBranch(final String url) {
    final String branch = SVNPathUtil.tail(url);
    myBranchField.setText(branch);

    if (initSelectedBranch(url)) return;
    myInfo = myRefresher.fun(myInfo);
    initSelectedBranch(url);
  }

  private boolean initSelectedBranch(final String branch) {
    for (WCInfoWithBranches.Branch item : myInfo.getBranches()) {
      if (item.getUrl().equals(branch)) {
        mySelectedBranch = item;
        return true;
      }
    }
    return false;
  }

  public void setDirection(final boolean fromHere) {
    if (fromHere) {
      myHereLabel.setText(SvnBundle.message("tab.repository.merge.panel.root.panel.from"));
      myThereLabel.setText(SvnBundle.message("tab.repository.merge.panel.root.panel.to"));
    } else {
      myThereLabel.setText(SvnBundle.message("tab.repository.merge.panel.root.panel.from"));
      myHereLabel.setText(SvnBundle.message("tab.repository.merge.panel.root.panel.to"));
    }
  }

  public void setOnlyOneRoot(final boolean onlyOneRoot) {
    myOnlyOneRoot = onlyOneRoot;
    myInclude.setEnabled(! myOnlyOneRoot);
    myInclude.setSelected(true);
  }

  public void selectCheckbox(final boolean select) {
    myInclude.setSelected(select);
  }

  public JPanel getContentPanel() {
    return myContentPanel;
  }

  private void createUIComponents() {
    myFixedSelectLocal = new FixedSizeButton(20);
  }

  public InfoHolder getInfo() {
    return new InfoHolder(mySelectedBranch, getLocalBranch(), myInclude.isSelected());
  }

  public void initSelection(final InfoHolder holder) {
    myInclude.setSelected(holder.isEnabled());
    if (holder.getBranch() != null) {
      refreshSelectedBranch(holder.getBranch().getUrl());
      calculateBranchPathByBranch(mySelectedBranch.getUrl(), holder.getLocal());
    }
  }

  public static class InfoHolder {
    private final WCInfoWithBranches.Branch myBranch;
    private final String myLocal;
    private final boolean myEnabled;

    public InfoHolder(final WCInfoWithBranches.Branch branch, final String local, final boolean enabled) {
      myBranch = branch;
      myLocal = local;
      myEnabled = enabled;
    }

    public WCInfoWithBranches.Branch getBranch() {
      return myBranch;
    }

    public String getLocal() {
      return myLocal;
    }

    public boolean isEnabled() {
      return myEnabled;
    }
  }

  public WCInfoWithBranches getWcInfo() {
    return myInfo;
  }

  public WCInfoWithBranches.Branch getBranch() {
    return mySelectedBranch;
  }

  public String getLocalBranch() {
    if (mySelectedBranch != null) {
      return myBranchToLocal.get(mySelectedBranch.getUrl());
    }
    return null;
  }

  public boolean isEnabled() {
    return myOnlyOneRoot || myInclude.isSelected();
  }
}