1 11 12 package org.eclipse.ui.ide.dialogs; 13 14 import org.eclipse.core.filesystem.EFS; 15 import org.eclipse.core.filesystem.IFileInfo; 16 import org.eclipse.core.filesystem.IFileStore; 17 import org.eclipse.core.filesystem.URIUtil; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.jface.dialogs.ErrorDialog; 22 import org.eclipse.jface.dialogs.IDialogConstants; 23 import org.eclipse.jface.window.Window; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.widgets.Button; 27 import org.eclipse.swt.widgets.Composite; 28 import org.eclipse.swt.widgets.Control; 29 import org.eclipse.swt.widgets.Event; 30 import org.eclipse.swt.widgets.Listener; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.dialogs.SelectionDialog; 34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 35 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 36 import org.eclipse.ui.internal.ide.dialogs.FileFolderSelectionDialog; 37 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils; 38 import org.eclipse.ui.internal.ide.dialogs.PathVariablesGroup; 39 40 60 public final class PathVariableSelectionDialog extends SelectionDialog { 61 private static final int EXTEND_ID = IDialogConstants.CLIENT_ID + 1; 62 63 private PathVariablesGroup pathVariablesGroup; 64 65 private int variableType; 66 67 75 public PathVariableSelectionDialog(Shell parentShell, int variableType) { 76 super(parentShell); 77 setTitle(IDEWorkbenchMessages.PathVariableSelectionDialog_title); 78 this.variableType = variableType; 79 pathVariablesGroup = new PathVariablesGroup(false, variableType, 80 new Listener() { 81 84 public void handleEvent(Event event) { 85 updateExtendButtonState(); 86 } 87 }); 88 setShellStyle(getShellStyle() | SWT.RESIZE); 89 } 90 91 92 95 protected void buttonPressed(int buttonId) { 96 if (buttonId == EXTEND_ID) { 97 FileFolderSelectionDialog dialog = new FileFolderSelectionDialog( 98 getShell(), false, variableType); 99 PathVariablesGroup.PathVariableElement selection = pathVariablesGroup 100 .getSelection()[0]; 101 dialog.setTitle(IDEWorkbenchMessages.PathVariableSelectionDialog_ExtensionDialog_title); 102 dialog.setMessage(NLS.bind(IDEWorkbenchMessages.PathVariableSelectionDialog_ExtensionDialog_description, selection.name)); 103 try { 105 dialog.setInput(EFS.getStore(URIUtil.toURI(selection.path))); 106 } catch (CoreException e) { 107 ErrorDialog.openError(getShell(), null, null, e.getStatus()); 108 } 109 if (dialog.open() == Window.OK 110 && pathVariablesGroup.performOk()) { 111 setExtensionResult(selection, (IFileStore) dialog.getResult()[0]); 112 super.okPressed(); 113 } 114 } else { 115 super.buttonPressed(buttonId); 116 } 117 } 118 119 122 protected void configureShell(Shell shell) { 123 super.configureShell(shell); 124 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 125 IIDEHelpContextIds.PATH_VARIABLE_SELECTION_DIALOG); 126 } 127 128 131 protected void createButtonsForButtonBar(Composite parent) { 132 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, 133 true); 134 createButton(parent, EXTEND_ID, IDEWorkbenchMessages.PathVariableSelectionDialog_extendButton, false); 135 createButton(parent, IDialogConstants.CANCEL_ID, 136 IDialogConstants.CANCEL_LABEL, false); 137 updateExtendButtonState(); 138 } 139 140 143 protected Control createDialogArea(Composite parent) { 144 Composite dialogArea = (Composite) super.createDialogArea(parent); 146 147 pathVariablesGroup.createContents(dialogArea); 148 return dialogArea; 149 } 150 151 152 155 public boolean close() { 156 pathVariablesGroup.dispose(); 157 return super.close(); 158 } 159 160 161 164 protected void okPressed() { 165 if (pathVariablesGroup.performOk()) { 167 PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup 168 .getSelection(); 169 String [] variableNames = new String [selection.length]; 170 171 for (int i = 0; i < selection.length; i++) { 172 variableNames[i] = selection[i].name; 173 } 174 setSelectionResult(variableNames); 175 } else { 176 setSelectionResult(null); 177 } 178 super.okPressed(); 179 } 180 181 188 private void setExtensionResult( 189 PathVariablesGroup.PathVariableElement variable, IFileStore extensionFile) { 190 IPath extensionPath = new Path(extensionFile.toString()); 191 int matchCount = extensionPath.matchingFirstSegments(variable.path); 192 IPath resultPath = new Path(variable.name); 193 194 extensionPath = extensionPath.removeFirstSegments(matchCount); 195 resultPath = resultPath.append(extensionPath); 196 setSelectionResult(new String [] { resultPath.toOSString() }); 197 } 198 199 203 private void updateExtendButtonState() { 204 PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup 205 .getSelection(); 206 Button extendButton = getButton(EXTEND_ID); 207 208 if (extendButton == null) { 209 return; 210 } 211 if (selection.length == 1) { 212 IFileInfo info = IDEResourceInfoUtils.getFileInfo(selection[0].path); 213 if (info.exists() && info.isDirectory()) { 214 extendButton.setEnabled(true); 215 } else { 216 extendButton.setEnabled(false); 217 } 218 219 } else { 220 extendButton.setEnabled(false); 221 } 222 } 223 224 } 225 | Popular Tags |