Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6577c8c4dae675fa5eeca4945f02ea0eb0540eb3 (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
/*******************************************************************************
 * Copyright (c) 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.jst.ws.internal.consumption.ui.widgets.object;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jst.j2ee.webservice.wsdd.BeanLink;
import org.eclipse.jst.j2ee.webservice.wsdd.internal.impl.PortComponentImpl;
import org.eclipse.jst.j2ee.webservice.wsdd.internal.impl.ServiceImplBeanImpl;
import org.eclipse.jst.ws.internal.common.ResourceUtils;
import org.eclipse.jst.ws.internal.consumption.common.JavaResourceFilter;
import org.eclipse.jst.ws.internal.consumption.ui.ConsumptionUIMessages;
import org.eclipse.jst.ws.internal.ui.common.UIUtils;
import org.eclipse.jst.ws.internal.ui.dialog.DialogUtils;
import org.eclipse.swt.SWT;
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.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.command.internal.env.core.common.StatusUtils;
import org.eclipse.wst.command.internal.env.ui.widgets.WidgetDataEvents;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;

public class JavaBeanSelectionWidget extends AbstractObjectSelectionWidget implements IObjectSelectionWidget
{
  private IProject           serverProject_ = null;
  private String             serverComponentName_ = null;
  private Composite          parent_ = null;
  private JavaResourceFilter filter_ = new JavaResourceFilter();
  private IResource          initialResource_ = null;
  private Listener statusListener;
  
  /*CONTEXT_ID PBCL0001 for the Bean Selection Page*/
  private String INFOPOP_PBCL_PAGE = "PBCL0001";
  
  private Text beanClassText_;
  /*CONTEXT_ID PBCL0002 for the Bean Selection field of the Bean Selection Page*/
  private String INFOPOP_PBCL_TEXT_BEAN_CLASS = "PBCL0002";

  private Button beanClassBrowseButton_;
  /*CONTEXT_ID PBCL0003 for the Bean Class Browse button of the Bean Selection Page*/
  private String INFOPOP_PBCL_BUTTON_BEAN_CLASS_BROWSE = "PBCL0003";

  private Button beanResourceBrowseButton_;
  /*CONTEXT_ID PBCL0004 for the Bean Resource Browse button of the Bean Selection Page*/
  private String INFOPOP_PBCL_BUTTON_BEAN_RESOURCE_BROWSE = "PBCL0004";
   
  public WidgetDataEvents addControls( Composite parent, Listener statusListener )
  {
	String       pluginId_ = "org.eclipse.jst.ws.consumption.ui";	  
    this.statusListener = statusListener;
    UIUtils      uiUtils  = new UIUtils( pluginId_ ); 
    
    parent_ = parent;
    
    Composite group = uiUtils.createComposite( parent, 4 );
    
    group.setToolTipText( ConsumptionUIMessages.TOOLTIP_PBCL_PAGE );
    PlatformUI.getWorkbench().getHelpSystem().setHelp( group, pluginId_ + "." + INFOPOP_PBCL_PAGE );
    
    beanClassText_ = uiUtils.createText( group, ConsumptionUIMessages.LABEL_BEAN_CLASS_NAME,
    		ConsumptionUIMessages.TOOLTIP_PBCL_TEXT_BEAN_CLASS,
                                         INFOPOP_PBCL_TEXT_BEAN_CLASS,
                                         SWT.SINGLE | SWT.BORDER);
    beanClassText_.addModifyListener(
      new ModifyListener()
      {
        public void modifyText(ModifyEvent e)
        {
          handleModifyBeanClassText();
        }
      }
    );
    
    beanClassBrowseButton_ = uiUtils.createPushButton( group, ConsumptionUIMessages.BUTTON_BROWSE_CLASSES,
    		ConsumptionUIMessages.TOOLTIP_PBCL_BUTTON_BEAN_CLASS_BROWSE,
                                                       INFOPOP_PBCL_BUTTON_BEAN_CLASS_BROWSE );
    beanClassBrowseButton_.addSelectionListener( new SelectionAdapter()
                                                 {
                                                   public void widgetSelected( SelectionEvent evt )
                                                   {
                                                     handleBrowseClasses();  
                                                   }
                                                 } );
    
    beanResourceBrowseButton_ = uiUtils.createPushButton( group, ConsumptionUIMessages.BUTTON_BROWSE_FILES,
    		ConsumptionUIMessages.TOOLTIP_PBCL_BUTTON_BEAN_RESOURCE_BROWSE,
                                                          INFOPOP_PBCL_BUTTON_BEAN_RESOURCE_BROWSE );
    beanResourceBrowseButton_.addSelectionListener( new SelectionAdapter()
                                                    {
                                                      public void widgetSelected( SelectionEvent evt )
                                                      {
                                                        handleBrowseFiles();  
                                                      }
                                                    } );
    return this;
  }
  
  private void handleModifyBeanClassText()
  {
    statusListener.handleEvent(null);
    serverProject_ = null;
    serverComponentName_ = null;
  }

  private void handleBrowseClasses()
  {
    Shell shell = parent_.getShell();
    IType itype = DialogUtils.browseClassesAsIType(shell, ResourcesPlugin.getWorkspace().getRoot().getProjects(), new ProgressMonitorDialog(shell));
    if (itype != null)
    {
      beanClassText_.setText(itype.getFullyQualifiedName());
      try
      {
        IResource res = itype.getCorrespondingResource();
        if (res != null)
        {
          serverProject_ = res.getProject();
          IVirtualComponent comp = ResourceUtils.getComponentOf(res);
          if (comp!=null)
          {
            serverComponentName_ = comp.getName();
          }
        }
        else
        {
          serverProject_ = null;
          serverComponentName_ = null;
        }
      }
      catch (JavaModelException jme)
      {
        serverProject_ = null;
        serverComponentName_ = null;
      }
    }
  }
  
  private void handleBrowseFiles()
  {
    IResource topResource = ResourceUtils.getWorkspaceRoot();
    IResource resource = DialogUtils.browseResources( parent_.getShell(), topResource, initialResource_, filter_ );
    setBeanClass(resource);
  }
  
  private void setBeanClass(IResource resource)
  {
    if( resource != null && resource instanceof IFile )
    {
      IPath  path     = resource.getFullPath();
      String basename = path.lastSegment();
      
      if( basename != null && basename.length() > 0 )
      {
        String beanPackage = org.eclipse.jst.ws.internal.common.ResourceUtils.getJavaResourcePackageName(path);
        String beanClass = (beanPackage == null || beanPackage.length() == 0 ? basename : (beanPackage + "." + basename));
        
        if( beanClass.toLowerCase().endsWith(".java") || beanClass.toLowerCase().endsWith(".class" ) )
        {
          beanClass = beanClass.substring( 0, beanClass.lastIndexOf('.') );
        }
        
        beanClassText_.setText(beanClass);
        serverProject_ =  ResourceUtils.getProjectOf(path);
        IVirtualComponent comp = ResourceUtils.getComponentOf(resource);
        
        if (comp!=null)
        {	
          serverComponentName_ = comp.getName();
        }        
        
      }
    }
  }

  private void setBeanClass(ServiceImplBeanImpl serviceImpl)
  {
    if( serviceImpl != null )
    {
      EObject eObject = serviceImpl.eContainer();
      if(eObject instanceof PortComponentImpl){
      	PortComponentImpl pci = (PortComponentImpl)eObject;
      	String beanClass = pci.getServiceEndpointInterface();
      	beanClassText_.setText(beanClass);
      }
    }
  }
  
  private void setBeanClass(BeanLink serviceImpl)
  {
    if( serviceImpl != null )
    {
      EObject eObject = serviceImpl.eContainer();
      if(eObject instanceof ServiceImplBeanImpl){
      	setBeanClass((ServiceImplBeanImpl)eObject);
      }
    }
  }
  
  public void setInitialSelection(IStructuredSelection initialSelection)
  {
    if (initialSelection != null && !initialSelection.isEmpty())
    {
      Object object = initialSelection.getFirstElement();
      if (object instanceof IFile)
      {
      	IFile iFile = (IFile)object;
      	String fileExt = iFile.getFileExtension().toLowerCase();
      	if (fileExt.equals("java") || fileExt.equals("class"))
          setBeanClass(iFile);
      }
      else if (object instanceof ICompilationUnit)
      	setBeanClass(((ICompilationUnit)object).getResource());
      else if (object instanceof ServiceImplBeanImpl)
      	setBeanClass((ServiceImplBeanImpl)object);
      else if(object instanceof BeanLink)
      	setBeanClass((BeanLink)object);
    }
  }
  
  public IStructuredSelection getObjectSelection()
  {
    return new StructuredSelection(beanClassText_.getText());
  }
  
  public IProject getProject()
  {
    return serverProject_;
  }

  public String getComponentName()
  {
    return serverComponentName_;
  }
  
  public IStatus getStatus()
  {
    String beanClassName = beanClassText_.getText().trim();
    if (beanClassName == null || beanClassName.length() <= 0)
    {
      return StatusUtils.errorStatus(ConsumptionUIMessages.PAGE_MSG_BEAN_CANNOT_BE_EMPTY);
    }
    return Status.OK_STATUS;
  }
  
  public IStatus validateSelection(IStructuredSelection objectSelection)
  {
    return Status.OK_STATUS;
  }
  
  public String getObjectSelectionDisplayableString() {
	// TODO Auto-generated method stub
    return "Java bean string ";
  }
}

Back to the top