Skip to main content
summaryrefslogtreecommitdiffstats
blob: f36151c4db506f8c46d22f2f307acb9d41b465f7 (plain) (blame)
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
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.ui.widgets;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.internal.ui.bundle.OM;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.ui.CDOItemProvider;

import org.eclipse.net4j.util.collection.IHistory;
import org.eclipse.net4j.util.collection.PreferenceHistory;
import org.eclipse.net4j.util.om.pref.OMPreference;
import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.ValidationContext;
import org.eclipse.net4j.util.ui.ValidationParticipant;
import org.eclipse.net4j.util.ui.widgets.HistoryText;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Listener;

/**
 * UI widget that provides visualization of all available {@link org.eclipse.emf.cdo.common.branch.CDOBranch branches},
 * and with the capability to select one.
 * 
 * @author Eike Stepper
 * @since 4.0
 */
public class SelectBranchComposite extends Composite implements ValidationParticipant
{
  private ValidationContext validationContext;

  private CDOSession session;

  private CDOBranch branch;

  private IHistory<String> branchHistory;

  private HistoryText branchText;

  private TreeViewer branchViewer;

  public SelectBranchComposite(Composite parent, int style, CDOSession session, CDOBranch branch)
  {
    super(parent, style);
    this.session = session;
    this.branch = branch;

    GridLayout gridLayout = UIUtil.createGridLayout(1);
    gridLayout.marginHeight = 5;
    gridLayout.horizontalSpacing = 5;
    gridLayout.verticalSpacing = 5;

    setLayout(gridLayout);

    String prefName = "PREF_HISTORY_BRANCHES-" + session.getRepositoryInfo().getUUID(); //$NON-NLS-1$
    OMPreference<String[]> pref = OM.PREFS.getArray(prefName);
    if (pref == null)
    {
      pref = OM.PREFS.initArray(prefName);
    }

    branchHistory = new PreferenceHistory(pref);

    branchText = new HistoryText(this, SWT.BORDER | SWT.SINGLE, branchHistory);
    branchText.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    branchText.getCombo().addModifyListener(new ModifyListener()
    {
      public void modifyText(ModifyEvent e)
      {
        setBranchFromPath();
      }
    });

    CDOItemProvider itemProvider = new CDOItemProvider(null);
    branchViewer = new TreeViewer(this, SWT.BORDER | SWT.SINGLE);
    branchViewer.getTree().setLayoutData(UIUtil.createGridData());
    branchViewer.setLabelProvider(itemProvider);
    branchViewer.setContentProvider(itemProvider);
    branchViewer.setInput(session.getBranchManager());
    branchViewer.addSelectionChangedListener(new ISelectionChangedListener()
    {
      public void selectionChanged(SelectionChangedEvent event)
      {
        setBranchFromViewer();
      }
    });

    branchText.setFocus();
    setBranchFromPath();
  }

  public ValidationContext getValidationContext()
  {
    return validationContext;
  }

  public void setValidationContext(ValidationContext validationContext)
  {
    this.validationContext = validationContext;
  }

  public CDOSession getSession()
  {
    return session;
  }

  public CDOBranch getBranch()
  {
    return branch;
  }

  public HistoryText getBranchText()
  {
    return branchText;
  }

  public TreeViewer getBranchViewer()
  {
    return branchViewer;
  }

  public void rememberSettings()
  {
    branchText.getHistory().add(branch.getPathName());
  }

  @Override
  public void addListener(int eventType, Listener listener)
  {
    super.addListener(eventType, listener);
    branchText.addListener(eventType, listener);
    branchViewer.getTree().addListener(eventType, listener);
  }

  @Override
  public void removeListener(int eventType, Listener listener)
  {
    super.removeListener(eventType, listener);
    branchText.removeListener(eventType, listener);
    branchViewer.getTree().removeListener(eventType, listener);
  }

  protected void branchChanged(CDOBranch newBranch)
  {
  }

  private void setBranchFromPath()
  {
    String branchPath = branchText.getText();
    CDOBranch newBranch = session.getBranchManager().getBranch(branchPath);
    if (newBranch != branch)
    {
      branch = newBranch;
      if (newBranch != null)
      {
        branchViewer.reveal(branch);
        branchViewer.setSelection(new StructuredSelection(branch));
      }
      else
      {
        branchViewer.setSelection(StructuredSelection.EMPTY);
      }

      branchChanged(newBranch);
    }

    validate();
  }

  private void setBranchFromViewer()
  {
    IStructuredSelection selection = (IStructuredSelection)branchViewer.getSelection();
    CDOBranch newBranch = (CDOBranch)selection.getFirstElement();
    if (newBranch != branch)
    {
      branch = newBranch;
      branchText.setText(branch.getPathName());
      branchChanged(newBranch);
    }

    validate();
  }

  private void validate()
  {
    if (validationContext != null)
    {
      validationContext.setValidationError(branchText.getCombo(), branch != null ? null : "Branch does not exist.");
    }
  }
}

Back to the top