1 11 package org.eclipse.jdt.internal.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Comparator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IStatus; 19 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Label; 27 import org.eclipse.swt.widgets.Shell; 28 29 import org.eclipse.jface.dialogs.IDialogConstants; 30 import org.eclipse.jface.viewers.ILabelProvider; 31 32 import org.eclipse.ui.dialogs.AbstractElementListSelectionDialog; 33 import org.eclipse.ui.dialogs.FilteredList; 34 35 import org.eclipse.jdt.internal.corext.util.Messages; 36 37 import org.eclipse.jdt.internal.ui.JavaUIMessages; 38 39 43 public class MultiElementListSelectionDialog extends AbstractElementListSelectionDialog { 44 45 private static class Page { 46 private Object [] elements; 47 public String filter; 48 public boolean okState= false; 49 50 public Page(Object [] elements) { 51 this.elements= elements; 52 } 53 } 54 55 private Page[] fPages; 56 private int fCurrentPage; 57 private int fNumberOfPages; 58 59 private Button fFinishButton; 60 private Button fBackButton; 61 private Button fNextButton; 62 63 private Label fPageInfoLabel; 64 private String fPageInfoMessage= JavaUIMessages.MultiElementListSelectionDialog_pageInfoMessage; 65 private Comparator fComparator; 66 67 72 public MultiElementListSelectionDialog(Shell parent, ILabelProvider renderer) { 73 super(parent, renderer); 74 } 75 76 81 public void setPageInfoMessage(String message) { 82 fPageInfoMessage= message; 83 } 84 85 89 public void setElements(Object [][] elements) { 90 fNumberOfPages= elements.length; 91 fPages= new Page[fNumberOfPages]; 92 for (int i= 0; i != fNumberOfPages; i++) 93 fPages[i]= new Page(elements[i]); 94 95 initializeResult(fNumberOfPages); 96 } 97 98 101 public int open() { 102 List selection= getInitialElementSelections(); 103 if (selection == null || selection.size() != fNumberOfPages) { 104 setInitialSelections(new Object [fNumberOfPages]); 105 selection= getInitialElementSelections(); 106 } 107 108 Assert.isTrue(selection.size() == fNumberOfPages); 109 110 return super.open(); 111 } 112 113 116 protected Control createDialogArea(Composite parent) { 117 Composite contents= (Composite) super.createDialogArea(parent); 118 119 createMessageArea(contents); 120 createFilterText(contents); 121 createFilteredList(contents); 122 123 fCurrentPage= 0; 124 setPageData(); 125 126 applyDialogFont(contents); 127 return contents; 128 } 129 130 133 protected void createButtonsForButtonBar(Composite parent) { 134 fBackButton= createButton(parent, IDialogConstants.BACK_ID, IDialogConstants.BACK_LABEL, false); 135 fNextButton= createButton(parent, IDialogConstants.NEXT_ID, IDialogConstants.NEXT_LABEL, true); 136 fFinishButton= createButton(parent, IDialogConstants.OK_ID, IDialogConstants.FINISH_LABEL, false); 137 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 138 } 139 140 143 protected Label createMessageArea(Composite parent) { 144 Composite composite= new Composite(parent, SWT.NONE); 145 146 GridLayout layout= new GridLayout(); 147 layout.marginHeight= 0; 148 layout.marginWidth= 0; 149 layout.horizontalSpacing= 5; 150 layout.numColumns= 2; 151 composite.setLayout(layout); 152 153 GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 154 composite.setLayoutData(data); 155 156 Label messageLabel= super.createMessageArea(composite); 157 158 fPageInfoLabel= new Label(composite, SWT.NULL); 159 fPageInfoLabel.setText(getPageInfoMessage()); 160 161 data= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 162 data.horizontalAlignment= GridData.END; 163 fPageInfoLabel.setLayoutData(data); 164 applyDialogFont(messageLabel); 165 return messageLabel; 166 } 167 168 171 protected void computeResult() { 172 setResult(fCurrentPage, getSelectedElements()); 173 } 174 175 178 protected void buttonPressed(int buttonId) { 179 if (buttonId == IDialogConstants.BACK_ID) { 180 turnPage(false); 181 } else if (buttonId == IDialogConstants.NEXT_ID) { 182 turnPage(true); 183 } else { 184 super.buttonPressed(buttonId); 185 } 186 } 187 188 191 protected void handleDefaultSelected() { 192 if (validateCurrentSelection()) { 193 if (fCurrentPage == fNumberOfPages - 1) { 194 buttonPressed(IDialogConstants.OK_ID); 195 } else { 196 buttonPressed(IDialogConstants.NEXT_ID); 197 } 198 } 199 } 200 201 204 protected void updateButtonsEnableState(IStatus status) { 205 boolean isOK= !status.matches(IStatus.ERROR); 206 fPages[fCurrentPage].okState= isOK; 207 208 boolean isAllOK= isOK; 209 for (int i= 0; i != fNumberOfPages; i++) 210 isAllOK = isAllOK && fPages[i].okState; 211 212 fFinishButton.setEnabled(isAllOK); 213 214 boolean nextButtonEnabled= isOK && (fCurrentPage < fNumberOfPages - 1); 215 216 fNextButton.setEnabled(nextButtonEnabled); 217 fBackButton.setEnabled(fCurrentPage != 0); 218 219 if (nextButtonEnabled) { 220 getShell().setDefaultButton(fNextButton); 221 } else if (isAllOK) { 222 getShell().setDefaultButton(fFinishButton); 223 } 224 } 225 226 private void turnPage(boolean toNextPage) { 227 Page page= fPages[fCurrentPage]; 228 229 String filter= getFilter(); 231 if (filter == null) 232 filter= ""; page.filter= filter; 234 235 Object [] selectedElements= getSelectedElements(); 237 List list= getInitialElementSelections(); 238 list.set(fCurrentPage, selectedElements); 239 240 setResult(fCurrentPage, getSelectedElements()); 242 243 if (toNextPage) { 244 if (fCurrentPage + 1 >= fNumberOfPages) 245 return; 246 247 fCurrentPage++; 248 } else { 249 if (fCurrentPage - 1 < 0) 250 return; 251 252 fCurrentPage--; 253 } 254 255 if (fPageInfoLabel != null && !fPageInfoLabel.isDisposed()) 256 fPageInfoLabel.setText(getPageInfoMessage()); 257 258 setPageData(); 259 260 validateCurrentSelection(); 261 } 262 263 private void setPageData() { 264 Page page= fPages[fCurrentPage]; 265 266 setListElements(page.elements); 268 269 String filter= page.filter; 271 if (filter == null) 272 filter= ""; setFilter(filter); 274 275 Object [] selectedElements= (Object []) getInitialElementSelections().get(fCurrentPage); 277 setSelection(selectedElements); 278 fFilteredList.setFocus(); 279 } 280 281 private String getPageInfoMessage() { 282 if (fPageInfoMessage == null) 283 return ""; 285 String [] args= new String [] { Integer.toString(fCurrentPage + 1), Integer.toString(fNumberOfPages) }; 286 return Messages.format(fPageInfoMessage, args); 287 } 288 289 private void initializeResult(int length) { 290 List result= new ArrayList (length); 291 for (int i= 0; i != length; i++) 292 result.add(null); 293 294 setResult(result); 295 } 296 297 301 public int getCurrentPage() { 302 return fCurrentPage; 303 } 304 305 311 public void setComparator(Comparator comparator) { 312 fComparator= comparator; 313 if (fFilteredList != null) 314 fFilteredList.setComparator(fComparator); 315 } 316 317 320 protected FilteredList createFilteredList(Composite parent) { 321 FilteredList filteredList= super.createFilteredList(parent); 322 if (fComparator != null) { 323 filteredList.setComparator(fComparator); 324 } 325 return filteredList; 326 } 327 328 } 329 | Popular Tags |