001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.site;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IStatus;
014: import org.eclipse.core.runtime.Status;
015: import org.eclipse.jface.dialogs.Dialog;
016: import org.eclipse.jface.dialogs.StatusDialog;
017: import org.eclipse.pde.internal.core.isite.ISiteArchive;
018: import org.eclipse.pde.internal.core.isite.ISiteModel;
019: import org.eclipse.pde.internal.ui.IHelpContextIds;
020: import org.eclipse.pde.internal.ui.PDEPlugin;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.ModifyEvent;
024: import org.eclipse.swt.events.ModifyListener;
025: import org.eclipse.swt.layout.GridData;
026: import org.eclipse.swt.layout.GridLayout;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Shell;
031: import org.eclipse.swt.widgets.Text;
032: import org.eclipse.ui.PlatformUI;
033:
034: public class NewArchiveDialog extends StatusDialog {
035:
036: private IStatus fErrorStatus;
037:
038: private IStatus fOkStatus;
039:
040: private Text fPathText;
041:
042: private ISiteArchive fSiteArchive;
043:
044: private ISiteModel fSiteModel;
045:
046: private Text fUrlText;
047:
048: public NewArchiveDialog(Shell shell, ISiteModel siteModel,
049: ISiteArchive archive) {
050: super (shell);
051: this .fSiteModel = siteModel;
052: this .fSiteArchive = archive;
053: }
054:
055: protected void createButtonsForButtonBar(Composite parent) {
056: super .createButtonsForButtonBar(parent);
057: dialogChanged();
058: }
059:
060: protected Control createDialogArea(Composite parent) {
061: Composite container = new Composite(parent, SWT.NULL);
062: GridLayout layout = new GridLayout();
063: layout.numColumns = 2;
064: layout.marginHeight = layout.marginWidth = 10;
065: container.setLayout(layout);
066: GridData gd = new GridData(GridData.FILL_BOTH);
067: container.setLayoutData(gd);
068:
069: createEntries(container);
070:
071: ModifyListener listener = new ModifyListener() {
072: public void modifyText(ModifyEvent e) {
073: dialogChanged();
074: }
075: };
076: fPathText.addModifyListener(listener);
077: fUrlText.addModifyListener(listener);
078: setTitle(PDEUIMessages.SiteEditor_NewArchiveDialog_title);
079: Dialog.applyDialogFont(container);
080: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
081: IHelpContextIds.NEW_ARCHIVE_DIALOG);
082: return container;
083: }
084:
085: private void createEntries(Composite container) {
086: Label label = new Label(container, SWT.NULL);
087: label.setText(PDEUIMessages.SiteEditor_NewArchiveDialog_path);
088: fPathText = new Text(container, SWT.SINGLE | SWT.BORDER);
089: fPathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
090:
091: label = new Label(container, SWT.NULL);
092: label.setText(PDEUIMessages.SiteEditor_NewArchiveDialog_url);
093: fUrlText = new Text(container, SWT.SINGLE | SWT.BORDER);
094: fUrlText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
095:
096: if (fSiteArchive != null) {
097: setIfDefined(fUrlText, fSiteArchive.getURL());
098: setIfDefined(fPathText, fSiteArchive.getPath());
099: }
100: }
101:
102: private IStatus createErrorStatus(String message) {
103: return new Status(IStatus.ERROR, PDEPlugin.getPluginId(),
104: IStatus.OK, message, null);
105: }
106:
107: private void dialogChanged() {
108: IStatus status = null;
109: if (fUrlText.getText().length() == 0
110: || fPathText.getText().length() == 0)
111: status = getEmptyErrorStatus();
112: else {
113: if (hasPath(fPathText.getText()))
114: status = createErrorStatus(PDEUIMessages.NewArchiveDialog_alreadyExists);
115: }
116: if (status == null)
117: status = getOKStatus();
118: updateStatus(status);
119: }
120:
121: private void execute() {
122: boolean add = (fSiteArchive == null);
123: if (fSiteArchive == null)
124: fSiteArchive = fSiteModel.getFactory().createArchive();
125:
126: try {
127: fSiteArchive.setURL(fUrlText.getText());
128: fSiteArchive.setPath(fPathText.getText());
129: if (add)
130: fSiteModel.getSite().addArchives(
131: new ISiteArchive[] { fSiteArchive });
132: } catch (CoreException e) {
133: PDEPlugin.logException(e);
134: }
135: }
136:
137: private IStatus getEmptyErrorStatus() {
138: if (fErrorStatus == null)
139: fErrorStatus = createErrorStatus(PDEUIMessages.SiteEditor_NewArchiveDialog_error);
140: return fErrorStatus;
141: }
142:
143: private IStatus getOKStatus() {
144: if (fOkStatus == null)
145: fOkStatus = new Status(IStatus.OK, PDEPlugin.getPluginId(),
146: IStatus.OK, "", //$NON-NLS-1$
147: null);
148: return fOkStatus;
149: }
150:
151: private boolean hasPath(String path) {
152: String currentPath = fSiteArchive != null ? fSiteArchive
153: .getPath() : null;
154:
155: ISiteModel model = fSiteModel;
156: ISiteArchive[] archives = model.getSite().getArchives();
157: for (int i = 0; i < archives.length; i++) {
158: ISiteArchive archive = archives[i];
159: String apath = archive.getPath();
160: if (currentPath != null && currentPath.equals(path)) {
161: // do not have to change path while editing
162: return false;
163: }
164: if (apath != null && apath.equals(path)) {
165: return true;
166: }
167: }
168: return false;
169: }
170:
171: protected void okPressed() {
172: execute();
173: super .okPressed();
174: }
175:
176: private void setIfDefined(Text text, String value) {
177: if (value != null)
178: text.setText(value);
179: }
180: }
|