FileDialog の FilterPath の設定 その 4
で、WizardNewProjectCreationPage クラスを調べています。
Browse ボタンはこんなコードで生成・設定されています。
// browse button browseButton = new Button(projectGroup, SWT.PUSH); browseButton.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_browseLabel); browseButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { handleLocationBrowseButtonPressed(); } });
handleLocationBrowseButtonPressed() メソッドが FileDialog を開くためのメソッドのようです。
void handleLocationBrowseButtonPressed() { DirectoryDialog dialog = new DirectoryDialog(locationPathField .getShell()); dialog.setMessage(IDEWorkbenchMessages.WizardNewProjectCreationPage_directoryLabel); String dirName = getProjectLocationFieldValue(); if (!dirName.equals("")) { //$NON-NLS-1$ File path = new File(dirName); if (path.exists()) dialog.setFilterPath(new Path(dirName).toOSString()); } String selectedDirectory = dialog.open(); if (selectedDirectory != null) { customLocationFieldValue = selectedDirectory; locationPathField.setText(customLocationFieldValue); } }
FileDialog クラスではなくて、DirectryDialog クラスでしたが、本質は同じでしょう。
setFilterPath の引数には dirName を使用した Path オブジェクトを指定しています。dirName に代入指定rのは... getProjectLocationFieldValue メソッドのようです。
private String getProjectLocationFieldValue() { if (locationPathField == null) return ""; //$NON-NLS-1$ return locationPathField.getText().trim(); }
ここで設定されているわけじゃないんですね。それじゃ、locationPathField を設定しているところを探しましょう。locationPathField は Text オブジェクトなので、Browse ボタンを生成している createUserSpecifiedProjectLocationGroup メソッドで生成・設定されているのでした。灯台もと暗しだ。
// Set the initial value first before listener // to avoid handling an event during the creation. if (initialLocationFieldValue == null) locationPathField.setText(Platform.getLocation().toOSString()); else locationPathField.setText(initialLocationFieldValue); locationPathField.addListener(SWT.Modify, locationModifyListener);
Platform.getLocation() ? どうやらこれがデフォルトの場所を示しているようです。そうか、プロジェクトを置く場所はそこだもんな。
そこからたどって各プロジェクトのワーキングディレクトリを探すことはできますが、デフォルトの場所以外の場所にあるプロジェクトにはこの方法は通用しません。
そういえば、Plugin.getLocation メソッドでディレクトリの IPath オブジェクトを返してくれるのです。
今まで注目していたのは、path とか directory とか file とか folder とかいった単語が含まれていたメソッドばかりでした。
location というのは、Out of Focus だったわけです。
で、調べてみると IJavaProject インタフェースには getLocation メソッドはないのですが、IProject にはありました。というか親の親のインタフェースの IResource インタフェースで定義されていました。
これを使って表示させてみると (p が IParoject オブジェクトです)
System.out.println("Path: " + p.getLocation());
次のように表示されました。
Default: D:/workbench/runtime-EclipseApplication/sample
ふぅー、やっとできた。