lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 1 | /********************************************************************** |
lisaacs | 6fd080b | 2008-04-24 01:40:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2007, 2008 IBM Corporation and others. |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - Initial API and implementation |
| 10 | **********************************************************************/ |
| 11 | package org.eclipse.jst.server.tomcat.ui.internal.editor; |
| 12 | |
| 13 | import java.beans.PropertyChangeEvent; |
| 14 | import java.beans.PropertyChangeListener; |
| 15 | |
| 16 | import org.eclipse.core.resources.IWorkspaceRoot; |
| 17 | import org.eclipse.core.resources.ResourcesPlugin; |
| 18 | import org.eclipse.core.runtime.IPath; |
| 19 | import org.eclipse.core.runtime.IStatus; |
| 20 | import org.eclipse.core.runtime.Path; |
| 21 | import org.eclipse.core.runtime.Status; |
| 22 | import org.eclipse.jst.server.tomcat.core.internal.ITomcatServer; |
| 23 | import org.eclipse.jst.server.tomcat.core.internal.ITomcatServerWorkingCopy; |
| 24 | import org.eclipse.jst.server.tomcat.core.internal.TomcatServer; |
| 25 | import org.eclipse.jst.server.tomcat.core.internal.command.SetDeployDirectoryCommand; |
| 26 | import org.eclipse.jst.server.tomcat.core.internal.command.SetInstanceDirectoryCommand; |
| 27 | import org.eclipse.jst.server.tomcat.core.internal.command.SetTestEnvironmentCommand; |
| 28 | import org.eclipse.jst.server.tomcat.ui.internal.ContextIds; |
| 29 | import org.eclipse.jst.server.tomcat.ui.internal.Messages; |
| 30 | import org.eclipse.jst.server.tomcat.ui.internal.TomcatUIPlugin; |
| 31 | import org.eclipse.osgi.util.NLS; |
| 32 | import org.eclipse.swt.SWT; |
| 33 | import org.eclipse.swt.events.ModifyEvent; |
| 34 | import org.eclipse.swt.events.ModifyListener; |
| 35 | import org.eclipse.swt.events.SelectionAdapter; |
| 36 | import org.eclipse.swt.events.SelectionEvent; |
| 37 | import org.eclipse.swt.layout.GridData; |
| 38 | import org.eclipse.swt.layout.GridLayout; |
| 39 | import org.eclipse.swt.widgets.Button; |
| 40 | import org.eclipse.swt.widgets.Composite; |
| 41 | import org.eclipse.swt.widgets.DirectoryDialog; |
| 42 | import org.eclipse.swt.widgets.Label; |
| 43 | import org.eclipse.swt.widgets.Text; |
| 44 | import org.eclipse.ui.IEditorInput; |
| 45 | import org.eclipse.ui.IEditorSite; |
| 46 | import org.eclipse.ui.PlatformUI; |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 47 | import org.eclipse.ui.forms.IFormColors; |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 48 | import org.eclipse.ui.forms.events.HyperlinkAdapter; |
| 49 | import org.eclipse.ui.forms.events.HyperlinkEvent; |
| 50 | import org.eclipse.ui.forms.widgets.ExpandableComposite; |
| 51 | import org.eclipse.ui.forms.widgets.FormToolkit; |
| 52 | import org.eclipse.ui.forms.widgets.Hyperlink; |
| 53 | import org.eclipse.ui.forms.widgets.Section; |
| 54 | import org.eclipse.ui.help.IWorkbenchHelpSystem; |
| 55 | import org.eclipse.wst.server.core.IPublishListener; |
| 56 | import org.eclipse.wst.server.core.IRuntime; |
| 57 | import org.eclipse.wst.server.core.IServer; |
| 58 | import org.eclipse.wst.server.core.util.PublishAdapter; |
| 59 | import org.eclipse.wst.server.ui.editor.ServerEditorSection; |
| 60 | /** |
| 61 | * Tomcat server general editor page. |
| 62 | */ |
| 63 | public class ServerLocationEditorSection extends ServerEditorSection { |
| 64 | protected Section section; |
| 65 | protected TomcatServer tomcatServer; |
| 66 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 67 | protected Hyperlink setDefaultDeployDir; |
| 68 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 69 | protected boolean defaultDeployDirIsSet; |
| 70 | |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 71 | protected Button serverDirMetadata; |
| 72 | protected Button serverDirInstall; |
| 73 | protected Button serverDirCustom; |
| 74 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 75 | protected Text serverDir; |
| 76 | protected Button serverDirBrowse; |
| 77 | protected Text deployDir; |
| 78 | protected Button deployDirBrowse; |
| 79 | protected boolean updating; |
| 80 | |
| 81 | protected PropertyChangeListener listener; |
| 82 | protected IPublishListener publishListener; |
| 83 | protected IPath workspacePath; |
| 84 | protected IPath defaultDeployPath; |
| 85 | |
| 86 | protected boolean allowRestrictedEditing; |
| 87 | protected IPath tempDirPath; |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 88 | protected IPath installDirPath; |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 89 | |
| 90 | // Avoid hardcoding this at some point |
| 91 | private final static String METADATADIR = ".metadata"; |
| 92 | /** |
| 93 | * ServerGeneralEditorPart constructor comment. |
| 94 | */ |
| 95 | public ServerLocationEditorSection() { |
| 96 | // do nothing |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add listeners to detect undo changes and publishing of the server. |
| 101 | */ |
| 102 | protected void addChangeListeners() { |
| 103 | listener = new PropertyChangeListener() { |
| 104 | public void propertyChange(PropertyChangeEvent event) { |
| 105 | if (updating) |
| 106 | return; |
| 107 | updating = true; |
| 108 | if (ITomcatServer.PROPERTY_INSTANCE_DIR.equals(event.getPropertyName()) |
| 109 | || ITomcatServer.PROPERTY_TEST_ENVIRONMENT.equals(event.getPropertyName())) { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 110 | updateServerDirButtons(); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 111 | updateServerDirFields(); |
| 112 | validate(); |
| 113 | } |
| 114 | else if (ITomcatServer.PROPERTY_DEPLOY_DIR.equals(event.getPropertyName())) { |
| 115 | String s = (String) event.getNewValue(); |
| 116 | ServerLocationEditorSection.this.deployDir.setText(s); |
| 117 | updateDefaultDeployLink(); |
| 118 | validate(); |
| 119 | } |
| 120 | updating = false; |
| 121 | } |
| 122 | }; |
| 123 | server.addPropertyChangeListener(listener); |
| 124 | |
| 125 | publishListener = new PublishAdapter() { |
| 126 | public void publishFinished(IServer server2, IStatus status) { |
| 127 | boolean flag = false; |
| 128 | if (status.isOK() && server2.getModules().length == 0) |
| 129 | flag = true; |
| 130 | if (flag != allowRestrictedEditing) { |
| 131 | allowRestrictedEditing = flag; |
| 132 | // Update the state of the fields |
| 133 | PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() { |
| 134 | public void run() { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 135 | boolean customServerDir = false; |
| 136 | if (!ServerLocationEditorSection.this.serverDirCustom.isDisposed()) |
| 137 | customServerDir = ServerLocationEditorSection.this.serverDirCustom.getSelection(); |
| 138 | if (!ServerLocationEditorSection.this.serverDirMetadata.isDisposed()) |
| 139 | ServerLocationEditorSection.this.serverDirMetadata.setEnabled(allowRestrictedEditing); |
| 140 | if (!ServerLocationEditorSection.this.serverDirInstall.isDisposed()) |
| 141 | ServerLocationEditorSection.this.serverDirInstall.setEnabled(allowRestrictedEditing); |
| 142 | if (!ServerLocationEditorSection.this.serverDirCustom.isDisposed()) |
| 143 | ServerLocationEditorSection.this.serverDirCustom.setEnabled(allowRestrictedEditing); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 144 | if (!ServerLocationEditorSection.this.serverDir.isDisposed()) |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 145 | ServerLocationEditorSection.this.serverDir.setEnabled(allowRestrictedEditing && customServerDir); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 146 | if (!ServerLocationEditorSection.this.serverDirBrowse.isDisposed()) |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 147 | ServerLocationEditorSection.this.serverDirBrowse.setEnabled(allowRestrictedEditing && customServerDir); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 148 | if (!ServerLocationEditorSection.this.setDefaultDeployDir.isDisposed()) |
| 149 | ServerLocationEditorSection.this.setDefaultDeployDir.setEnabled(allowRestrictedEditing); |
| 150 | if (!ServerLocationEditorSection.this.deployDir.isDisposed()) |
| 151 | ServerLocationEditorSection.this.deployDir.setEnabled(allowRestrictedEditing); |
| 152 | if (!ServerLocationEditorSection.this.deployDirBrowse.isDisposed()) |
| 153 | ServerLocationEditorSection.this.deployDirBrowse.setEnabled(allowRestrictedEditing); |
| 154 | } |
| 155 | }); |
| 156 | } |
| 157 | } |
| 158 | }; |
| 159 | server.getOriginal().addPublishListener(publishListener); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Creates the SWT controls for this workbench part. |
| 164 | * |
| 165 | * @param parent the parent control |
| 166 | */ |
| 167 | public void createSection(Composite parent) { |
| 168 | super.createSection(parent); |
| 169 | FormToolkit toolkit = getFormToolkit(parent.getDisplay()); |
| 170 | |
| 171 | section = toolkit.createSection(parent, ExpandableComposite.TWISTIE | ExpandableComposite.EXPANDED |
| 172 | | ExpandableComposite.TITLE_BAR | Section.DESCRIPTION | ExpandableComposite.FOCUS_TITLE); |
| 173 | section.setText(Messages.serverEditorLocationsSection); |
| 174 | section.setDescription(Messages.serverEditorLocationsDescription); |
| 175 | section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); |
| 176 | |
| 177 | Composite composite = toolkit.createComposite(section); |
| 178 | GridLayout layout = new GridLayout(); |
| 179 | layout.numColumns = 3; |
| 180 | layout.marginHeight = 5; |
| 181 | layout.marginWidth = 10; |
| 182 | layout.verticalSpacing = 5; |
| 183 | layout.horizontalSpacing = 15; |
| 184 | composite.setLayout(layout); |
| 185 | composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL)); |
| 186 | IWorkbenchHelpSystem whs = PlatformUI.getWorkbench().getHelpSystem(); |
| 187 | whs.setHelp(composite, ContextIds.SERVER_EDITOR); |
| 188 | whs.setHelp(section, ContextIds.SERVER_EDITOR); |
| 189 | toolkit.paintBordersFor(composite); |
| 190 | section.setClient(composite); |
| 191 | |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 192 | serverDirMetadata = toolkit.createButton(composite, |
| 193 | NLS.bind(Messages.serverEditorServerDirMetadata, Messages.serverEditorDoesNotModify), SWT.RADIO); |
| 194 | GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 195 | data.horizontalSpan = 3; |
| 196 | serverDirMetadata.setLayoutData(data); |
| 197 | serverDirMetadata.addSelectionListener(new SelectionAdapter() { |
| 198 | public void widgetSelected(SelectionEvent e) { |
| 199 | if (updating || !serverDirMetadata.getSelection()) |
| 200 | return; |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 201 | updating = true; |
| 202 | execute(new SetTestEnvironmentCommand(tomcatServer, true)); |
| 203 | updateServerDirFields(); |
| 204 | updating = false; |
| 205 | validate(); |
| 206 | } |
| 207 | }); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 208 | |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 209 | serverDirInstall = toolkit.createButton(composite, |
| 210 | NLS.bind(Messages.serverEditorServerDirInstall, Messages.serverEditorTakesControl), SWT.RADIO); |
| 211 | data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 212 | data.horizontalSpan = 3; |
| 213 | serverDirInstall.setLayoutData(data); |
| 214 | serverDirInstall.addSelectionListener(new SelectionAdapter() { |
| 215 | public void widgetSelected(SelectionEvent e) { |
| 216 | if (updating || !serverDirInstall.getSelection()) |
| 217 | return; |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 218 | updating = true; |
| 219 | execute(new SetTestEnvironmentCommand(tomcatServer, false)); |
| 220 | updateServerDirFields(); |
| 221 | updating = false; |
| 222 | validate(); |
| 223 | } |
| 224 | }); |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 225 | |
| 226 | serverDirCustom = toolkit.createButton(composite, |
| 227 | NLS.bind(Messages.serverEditorServerDirCustom, Messages.serverEditorDoesNotModify), SWT.RADIO); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 228 | data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 229 | data.horizontalSpan = 3; |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 230 | serverDirCustom.setLayoutData(data); |
| 231 | serverDirCustom.addSelectionListener(new SelectionAdapter() { |
| 232 | public void widgetSelected(SelectionEvent e) { |
| 233 | if (updating || !serverDirCustom.getSelection()) |
| 234 | return; |
| 235 | updating = true; |
| 236 | execute(new SetTestEnvironmentCommand(tomcatServer, true)); |
| 237 | updateServerDirFields(); |
| 238 | updating = false; |
| 239 | validate(); |
| 240 | } |
| 241 | }); |
| 242 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 243 | // server directory |
| 244 | Label label = createLabel(toolkit, composite, Messages.serverEditorServerDir); |
| 245 | data = new GridData(SWT.BEGINNING, SWT.CENTER, false, false); |
| 246 | label.setLayoutData(data); |
| 247 | |
| 248 | serverDir = toolkit.createText(composite, null, SWT.SINGLE); |
| 249 | data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
deboer | 58ed686 | 2008-03-24 22:53:42 +0000 | [diff] [blame] | 250 | data.widthHint = 75; |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 251 | serverDir.setLayoutData(data); |
| 252 | serverDir.addModifyListener(new ModifyListener() { |
| 253 | public void modifyText(ModifyEvent e) { |
| 254 | if (updating) |
| 255 | return; |
| 256 | updating = true; |
| 257 | execute(new SetInstanceDirectoryCommand(tomcatServer, getServerDir())); |
| 258 | updating = false; |
| 259 | validate(); |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | serverDirBrowse = toolkit.createButton(composite, Messages.editorBrowse, SWT.PUSH); |
| 264 | serverDirBrowse.addSelectionListener(new SelectionAdapter() { |
| 265 | public void widgetSelected(SelectionEvent se) { |
| 266 | DirectoryDialog dialog = new DirectoryDialog(serverDir.getShell()); |
| 267 | dialog.setMessage(Messages.serverEditorBrowseDeployMessage); |
| 268 | dialog.setFilterPath(serverDir.getText()); |
| 269 | String selectedDirectory = dialog.open(); |
| 270 | if (selectedDirectory != null && !selectedDirectory.equals(serverDir.getText())) { |
| 271 | updating = true; |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 272 | // Make relative if relative to the workspace |
| 273 | IPath path = new Path(selectedDirectory); |
| 274 | if (workspacePath.isPrefixOf(path)) { |
| 275 | int cnt = path.matchingFirstSegments(workspacePath); |
| 276 | path = path.removeFirstSegments(cnt).setDevice(null); |
| 277 | selectedDirectory = path.toOSString(); |
| 278 | } |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 279 | execute(new SetInstanceDirectoryCommand(tomcatServer, selectedDirectory)); |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 280 | updateServerDirButtons(); |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 281 | updateServerDirFields(); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 282 | updating = false; |
| 283 | validate(); |
| 284 | } |
| 285 | } |
| 286 | }); |
| 287 | serverDirBrowse.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); |
| 288 | |
| 289 | // deployment directory link |
| 290 | setDefaultDeployDir = toolkit.createHyperlink(composite, |
| 291 | NLS.bind(Messages.serverEditorSetDefaultDeployDirLink, ""), SWT.WRAP); |
| 292 | setDefaultDeployDir.addHyperlinkListener(new HyperlinkAdapter() { |
| 293 | public void linkActivated(HyperlinkEvent e) { |
| 294 | updating = true; |
| 295 | execute(new SetDeployDirectoryCommand(tomcatServer, ITomcatServerWorkingCopy.DEFAULT_DEPLOYDIR)); |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 296 | deployDir.setText(ITomcatServerWorkingCopy.DEFAULT_DEPLOYDIR); |
| 297 | updateDefaultDeployLink(); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 298 | updating = false; |
| 299 | validate(); |
| 300 | } |
| 301 | }); |
| 302 | data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 303 | data.horizontalSpan = 3; |
| 304 | setDefaultDeployDir.setLayoutData(data); |
| 305 | |
| 306 | // deployment directory |
| 307 | label = createLabel(toolkit, composite, Messages.serverEditorDeployDir); |
| 308 | data = new GridData(SWT.BEGINNING, SWT.CENTER, false, false); |
| 309 | label.setLayoutData(data); |
| 310 | |
| 311 | deployDir = toolkit.createText(composite, null); |
| 312 | data = new GridData(SWT.FILL, SWT.CENTER, true, false); |
| 313 | deployDir.setLayoutData(data); |
| 314 | deployDir.addModifyListener(new ModifyListener() { |
| 315 | public void modifyText(ModifyEvent e) { |
| 316 | if (updating) |
| 317 | return; |
| 318 | updating = true; |
| 319 | execute(new SetDeployDirectoryCommand(tomcatServer, deployDir.getText().trim())); |
| 320 | updateDefaultDeployLink(); |
| 321 | updating = false; |
| 322 | validate(); |
| 323 | } |
| 324 | }); |
| 325 | |
| 326 | deployDirBrowse = toolkit.createButton(composite, Messages.editorBrowse, SWT.PUSH); |
| 327 | deployDirBrowse.addSelectionListener(new SelectionAdapter() { |
| 328 | public void widgetSelected(SelectionEvent se) { |
| 329 | DirectoryDialog dialog = new DirectoryDialog(deployDir.getShell()); |
| 330 | dialog.setMessage(Messages.serverEditorBrowseDeployMessage); |
| 331 | dialog.setFilterPath(deployDir.getText()); |
| 332 | String selectedDirectory = dialog.open(); |
| 333 | if (selectedDirectory != null && !selectedDirectory.equals(deployDir.getText())) { |
| 334 | updating = true; |
| 335 | execute(new SetDeployDirectoryCommand(tomcatServer, selectedDirectory)); |
| 336 | deployDir.setText(selectedDirectory); |
| 337 | updating = false; |
| 338 | validate(); |
| 339 | } |
| 340 | } |
| 341 | }); |
| 342 | deployDirBrowse.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); |
| 343 | |
| 344 | initialize(); |
| 345 | } |
| 346 | |
| 347 | protected Label createLabel(FormToolkit toolkit, Composite parent, String text) { |
| 348 | Label label = toolkit.createLabel(parent, text); |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 349 | label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 350 | return label; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * @see ServerEditorSection#dispose() |
| 355 | */ |
| 356 | public void dispose() { |
| 357 | if (server != null) { |
| 358 | server.removePropertyChangeListener(listener); |
| 359 | if (server.getOriginal() != null) |
| 360 | server.getOriginal().removePublishListener(publishListener); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @see ServerEditorSection#init(IEditorSite, IEditorInput) |
| 366 | */ |
| 367 | public void init(IEditorSite site, IEditorInput input) { |
| 368 | super.init(site, input); |
| 369 | |
| 370 | // Cache workspace and default deploy paths |
| 371 | IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 372 | workspacePath = root.getLocation(); |
| 373 | defaultDeployPath = new Path(ITomcatServerWorkingCopy.DEFAULT_DEPLOYDIR); |
| 374 | |
| 375 | if (server != null) { |
| 376 | tomcatServer = (TomcatServer) server.loadAdapter(TomcatServer.class, null); |
| 377 | addChangeListeners(); |
| 378 | } |
| 379 | initialize(); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Initialize the fields in this editor. |
| 384 | */ |
| 385 | protected void initialize() { |
| 386 | if (serverDir== null || tomcatServer == null) |
| 387 | return; |
| 388 | updating = true; |
| 389 | |
| 390 | IRuntime runtime = server.getRuntime(); |
| 391 | // If not Tomcat 3.2, update description to mention catalina.base |
| 392 | if (runtime != null && runtime.getRuntimeType().getId().indexOf("32") < 0) |
| 393 | section.setDescription(Messages.serverEditorLocationsDescription2); |
deboer | ee724c6 | 2007-07-12 21:20:59 +0000 | [diff] [blame] | 394 | if (runtime != null) |
| 395 | installDirPath = runtime.getLocation(); |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 396 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 397 | // determine if editing of locations is allowed |
| 398 | allowRestrictedEditing = false; |
| 399 | IPath basePath = tomcatServer.getRuntimeBaseDirectory(); |
| 400 | if (!readOnly) { |
| 401 | // If server has not been published, or server is published with no modules, allow editing |
| 402 | // TODO Find better way to determine if server hasn't been published |
| 403 | if ((basePath != null && !basePath.append("conf").toFile().exists()) |
| 404 | || (server.getOriginal().getServerPublishState() == IServer.PUBLISH_STATE_NONE |
| 405 | && server.getOriginal().getModules().length == 0)) { |
| 406 | allowRestrictedEditing = true; |
| 407 | } |
| 408 | } |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 409 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 410 | // Update server related fields |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 411 | updateServerDirButtons(); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 412 | updateServerDirFields(); |
| 413 | |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 414 | serverDirMetadata.setEnabled(allowRestrictedEditing); |
| 415 | serverDirInstall.setEnabled(allowRestrictedEditing); |
| 416 | serverDirCustom.setEnabled(allowRestrictedEditing); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 417 | |
| 418 | // Update deployment related fields |
| 419 | updateDefaultDeployLink(); |
| 420 | |
| 421 | deployDir.setText(tomcatServer.getDeployDirectory()); |
| 422 | |
| 423 | setDefaultDeployDir.setEnabled(allowRestrictedEditing); |
| 424 | deployDir.setEnabled(allowRestrictedEditing); |
| 425 | deployDirBrowse.setEnabled(allowRestrictedEditing); |
| 426 | |
| 427 | updating = false; |
| 428 | validate(); |
| 429 | } |
| 430 | |
| 431 | protected String getServerDir() { |
| 432 | String dir = null; |
| 433 | if (serverDir != null) { |
| 434 | dir = serverDir.getText().trim(); |
| 435 | IPath path = new Path(dir); |
| 436 | // Adjust if the temp dir is known and has been entered |
| 437 | if (tempDirPath != null && tempDirPath.equals(path)) |
| 438 | dir = null; |
| 439 | // If under the workspace, make relative |
| 440 | else if (workspacePath.isPrefixOf(path)) { |
| 441 | int cnt = path.matchingFirstSegments(workspacePath); |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 442 | path = path.removeFirstSegments(cnt).setDevice(null); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 443 | dir = path.toOSString(); |
| 444 | } |
| 445 | } |
| 446 | return dir; |
| 447 | } |
| 448 | |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 449 | protected void updateServerDirButtons() { |
| 450 | if (tomcatServer.getInstanceDirectory() == null) { |
| 451 | IPath path = tomcatServer.getRuntimeBaseDirectory(); |
deboer | 5bfbcee | 2008-06-20 15:08:43 +0000 | [diff] [blame] | 452 | if (path != null && path.equals(installDirPath)) { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 453 | serverDirInstall.setSelection(true); |
| 454 | serverDirMetadata.setSelection(false); |
| 455 | serverDirCustom.setSelection(false); |
deboer | 5bfbcee | 2008-06-20 15:08:43 +0000 | [diff] [blame] | 456 | } else { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 457 | serverDirMetadata.setSelection(true); |
| 458 | serverDirInstall.setSelection(false); |
| 459 | serverDirCustom.setSelection(false); |
| 460 | } |
deboer | 5bfbcee | 2008-06-20 15:08:43 +0000 | [diff] [blame] | 461 | } else { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 462 | serverDirCustom.setSelection(true); |
| 463 | serverDirMetadata.setSelection(false); |
| 464 | serverDirInstall.setSelection(false); |
| 465 | } |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 466 | } |
| 467 | |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 468 | protected void updateServerDirFields() { |
| 469 | updateServerDir(); |
| 470 | boolean customServerDir = serverDirCustom.getSelection(); |
| 471 | serverDir.setEnabled(allowRestrictedEditing && customServerDir); |
| 472 | serverDirBrowse.setEnabled(allowRestrictedEditing && customServerDir); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 473 | } |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 474 | |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 475 | protected void updateServerDir() { |
| 476 | IPath path = tomcatServer.getRuntimeBaseDirectory(); |
deboer | 5bfbcee | 2008-06-20 15:08:43 +0000 | [diff] [blame] | 477 | if (path == null) |
| 478 | serverDir.setText(""); |
| 479 | else if (workspacePath.isPrefixOf(path)) { |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 480 | int cnt = path.matchingFirstSegments(workspacePath); |
lisaacs | 98612a4 | 2007-02-17 16:31:28 +0000 | [diff] [blame] | 481 | path = path.removeFirstSegments(cnt).setDevice(null); |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 482 | serverDir.setText(path.toOSString()); |
| 483 | // cache the relative temp dir path if that is what we have |
| 484 | if (tempDirPath == null) { |
| 485 | if (tomcatServer.isTestEnvironment() && tomcatServer.getInstanceDirectory() == null) |
| 486 | tempDirPath = path; |
| 487 | } |
deboer | 5bfbcee | 2008-06-20 15:08:43 +0000 | [diff] [blame] | 488 | } else |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 489 | serverDir.setText(path.toOSString()); |
| 490 | } |
| 491 | |
| 492 | protected void updateDefaultDeployLink() { |
| 493 | boolean newState = defaultDeployPath.equals(new Path(tomcatServer.getDeployDirectory())); |
| 494 | if (newState != defaultDeployDirIsSet) { |
| 495 | setDefaultDeployDir.setText( |
| 496 | newState ? Messages.serverEditorSetDefaultDeployDirLink2 |
| 497 | : Messages.serverEditorSetDefaultDeployDirLink); |
| 498 | defaultDeployDirIsSet = newState; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * @see ServerEditorSection#getSaveStatus() |
| 504 | */ |
| 505 | public IStatus[] getSaveStatus() { |
| 506 | if (tomcatServer != null) { |
| 507 | // Check the instance directory |
| 508 | String dir = tomcatServer.getInstanceDirectory(); |
| 509 | if (dir != null) { |
| 510 | IPath path = new Path(dir); |
| 511 | // Must not be the same as the workspace location |
| 512 | if (dir.length() == 0 || workspacePath.equals(path)) { |
| 513 | return new IStatus [] { |
| 514 | new Status(IStatus.ERROR, TomcatUIPlugin.PLUGIN_ID, Messages.errorServerDirIsRoot)}; |
| 515 | } |
| 516 | // User specified value may not be under the ".metadata" folder of the workspace |
| 517 | else if (workspacePath.isPrefixOf(path) |
| 518 | || (!path.isAbsolute() && METADATADIR.equals(path.segment(0)))) { |
| 519 | int cnt = path.matchingFirstSegments(workspacePath); |
| 520 | if (METADATADIR.equals(path.segment(cnt))) { |
| 521 | return new IStatus [] { |
| 522 | new Status(IStatus.ERROR, TomcatUIPlugin.PLUGIN_ID, NLS.bind(Messages.errorServerDirUnderRoot, METADATADIR))}; |
| 523 | } |
| 524 | } |
lisaacs | 63f67d2 | 2008-04-23 18:53:47 +0000 | [diff] [blame] | 525 | else if (path.equals(installDirPath)) |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 526 | return new IStatus [] { |
| 527 | new Status(IStatus.ERROR, TomcatUIPlugin.PLUGIN_ID, |
| 528 | NLS.bind(Messages.errorServerDirCustomNotInstall, |
| 529 | NLS.bind(Messages.serverEditorServerDirInstall, "").trim()))}; |
| 530 | } |
| 531 | else { |
| 532 | IPath path = tomcatServer.getRuntimeBaseDirectory(); |
| 533 | // If non-custom instance dir is not the install and metadata isn't the selection, return error |
| 534 | if (!path.equals(installDirPath) && !serverDirMetadata.getSelection()) { |
| 535 | return new IStatus [] { |
| 536 | new Status(IStatus.ERROR, TomcatUIPlugin.PLUGIN_ID, |
| 537 | NLS.bind(Messages.errorServerDirCustomNotMetadata, |
| 538 | NLS.bind(Messages.serverEditorServerDirMetadata, "").trim()))}; |
| 539 | } |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // Check the deployment directory |
| 543 | dir = tomcatServer.getDeployDirectory(); |
| 544 | // Deploy directory must be set |
| 545 | if (dir == null || dir.length() == 0) { |
| 546 | return new IStatus [] { |
| 547 | new Status(IStatus.ERROR, TomcatUIPlugin.PLUGIN_ID, Messages.errorDeployDirNotSpecified)}; |
| 548 | } |
| 549 | } |
| 550 | // use default implementation to return success |
| 551 | return super.getSaveStatus(); |
| 552 | } |
| 553 | |
| 554 | protected void validate() { |
| 555 | if (tomcatServer != null) { |
| 556 | // Validate instance directory |
| 557 | String dir = tomcatServer.getInstanceDirectory(); |
| 558 | if (dir != null) { |
| 559 | IPath path = new Path(dir); |
| 560 | // Must not be the same as the workspace location |
| 561 | if (dir.length() == 0 || workspacePath.equals(path)) { |
| 562 | setErrorMessage(Messages.errorServerDirIsRoot); |
| 563 | return; |
| 564 | } |
| 565 | // User specified value may not be under the ".metadata" folder of the workspace |
| 566 | else if (workspacePath.isPrefixOf(path) |
| 567 | || (!path.isAbsolute() && METADATADIR.equals(path.segment(0)))) { |
| 568 | int cnt = path.matchingFirstSegments(workspacePath); |
| 569 | if (METADATADIR.equals(path.segment(cnt))) { |
| 570 | setErrorMessage(NLS.bind(Messages.errorServerDirUnderRoot, METADATADIR)); |
| 571 | return; |
| 572 | } |
| 573 | } |
lisaacs | 63f67d2 | 2008-04-23 18:53:47 +0000 | [diff] [blame] | 574 | else if (path.equals(installDirPath)) { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 575 | setErrorMessage(NLS.bind(Messages.errorServerDirCustomNotInstall, |
| 576 | NLS.bind(Messages.serverEditorServerDirInstall, "").trim())); |
| 577 | return; |
| 578 | } |
| 579 | } |
| 580 | else { |
| 581 | IPath path = tomcatServer.getRuntimeBaseDirectory(); |
| 582 | // If non-custom instance dir is not the install and metadata isn't the selection, return error |
deboer | 5bfbcee | 2008-06-20 15:08:43 +0000 | [diff] [blame] | 583 | if (path != null && !path.equals(installDirPath) && !serverDirMetadata.getSelection()) { |
lisaacs | 5f5cd56 | 2007-03-29 00:27:09 +0000 | [diff] [blame] | 584 | setErrorMessage(NLS.bind(Messages.errorServerDirCustomNotMetadata, |
| 585 | NLS.bind(Messages.serverEditorServerDirMetadata, "").trim())); |
| 586 | } |
lisaacs | 78d6d2f | 2007-02-10 20:47:57 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | // Check the deployment directory |
| 590 | dir = tomcatServer.getDeployDirectory(); |
| 591 | // Deploy directory must be set |
| 592 | if (dir == null || dir.length() == 0) { |
| 593 | setErrorMessage(Messages.errorDeployDirNotSpecified); |
| 594 | return; |
| 595 | } |
| 596 | } |
| 597 | // All is okay, clear any previous error |
| 598 | setErrorMessage(null); |
| 599 | } |
| 600 | } |