1 11 package org.eclipse.jdt.internal.ui.fix; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.events.SelectionListener; 25 import org.eclipse.swt.graphics.Point; 26 import org.eclipse.swt.graphics.Rectangle; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.layout.GridLayout; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Label; 33 import org.eclipse.swt.widgets.Shell; 34 import org.eclipse.swt.widgets.TabFolder; 35 import org.eclipse.swt.widgets.TabItem; 36 37 import org.eclipse.jface.dialogs.IDialogConstants; 38 import org.eclipse.jface.dialogs.IDialogSettings; 39 import org.eclipse.jface.dialogs.StatusDialog; 40 41 import org.eclipse.jdt.internal.corext.util.Messages; 42 43 import org.eclipse.jdt.ui.JavaUI; 44 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 47 import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpTabPage; 48 import org.eclipse.jdt.internal.ui.preferences.cleanup.CodeFormatingTabPage; 49 import org.eclipse.jdt.internal.ui.preferences.cleanup.CodeStyleTabPage; 50 import org.eclipse.jdt.internal.ui.preferences.cleanup.MemberAccessesTabPage; 51 import org.eclipse.jdt.internal.ui.preferences.cleanup.MissingCodeTabPage; 52 import org.eclipse.jdt.internal.ui.preferences.cleanup.UnnecessaryCodeTabPage; 53 import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage; 54 import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.IModificationListener; 55 56 public class CleanUpSaveParticipantConfigurationModifyDialog extends StatusDialog implements IModificationListener { 57 58 private static final String DS_KEY_PREFERRED_WIDTH= "clean_up_save_particpant_modify_dialog.preferred_width"; private static final String DS_KEY_PREFERRED_HEIGHT= "clean_up_save_particpant_modify_dialog.preferred_height"; private static final String DS_KEY_PREFERRED_X= "clean_up_save_particpant_modify_dialog.preferred_x"; private static final String DS_KEY_PREFERRED_Y= "clean_up_save_particpant_modify_dialog.preferred_y"; private static final String DS_KEY_LAST_FOCUS= "clean_up_save_particpant_modify_dialog.last_focus"; 64 private static final int APPLY_BUTTON_ID= IDialogConstants.CLIENT_ID; 65 66 private final Map fWorkingValues; 67 private Map fOrginalValues; 68 private final List fTabPages; 69 private final IDialogSettings fDialogSettings; 70 private TabFolder fTabFolder; 71 private Button fApplyButton; 72 private CleanUpTabPage[] fPages; 73 private Label fCountLabel; 74 75 public CleanUpSaveParticipantConfigurationModifyDialog(Shell parentShell, Map settings, String title) { 76 super(parentShell); 77 78 setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX); 79 80 setTitle(title); 81 fWorkingValues= settings; 82 fOrginalValues= new HashMap (settings); 83 setStatusLineAboveButtons(false); 84 fTabPages= new ArrayList (); 85 fDialogSettings= JavaPlugin.getDefault().getDialogSettings(); 86 } 87 88 public void create() { 89 super.create(); 90 int lastFocusNr= 0; 91 try { 92 lastFocusNr= fDialogSettings.getInt(DS_KEY_LAST_FOCUS); 93 if (lastFocusNr < 0) 94 lastFocusNr= 0; 95 if (lastFocusNr > fTabPages.size() - 1) 96 lastFocusNr= fTabPages.size() - 1; 97 } catch (NumberFormatException x) { 98 lastFocusNr= 0; 99 } 100 101 fTabFolder.setSelection(lastFocusNr); 102 ((ModifyDialogTabPage)fTabFolder.getSelection()[0].getData()).setInitialFocus(); 103 } 104 105 protected Control createDialogArea(Composite parent) { 106 final Composite composite= (Composite)super.createDialogArea(parent); 107 108 fTabFolder= new TabFolder(composite, SWT.NONE); 109 fTabFolder.setFont(composite.getFont()); 110 fTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 111 112 fPages= createTabPages(fWorkingValues); 113 114 fCountLabel= new Label(composite, SWT.NONE); 115 fCountLabel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); 116 updateCountLabel(); 117 118 applyDialogFont(composite); 119 120 fTabFolder.addSelectionListener(new SelectionListener() { 121 public void widgetDefaultSelected(SelectionEvent e) {} 122 123 public void widgetSelected(SelectionEvent e) { 124 final TabItem tabItem= (TabItem)e.item; 125 final ModifyDialogTabPage page= (ModifyDialogTabPage)tabItem.getData(); 126 fDialogSettings.put(DS_KEY_LAST_FOCUS, fTabPages.indexOf(page)); 127 page.makeVisible(); 128 } 129 }); 130 131 updateStatus(StatusInfo.OK_STATUS); 132 133 return composite; 134 } 135 136 protected CleanUpTabPage[] createTabPages(Map workingValues) { 137 CleanUpTabPage[] result= new CleanUpTabPage[5]; 138 result[0]= new CodeStyleTabPage(this, workingValues, true); 139 result[1]= new MemberAccessesTabPage(this, workingValues, true); 140 result[2]= new UnnecessaryCodeTabPage(this, workingValues, true); 141 result[3]= new MissingCodeTabPage(this, workingValues, true); 142 result[4]= new CodeFormatingTabPage(this, workingValues, true); 143 144 addTabPage(SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_CodeStyle_TabPage, result[0]); 145 addTabPage(SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_MemberAccesses_TabPage, result[1]); 146 addTabPage(SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_UnnecessaryCode_TabPage, result[2]); 147 addTabPage(SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_MissingCode_TabPage, result[3]); 148 addTabPage(SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_CodeOrganizing_TabPage, result[4]); 149 150 return result; 151 } 152 153 public void updateStatus(IStatus status) { 154 int count= 0; 155 for (int i= 0; i < fPages.length; i++) { 156 count+= fPages[i].getSelectedCleanUpCount(); 157 } 158 if (count == 0) { 159 super.updateStatus(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_SelectAnAction_Error)); 160 } else { 161 if (status == null) { 162 super.updateStatus(StatusInfo.OK_STATUS); 163 } else { 164 super.updateStatus(status); 165 } 166 } 167 } 168 169 174 protected Point getInitialSize() { 175 Point initialSize= super.getInitialSize(); 176 try { 177 int lastWidth= fDialogSettings.getInt(DS_KEY_PREFERRED_WIDTH); 178 if (initialSize.x > lastWidth) 179 lastWidth= initialSize.x; 180 int lastHeight= fDialogSettings.getInt(DS_KEY_PREFERRED_HEIGHT); 181 if (initialSize.y > lastHeight) 182 lastHeight= initialSize.y; 183 return new Point(lastWidth, lastHeight); 184 } catch (NumberFormatException ex) { 185 } 186 return initialSize; 187 } 188 189 194 protected Point getInitialLocation(Point initialSize) { 195 try { 196 return new Point(fDialogSettings.getInt(DS_KEY_PREFERRED_X), fDialogSettings.getInt(DS_KEY_PREFERRED_Y)); 197 } catch (NumberFormatException ex) { 198 return super.getInitialLocation(initialSize); 199 } 200 } 201 202 public boolean close() { 203 final Rectangle shell= getShell().getBounds(); 204 205 fDialogSettings.put(DS_KEY_PREFERRED_WIDTH, shell.width); 206 fDialogSettings.put(DS_KEY_PREFERRED_HEIGHT, shell.height); 207 fDialogSettings.put(DS_KEY_PREFERRED_X, shell.x); 208 fDialogSettings.put(DS_KEY_PREFERRED_Y, shell.y); 209 210 return super.close(); 211 } 212 213 218 protected void okPressed() { 219 applyPressed(); 220 super.okPressed(); 221 } 222 223 protected void buttonPressed(int buttonId) { 224 if (buttonId == APPLY_BUTTON_ID) { 225 applyPressed(); 226 } else { 227 super.buttonPressed(buttonId); 228 } 229 } 230 231 private void applyPressed() { 232 fOrginalValues= new HashMap (fWorkingValues); 233 updateStatus(StatusInfo.OK_STATUS); 234 } 235 236 protected void createButtonsForButtonBar(Composite parent) { 237 fApplyButton= createButton(parent, APPLY_BUTTON_ID, SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_Apply_Button, false); 238 fApplyButton.setEnabled(false); 239 240 GridLayout layout= (GridLayout)parent.getLayout(); 241 layout.numColumns++; 242 layout.makeColumnsEqualWidth= false; 243 Label label= new Label(parent, SWT.NONE); 244 GridData data= new GridData(); 245 data.widthHint= layout.horizontalSpacing; 246 label.setLayoutData(data); 247 super.createButtonsForButtonBar(parent); 248 } 249 250 protected final void addTabPage(String title, ModifyDialogTabPage tabPage) { 251 final TabItem tabItem= new TabItem(fTabFolder, SWT.NONE); 252 applyDialogFont(tabItem.getControl()); 253 tabItem.setText(title); 254 tabItem.setData(tabPage); 255 tabItem.setControl(tabPage.createContents(fTabFolder)); 256 fTabPages.add(tabPage); 257 } 258 259 protected void updateButtonsEnableState(IStatus status) { 260 super.updateButtonsEnableState(status); 261 if (fApplyButton != null && !fApplyButton.isDisposed()) { 262 fApplyButton.setEnabled(hasChanges() && !status.matches(IStatus.ERROR)); 263 } 264 } 265 266 private boolean hasChanges() { 267 for (Iterator iterator= fWorkingValues.keySet().iterator(); iterator.hasNext();) { 268 String key= (String )iterator.next(); 269 if (!fWorkingValues.get(key).equals(fOrginalValues.get(key))) 270 return true; 271 } 272 return false; 273 } 274 275 278 public void valuesModified() { 279 updateCountLabel(); 280 updateStatus(StatusInfo.OK_STATUS); 281 } 282 283 private void updateCountLabel() { 284 int size= 0, count= 0; 285 for (int i= 0; i < fPages.length; i++) { 286 size+= fPages[i].getCleanUpCount(); 287 count+= fPages[i].getSelectedCleanUpCount(); 288 } 289 290 fCountLabel.setText(Messages.format(SaveParticipantMessages.CleanUpSaveParticipantConfigurationModifyDialog_XofYSelected_Label, new Object [] {new Integer (count), new Integer (size)})); 291 } 292 } 293 | Popular Tags |