KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > jres > JREsComboBlock


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.jdt.internal.debug.ui.jres;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.ListenerList;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
27 import org.eclipse.jdt.internal.debug.ui.actions.ControlAccessibleListener;
28 import org.eclipse.jdt.launching.IVMInstall;
29 import org.eclipse.jdt.launching.IVMInstallType;
30 import org.eclipse.jdt.launching.JavaRuntime;
31 import org.eclipse.jdt.launching.VMStandin;
32 import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
33 import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
34 import org.eclipse.jface.util.IPropertyChangeListener;
35 import org.eclipse.jface.util.PropertyChangeEvent;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.graphics.Font;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Event;
46 import org.eclipse.swt.widgets.Group;
47 import org.eclipse.swt.widgets.Listener;
48 import org.eclipse.swt.widgets.Shell;
49
50 import com.ibm.icu.text.MessageFormat;
51
52 /**
53  * A composite that displays installed JRE's in a combo box, with a 'manage...'
54  * button to modify installed JREs.
55  * <p>
56  * This block implements ISelectionProvider - it sends selection change events
57  * when the checked JRE in the table changes, or when the "use default" button
58  * check state changes.
59  * </p>
60  */

61 public class JREsComboBlock {
62     
63     public static final String JavaDoc PROPERTY_JRE = "PROPERTY_JRE"; //$NON-NLS-1$
64

65     /**
66      * This block's control
67      */

68     private Composite fControl;
69     
70     /**
71      * VMs being displayed
72      */

73     private List JavaDoc fVMs = new ArrayList JavaDoc();
74     
75     /**
76      * The main control
77      */

78     private Combo fCombo;
79     
80     // Action buttons
81
private Button fManageButton;
82         
83     /**
84      * JRE change listeners
85      */

86     private ListenerList fListeners = new ListenerList();
87     
88     /**
89      * Default JRE descriptor or <code>null</code> if none.
90      */

91     private JREDescriptor fDefaultDescriptor = null;
92     
93     /**
94      * Specific JRE descriptor or <code>null</code> if none.
95      */

96     private JREDescriptor fSpecificDescriptor = null;
97
98     /**
99      * Default JRE radio button or <code>null</code> if none
100      */

101     private Button fDefaultButton = null;
102     
103     /**
104      * Selected JRE radio button
105      */

106     private Button fSpecificButton = null;
107     
108     /**
109      * The title used for the JRE block
110      */

111     private String JavaDoc fTitle = null;
112     
113     /**
114      * Selected JRE profile radio button
115      */

116     private Button fEnvironmentsButton = null;
117     
118     /**
119      * Combo box of JRE profiles
120      */

121     private Combo fEnvironmentsCombo = null;
122     
123     private Button fManageEnvironmentsButton = null;
124     
125     // a path to an unavailable JRE
126
private IPath fErrorPath;
127     
128     /**
129      * List of execution environments
130      */

131     private List JavaDoc fEnvironments = new ArrayList JavaDoc();
132     
133     private IStatus fStatus = OK_STATUS;
134     
135     private static IStatus OK_STATUS = new Status(IStatus.OK, JDIDebugUIPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
136

137     public void addPropertyChangeListener(IPropertyChangeListener listener) {
138         fListeners.add(listener);
139     }
140     
141     public void removePropertyChangeListener(IPropertyChangeListener listener) {
142         fListeners.remove(listener);
143     }
144     
145     private void firePropertyChange() {
146         PropertyChangeEvent event = new PropertyChangeEvent(this, PROPERTY_JRE, null, getPath());
147         Object JavaDoc[] listeners = fListeners.getListeners();
148         for (int i = 0; i < listeners.length; i++) {
149             IPropertyChangeListener listener = (IPropertyChangeListener) listeners[i];
150             listener.propertyChange(event);
151         }
152     }
153     
154     /**
155      * Creates this block's control in the given control.
156      *
157      * @param anscestor containing control
158      */

159     public void createControl(Composite ancestor) {
160         Font font = ancestor.getFont();
161         fControl = SWTFactory.createComposite(ancestor, font, 1, 1, GridData.FILL_BOTH);
162         if (fTitle == null) {
163             fTitle = JREMessages.JREsComboBlock_3;
164         }
165         Group group = SWTFactory.createGroup(fControl, fTitle, 1, 1, GridData.FILL_HORIZONTAL);
166         Composite comp = SWTFactory.createComposite(group, font, 3, 1, GridData.FILL_BOTH, 0, 0);
167     // display a 'use default JRE' check box
168
if (fDefaultDescriptor != null) {
169             fDefaultButton = SWTFactory.createRadioButton(comp, fDefaultDescriptor.getDescription(), 3);
170             fDefaultButton.addSelectionListener(new SelectionAdapter() {
171                 public void widgetSelected(SelectionEvent e) {
172                     if (fDefaultButton.getSelection()) {
173                         setUseDefaultJRE();
174                         setStatus(OK_STATUS);
175                         firePropertyChange();
176                     }
177                 }
178             });
179         }
180         
181     //specific JRE type
182
String JavaDoc text = JREMessages.JREsComboBlock_1;
183         if (fSpecificDescriptor != null) {
184             text = fSpecificDescriptor.getDescription();
185         }
186         fSpecificButton = SWTFactory.createRadioButton(comp, text, 1);
187         fSpecificButton.addSelectionListener(new SelectionAdapter() {
188             public void widgetSelected(SelectionEvent e) {
189                 if (fSpecificButton.getSelection()) {
190                     fCombo.setEnabled(true);
191                     if (fCombo.getText().length() == 0 && !fVMs.isEmpty()) {
192                         fCombo.select(0);
193                     }
194                     if (fVMs.isEmpty()) {
195                         setError(JREMessages.JREsComboBlock_0);
196                     } else {
197                         setStatus(OK_STATUS);
198                     }
199                     fEnvironmentsCombo.setEnabled(false);
200                     firePropertyChange();
201                 }
202             }
203         });
204         fCombo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
205         fCombo.setFont(font);
206         GridData data= new GridData(GridData.FILL_HORIZONTAL);
207         data.horizontalSpan = 1;
208         fCombo.setLayoutData(data);
209         ControlAccessibleListener.addListener(fCombo, fSpecificButton.getText());
210         
211         fCombo.addSelectionListener(new SelectionAdapter() {
212             public void widgetSelected(SelectionEvent e) {
213                 setStatus(OK_STATUS);
214                 firePropertyChange();
215             }
216         });
217                 
218         fManageButton = createPushButton(comp, JREMessages.JREsComboBlock_2);
219         fManageButton.addListener(SWT.Selection, new Listener() {
220             public void handleEvent(Event event) {
221                 showPrefPage("org.eclipse.jdt.debug.ui.preferences.VMPreferencePage"); //$NON-NLS-1$
222
}
223         });
224         fillWithWorkspaceJREs();
225         
226     //execution environments
227
fEnvironmentsButton = SWTFactory.createRadioButton(comp, JREMessages.JREsComboBlock_4);
228         fEnvironmentsButton.addSelectionListener(new SelectionAdapter() {
229             public void widgetSelected(SelectionEvent e) {
230                 if (fEnvironmentsButton.getSelection()) {
231                     fCombo.setEnabled(false);
232                     if (fEnvironmentsCombo.getText().length() == 0 && !fEnvironments.isEmpty()) {
233                         fEnvironmentsCombo.select(0);
234                     }
235                     fEnvironmentsCombo.setEnabled(true);
236                     if (fEnvironments.isEmpty()) {
237                         setError(JREMessages.JREsComboBlock_5);
238                     } else {
239                         setStatus(OK_STATUS);
240                     }
241                     firePropertyChange();
242                 }
243             }
244         });
245         
246         
247         fEnvironmentsCombo = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
248         fEnvironmentsCombo.setFont(font);
249         data= new GridData(GridData.FILL_HORIZONTAL);
250         data.horizontalSpan = 1;
251         fEnvironmentsCombo.setLayoutData(data);
252         
253         fEnvironmentsCombo.addSelectionListener(new SelectionAdapter() {
254             public void widgetSelected(SelectionEvent e) {
255                 setPath(JavaRuntime.newJREContainerPath(getEnvironment()));
256                 firePropertyChange();
257             }
258         });
259         
260         fManageEnvironmentsButton = createPushButton(comp, JREMessages.JREsComboBlock_14);
261         fManageEnvironmentsButton.addListener(SWT.Selection, new Listener() {
262             public void handleEvent(Event event) {
263                 showPrefPage("org.eclipse.jdt.debug.ui.jreProfiles"); //$NON-NLS-1$
264
}
265         });
266         
267         fillWithWorkspaceProfiles();
268     }
269     
270     /**
271      * Opens the given preference page, and updates when closed.
272      *
273      * @param id pref page id
274      * @param page pref page
275      */

276     private void showPrefPage(String JavaDoc id/*, IPreferencePage page*/) {
277         IVMInstall prevJRE = getJRE();
278         IExecutionEnvironment prevEnv = getEnvironment();
279         JDIDebugUIPlugin.showPreferencePage(id);
280         fillWithWorkspaceJREs();
281         fillWithWorkspaceProfiles();
282         restoreCombo(fVMs, prevJRE, fCombo);
283         restoreCombo(fEnvironments, prevEnv, fEnvironmentsCombo);
284         // update text
285
setDefaultJREDescriptor(fDefaultDescriptor);
286         if (isDefaultJRE()) {
287             // reset in case default has changed
288
setUseDefaultJRE();
289         }
290         setPath(getPath());
291         firePropertyChange();
292     }
293     
294     private void restoreCombo(List JavaDoc elements, Object JavaDoc element, Combo combo) {
295         int index = -1;
296         if (element != null) {
297             index = elements.indexOf(element);
298         }
299         if (index >= 0) {
300             combo.select(index);
301         } else {
302             combo.select(0);
303         }
304     }
305     
306     protected Button createPushButton(Composite parent, String JavaDoc label) {
307         return SWTFactory.createPushButton(parent, label, null);
308     }
309     
310     /**
311      * Returns this block's control
312      *
313      * @return control
314      */

315     public Control getControl() {
316         return fControl;
317     }
318     
319     /**
320      * Sets the JREs to be displayed in this block
321      *
322      * @param vms JREs to be displayed
323      */

324     protected void setJREs(List JavaDoc jres) {
325         fVMs.clear();
326         fVMs.addAll(jres);
327         // sort by name
328
Collections.sort(fVMs, new Comparator JavaDoc() {
329             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
330                 IVMInstall left = (IVMInstall)o1;
331                 IVMInstall right = (IVMInstall)o2;
332                 return left.getName().compareToIgnoreCase(right.getName());
333             }
334
335             public boolean equals(Object JavaDoc obj) {
336                 return obj == this;
337             }
338         });
339         // now make an array of names
340
String JavaDoc[] names = new String JavaDoc[fVMs.size()];
341         Iterator JavaDoc iter = fVMs.iterator();
342         int i = 0;
343         while (iter.hasNext()) {
344             IVMInstall vm = (IVMInstall)iter.next();
345             names[i] = vm.getName();
346             i++;
347         }
348         fCombo.setItems(names);
349         fCombo.setVisibleItemCount(Math.min(names.length, 20));
350     }
351     
352     protected Shell getShell() {
353         return getControl().getShell();
354     }
355
356     /**
357      * Selects a specific JRE based on type/name.
358      *
359      * @param vm JRE or <code>null</code>
360      */

361     private void selectJRE(IVMInstall vm) {
362         fSpecificButton.setSelection(true);
363         fDefaultButton.setSelection(false);
364         fEnvironmentsButton.setSelection(false);
365         fCombo.setEnabled(true);
366         fEnvironmentsCombo.setEnabled(false);
367         if (vm != null) {
368             int index = fVMs.indexOf(vm);
369             if (index >= 0) {
370                 fCombo.select(index);
371             }
372         }
373         firePropertyChange();
374     }
375     
376     /**
377      * Selects a JRE based environment.
378      *
379      * @param env environment or <code>null</code>
380      */

381     private void selectEnvironment(IExecutionEnvironment env) {
382         fSpecificButton.setSelection(false);
383         fDefaultButton.setSelection(false);
384         fCombo.setEnabled(false);
385         fEnvironmentsButton.setSelection(true);
386         fEnvironmentsCombo.setEnabled(true);
387         if (env != null) {
388             int index = fEnvironments.indexOf(env);
389             if (index >= 0) {
390                 fEnvironmentsCombo.select(index);
391             }
392         }
393         firePropertyChange();
394     }
395     
396     /**
397      * Returns the selected JRE or <code>null</code> if none.
398      *
399      * @return the selected JRE or <code>null</code> if none
400      */

401     public IVMInstall getJRE() {
402         int index = fCombo.getSelectionIndex();
403         if (index >= 0) {
404             return (IVMInstall)fVMs.get(index);
405         }
406         return null;
407     }
408     
409     /**
410      * Returns the selected Environment or <code>null</code> if none.
411      *
412      * @return the selected Environment or <code>null</code> if none
413      */

414     private IExecutionEnvironment getEnvironment() {
415         int index = fEnvironmentsCombo.getSelectionIndex();
416         if (index >= 0) {
417             return (IExecutionEnvironment)fEnvironments.get(index);
418         }
419         return null;
420     }
421     
422     /**
423      * Populates the JRE table with existing JREs defined in the workspace.
424      */

425     protected void fillWithWorkspaceJREs() {
426         // fill with JREs
427
List JavaDoc standins = new ArrayList JavaDoc();
428         IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
429         for (int i = 0; i < types.length; i++) {
430             IVMInstallType type = types[i];
431             IVMInstall[] installs = type.getVMInstalls();
432             for (int j = 0; j < installs.length; j++) {
433                 IVMInstall install = installs[j];
434                 standins.add(new VMStandin(install));
435             }
436         }
437         setJREs(standins);
438     }
439     
440     /**
441      * Populates the JRE profile combo with profiles defined in the workspace.
442      */

443     protected void fillWithWorkspaceProfiles() {
444         fEnvironments.clear();
445         IExecutionEnvironment[] environments = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
446         for (int i = 0; i < environments.length; i++) {
447             fEnvironments.add(environments[i]);
448         }
449         // sort by name
450
Collections.sort(fEnvironments, new Comparator JavaDoc() {
451             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
452                 IExecutionEnvironment left = (IExecutionEnvironment)o1;
453                 IExecutionEnvironment right = (IExecutionEnvironment)o2;
454                 return left.getId().compareToIgnoreCase(right.getId());
455             }
456
457             public boolean equals(Object JavaDoc obj) {
458                 return obj == this;
459             }
460         });
461         // now make an array of names
462
String JavaDoc[] names = new String JavaDoc[fEnvironments.size()];
463         Iterator JavaDoc iter = fEnvironments.iterator();
464         int i = 0;
465         while (iter.hasNext()) {
466             IExecutionEnvironment env = (IExecutionEnvironment)iter.next();
467             IPath path = JavaRuntime.newJREContainerPath(env);
468             IVMInstall install = JavaRuntime.getVMInstall(path);
469             if (install != null) {
470                 names[i] = MessageFormat.format(JREMessages.JREsComboBlock_15, new String JavaDoc[]{env.getId(), install.getName()});
471             } else {
472                 names[i] = MessageFormat.format(JREMessages.JREsComboBlock_16, new String JavaDoc[]{env.getId()});
473             }
474             i++;
475         }
476         fEnvironmentsCombo.setItems(names);
477         fEnvironmentsCombo.setVisibleItemCount(Math.min(names.length, 20));
478     }
479     
480     /**
481      * Sets the Default JRE Descriptor for this block.
482      *
483      * @param descriptor default JRE descriptor
484      */

