KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > MirrorsDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.update.internal.ui.wizards;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.Preferences;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.FocusAdapter;
20 import org.eclipse.swt.events.FocusEvent;
21 import org.eclipse.swt.events.MouseEvent;
22 import org.eclipse.swt.events.MouseListener;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.List;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Text;
31 import org.eclipse.update.core.ISiteWithMirrors;
32 import org.eclipse.update.core.IURLEntry;
33 import org.eclipse.update.internal.core.UpdateCore;
34 import org.eclipse.update.internal.ui.UpdateUIMessages;
35
36
37 public class MirrorsDialog extends Dialog {
38     //private ISiteWithMirrors site;
39
private String JavaDoc siteName;
40     private IURLEntry[] mirrors;
41     private List mirrorsList;
42     private IURLEntry mirrorSelected;
43     private Button automaticallyChooseMirrorCheckbox;
44     //private Button okButton;
45
/**
46      * @param parentShell
47      */

48     public MirrorsDialog(Shell parentShell, ISiteWithMirrors site, String JavaDoc siteName) {
49         super(parentShell);
50         setShellStyle(getShellStyle()|SWT.RESIZE);
51         //this.site = site;
52
this.siteName = siteName;
53         try {
54             this.mirrors = site.getMirrorSiteEntries();
55         } catch (CoreException e) {
56             // ignore, as the mirrors have already been queried earlier, so we don't expect error here
57
}
58     }
59     
60     protected void createButtonsForButtonBar(Composite parent) {
61         createButton(
62                 parent,
63                 IDialogConstants.OK_ID,
64                 IDialogConstants.OK_LABEL,
65                 true);
66         createButton(
67             parent,
68             IDialogConstants.CANCEL_ID,
69             IDialogConstants.CANCEL_LABEL,
70             false);
71     }
72     
73     protected Control createDialogArea(Composite parent) {
74         Composite composite = new Composite(parent, SWT.NONE);
75         GridLayout layout = new GridLayout();
76         layout.numColumns = 2;
77         layout.marginHeight = 10;
78         layout.marginWidth = 10;
79         layout.verticalSpacing = 10;
80         composite.setLayout(layout);
81         GridData data = new GridData(GridData.FILL_BOTH);
82         data.widthHint = 450;
83         data.heightHint = 300;
84         composite.setLayoutData(data);
85         
86         Text text = new Text(composite, SWT.WRAP );
87         data = new GridData(GridData.FILL_HORIZONTAL);
88         data.horizontalSpan = 2;
89         text.setLayoutData(data);
90         text.setText(NLS.bind(UpdateUIMessages.MirrorsDialog_text, siteName));
91         text.setBackground(parent.getBackground());
92         text.setEditable(false);
93         // the text should not receive focus
94
text.addFocusListener(new FocusAdapter() {
95             public void focusGained(FocusEvent e) {
96                 mirrorsList.setFocus();
97             }
98         });
99         
100         mirrorsList = new List(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
101         data = new GridData(GridData.FILL_BOTH);
102         data.horizontalSpan = 2;
103         mirrorsList.setLayoutData(data);
104         
105         for (int i=0; i<mirrors.length; i++)
106             mirrorsList.add(mirrors[i].getAnnotation());
107         mirrorsList.add(siteName);
108
109         mirrorsList.select(0);
110         
111         automaticallyChooseMirrorCheckbox =
112             new Button(composite, SWT.CHECK | SWT.LEFT);
113         automaticallyChooseMirrorCheckbox.setText(UpdateUIMessages.MainPreferencePage_automaticallyChooseMirror);
114         data = new GridData();
115         data.horizontalSpan = 2;
116         automaticallyChooseMirrorCheckbox.setLayoutData(data);
117         
118         Dialog.applyDialogFont(composite);
119         
120         mirrorsList.addMouseListener( new MouseListener() {
121                 public void mouseDoubleClick(MouseEvent e) {
122                     okPressed();
123                 }
124                 public void mouseDown(MouseEvent e) {
125                     // do nothing
126
}
127                 public void mouseUp(MouseEvent e) {
128                     // do nothing
129
}
130             }
131         );
132         return composite;
133     }
134         
135     protected void okPressed() {
136         int i = mirrorsList.getSelectionIndex();
137         // first entry is the site itself
138
if (i <mirrors.length)
139             mirrorSelected = mirrors[i];
140         if (automaticallyChooseMirrorCheckbox.getSelection()) {
141             Preferences prefs = UpdateCore.getPlugin().getPluginPreferences();
142             prefs.setValue(UpdateCore.P_AUTOMATICALLY_CHOOSE_MIRROR, true);
143             UpdateCore.getPlugin().savePluginPreferences();
144         }
145         super.okPressed();
146     }
147     
148     public IURLEntry getMirror() {
149         return mirrorSelected;
150     }
151
152    protected void configureShell(Shell shell) {
153        super.configureShell(shell);
154        shell.setText(UpdateUIMessages.MirrorsDialog_title);
155     }
156 }
157
Popular Tags