Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4fa32e9053f30210a0f8364de4f8414c51072495 (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
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (c) 2004 - 2011 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:
 *    Victor Roldan Betancort - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.ui.widgets;

import org.eclipse.emf.cdo.internal.ui.bundle.OM;
import org.eclipse.emf.cdo.internal.ui.messages.Messages;

import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.collection.IHistory;
import org.eclipse.net4j.util.collection.PreferenceHistory;
import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.widgets.HistoryText;
import org.eclipse.net4j.util.ui.widgets.PreferenceButton;

import org.eclipse.emf.common.util.URI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

/**
 * @author Victor Roldan Betancort
 * @since 2.0
 */
public class SessionComposite extends Composite
{
  private IHistory<String> connectorHistory = new PreferenceHistory(OM.PREF_HISTORY_CONNECTORS);

  private IHistory<String> repositoryHistory = new PreferenceHistory(OM.PREF_HISTORY_REPOSITORIES);

  private HistoryText connectorText;

  private Label exampleLabel;

  private HistoryText repositoryText;

  private PreferenceButton automaticButton;

  private String connectorDescription;

  private String repositoryName;

  private boolean automaticRegistry;

  public SessionComposite(Composite parent, int style)
  {
    super(parent, style);
    setLayoutData(UIUtil.createGridData());
    setLayout(new GridLayout(2, false));

    new Label(this, SWT.NONE).setText(Messages.getString("SessionComposite.0")); //$NON-NLS-1$
    connectorText = new HistoryText(this, SWT.BORDER | SWT.SINGLE, connectorHistory);
    connectorText.getCombo().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
    connectorText.getCombo().addModifyListener(new ModifyListener()
    {
      public void modifyText(ModifyEvent e)
      {
        connectorDescription = connectorText.getText();
      }
    });

    if (connectorHistory.isEmpty())
    {
      new Label(this, SWT.NONE);
      exampleLabel = new Label(this, SWT.NONE);
      exampleLabel.setText(Messages.getString("SessionComposite.1")); //$NON-NLS-1$
      exampleLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
    }

    new Label(this, SWT.NONE).setText(Messages.getString("SessionComposite.2")); //$NON-NLS-1$
    repositoryText = new HistoryText(this, SWT.BORDER | SWT.SINGLE, repositoryHistory);
    repositoryText.getCombo().setLayoutData(new GridData(150, SWT.DEFAULT));
    repositoryText.getCombo().addModifyListener(new ModifyListener()
    {
      public void modifyText(ModifyEvent e)
      {
        repositoryName = repositoryText.getText();
      }
    });

    new Label(this, SWT.NONE);
    automaticButton = new PreferenceButton(this, SWT.CHECK, Messages.getString("SessionComposite.3"), //$NON-NLS-1$
        OM.PREF_AUTOMATIC_PACKAGE_REGISTRY);
    automaticButton.getButton().addSelectionListener(new SelectionAdapter()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        automaticRegistry = automaticButton.getSelection();
      }
    });

    connectorText.setFocus();
    connectorText.getCombo().addFocusListener(new FocusListener()
    {
      public void focusGained(FocusEvent e)
      {
        if (exampleLabel != null)
        {
          exampleLabel.setVisible(true);
        }
      }

      public void focusLost(FocusEvent e)
      {
        if (exampleLabel != null)
        {
          exampleLabel.setVisible(false);
        }
      }
    });

    connectorDescription = connectorText.getText();
    repositoryName = repositoryText.getText();
    automaticRegistry = automaticButton.getSelection();
  }

  public IHistory<String> getConnectorHistory()
  {
    return connectorHistory;
  }

  public IHistory<String> getRepositoryHistory()
  {
    return repositoryHistory;
  }

  public HistoryText getConnectorText()
  {
    return connectorText;
  }

  public Label getExampleLabel()
  {
    return exampleLabel;
  }

  public HistoryText getRepositoryText()
  {
    return repositoryText;
  }

  public PreferenceButton getAutomaticButton()
  {
    return automaticButton;
  }

  public boolean isAutomaticRegistry()
  {
    return automaticRegistry;
  }

  public String getSessionDescription()
  {
    StringBuilder builder = new StringBuilder();
    builder.append(connectorDescription);
    builder.append("?repositoryName="); //$NON-NLS-1$
    builder.append(repositoryName);
    if (automaticRegistry)
    {
      builder.append("&automaticPackageRegistry=true"); //$NON-NLS-1$
    }

    return builder.toString();
  }

  /**
   * @since 3.0
   */
  public boolean isDescriptionValid()
  {
    URI uri = URI.createURI(getSessionDescription());
    return uri.hasAuthority() && uri.host() != null && !StringUtil.isEmpty(uri.host())
        && !StringUtil.isEmpty(repositoryName);
  }

  public void rememberSettings()
  {
    connectorText.getHistory().add(connectorDescription);
    repositoryText.getHistory().add(repositoryName);
    automaticButton.getPreference().setValue(automaticRegistry);
  }

  @Override
  public void addListener(int eventType, Listener listener)
  {
    super.addListener(eventType, listener);
    connectorText.addListener(eventType, listener);
    repositoryText.addListener(eventType, listener);
    automaticButton.addListener(eventType, listener);
  }

  @Override
  public void removeListener(int eventType, Listener listener)
  {
    super.removeListener(eventType, listener);
    connectorText.removeListener(eventType, listener);
    repositoryText.removeListener(eventType, listener);
    automaticButton.removeListener(eventType, listener);
  }
}

Back to the top