1 11 package org.eclipse.compare.internal; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.compare.CompareConfiguration; 16 import org.eclipse.compare.CompareEditorInput; 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.jface.dialogs.*; 19 import org.eclipse.jface.operation.IRunnableWithProgress; 20 import org.eclipse.jface.preference.IPreferenceStore; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.graphics.Point; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.widgets.*; 27 import org.eclipse.ui.PlatformUI; 28 29 36 public class CompareDialog extends TrayDialog implements IPropertyChangeListener { 37 38 private final CompareEditorInput fCompareEditorInput; 39 private Button fCommitButton; 40 private Label statusLabel; 41 boolean hasSettings = true; 42 private final DialogCompareContainer fContainer = new DialogCompareContainer(); 43 44 private class DialogCompareContainer extends CompareContainer { 45 46 49 public void run(boolean fork, boolean cancelable, 50 IRunnableWithProgress runnable) throws InvocationTargetException , 51 InterruptedException { 52 ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell()); 53 dialog.run(fork, cancelable, runnable); 54 } 55 56 59 public void setStatusMessage(String message) { 60 if (statusLabel != null && !statusLabel.isDisposed()) { 61 if (message == null) { 62 statusLabel.setText(""); } else { 64 statusLabel.setText(message); 65 } 66 } 67 } 68 } 69 70 75 public CompareDialog(Shell shell, CompareEditorInput input) { 76 super(shell); 77 setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX); 78 Assert.isNotNull(input); 79 fCompareEditorInput= input; 80 } 81 82 85 public boolean close() { 86 if (super.close()) { 87 if (fCompareEditorInput != null) 88 fCompareEditorInput.removePropertyChangeListener(this); 89 return true; 90 } 91 return false; 92 } 93 94 97 protected void createButtonsForButtonBar(Composite parent) { 98 fCommitButton= createButton(parent, IDialogConstants.OK_ID, getOKButtonLabel(), true); 99 fCommitButton.setEnabled(isOKEnabled()); 100 if (isCancelable()) { 101 createButton(parent, IDialogConstants.CANCEL_ID, getCancelButtonLabel(), false); 102 } 103 } 104 105 private String getCancelButtonLabel() { 106 return fCompareEditorInput.getCancelButtonLabel(); 107 } 108 109 private boolean isCancelable() { 110 return isInputEditable() || isElementSelectionDialog(); 111 } 112 113 private String getOKButtonLabel() { 114 return fCompareEditorInput.getOKButtonLabel(); 115 } 116 117 private boolean isElementSelectionDialog() { 118 return fCompareEditorInput.isEditionSelectionDialog(); 119 } 120 121 130 protected boolean isInputEditable() { 131 return fCompareEditorInput.getCompareConfiguration().isLeftEditable() 132 || fCompareEditorInput.getCompareConfiguration().isRightEditable(); 133 } 134 135 138 public void propertyChange(PropertyChangeEvent event) { 139 if (event.getProperty().equals(CompareEditorInput.DIRTY_STATE) 140 || event.getProperty().equals(CompareEditorInput.PROP_SELECTED_EDITION)) { 141 if (fCommitButton != null && fCompareEditorInput != null) 142 fCommitButton.setEnabled(isOKEnabled()); 143 } else if (event.getProperty().equals(CompareEditorInput.PROP_TITLE)) { 144 getShell().setText(fCompareEditorInput.getTitle()); 145 } else if (event.getProperty().equals(CompareEditorInput.PROP_TITLE_IMAGE)) { 146 getShell().setImage(fCompareEditorInput.getTitleImage()); 147 } 148 } 149 150 private boolean isOKEnabled() { 151 if (isInputEditable()) 152 return fCompareEditorInput.isDirty(); 153 if (isElementSelectionDialog()) 154 return getSelectedElement() != null; 155 return true; 156 } 157 158 private Object getSelectedElement() { 159 return fCompareEditorInput.getSelectedEdition(); 160 } 161 162 165 protected Control createDialogArea(Composite parent2) { 166 167 Composite parent= (Composite) super.createDialogArea(parent2); 168 169 Control c= fCompareEditorInput.createContents(parent); 170 c.setLayoutData(new GridData(GridData.FILL_BOTH)); 171 172 IPreferenceStore store= fCompareEditorInput.getCompareConfiguration().getPreferenceStore(); 173 if (store != null) { 174 if (store.getBoolean(ComparePreferencePage.SHOW_MORE_INFO)) { 175 statusLabel = new Label(parent, SWT.NONE); 176 statusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 177 } 178 } 179 180 Shell shell= c.getShell(); 181 shell.setText(fCompareEditorInput.getTitle()); 182 shell.setImage(fCompareEditorInput.getTitleImage()); 183 applyDialogFont(parent); 184 return parent; 185 } 186 187 190 public int open() { 191 fCompareEditorInput.addPropertyChangeListener(this); 194 fCompareEditorInput.setContainer(fContainer); 195 return super.open(); 196 } 197 198 201 protected void buttonPressed(int buttonId) { 202 if (buttonId == OK) { 203 if (!fCompareEditorInput.okPressed()) 204 return; 205 } else if (buttonId == CANCEL) { 206 fCompareEditorInput.cancelPressed(); 207 } 208 super.buttonPressed(buttonId); 209 } 210 211 214 protected IDialogSettings getDialogBoundsSettings() { 215 IDialogSettings compareSettings = CompareUIPlugin.getDefault().getDialogSettings(); 216 String sectionName = this.getClass().getName(); 217 IDialogSettings dialogSettings = compareSettings.getSection(sectionName); 218 if (dialogSettings == null) { 219 hasSettings = false; 220 dialogSettings = compareSettings.addNewSection(sectionName); 221 } 222 return dialogSettings; 223 } 224 225 228 protected void configureShell(Shell newShell) { 229 super.configureShell(newShell); 230 if (getHelpContextId() != null) 231 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, getHelpContextId()); 232 } 233 234 240 public String getHelpContextId() { 241 return ICompareContextIds.COMPARE_DIALOG; 242 } 243 244 247 protected Point getInitialSize() { 248 Point initialSize = super.getInitialSize(); 249 if (hasSettings) { 250 return initialSize; 251 } 252 return getDefaultSize(); 253 } 254 255 261 protected Point getDefaultSize() { 262 int width= 0; 263 int height= 0; 264 Shell shell= getParentShell(); 265 if (shell != null) { 266 Point parentSize= shell.getSize(); 267 width= parentSize.x-100; 268 height= parentSize.y-100; 269 } 270 if (width < 700) 271 width= 700; 272 if (height < 500) 273 height= 500; 274 return new Point(width, height); 275 } 276 277 281 protected final CompareEditorInput getInput() { 282 return fCompareEditorInput; 283 } 284 285 } 286 | Popular Tags |