PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/gwt/plugins/in.cypal.studio.gwt.ui/src/in/cypal/studio/gwt/ui/preferences/GwtVersionsPreferencePage.java

http://cypal-studio.googlecode.com/
Java | 203 lines | 168 code | 32 blank | 3 comment | 17 complexity | 8d91a386cac23eb5dcc8f0afdfbce2b8 MD5 | raw file
  1. package in.cypal.studio.gwt.ui.preferences;
  2. import in.cypal.studio.gwt.core.common.Constants;
  3. import in.cypal.studio.gwt.core.common.GwtRuntime;
  4. import in.cypal.studio.gwt.ui.Activator;
  5. import in.cypal.studio.gwt.ui.common.GwtLabelProvider;
  6. import in.cypal.studio.gwt.ui.common.Util;
  7. import java.io.File;
  8. import java.lang.reflect.Field;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.net.URLClassLoader;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. import org.eclipse.jface.dialogs.MessageDialog;
  16. import org.eclipse.jface.preference.PreferencePage;
  17. import org.eclipse.jface.viewers.ArrayContentProvider;
  18. import org.eclipse.jface.viewers.CheckStateChangedEvent;
  19. import org.eclipse.jface.viewers.CheckboxTableViewer;
  20. import org.eclipse.jface.viewers.ICheckStateListener;
  21. import org.eclipse.jface.viewers.ISelectionChangedListener;
  22. import org.eclipse.jface.viewers.IStructuredSelection;
  23. import org.eclipse.jface.viewers.SelectionChangedEvent;
  24. import org.eclipse.swt.SWT;
  25. import org.eclipse.swt.events.SelectionAdapter;
  26. import org.eclipse.swt.events.SelectionEvent;
  27. import org.eclipse.swt.layout.GridData;
  28. import org.eclipse.swt.layout.GridLayout;
  29. import org.eclipse.swt.widgets.Button;
  30. import org.eclipse.swt.widgets.Composite;
  31. import org.eclipse.swt.widgets.Control;
  32. import org.eclipse.swt.widgets.DirectoryDialog;
  33. import org.eclipse.swt.widgets.Label;
  34. import org.eclipse.ui.IWorkbench;
  35. import org.eclipse.ui.IWorkbenchPreferencePage;
  36. public class GwtVersionsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
  37. private CheckboxTableViewer tableViewer;
  38. private Button addButton;
  39. private Button deleteButton;
  40. private IWorkbench workbench;
  41. List<GwtRuntime> runtimes;
  42. public GwtVersionsPreferencePage() {
  43. setPreferenceStore(Util.getPreferenceStore());
  44. setDescription("Add or Remove available GWT versions. The checked version is set as the default one.");
  45. runtimes = new ArrayList<GwtRuntime>();
  46. runtimes.addAll(Arrays.asList(Util.getRuntimes()));
  47. }
  48. public void init(IWorkbench workbench) {
  49. this.workbench = workbench;
  50. }
  51. @Override
  52. protected Control createContents(Composite parent) {
  53. Composite composite = new Composite(parent, SWT.NONE);
  54. composite.setLayout(new GridLayout(2, false));
  55. Label label = new Label(composite, SWT.NONE);
  56. label.setText("GWT Runtimes:");
  57. label.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 2, 1));
  58. tableViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER | SWT.SINGLE);
  59. tableViewer.setContentProvider(new ArrayContentProvider());
  60. tableViewer.setLabelProvider(new GwtLabelProvider());
  61. tableViewer.setInput(runtimes);
  62. GridData layoutData = new GridData(SWT.FILL, SWT.NONE, true, false, 1, 4);
  63. layoutData.heightHint = 100;
  64. tableViewer.getTable().setLayoutData(layoutData);
  65. addButton = new Button(composite, SWT.PUSH);
  66. addButton.setText("Add...");
  67. addButton.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false));
  68. deleteButton = new Button(composite, SWT.PUSH);
  69. deleteButton.setText("Delete");
  70. deleteButton.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false));
  71. deleteButton.setEnabled(false);
  72. addListeners();
  73. updateTable(null);
  74. return composite;
  75. }
  76. private void addListeners() {
  77. tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
  78. public void selectionChanged(SelectionChangedEvent event) {
  79. boolean isEmptySelection = event.getSelection().isEmpty();
  80. deleteButton.setEnabled(!isEmptySelection);
  81. }
  82. });
  83. tableViewer.addCheckStateListener(new ICheckStateListener() {
  84. public void checkStateChanged(CheckStateChangedEvent event) {
  85. if (event.getChecked()) {
  86. GwtRuntime defaultRuntime = (GwtRuntime) event.getElement();
  87. defaultRuntime.setWorkspaceDefault(true);
  88. updateTable(defaultRuntime);
  89. }
  90. }
  91. });
  92. addButton.addSelectionListener(new SelectionAdapter() {
  93. @Override
  94. public void widgetSelected(SelectionEvent e) {
  95. doAdd();
  96. }
  97. });
  98. deleteButton.addSelectionListener(new SelectionAdapter() {
  99. @Override
  100. public void widgetSelected(SelectionEvent e) {
  101. IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
  102. runtimes.remove(selection.getFirstElement());
  103. updateTable(null);
  104. }
  105. });
  106. }
  107. protected void doAdd() {
  108. DirectoryDialog dialog = new DirectoryDialog(getShell());
  109. dialog.setMessage("Browse for the GWT installation:");
  110. String location = dialog.open();
  111. if (location != null) {
  112. File file = new File(location + File.separatorChar + Util.getGwtDevLibJarName());
  113. if (!file.exists()) {
  114. MessageDialog.openError(getShell(), "Invalid directory", "The directory doesn't contain '" + Util.getGwtDevLibJarName() + "' file.");
  115. } else {
  116. try {
  117. URLClassLoader classLoader = new URLClassLoader(new URL[] { file.toURI().toURL() }, this.getClass().getClassLoader());
  118. @SuppressWarnings("unchecked")
  119. Class aboutClass = classLoader.loadClass(Constants.GWT_ABOUT_CLASS);
  120. Field versionField = aboutClass.getDeclaredField("GWT_VERSION");
  121. String version = (String) versionField.get(aboutClass);
  122. GwtRuntime gwtRuntime = new GwtRuntime();
  123. gwtRuntime.setName(version);
  124. gwtRuntime.setLocation(location);
  125. runtimes.add(gwtRuntime);
  126. updateTable(null);
  127. } catch (MalformedURLException e) {
  128. Activator.logException(e);
  129. MessageDialog.openError(getShell(), "Error Occured", "Could not determine the GWT version.");
  130. } catch (Exception e) {
  131. Activator.logException(e);
  132. MessageDialog.openError(getShell(), "Error Occured", "The '" + Util.getGwtDevLibJarName() + "' file seems to be corrupted");
  133. }
  134. }
  135. }
  136. }
  137. private void updateTable(GwtRuntime defaultRuntime) {
  138. if (defaultRuntime == null) {
  139. // first search in the list of runtimes
  140. for (GwtRuntime gwtRuntime : runtimes) {
  141. if (gwtRuntime.isWorkspaceDefault()) {
  142. defaultRuntime = gwtRuntime;
  143. break;
  144. }
  145. }
  146. // if nothing set, then set the first one
  147. if (defaultRuntime == null && runtimes.size() > 0) {
  148. defaultRuntime = runtimes.get(0);
  149. defaultRuntime.setWorkspaceDefault(true);
  150. }
  151. }
  152. tableViewer.refresh();
  153. if (defaultRuntime != null) {
  154. // unset others
  155. for (GwtRuntime gwtRuntime : runtimes) {
  156. if (gwtRuntime != defaultRuntime)
  157. gwtRuntime.setWorkspaceDefault(false);
  158. }
  159. tableViewer.setCheckedElements(new Object[] { defaultRuntime });
  160. }
  161. }
  162. @Override
  163. public boolean performOk() {
  164. Util.setRuntimes(runtimes.toArray(new GwtRuntime[runtimes.size()]));
  165. return true;
  166. }
  167. @Override
  168. protected void performDefaults() {
  169. super.performDefaults();
  170. }
  171. }