1 11 package org.eclipse.update.internal.ui.wizards; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.jface.dialogs.Dialog; 19 import org.eclipse.jface.dialogs.IDialogConstants; 20 import org.eclipse.jface.dialogs.StatusDialog; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.ModifyEvent; 24 import org.eclipse.swt.events.ModifyListener; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 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.update.internal.ui.URLCoder; 34 import org.eclipse.update.internal.ui.UpdateUI; 35 import org.eclipse.update.internal.ui.UpdateUIMessages; 36 import org.eclipse.update.internal.ui.model.SiteBookmark; 37 import org.eclipse.update.internal.ui.model.UpdateModel; 38 39 40 public class NewUpdateSiteDialog extends StatusDialog { 41 42 protected Text name; 43 protected Text url; 44 private Button okButton; 45 private boolean enableOK = false; 46 private SiteBookmark[] siteBookmarks; 47 50 public NewUpdateSiteDialog(Shell parentShell) { 51 super(parentShell); 52 enableOK = false; 53 } 54 55 public NewUpdateSiteDialog(Shell parentShell, SiteBookmark[] siteBookmarks) { 56 57 this(parentShell); 58 this.siteBookmarks = siteBookmarks; 59 } 60 61 public NewUpdateSiteDialog(Shell parentShell, boolean enableOkButtons) { 62 super(parentShell); 63 enableOK = enableOkButtons; 64 } 65 66 public NewUpdateSiteDialog(Shell parentShell, boolean enableOkButtons, SiteBookmark[] siteBookmarks) { 67 this(parentShell, enableOkButtons); 68 this.siteBookmarks = siteBookmarks; 69 } 70 71 protected void createButtonsForButtonBar(Composite parent) { 72 73 okButton = createButton( 75 parent, 76 IDialogConstants.OK_ID, 77 IDialogConstants.OK_LABEL, 78 true); 79 createButton( 80 parent, 81 IDialogConstants.CANCEL_ID, 82 IDialogConstants.CANCEL_LABEL, 83 false); 84 85 okButton.setEnabled(enableOK); 86 87 } 88 89 protected Control createDialogArea(Composite parent) { 90 Composite composite = new Composite(parent, SWT.NONE); 91 GridLayout layout = new GridLayout(); 92 layout.numColumns = 2; 93 composite.setLayout(layout); 94 GridData data = new GridData(); 95 data.widthHint = 350; 96 composite.setLayoutData(data); 97 98 Label label = new Label(composite, SWT.NONE); 99 label.setText(UpdateUIMessages.NewUpdateSiteDialog_name); 100 101 name = new Text(composite, SWT.BORDER); 102 name.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 103 name.addModifyListener(new ModifyListener() { 104 public void modifyText(ModifyEvent e) { 105 verifyComplete(); 106 } 107 }); 108 109 label = new Label(composite, SWT.NONE); 110 label.setText(UpdateUIMessages.NewUpdateSiteDialog_url); 111 112 url = new Text(composite, SWT.BORDER); 113 url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 114 url.addModifyListener(new ModifyListener() { 115 public void modifyText(ModifyEvent e) { 116 verifyComplete(); 117 } 118 }); 119 120 initializeFields(); 121 Dialog.applyDialogFont(composite); 122 return composite; 123 } 124 125 protected void initializeFields() { 126 url.setText("http://"); } 128 129 protected void okPressed() { 130 update(); 131 super.okPressed(); 132 } 133 134 protected void update() { 135 try { 136 UpdateModel model = UpdateUI.getDefault().getUpdateModel(); 137 SiteBookmark bookmark = new SiteBookmark(name.getText(), new URL (url.getText()), false); 138 bookmark.setSelected(true); 139 model.addBookmark(bookmark); 140 model.saveBookmarks(); 141 } catch (MalformedURLException e) { 142 } 143 } 144 145 private void verifyComplete() { 146 147 if (okButton == null) { 148 return; 149 } 150 151 if (name.getText().trim().length() == 0 || url.getText().trim().length() == 0) { 152 okButton.setEnabled(false); 153 this.updateStatus( new Status(IStatus.ERROR, UpdateUI.getPluginId(), IStatus.OK, UpdateUIMessages.NewUpdateSiteDialog_error_nameOrUrlNotSpecified, null)); 154 return; 155 } 156 157 try { 158 URL newURL = new URL (URLCoder.decode(url.getText().trim())); 159 if (url.getEditable()) { 160 okButton.setEnabled(!newURL.getProtocol().equals("file")); if (newURL.getProtocol().equals("file")) { okButton.setEnabled(false); 163 this.updateStatus( new Status(IStatus.ERROR, UpdateUI.getPluginId(), IStatus.OK, UpdateUIMessages.NewUpdateSiteDialog_error_incorrectUrl, null)); 164 return; 165 } 166 } 167 } catch (Exception e) { 168 okButton.setEnabled(false); 169 this.updateStatus( new Status(IStatus.ERROR, UpdateUI.getPluginId(), IStatus.OK, UpdateUIMessages.NewUpdateSiteDialog_error_incorrectUrl, null)); 170 return; 171 } 172 173 if (isDuplicate()) { 174 return; 175 } else { 176 okButton.setEnabled(true); 177 this.updateStatus( new Status(IStatus.OK, UpdateUI.getPluginId(), IStatus.OK, "", null)); } 179 180 181 } 182 183 private boolean isDuplicate() { 184 185 if ( siteBookmarks == null) 186 return false; 187 188 for( int i = 0; i < this.siteBookmarks.length; i++) { 189 if ( !isCurrentlyEditedSiteBookmark(i)) { 190 if (siteBookmarks[i].getLabel().equals(name.getText().trim())) { 191 okButton.setEnabled(false); 192 this.updateStatus( new Status(IStatus.ERROR, UpdateUI.getPluginId(), IStatus.OK, UpdateUIMessages.NewUpdateSiteDialog_error_duplicateName, null)); 193 return true; 194 } else if (siteBookmarks[i].getURL().toString().trim().equals(url.getText().trim())) { 195 okButton.setEnabled(false); 196 this.updateStatus( new Status(IStatus.ERROR, UpdateUI.getPluginId(), IStatus.OK, NLS.bind(UpdateUIMessages.NewUpdateSiteDialog_error_duplicateUrl, siteBookmarks[i].getLabel()), null)); 197 return true; 198 } 199 } 200 } 201 return false; 202 } 203 204 protected boolean isCurrentlyEditedSiteBookmark( int index) { 205 return false; 206 } 207 208 209 protected void updateButtonsEnableState(IStatus status) { 210 if (okButton != null && !okButton.isDisposed()) 211 okButton.setEnabled(!status.matches(IStatus.ERROR)); 212 } 213 214 215 216 } 217 | Popular Tags |