485     public void setDefaultJREDescriptor(JREDescriptor descriptor) {
486         fDefaultDescriptor = descriptor;
487         setButtonTextFromDescriptor(fDefaultButton, descriptor);
488     }
489     
490     private void setButtonTextFromDescriptor(Button button, JREDescriptor descriptor) {
491         if (button != null) {
492             //update the description & JRE in case it has changed
493
String JavaDoc currentText = button.getText();
494             String JavaDoc newText = descriptor.getDescription();
495             if (!newText.equals(currentText)) {
496                 button.setText(newText);
497                 fControl.layout();
498             }
499         }
500     }
501
502     /**
503      * Sets the specific JRE Descriptor for this block.
504      *
505      * @param descriptor specific JRE descriptor
506      */

507     public void setSpecificJREDescriptor(JREDescriptor descriptor) {
508         fSpecificDescriptor = descriptor;
509         setButtonTextFromDescriptor(fSpecificButton, descriptor);
510     }
511     
512     /**
513      * Returns whether the 'use default JRE' button is checked.
514      *
515      * @return whether the 'use default JRE' button is checked
516      */

517     public boolean isDefaultJRE() {
518         if (fDefaultButton != null) {
519             return fDefaultButton.getSelection();
520         }
521         return false;
522     }
523     
524     /**
525      * Sets this control to use the 'default' JRE.
526      */

