メインクラスを探す その 2

JavaMainTab クラスで [Search...] ボタンを作っているのは createMainTypeEditor メソッドの中にあります。

		fSearchButton = createPushButton(mainGroup,LauncherMessages.JavaMainTab_Searc_h_5, null); //$NON-NLS-1$
		fSearchButton.addSelectionListener(fListener);

重要なのは 2 行目のリスナを登録しているところです。fListener とは何者か?

	private WidgetListener fListener = new WidgetListener();

だそうです。WidgetListener クラスは内部クラスで次のように定義されています。

	private class WidgetListener implements ModifyListener, SelectionListener {
		public void modifyText(ModifyEvent e) {
			updateLaunchConfigurationDialog();
		}
		public void widgetSelected(SelectionEvent e) {
			Object source = e.getSource();
			if (source == fProjButton) {
				handleProjectButtonSelected();
			} else if (source == fSearchButton) {
				handleSearchButtonSelected();
			} else {
				updateLaunchConfigurationDialog();
			}
		}
		public void widgetDefaultSelected(SelectionEvent e) {
		}
	}

なんかこういうリスナはやだなぁ。なんでわざわざ if 文で選択させるようなコード書くんだろう?

それはそれとして、[Search...] ボタンの時は handleSearchButtonSelected メソッドに飛ぶようですが、そのメソッドは長い ... ^^;;;;

	protected void handleSearchButtonSelected() {
		
		IJavaProject javaProject = getJavaProject();
		IJavaElement[] elements = null;
		if ((javaProject == null) || !javaProject.exists()) {
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IJavaModel model = JavaCore.create(root);
			if (model != null) {
				try {
					elements = model.getJavaProjects();
				} catch (JavaModelException e) {
				}
			}
		} else {
			elements = new IJavaElement[]{javaProject};
		}		
		if (elements == null) {
			elements = new IJavaElement[]{};
		}
		int constraints = IJavaSearchScope.SOURCES;
		if (fSearchExternalJarsCheckButton.getSelection()) {
			constraints |= IJavaSearchScope.APPLICATION_LIBRARIES;
			constraints |= IJavaSearchScope.SYSTEM_LIBRARIES;
		}		
		IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(elements, constraints);
		
		MainMethodSearchEngine engine = new MainMethodSearchEngine();
		IType[] types = null;
		try {
			types = engine.searchMainMethods(getLaunchConfigurationDialog(), searchScope, fConsiderInheritedMainButton.getSelection());
		} catch (InvocationTargetException e) {
			setErrorMessage(e.getMessage());
			return;
		} catch (InterruptedException e) {
			setErrorMessage(e.getMessage());
			return;
		}
		
		Shell shell = getShell();
		SelectionDialog dialog = new MainTypeSelectionDialog(shell, types); 
		dialog.setTitle(LauncherMessages.JavaMainTab_Choose_Main_Type_11); //$NON-NLS-1$
		dialog.setMessage(LauncherMessages.JavaMainTab_Choose_a_main__type_to_launch__12); //$NON-NLS-1$
		if (dialog.open() == Window.CANCEL) {
			return;
		}
		
		Object[] results = dialog.getResult();
		if ((results == null) || (results.length < 1)) {
			return;
		}		
		IType type = (IType)results[0];
		if (type != null) {
			fMainText.setText(type.getFullyQualifiedName());
			javaProject = type.getJavaProject();
			fProjText.setText(javaProject.getElementName());
		}
	}