Skip to content

Commit

Permalink
add menu "Preview as Read-Only With ABAP Cleaner..." (SAP#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrassau committed Oct 1, 2023
1 parent e3b29ea commit fecc194
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 15 deletions.
45 changes: 45 additions & 0 deletions com.sap.adt.abapcleaner.gui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
id="com.sap.adt.abapcleaner.cleanup.interactive"
name="Clean Up With Interactive ABAP Cleaner...">
</command>
<command
categoryId="com.sap.adt.tools.abapsource.ui.source.category"
description="preview the cleanup result on the ABAP cleaner UI in read-only mode, without locking or changing the code object"
id="com.sap.adt.abapcleaner.cleanup.readonly"
name="Preview as Read-Only With ABAP Cleaner...">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
Expand Down Expand Up @@ -55,6 +61,23 @@
</with>
</visibleWhen>
</command>
<command
commandId="com.sap.adt.abapcleaner.cleanup.readonly"
icon="resources/CommandIcon.png"
style="push">
<visibleWhen
checkEnabled="false">
<with
variable="activePart">
<test
args="com.sap.adt.abapcleaner.cleanup.readonly"
forcePluginActivation="true"
property="com.sap.adt.tools.abapsource.ui.editormenu.shouldShowContextMenu"
value="true">
</test>
</with>
</visibleWhen>
</command>
</menuContribution>
</extension>
<extension
Expand Down Expand Up @@ -91,6 +114,22 @@
</with>
</activeWhen>
</handler>
<handler
class="com.sap.adt.abapcleaner.gui.eclipse.AbapCleanerHandlerReadOnly"
commandId="com.sap.adt.abapcleaner.cleanup.readonly">
<activeWhen>
<with
variable="activeContexts">
<iterate
ifEmpty="false"
operator="or">
<equals
value="com.sap.adt.tools.abapsource.ui.abapCode">
</equals>
</iterate>
</with>
</activeWhen>
</handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
Expand All @@ -106,6 +145,12 @@
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+M2+4">
</key>
<key
commandId="com.sap.adt.abapcleaner.cleanup.readonly"
contextId="com.sap.adt.tools.abapsource.ui.abapCode"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+M2+5">
</key>
</extension>
<extension
id="com.sap.adt.abapcleaner.standalone.app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void main(String[] args) {

if (commandLineArgs == null) {
// start the interactive (stand-alone) UI
cleanInteractively(null, ABAP.NEWEST_RELEASE, null, false, null, null);
cleanInteractively(null, ABAP.NEWEST_RELEASE, null, false, null, null, false);

} else {
if (commandLineArgs.hasErrors()) {
Expand Down Expand Up @@ -303,7 +303,7 @@ public static CleanupResult cleanAutomatically(String sourceCode, String abapRel
}
}

public static CleanupResult cleanInteractively(String sourceCode, String abapRelease, CleanupRange cleanupRange, boolean isPlugin, String sourcePageTitle, CodeDisplayColors codeDisplayColors) {
public static CleanupResult cleanInteractively(String sourceCode, String abapRelease, CleanupRange cleanupRange, boolean isPlugin, String sourcePageTitle, CodeDisplayColors codeDisplayColors, boolean readOnly) {
initialize();

Persistency persistency = Persistency.get();
Expand All @@ -313,7 +313,7 @@ public static CleanupResult cleanInteractively(String sourceCode, String abapRel

FrmMain window = new FrmMain();
window.codeDisplayColors = (codeDisplayColors != null) ? codeDisplayColors : CodeDisplayColors.createDefault();
window.open(isPlugin, sourcePageTitle, sourceCode, abapRelease, cleanupRange);
window.open(isPlugin, sourcePageTitle, sourceCode, abapRelease, cleanupRange, readOnly);

if (window.resultCode != null)
return window.resultCode.toCleanupResult();
Expand All @@ -340,7 +340,7 @@ public FrmMain() {
/**
* @wbp.parser.entryPoint
*/
public void open(boolean isPlugin, String sourcePageTitle, String sourceCode, String abapRelease, CleanupRange cleanupRange) {
public void open(boolean isPlugin, String sourcePageTitle, String sourceCode, String abapRelease, CleanupRange cleanupRange, boolean readOnly) {
this.isPlugin = isPlugin;

Display display = Display.getDefault();
Expand Down Expand Up @@ -370,6 +370,14 @@ public void open(boolean isPlugin, String sourcePageTitle, String sourceCode, St
mmuCodeClearDisplay.dispose();
mmuCodeSeparator.dispose();
mmuCodeExit.dispose();

if (readOnly) {
mmuCodeApplyAndClose.dispose();
mmuCodeCancel.setText(getMenuItemTextWithAccelerator("&Close Read-Only Preview", SWT.ESC));
btnApplyAndClose.dispose();
btnCancel.setText("Close Read-Only Preview");
btnCancel.requestLayout();
}
} else {
mmuCodeCancel.dispose();
mmuCodeApplyAndClose.dispose();
Expand Down Expand Up @@ -1418,22 +1426,36 @@ private boolean refreshCode(String newSourceName, String newSourcePath, String n
}

// show result
shell.setText(Program.PRODUCT_NAME + " - " + sourceName);
String abapReleaseInfo = getReleaseInfo(newAbapRelease);
shell.setText(Program.PRODUCT_NAME + " - " + sourceName + abapReleaseInfo);
codeDisplay.setInfo(sourceName, sourcePath, sourceCode, abapRelease, curProfile.getSingleActiveRule());
codeDisplay.refreshCode(result.getResultingCode(), result.getResultingDiffDoc(), topLineIndex, curLineIndex, selectionStartLine);
if (Program.showDevFeatures())
shell.setText(Program.PRODUCT_NAME + " - " + sourceName + " - " + result.getCalculationTimeInfo());

if (Program.showDevFeatures()) {
shell.setText(Program.PRODUCT_NAME + " - " + sourceName + abapReleaseInfo + " - " + result.getCalculationTimeInfo());
}
// remember the resulting Code instance; the CleanupResult will only be created from it when the window is closed
resultCode = result.getResultingCode();

if (result.getLogSummary() != null) { // even with result.getSuccess() == true, there may be warnings in the log
if (showMessages) // TODO: otherwise, display it on a Label? (for 'Watch and Modify Clipboard' function)
if (showMessages) { // TODO: otherwise, display it on a Label? (for 'Watch and Modify Clipboard' function)
Message.show(result.getLogSummary());
}
}
return true;
}

private String getReleaseInfo(String abapRelease) {
if (StringUtil.isNullOrEmpty(abapRelease))
return "";

// convert "757" into "7.57"
String abapReleaseDisplay = abapRelease;
if (ABAP.consistsOfDigitsOnly(abapRelease) && abapRelease.length() == 3)
abapReleaseDisplay = abapRelease.substring(0, 1) + "." + abapRelease.substring(1);

return " (ABAP " + abapReleaseDisplay + ")";
}

private Task runJobWithProgressUiIfNeeded(BackgroundJob job) {
return runJobWithProgressUiIfNeeded(job, Job.CODE_LENGTH_TO_SHOW_PROGRESS_FORM);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class AbapCleanerHandlerAutomatic extends AbapCleanerHandlerBase {
public AbapCleanerHandlerAutomatic() {
super(false);
super(false, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@
public abstract class AbapCleanerHandlerBase extends AbstractAdtEditorHandler {

private boolean interactive;
private boolean readOnly;

protected AbapCleanerHandlerBase(boolean interactive) {
protected AbapCleanerHandlerBase(boolean interactive, boolean readOnly) {
this.interactive = interactive;
this.readOnly = readOnly;
}

@Override
Expand All @@ -68,7 +70,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
// get the ADT source page and file and check whether editing is allowed
IAbapSourcePage adtSourcePage = getAdtSourcePage(HandlerUtil.getActiveEditor(event));
IFile file = ((IFileEditorInput) adtSourcePage.getEditorInput()).getFile();
if (!adtSourcePage.validateEdit(file)) {
if (!readOnly && !adtSourcePage.validateEdit(file)) {
return null;
}

Expand All @@ -93,15 +95,15 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
if (interactive) {
// get ADT color settings to make code in the ABAP cleaner UI appear similar to code in ADT
CodeDisplayColors codeDisplayColors = createCodeDisplayColors();
result = FrmMain.cleanInteractively(oldSource, abapRelease, cleanupRange, true, adtSourcePage.getTitle(), codeDisplayColors);
result = FrmMain.cleanInteractively(oldSource, abapRelease, cleanupRange, true, adtSourcePage.getTitle(), codeDisplayColors, readOnly);
} else {
result = FrmMain.cleanAutomatically(oldSource, abapRelease, cleanupRange, null, false);
}

if (result == null)
return null;

if (result.hasCleanedCode())
if (result.hasCleanedCode() && !readOnly)
replaceTextInDocument(document, adtSourcePage, oldSource, result.cleanedCode, new CleanupResultWrapper(result));
else if (result.hasErrorMessage())
MessageDialog.openError(adtSourcePage.getSite().getShell(), Program.PRODUCT_NAME, result.errorMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class AbapCleanerHandlerInteractive extends AbapCleanerHandlerBase {
public AbapCleanerHandlerInteractive() {
super(true);
super(true, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sap.adt.abapcleaner.gui.eclipse;

public class AbapCleanerHandlerReadOnly extends AbapCleanerHandlerBase {
public AbapCleanerHandlerReadOnly() {
super(true, true);
}
}

0 comments on commit fecc194

Please sign in to comment.