527     private void setUseDefaultJRE() {
528         if (fDefaultDescriptor != null) {
529             fDefaultButton.setSelection(true);
530             fSpecificButton.setSelection(false);
531             fEnvironmentsButton.setSelection(false);
532             fCombo.setEnabled(false);
533             fEnvironmentsCombo.setEnabled(false);
534             firePropertyChange();
535         }
536     }
537     
538     /**
539      * Sets the title used for this JRE block
540      *
541      * @param title title for this JRE block
542      */

543     public void setTitle(String JavaDoc title) {
544         fTitle = title;
545     }
546
547     /**
548      * Refresh the default JRE description.
549      */

550     public void refresh() {
551         setDefaultJREDescriptor(fDefaultDescriptor);
552     }
553     
554     /**
555      * Returns a classpath container path identifying the selected JRE.
556      *
557      * @return classpath container path or <code>null</code>
558      * @since 3.2
559      */

560     public IPath getPath() {
561         if (!getStatus().isOK() && fErrorPath != null) {
562             return fErrorPath;
563         }
564         if (fEnvironmentsButton.getSelection()) {
565             int index = fEnvironmentsCombo.getSelectionIndex();
566             if (index >= 0) {
567                 IExecutionEnvironment env = (IExecutionEnvironment) fEnvironments.get(index);
568                 return JavaRuntime.newJREContainerPath(env);
569             }
570             return null;
571         }
572         if (fSpecificButton.getSelection()) {
573             int index = fCombo.getSelectionIndex();
574             if (index >= 0) {
575                 IVMInstall vm = (IVMInstall) fVMs.get(index);
576                 return JavaRuntime.newJREContainerPath(vm);
577             }
578             return null;
579         }
580         return JavaRuntime.newDefaultJREContainerPath();
581     }
582     
583     /**
584      * Sets the selection based on the given container path and returns
585      * a status indicating if the selection was successful.
586      *
587      * @param containerPath
588      * @return status
589      */

