| Conditions | 15 |
| Total Lines | 205 |
| Code Lines | 137 |
| Lines | 24 |
| Ratio | 11.71 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like br.ufrj.ppgi.greco.kettle.GraphSemanticLevelMarkerStepDialog.open() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package br.ufrj.ppgi.greco.kettle; |
||
| 113 | public String open() { |
||
| 114 | |||
| 115 | Shell parent = getParent(); |
||
| 116 | Display display = parent.getDisplay(); |
||
| 117 | |||
| 118 | shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MIN | SWT.MAX); |
||
| 119 | props.setLook(shell); |
||
| 120 | setShellImage(shell, input); |
||
| 121 | |||
| 122 | // ModifyListener padrao |
||
| 123 | ModifyListener lsMod = new ModifyListener() { |
||
| 124 | public void modifyText(ModifyEvent e) { |
||
| 125 | input.setChanged(); |
||
| 126 | } |
||
| 127 | }; |
||
| 128 | boolean changed = input.hasChanged(); |
||
| 129 | |||
| 130 | FormLayout formLayout = new FormLayout(); |
||
| 131 | formLayout.marginWidth = Const.FORM_MARGIN; |
||
| 132 | formLayout.marginHeight = Const.FORM_MARGIN; |
||
| 133 | |||
| 134 | shell.setLayout(formLayout); |
||
| 135 | |||
| 136 | shell.setText(dialogTitle); |
||
| 137 | |||
| 138 | int middle = props.getMiddlePct(); |
||
| 139 | int margin = Const.MARGIN; |
||
| 140 | |||
| 141 | // Adiciona um label e um input text no topo do dialog shell |
||
| 142 | wlStepname = new Label(shell, SWT.RIGHT); |
||
| 143 | wlStepname.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.StepNameField.Label")); |
||
| 144 | props.setLook(wlStepname); |
||
| 145 | |||
| 146 | fdlStepname = new FormData(); |
||
| 147 | fdlStepname.left = new FormAttachment(0, 0); |
||
| 148 | fdlStepname.right = new FormAttachment(middle, -margin); |
||
| 149 | fdlStepname.top = new FormAttachment(0, margin); |
||
| 150 | wlStepname.setLayoutData(fdlStepname); |
||
| 151 | |||
| 152 | wStepname = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
||
| 153 | wStepname.setText(stepname); |
||
| 154 | props.setLook(wStepname); |
||
| 155 | |||
| 156 | wStepname.addModifyListener(lsMod); |
||
| 157 | fdStepname = new FormData(); |
||
| 158 | fdStepname.left = new FormAttachment(middle, 0); |
||
| 159 | fdStepname.top = new FormAttachment(0, margin); |
||
| 160 | fdStepname.right = new FormAttachment(100, 0); |
||
| 161 | wStepname.setLayoutData(fdStepname); |
||
| 162 | Control lastControl = wStepname; |
||
| 163 | |||
| 164 | // Chama metodo que adiciona os widgets especificos da janela |
||
| 165 | lastControl = buildContents(lastControl, lsMod); |
||
| 166 | |||
| 167 | // Bot�es para busca de arquivo |
||
| 168 | wlBrowse = new Label(shell, SWT.RIGHT); |
||
| 169 | wlBrowse.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.LOV.File")); |
||
| 170 | props.setLook(wlBrowse); |
||
| 171 | fdlBrowse = new FormData(); |
||
| 172 | fdlBrowse.left = new FormAttachment(0, 0); |
||
| 173 | fdlBrowse.top = new FormAttachment(wOutputGroup, margin); |
||
| 174 | fdlBrowse.right = new FormAttachment(middle, -margin); |
||
| 175 | wlBrowse.setLayoutData(fdlBrowse); |
||
| 176 | |||
| 177 | wbBrowse = new Button(shell, SWT.PUSH | SWT.CENTER); |
||
| 178 | props.setLook(wbBrowse); |
||
| 179 | wbBrowse.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.Btn.Browse")); |
||
| 180 | fdbBrowse = new FormData(); |
||
| 181 | fdbBrowse.right = new FormAttachment(100, 0); |
||
| 182 | fdbBrowse.top = new FormAttachment(wOutputGroup, margin); |
||
| 183 | wbBrowse.setLayoutData(fdbBrowse); |
||
| 184 | |||
| 185 | wBrowse = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
||
| 186 | props.setLook(wBrowse); |
||
| 187 | wBrowse.addModifyListener(lsMod); |
||
| 188 | fdBrowse = new FormData(); |
||
| 189 | fdBrowse.left = new FormAttachment(middle, 0); |
||
| 190 | fdBrowse.right = new FormAttachment(wbBrowse, -margin); |
||
| 191 | fdBrowse.top = new FormAttachment(wOutputGroup, margin); |
||
| 192 | wBrowse.setLayoutData(fdBrowse); |
||
| 193 | |||
| 194 | wlRules = new Label(shell, SWT.RIGHT); |
||
| 195 | wlRules.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.SemanticFramework.File")); |
||
| 196 | props.setLook(wlRules); |
||
| 197 | fdlRules = new FormData(); |
||
| 198 | fdlRules.left = new FormAttachment(0, 0); |
||
| 199 | fdlRules.top = new FormAttachment(wBrowse, margin); |
||
| 200 | fdlRules.right = new FormAttachment(middle, -margin); |
||
| 201 | wlRules.setLayoutData(fdlRules); |
||
| 202 | |||
| 203 | wbRules = new Button(shell, SWT.PUSH | SWT.CENTER); |
||
| 204 | props.setLook(wbRules); |
||
| 205 | wbRules.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.Btn.Browse")); |
||
| 206 | fdbRules = new FormData(); |
||
| 207 | fdbRules.right = new FormAttachment(100, 0); |
||
| 208 | fdbRules.top = new FormAttachment(wBrowse, margin); |
||
| 209 | wbRules.setLayoutData(fdbRules); |
||
| 210 | |||
| 211 | wRules = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); |
||
| 212 | props.setLook(wRules); |
||
| 213 | wRules.addModifyListener(lsMod); |
||
| 214 | fdRules = new FormData(); |
||
| 215 | fdRules.left = new FormAttachment(middle, 0); |
||
| 216 | fdRules.right = new FormAttachment(wbRules, -margin); |
||
| 217 | fdRules.top = new FormAttachment(wBrowse, margin); |
||
| 218 | wRules.setLayoutData(fdRules); |
||
| 219 | |||
| 220 | // Bottom buttons |
||
| 221 | wOK = new Button(shell, SWT.PUSH); |
||
| 222 | wOK.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.Btn.OK")); //$NON-NLS-1$ |
||
| 223 | wCancel = new Button(shell, SWT.PUSH); |
||
| 224 | wCancel.setText(BaseMessages.getString(PKG, "GraphSemanticLevelMarkerStep.Btn.Cancel")); //$NON-NLS-1$ |
||
| 225 | setButtonPositions(new Button[] { wOK, wCancel }, margin, wbRules); |
||
| 226 | |||
| 227 | // Add listeners |
||
| 228 | lsCancel = new Listener() { |
||
| 229 | public void handleEvent(Event e) { |
||
| 230 | cancel(); |
||
| 231 | } |
||
| 232 | }; |
||
| 233 | lsOK = new Listener() { |
||
| 234 | public void handleEvent(Event e) { |
||
| 235 | ok(); |
||
| 236 | } |
||
| 237 | }; |
||
| 238 | |||
| 239 | wBrowse.addModifyListener(new ModifyListener() { |
||
| 240 | public void modifyText(ModifyEvent arg0) { |
||
| 241 | wBrowse.setToolTipText(transMeta.environmentSubstitute(wBrowse.getText())); |
||
| 242 | } |
||
| 243 | }); |
||
| 244 | |||
| 245 | wbBrowse.addSelectionListener(new SelectionAdapter() { |
||
| 246 | View Code Duplication | public void widgetSelected(SelectionEvent e) { |
|
|
|
|||
| 247 | FileDialog dialog = new FileDialog(shell, SWT.OPEN); |
||
| 248 | dialog.setFilterExtensions(new String[] { "*.xml;*.XML", "*" }); |
||
| 249 | if (wBrowse.getText() != null) { |
||
| 250 | dialog.setFileName(wBrowse.getText()); |
||
| 251 | } |
||
| 252 | |||
| 253 | dialog.setFilterNames(new String[] { "XML files", "All files" }); |
||
| 254 | |||
| 255 | if (dialog.open() != null) { |
||
| 256 | String str = dialog.getFilterPath() + System.getProperty("file.separator") + dialog.getFileName(); |
||
| 257 | wBrowse.setText(str); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | }); |
||
| 261 | |||
| 262 | wbRules.addSelectionListener(new SelectionAdapter() { |
||
| 263 | View Code Duplication | public void widgetSelected(SelectionEvent e) { |
|
| 264 | FileDialog dialog = new FileDialog(shell, SWT.OPEN); |
||
| 265 | dialog.setFilterExtensions(new String[] { "*.xml;*.XML", "*" }); |
||
| 266 | if (wRules.getText() != null) { |
||
| 267 | dialog.setFileName(wRules.getText()); |
||
| 268 | } |
||
| 269 | |||
| 270 | dialog.setFilterNames(new String[] { "XML files", "All files" }); |
||
| 271 | |||
| 272 | if (dialog.open() != null) { |
||
| 273 | String str = dialog.getFilterPath() + System.getProperty("file.separator") + dialog.getFileName(); |
||
| 274 | wRules.setText(str); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | }); |
||
| 278 | |||
| 279 | wCancel.addListener(SWT.Selection, lsCancel); |
||
| 280 | wOK.addListener(SWT.Selection, lsOK); |
||
| 281 | |||
| 282 | // It closes the window affirmatively when the user press enter in one |
||
| 283 | // of the text input fields |
||
| 284 | lsDef = new SelectionAdapter() { |
||
| 285 | public void widgetDefaultSelected(SelectionEvent e) { |
||
| 286 | ok(); |
||
| 287 | } |
||
| 288 | }; |
||
| 289 | wStepname.addSelectionListener(lsDef); |
||
| 290 | addSelectionListenerToControls(lsDef); |
||
| 291 | |||
| 292 | // Detect X or ALT-F4 or something that kills this window... |
||
| 293 | shell.addShellListener(new ShellAdapter() { |
||
| 294 | public void shellClosed(ShellEvent e) { |
||
| 295 | cancel(); |
||
| 296 | } |
||
| 297 | }); |
||
| 298 | |||
| 299 | // Populate the data of the controls |
||
| 300 | getData(); |
||
| 301 | |||
| 302 | // Set the shell size, based upon previous time... |
||
| 303 | setSize(); |
||
| 304 | |||
| 305 | // Alarga um pouco mais a janela |
||
| 306 | Rectangle shellBounds = shell.getBounds(); |
||
| 307 | shellBounds.width += 5; |
||
| 308 | shell.setBounds(shellBounds); |
||
| 309 | |||
| 310 | input.setChanged(changed); |
||
| 311 | |||
| 312 | shell.open(); |
||
| 313 | while (!shell.isDisposed()) { |
||
| 314 | if (!display.readAndDispatch()) |
||
| 315 | display.sleep(); |
||
| 316 | } |
||
| 317 | return stepname; |
||
| 318 | } |
||
| 364 |