KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > NewArchiveDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
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.pde.internal.ui.editor.site;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.dialogs.StatusDialog;
18 import org.eclipse.pde.internal.core.isite.ISiteArchive;
19 import org.eclipse.pde.internal.core.isite.ISiteModel;
20 import org.eclipse.pde.internal.ui.IHelpContextIds;
21 import org.eclipse.pde.internal.ui.PDEPlugin;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.ModifyEvent;
25 import org.eclipse.swt.events.ModifyListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.PlatformUI;
34
35 public class NewArchiveDialog extends StatusDialog {
36
37     private IStatus fErrorStatus;
38
39     private IStatus fOkStatus;
40
41     private Text fPathText;
42
43     private ISiteArchive fSiteArchive;
44
45     private ISiteModel fSiteModel;
46
47     private Text fUrlText;
48
49     public NewArchiveDialog(Shell shell, ISiteModel siteModel,
50             ISiteArchive archive) {
51         super(shell);
52         this.fSiteModel = siteModel;
53         this.fSiteArchive = archive;
54     }
55
56     protected void createButtonsForButtonBar(Composite parent) {
57         super.createButtonsForButtonBar(parent);
58         dialogChanged();
59     }
60
61     protected Control createDialogArea(Composite parent) {
62         Composite container = new Composite(parent, SWT.NULL);
63         GridLayout layout = new GridLayout();
64         layout.numColumns = 2;
65         layout.marginHeight = layout.marginWidth = 10;
66         container.setLayout(layout);
67         GridData gd = new GridData(GridData.FILL_BOTH);
68         container.setLayoutData(gd);
69
70         createEntries(container);
71
72         ModifyListener listener = new ModifyListener() {
73             public void modifyText(ModifyEvent e) {
74                 dialogChanged();
75             }
76         };
77         fPathText.addModifyListener(listener);
78         fUrlText.addModifyListener(listener);
79         setTitle(PDEUIMessages.SiteEditor_NewArchiveDialog_title);
80         Dialog.applyDialogFont(container);
81         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.NEW_ARCHIVE_DIALOG);
82         return container;
83     }
84
85     private void createEntries(Composite container) {
86         Label label = new Label(container, SWT.NULL);
87         label.setText(PDEUIMessages.SiteEditor_NewArchiveDialog_path);
88         fPathText = new Text(container, SWT.SINGLE | SWT.BORDER);
89         fPathText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
90
91         label = new Label(container, SWT.NULL);
92         label.setText(PDEUIMessages.SiteEditor_NewArchiveDialog_url);
93         fUrlText = new Text(container, SWT.SINGLE | SWT.BORDER);
94         fUrlText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
95
96         if (fSiteArchive != null) {
97             setIfDefined(fUrlText, fSiteArchive.getURL());
98             setIfDefined(fPathText, fSiteArchive.getPath());
99         }
100     }
101
102     private IStatus createErrorStatus(String JavaDoc message) {
103         return new Status(IStatus.ERROR, PDEPlugin.getPluginId(), IStatus.OK,
104                 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 JavaDoc path) {
152         String JavaDoc currentPath = fSiteArchive != null ? fSiteArchive.getPath()
153                 : 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 JavaDoc 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 JavaDoc value) {
177         if (value != null)
178             text.setText(value);
179     }
180 }
181
Popular Tags