590     public void setPath(IPath containerPath) {
591         fErrorPath = null;
592         setStatus(OK_STATUS);
593         if (JavaRuntime.newDefaultJREContainerPath().equals(containerPath)) {
594             setUseDefaultJRE();
595         } else {
596             String JavaDoc envId = JavaRuntime.getExecutionEnvironmentId(containerPath);
597             if (envId != null) {
598                 IExecutionEnvironmentsManager manager = JavaRuntime.getExecutionEnvironmentsManager();
599                 IExecutionEnvironment environment = manager.getEnvironment(envId);
600                 if (environment == null) {
601                     fErrorPath = containerPath;
602                     selectEnvironment(environment);
603                     setError(MessageFormat.format(JREMessages.JREsComboBlock_6, new String JavaDoc[]{envId}));
604                 } else {
605                     selectEnvironment(environment);
606                     IVMInstall[] installs = environment.getCompatibleVMs();
607                     if (installs.length == 0) {
608                         setError(MessageFormat.format(JREMessages.JREsComboBlock_7, new String JavaDoc[]{environment.getId()}));
609                     }
610                 }
611             } else {
612                 IVMInstall install = JavaRuntime.getVMInstall(containerPath);
613                 if (install == null) {
614                     selectJRE(install);
615                     fErrorPath = containerPath;
616                     String JavaDoc installTypeId = JavaRuntime.getVMInstallTypeId(containerPath);
617                     if (installTypeId == null) {
618                         setError(JREMessages.JREsComboBlock_8);
619                     } else {
620                         IVMInstallType installType = JavaRuntime.getVMInstallType(installTypeId);
621                         if (installType == null) {
622                             setError(MessageFormat.format(JREMessages.JREsComboBlock_9, new String JavaDoc[]{installTypeId}));
623                         } else {
624                             String JavaDoc installName = JavaRuntime.getVMInstallName(containerPath);
625                             if (installName == null) {
626                                 setError(MessageFormat.format(JREMessages.JREsComboBlock_10, new String JavaDoc[]{installType.getName()}));
627                             } else {
628                                 setError(MessageFormat.format(JREMessages.JREsComboBlock_11, new String JavaDoc[]{installName, installType.getName()}));
629                             }
630                         }
631                     }
632                 } else {
633                     selectJRE(install);
634                     File JavaDoc location = install.getInstallLocation();
635                     if (location == null) {
636                         setError(JREMessages.JREsComboBlock_12);
637                     } else if (!location.exists()) {
638                         setError(JREMessages.JREsComboBlock_13);
639                     }
640                 }
641             }
642         }
643     }
644     
645     private void setError(String JavaDoc message) {
646         setStatus(new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(),
647                 IJavaDebugUIConstants.INTERNAL_ERROR, message, null));
648     }
649     
650     /**
651      * Returns the status of the JRE selection.
652      *
653      * @return status
654      */

655     public IStatus getStatus() {
656         return fStatus;
657     }
658     
659     private void setStatus(IStatus status) {
660         fStatus = status;
661     }
662 }
663
Popular Tags