1 11 12 package org.eclipse.ui.internal.ide.dialogs; 13 14 import java.io.File ; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.Path; 18 import org.eclipse.jface.dialogs.IDialogConstants; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.widgets.*; 21 import org.eclipse.ui.dialogs.SelectionDialog; 22 import org.eclipse.ui.help.WorkbenchHelp; 23 import org.eclipse.ui.internal.ide.IHelpContextIds; 24 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 25 26 46 public class PathVariableSelectionDialog extends SelectionDialog { 47 private static final int EXTEND_ID = IDialogConstants.CLIENT_ID + 1; 48 private PathVariablesGroup pathVariablesGroup; 49 private int variableType; 50 51 59 public PathVariableSelectionDialog(Shell parentShell, int variableType) { 60 super(parentShell); 61 setTitle(IDEWorkbenchMessages.getString("PathVariableSelectionDialog.title")); this.variableType = variableType; 63 pathVariablesGroup = new PathVariablesGroup( 64 false, 65 variableType, 66 new Listener() { 67 public void handleEvent(Event event) { 68 updateExtendButtonState(); 69 } 70 } 71 ); 72 setShellStyle(getShellStyle() | SWT.RESIZE); 73 } 74 79 protected void buttonPressed(int buttonId) { 80 if (buttonId == EXTEND_ID) { 81 FileFolderSelectionDialog dialog = new FileFolderSelectionDialog(getShell(), false, variableType); 82 PathVariablesGroup.PathVariableElement selection = pathVariablesGroup.getSelection()[0]; 83 dialog.setTitle(IDEWorkbenchMessages.getString("PathVariableSelectionDialog.ExtensionDialog.title")); dialog.setMessage(IDEWorkbenchMessages.format("PathVariableSelectionDialog.ExtensionDialog.description", new Object [] {selection.name})); dialog.setInput(selection.path.toFile()); 86 if (dialog.open() == FileFolderSelectionDialog.OK && pathVariablesGroup.performOk()) { 87 setExtensionResult(selection, (File ) dialog.getResult()[0]); 88 super.okPressed(); 89 } 90 } 91 else 92 super.buttonPressed(buttonId); 93 } 94 97 protected void configureShell(Shell shell) { 98 super.configureShell(shell); 99 WorkbenchHelp.setHelp(shell, IHelpContextIds.PATH_VARIABLE_SELECTION_DIALOG); 100 } 101 106 protected void createButtonsForButtonBar(Composite parent) { 107 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 108 createButton(parent, EXTEND_ID, IDEWorkbenchMessages.getString("PathVariableSelectionDialog.extendButton"), false); createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 110 updateExtendButtonState(); 111 } 112 115 protected Control createDialogArea(Composite parent) { 116 Composite dialogArea = (Composite)super.createDialogArea(parent); 118 119 pathVariablesGroup.createContents(dialogArea); 120 return dialogArea; 121 } 122 126 public boolean close() { 127 pathVariablesGroup.dispose(); 128 return super.close(); 129 } 130 133 protected void okPressed() { 134 if (pathVariablesGroup.performOk()) { 135 PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup.getSelection(); 136 String [] variableNames = new String [selection.length]; 137 138 for (int i = 0; i < selection.length; i++) 139 variableNames[i] = selection[i].name; 140 setSelectionResult(variableNames); 141 } 142 else { 143 setSelectionResult(null); 144 } 145 super.okPressed(); 146 } 147 154 private void setExtensionResult(PathVariablesGroup.PathVariableElement variable, File extensionFile) { 155 IPath extensionPath = new Path(extensionFile.getPath()); 156 int matchCount = extensionPath.matchingFirstSegments(variable.path); 157 IPath resultPath = new Path(variable.name); 158 159 extensionPath = extensionPath.removeFirstSegments(matchCount); 160 resultPath = resultPath.append(extensionPath); 161 setSelectionResult(new String [] {resultPath.toOSString()}); 162 } 163 167 protected void updateExtendButtonState() { 168 PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup.getSelection(); 169 Button extendButton = getButton(EXTEND_ID); 170 171 if (extendButton == null) 172 return; 173 if (selection.length == 1) { 174 File file = selection[0].path.toFile(); 175 if (file.exists() == false || file.isFile()) 176 extendButton.setEnabled(false); 177 else 178 extendButton.setEnabled(true); 179 } 180 else 181 extendButton.setEnabled(false); 182 } 183 184 } 185 | Popular Tags |