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: d61142eafc6bdf7cf204bcdea456bce7362e4b33 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xsd.ui.internal.properties;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wst.xsd.ui.internal.util.ViewUtility;


public abstract class OptionsTextCellEditor extends CellEditor implements SelectionListener, KeyListener
{
	private Composite fEditor;
	protected Text fText;
  protected boolean isTextReadOnly;
	Button moreButton;
	Shell dialog;

	protected Object fValue;
	int selection;
	Object typeObject;
		
	private class ComboCellLayout extends Layout 
	{
		public void layout(Composite editor, boolean force)
		{
			Rectangle bounds= editor.getClientArea();
			Point size= moreButton.computeSize(SWT.DEFAULT, bounds.height, force);
			fText.setBounds(0, 0, bounds.width - size.x, bounds.height);
			moreButton.setBounds(bounds.width - size.x, 0, size.x, size.y);
		}
		
		public Point computeSize(Composite editor, int wHint, int hHint, boolean force)
		{
			if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT)
			{
				return new Point(wHint, hHint);
			}
			Point size= fText.computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
//			size.x += moreButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, force).x;
			return size;
		}
	}

/**
 * Creates a new combo box cell editor with the given choices.
 */
	 public OptionsTextCellEditor(Composite parent)
	 {
		 super(parent);
	 }

  /* (non-Javadoc)
   * @see org.eclipse.jface.viewers.CellEditor#createControl(org.eclipse.swt.widgets.Composite)
   */
	protected Control createControl(Composite parent)
	{
		fEditor = ViewUtility.createComposite(parent, 2);
		fEditor.setLayout(new ComboCellLayout());
 
    if (isTextReadOnly)
    {
		  fText = new Text(fEditor, SWT.LEFT | SWT.READ_ONLY);
    }
    else
    {
      fText = new Text(fEditor, SWT.LEFT);
    }
//    fText.setEnabled(false);

		fText.setBackground(parent.getBackground());
		fText.setText("");
    fText.addKeyListener(this);
		fText.addFocusListener(new FocusAdapter()
    {
			public void focusLost(FocusEvent e)
      {
				if (!moreButton.isFocusControl())
				{
					OptionsTextCellEditor.this.focusLost();
				}
			}
		});
			
		moreButton = ViewUtility.createPushButton(fEditor, "...");
		moreButton.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				// System.out.println("More Button Clicked");
				openDialog();
			}
		});
    moreButton.addKeyListener(this);
		moreButton.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e) {
				if (!fText.isFocusControl() && (dialog==null || 
								dialog.isDisposed() || 
								(dialog!=null && !dialog.isFocusControl())))
				{
          // System.out.println("MoreButton focusLost");
					OptionsTextCellEditor.this.focusLost();
				}
			}
		});


		setValueValid(true);

		return fEditor;
	}
  
  public void activate()
  {
    // System.out.println("Cell editor activated");
    fText.setText(fValue == null ? "" : fValue.toString());
  }

	protected void focusLost() {
		// System.out.println("CELLEDITOR FOCUS LOST");
		if (isActivated()) {
			applyEditorValueAndDeactivate();
		}
	}

	void applyEditorValueAndDeactivate() {
		//	must set the selection before getting value
//		if (dialog != null  && !dialog.isDisposed())
//		{
//			dialog.close();
//			dialog.dispose();
//		}
		fireApplyEditorValue();
		deactivate();
	}

  public void keyPressed(KeyEvent e)
  {
    if (e.character == SWT.ESC)
     { // Escape character
      fireCancelEditor();
    }
    else if ((e.character == SWT.CR) || (e.character == SWT.LF))
     { // Return key
      applyEditorValueAndDeactivate();
    }
  }
  
  public void keyReleased(KeyEvent e)
  {
    
  }
  
	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.CellEditor#doGetValue()
   * Returns the cell editor's value.
	 */
	protected Object doGetValue() 
	{
		return fValue;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.CellEditor#doSetFocus()
   * Set the focus to the cell editor's UI representation.
   */
	protected void doSetFocus()
	{
//		fButton.setFocus();
//		System.out.println("doSetFocus() " + moreButton.setFocus());
		fText.setFocus();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.CellEditor#doSetValue(java.lang.Object)
	 * Sets the value of the cell editor to the given value.
   */
	protected void doSetValue(Object value)
	{
		fValue = value;
	}

  protected Point getButtonAbsoluteLocation()
  {
    Rectangle buttonBounds = moreButton.getBounds();
    int x = buttonBounds.x;
    int y = buttonBounds.y;
    Control c = moreButton;
    while (c != null)
     {
      c = c.getParent();
      if (c == null)
        break;
      x += c.getBounds().x;
      y += c.getBounds().y;
    }
    x += buttonBounds.width + 5;
    y += buttonBounds.height;
    Point p = new Point(x,y);
    return p;
  }
  
  protected void cancel()
  {
    dialog.close();
    dialog.dispose();
  }

	protected abstract void openDialog();	
  
	public void widgetSelected(SelectionEvent e)
	{
	}
	public void widgetDefaultSelected(SelectionEvent e)
  {
  }
}

Back to the top