Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ebc00953fda8b752c6baa7bb2b9b5f95f35e940 (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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.wsdl.ui.internal.asd.properties.sections;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorActionBarContributor;
import org.eclipse.ui.views.properties.tabbed.ISection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.wst.wsdl.ui.internal.asd.design.DesignViewGraphicsConstants;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IASDObject;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IASDObjectListener;

public class ASDAbstractSection implements ISection, IASDObjectListener, Listener, SelectionListener
{
	private TabbedPropertySheetWidgetFactory factory;
	private Object elementModel;
	protected boolean isReadOnly = false;
	protected Composite composite;
	protected int rightMarginSpace;
	protected int tableMinimumWidth = 50;
	protected CustomListener customListener = new CustomListener();
	
	protected List listeners = new ArrayList();
	
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage)
	{
		createControls(parent, tabbedPropertySheetPage.getWidgetFactory());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.wst.common.ui.properties.internal.provisional.TabbedPropertySheetWidgetFactory)
	 */
	public void createControls(Composite parent, TabbedPropertySheetWidgetFactory aFactory)
	{
		this.factory = aFactory;
		GC gc = new GC(parent);
		Point extent = gc.textExtent("  ...  "); //$NON-NLS-1$
		rightMarginSpace = extent.x;
		gc.dispose();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	public void setInput(IWorkbenchPart part, ISelection selection)
	{
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection)selection).getFirstElement();
		elementModel = input;
		attachListener(elementModel);
		
		if (input instanceof IASDObject) {
			isReadOnly = ((IASDObject) input).isReadOnly();
		}
		
		refresh();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#aboutToBeShown()
	 */
	public void aboutToBeShown()
	{
		refresh();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#aboutToBeHidden()
	 */
	public void aboutToBeHidden()
	{
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#dispose()
	 */
	public void dispose()
	{
		unattachAllListeners();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#getMinimumHeight()
	 */
	public int getMinimumHeight()
	{
		return SWT.DEFAULT;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#shouldUseExtraSpace()
	 */
	public boolean shouldUseExtraSpace()
	{
		return true;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ISection#refresh()
	 */
	public void refresh()
	{
		if (!composite.isDisposed())
		{
			if (isReadOnly)
			{
				composite.setEnabled(false);
			}
			else
			{
				composite.setEnabled(true);
			}
		}
	}
	
	/**
	 * Get the widget factory.
	 * @return the widget factory.
	 */
	public TabbedPropertySheetWidgetFactory getWidgetFactory() {
		return factory;
	}
	
	public void propertyChanged(Object object, String property)
	{
		refresh();
	}
	
	
	public void doWidgetDefaultSelected(SelectionEvent e)
	{}
	
	public void doWidgetSelected(SelectionEvent e)
	{}
	
	/**
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(SelectionEvent)
	 */
	public void widgetDefaultSelected(SelectionEvent e)
	{
		if (isListenerEnabled() && !isInDoHandle) 
		{
			isInDoHandle = true;
			doWidgetDefaultSelected(e);
			isInDoHandle = false;
		}
		
	}
	
	/**
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent)
	 */
	public void widgetSelected(SelectionEvent e)
	{
		if (isListenerEnabled() && !isInDoHandle) 
		{
			isInDoHandle = true;
			doWidgetSelected(e);
			isInDoHandle = false;
		}
		
	}
	
	boolean listenerEnabled = true;
	/**
	 * Get the value of listenerEnabled.
	 * @return value of listenerEnabled.
	 */
	public boolean isListenerEnabled() 
	{
		return listenerEnabled;
	}
	
    public void applyTextListeners(Control control)
    {
      control.addListener(SWT.FocusOut, customListener);
      control.addListener(SWT.KeyDown, customListener);
    }
    
    public void removeListeners(Control control)
    {
      control.removeListener(SWT.FocusOut, customListener);
      control.removeListener(SWT.KeyDown, customListener);
    }
	
	/**
	 * Set the value of listenerEnabled.
	 * @param v  Value to assign to listenerEnabled.
	 */
	public void setListenerEnabled(boolean  v) 
	{
		this.listenerEnabled = v;
	}
	
	public void handleEvent(Event event)
	{
		if (isListenerEnabled() && !isInDoHandle) 
		{
			isInDoHandle = true;
//			startDelayedEvent(event);
			doHandleEvent(event);
			isInDoHandle = false;
		} // end of if ()
	}
	
	public void doHandleEvent(Event event)
	{
		
	}
	
	protected DelayedEvent delayedTask;
	
	protected void startDelayedEvent(Event e)
	{
		if (delayedTask == null ||
				delayedTask.getEvent() == null)
		{
			delayedTask = new DelayedEvent();
			delayedTask.setEvent(e);
			Display.getDefault().timerExec(500,delayedTask);
		}
		else
		{
			Event delayedEvent = delayedTask.getEvent();
			
			if (e.widget == delayedEvent.widget &&
					e.type == delayedEvent.type)
			{
				// same event, just different data, delay new event
				delayedTask.setEvent(null);
			}
			delayedTask = new DelayedEvent();
			delayedTask.setEvent(e);
			Display.getDefault().timerExec(500,delayedTask);
		}
	}
	
	class DelayedEvent implements Runnable
	{
		protected Event event;
		
		/*
		 * @see Runnable#run()
		 */
		public void run()
		{
			if (event != null)
			{
				isInDoHandle = true;
				doHandleEvent(event);
				isInDoHandle = false;
				event = null;
			}
		}
		
		/**
		 * Gets the event.
		 * @return Returns a Event
		 */
		public Event getEvent()
		{
			return event;
		}
		
		/**
		 * Sets the event.
		 * @param event The event to set
		 */
		public void setEvent(Event event)
		{
			this.event = event;
		}
		
	}
	   
    class CustomListener implements Listener
    {
      boolean isHandlingEvent = false;
      public void handleEvent(Event event)
      {
        if (isListenerEnabled() && !isReadOnly) 
        {
          switch (event.type)
          {
            case SWT.KeyDown :
            {
              if (event.character == SWT.CR)
              {
                if (!isHandlingEvent)
                {
                  isHandlingEvent = true;
                  doHandleEvent(event);
                  isHandlingEvent = false;
                }
              }
              break;
            }
            case SWT.FocusOut :
            {
              if (!isHandlingEvent)
              {
                isHandlingEvent = true;
                doHandleEvent(event);
                isHandlingEvent = false;
              }
              break;
            }
          }
        }
      }
    }
	
	protected boolean isInDoHandle;
	/**
	 * Get the value of isInDoHandle.
	 * @return value of isInDoHandle.
	 */
	public boolean isInDoHandle() 
	{
		return isInDoHandle;
	}
	
	static protected IStatusLineManager getStatusLineManager(IEditorPart editorPart)
	{ 
		IStatusLineManager result = null;
		try
		{                       
			EditorActionBarContributor contributor = (EditorActionBarContributor)editorPart.getEditorSite().getActionBarContributor();
			result = contributor.getActionBars().getStatusLineManager();
		}
		catch (Exception e)
		{
		}  
		return result;
	}
	
	public Object getModel() {
		return elementModel;
	}
	
	protected void attachListener(Object object) {
		if (object instanceof IASDObject && !listeners.contains(object)) {
			((IASDObject) object).registerListener(this);
			listeners.add(object);
		}
	}
	
	protected void unattachAllListeners() {
		Iterator it = listeners.iterator();
		while (it.hasNext()) {
			Object item = it.next();
			((IASDObject) item).unregisterListener(this);
		}
	}
	
	protected void setControlForegroundColor(Control control) {
		if (control != null) {
			if (isReadOnly) {
				control.setForeground(DesignViewGraphicsConstants.readOnlyLabelColor);
			}
			else {
				control.setForeground(DesignViewGraphicsConstants.labelColor);
			}
		}
	}
    
    protected void executeCommand(Command command) {
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        
        if (page.getActiveEditor() != null && page.getActiveEditor().getAdapter(CommandStack.class) != null) {
            CommandStack stack = (CommandStack) page.getActiveEditor().getAdapter(CommandStack.class);
            stack.execute(command);
        }
        else {
            command.execute();
        }
    }
}

Back to the top