Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2a843d16f77d37311a7042a85a8504d21523185 (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
/*******************************************************************************
 * Copyright (c) 2001, 2010 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 org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.ui.internal.ISelectionMapper;
import org.eclipse.wst.wsdl.ui.internal.WSDLEditorPlugin;
import org.eclipse.wst.wsdl.ui.internal.asd.ASDEditorCSHelpIds;
import org.eclipse.wst.wsdl.ui.internal.asd.Messages;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IBinding;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IInterface;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.IMessage;
import org.eclipse.wst.wsdl.ui.internal.asd.facade.INamedObject;
import org.eclipse.wst.wsdl.ui.internal.refactor.actions.RenameComponentAction;

public class NameSection extends ASDAbstractSection implements IHyperlinkListener {
	protected static final String NEW_STRING = Messages._UI_BUTTON_NEW; //$NON-NLS-1$
	protected static final String BROWSE_STRING = Messages._UI_BUTTON_BROWSE; //$NON-NLS-1$
	protected boolean isTraversing = false;
	CLabel nameLabel;
	protected Text nameText;
  /**
   * Clicking on it invokes the refactor->rename action.
   */
  protected ImageHyperlink renameHyperlink;
  private Composite lightBulbComposite;
  private Composite topComposite;
	
	/**
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.ITabbedPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.wst.common.ui.properties.internal.provisional.TabbedPropertySheetWidgetFactory)
	 */
	public void createControls(Composite parent, TabbedPropertySheetWidgetFactory factory)
	{
		super.createControls(parent, factory);
		
		topComposite = getWidgetFactory().createFlatFormComposite(parent);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		topComposite.setLayout(gridLayout);

		composite = getWidgetFactory().createComposite(topComposite);
		gridLayout = new GridLayout();

		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		GridData data = new GridData(GridData.FILL_BOTH);
		composite.setLayoutData(data);	

		nameLabel = getWidgetFactory().createCLabel(composite, Messages._UI_LABEL_NAME); 
		data = new GridData();
		data.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
	    data.grabExcessHorizontalSpace = false;
		nameLabel.setLayoutData(data);
		
		nameText = getWidgetFactory().createText(composite, ""); //$NON-NLS-1$
		nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		PlatformUI.getWorkbench().getHelpSystem().setHelp(nameText, ASDEditorCSHelpIds.PROPERTIES_NAME_TEXT);
		
		applyTextListeners(nameText);
	}
	
  private void showRefactorButton() {
    if (isReadOnly) {
      return;
    }
      
    if (lightBulbComposite == null) {
    	lightBulbComposite = getWidgetFactory().createComposite(topComposite);
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginWidth = 0;
		lightBulbComposite.setLayout(gridLayout);
		lightBulbComposite.setLayoutData(new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false));
    }
    
    if (renameHyperlink == null) {
      renameHyperlink = getWidgetFactory().createImageHyperlink(lightBulbComposite, SWT.NONE);

      renameHyperlink.setImage(WSDLEditorPlugin.getInstance().getImage("icons/quickassist.gif")); //$NON-NLS-1$
      renameHyperlink.setToolTipText(Messages._UI_TOOLTIP_RENAME_REFACTOR);
      renameHyperlink.setLayoutData(new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false));
      renameHyperlink.addHyperlinkListener(this);
    }

    renameHyperlink.setVisible(true);
  }
  
  private void hideRefactorButton() {
    if (isReadOnly) {
      return;
    }

    if (renameHyperlink == null) {
      return;
    }

    renameHyperlink.setVisible(false);
  }
  
  
	/*
	 * @see org.eclipse.wst.common.ui.properties.internal.provisional.view.ITabbedPropertySection#refresh()
	 */
	public void refresh() {
		super.refresh();
		if (nameText.isDisposed() || nameText.isFocusControl()) {
			return;
		}
		
		setListenerEnabled(false);
		String name = null;
		if (getModel() instanceof INamedObject) {
			name = ((INamedObject) getModel()).getName();
		}
		else if (getModel() instanceof EditPart) {
			Object model = ((EditPart) getModel()).getModel(); 
			name = ((INamedObject) model).getName();
		}
		
		if (name == null) {
			name = ""; //$NON-NLS-1$
		}
		
		nameText.setText(name);
		setControlForegroundColor(nameText);
    
    if (canRefactor()) {
      showRefactorButton();
    }
    else {
      hideRefactorButton();
    }
    setListenerEnabled(true);
	}
	
	public boolean shouldUseExtraSpace()
	{
		return false;
	}
	
	public void doHandleEvent(Event event)
	{
		if (event.widget == nameText && !nameText.isDisposed()) {
			String newValue = nameText.getText();
			Object model = getModel();
			INamedObject namedObject = null;
			
			if (model instanceof INamedObject) {
				namedObject = (INamedObject) model;
			}
			
			if (namedObject != null) {
				if ( !newValue.equals( namedObject.getName() ) ){
				  Command command = namedObject.getSetNameCommand(newValue);
				  executeCommand(command);
				}
			}
		}
	}
  
  private void invokeRenameRefactoring() {
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    Definition definition = (Definition)editor.getAdapter(Definition.class);
    ISelection selection = editor.getSite().getSelectionProvider().getSelection();
    ISelectionMapper mapper = (ISelectionMapper) editor.getAdapter(ISelectionMapper.class);
    selection = mapper != null ? mapper.mapSelection(selection) : selection;
    RenameComponentAction action = new RenameComponentAction(selection, definition);
    action.update(selection);
    action.run();
  }

  private Object getRealModel() {
    Object realModel = getModel();
    
    if (realModel instanceof EditPart) {
      realModel = ((EditPart) getModel()).getModel();
    }
    
    return realModel;
  }

  /**
   * Determines if the model object's name can be refactored.
   * @return true if the model object can be refactored, false otherwise.
   */
  private boolean canRefactor() {
    Object model = getRealModel();

    boolean canRefactor = model instanceof IMessage || 
                          model instanceof IBinding || 
                          model instanceof IInterface;
    return canRefactor;
  }
  
  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.ui.forms.events.IHyperlinkListener#linkActivated(org.eclipse.ui.forms.events.HyperlinkEvent)
   */
  public void linkActivated(HyperlinkEvent e)
  {
    invokeRenameRefactoring();
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.ui.forms.events.IHyperlinkListener#linkEntered(org.eclipse.ui.forms.events.HyperlinkEvent)
   */
  public void linkEntered(HyperlinkEvent e)
  {
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.eclipse.ui.forms.events.IHyperlinkListener#linkExited(org.eclipse.ui.forms.events.HyperlinkEvent)
   */
  public void linkExited(HyperlinkEvent e)
  {
  }
  
  protected boolean shouldPerformComboSelection(Event event, Object selectedItem)
  {
    // if traversing through combobox, don't automatically pop up
    // the browse and new dialog boxes
    boolean wasTraversing = isTraversing;
    if (isTraversing)
      isTraversing = false;
      
    // we only care about default selecting (hitting enter in combobox)
    // for browse.. and new..
    if (event.type == SWT.DefaultSelection)
    {
      if (!(selectedItem instanceof String))
        return false;
      if (!(BROWSE_STRING.equals(selectedItem) || NEW_STRING.equals(selectedItem)))
        return false;
    }
    
    if (wasTraversing && selectedItem instanceof String)
    {
      if (BROWSE_STRING.equals(selectedItem) || NEW_STRING.equals(selectedItem))
        return false;
    }
    return true;
  }
}

Back to the top