Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 8533cbd41d811a048dc6a5055ead320f32030067 (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
/***************************************************************************
 * Copyright (c) 2004 - 2009 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.net4j.util.ui.prefs;

import org.eclipse.net4j.util.internal.ui.bundle.OM;
import org.eclipse.net4j.util.om.pref.OMPreferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * @author Eike Stepper
 */
public abstract class OMPreferencePage extends PreferencePage implements IWorkbenchPreferencePage
{
  private IWorkbench workbench;

  private OMPreferenceStore preferenceStore;

  private SelectionListener selectionListener = new SelectionListener()
  {
    public void widgetDefaultSelected(SelectionEvent e)
    {
      dialogChanged();
    }

    public void widgetSelected(SelectionEvent e)
    {
      dialogChanged();
    }
  };

  private ModifyListener modifyListener = new ModifyListener()
  {
    public void modifyText(ModifyEvent e)
    {
      dialogChanged();
    }
  };

  public OMPreferencePage(OMPreferences preferences)
  {
    preferenceStore = new OMPreferenceStore(preferences);
  }

  public OMPreferences getPreferences()
  {
    return preferenceStore.getPreferences();
  }

  public IWorkbench getWorkbench()
  {
    return workbench;
  }

  public void init(IWorkbench workbench)
  {
    this.workbench = workbench;
  }

  @Override
  protected IPreferenceStore doGetPreferenceStore()
  {
    return preferenceStore;
  }

  protected final SelectionListener getSelectionListener()
  {
    return selectionListener;
  }

  protected final ModifyListener getModifyListener()
  {
    return modifyListener;
  }

  protected void dialogChanged()
  {
  }

  @Override
  protected final Control createContents(Composite parent)
  {
    try
    {
      Control control = createUI(parent);
      dialogChanged();
      addListeners(control);
      return control;
    }
    catch (RuntimeException ex)
    {
      OM.LOG.error(ex);
      throw ex;
    }
  }

  protected void addListeners(Control control)
  {
    if (control instanceof Text)
    {
      Text c = (Text)control;
      c.addModifyListener(modifyListener);
    }

    if (control instanceof Combo)
    {
      Combo c = (Combo)control;
      c.addModifyListener(modifyListener);
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof CCombo)
    {
      CCombo c = (CCombo)control;
      c.addModifyListener(modifyListener);
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof List)
    {
      List c = (List)control;
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof DateTime)
    {
      DateTime c = (DateTime)control;
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof Table)
    {
      Table c = (Table)control;
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof Tree)
    {
      Table c = (Table)control;
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof Button)
    {
      Button c = (Button)control;
      c.addSelectionListener(selectionListener);
    }

    if (control instanceof Composite)
    {
      Composite c = (Composite)control;
      for (Control child : c.getChildren())
      {
        addListeners(child);
      }
    }
  }

  protected abstract Control createUI(Composite parent);
}

Back to the top