mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
diff: allow to redefine DialogWrapper's actions in diff window
This commit is contained in:
@@ -16,9 +16,11 @@
|
||||
package com.intellij.diff;
|
||||
|
||||
import com.intellij.openapi.ui.WindowWrapper;
|
||||
import com.intellij.openapi.util.BooleanGetter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class DiffDialogHints {
|
||||
@@ -32,7 +34,9 @@ public class DiffDialogHints {
|
||||
//
|
||||
|
||||
@Nullable private final WindowWrapper.Mode myMode;
|
||||
@Nullable private final Component myParent;
|
||||
@Nullable private Component myParent;
|
||||
@Nullable private BooleanGetter myOkAction;
|
||||
@Nullable private BooleanGetter myCancelAction;
|
||||
|
||||
public DiffDialogHints(@Nullable WindowWrapper.Mode mode) {
|
||||
this(mode, null);
|
||||
@@ -43,6 +47,18 @@ public class DiffDialogHints {
|
||||
myParent = parent;
|
||||
}
|
||||
|
||||
public void setParent(@Nullable Component value) {
|
||||
myParent = value;
|
||||
}
|
||||
|
||||
public void setOkAction(@Nullable BooleanGetter value) {
|
||||
myOkAction = value;
|
||||
}
|
||||
|
||||
public void setCancelAction(@Nullable BooleanGetter value) {
|
||||
myCancelAction = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Getters
|
||||
//
|
||||
@@ -56,4 +72,14 @@ public class DiffDialogHints {
|
||||
public Component getParent() {
|
||||
return myParent;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BooleanGetter getOkAction() {
|
||||
return myOkAction;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BooleanGetter getCancelAction() {
|
||||
return myCancelAction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,8 @@ public abstract class DiffWindowBase {
|
||||
myProcessor.requestFocus(); // TODO: not needed for modal dialogs. Make a flag in WindowWrapperBuilder ?
|
||||
}
|
||||
})
|
||||
.setOkAction(myHints.getOkAction())
|
||||
.setCancelAction(myHints.getCancelAction())
|
||||
.build();
|
||||
myWrapper.setImage(ImageLoader.loadFromResource("/diff/Diff.png"));
|
||||
Disposer.register(myWrapper, myProcessor);
|
||||
|
||||
@@ -821,6 +821,10 @@ public class DiffUtil {
|
||||
boolean isUnderDialog = LaterInvocator.isInModalContext();
|
||||
mode = isUnderDialog ? WindowWrapper.Mode.MODAL : WindowWrapper.Mode.FRAME;
|
||||
}
|
||||
if (mode == WindowWrapper.Mode.FRAME &&
|
||||
(hints.getOkAction() != null || hints.getCancelAction() != null)) {
|
||||
mode = WindowWrapper.Mode.MODAL;
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.intellij.openapi.ui;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.WindowWrapper.Mode;
|
||||
import com.intellij.openapi.util.BooleanGetter;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -22,6 +23,8 @@ public class WindowWrapperBuilder {
|
||||
@Nullable private JComponent myPreferredFocusedComponent;
|
||||
@Nullable private String myDimensionServiceKey;
|
||||
@Nullable private Runnable myOnShowCallback;
|
||||
@Nullable private BooleanGetter myOkAction;
|
||||
@Nullable private BooleanGetter myCancelAction;
|
||||
|
||||
public WindowWrapperBuilder(@NotNull Mode mode, @NotNull JComponent component) {
|
||||
myMode = mode;
|
||||
@@ -64,6 +67,18 @@ public class WindowWrapperBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public WindowWrapperBuilder setOkAction(@Nullable BooleanGetter value) {
|
||||
myOkAction = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public WindowWrapperBuilder setCancelAction(@Nullable BooleanGetter value) {
|
||||
myCancelAction = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public WindowWrapper build() {
|
||||
switch (myMode) {
|
||||
@@ -92,7 +107,10 @@ public class WindowWrapperBuilder {
|
||||
myDialog = builder.myParent != null
|
||||
? new MyDialogWrapper(builder.myParent, builder.myComponent)
|
||||
: new MyDialogWrapper(builder.myProject, builder.myComponent);
|
||||
myDialog.setParameters(builder.myDimensionServiceKey, builder.myPreferredFocusedComponent);
|
||||
myDialog.setParameters(builder.myDimensionServiceKey,
|
||||
builder.myPreferredFocusedComponent,
|
||||
builder.myOkAction,
|
||||
builder.myCancelAction);
|
||||
|
||||
final Runnable onShowCallback = builder.myOnShowCallback;
|
||||
if (onShowCallback != null) {
|
||||
@@ -164,13 +182,15 @@ public class WindowWrapperBuilder {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
myDialog.close(DialogWrapper.CANCEL_EXIT_CODE);
|
||||
myDialog.getCancelAction().actionPerformed(null);
|
||||
}
|
||||
|
||||
private static class MyDialogWrapper extends DialogWrapper {
|
||||
@NotNull private JComponent myComponent;
|
||||
@Nullable private String myDimensionServiceKey;
|
||||
@Nullable private JComponent myPreferredFocusedComponent;
|
||||
@Nullable private BooleanGetter myOkAction;
|
||||
@Nullable private BooleanGetter myCancelAction;
|
||||
|
||||
public MyDialogWrapper(@Nullable Project project, @NotNull JComponent component) {
|
||||
super(project, true);
|
||||
@@ -183,15 +203,20 @@ public class WindowWrapperBuilder {
|
||||
}
|
||||
|
||||
public void setParameters(@Nullable String dimensionServiceKey,
|
||||
@Nullable JComponent preferredFocusedComponent) {
|
||||
@Nullable JComponent preferredFocusedComponent,
|
||||
@Nullable BooleanGetter okAction,
|
||||
@Nullable BooleanGetter cancelAction) {
|
||||
myDimensionServiceKey = dimensionServiceKey;
|
||||
myPreferredFocusedComponent = preferredFocusedComponent;
|
||||
myOkAction = okAction;
|
||||
myCancelAction = cancelAction;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected Border createContentPaneBorder() {
|
||||
return null;
|
||||
if (myOkAction == null && myCancelAction == null) return null;
|
||||
return super.createContentPaneBorder();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -203,13 +228,25 @@ public class WindowWrapperBuilder {
|
||||
@NotNull
|
||||
@Override
|
||||
protected Action[] createActions() {
|
||||
return new Action[0];
|
||||
if (myOkAction == null && myCancelAction == null) return new Action[0];
|
||||
return super.createActions();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doOKAction() {
|
||||
if (myOkAction == null || myOkAction.get()) super.doOKAction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doCancelAction() {
|
||||
if (myCancelAction == null || myCancelAction.get()) super.doCancelAction();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected JComponent createSouthPanel() {
|
||||
return null;
|
||||
if (myOkAction == null && myCancelAction == null) return null;
|
||||
return super.createSouthPanel();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -236,6 +273,7 @@ public class WindowWrapperBuilder {
|
||||
|
||||
public FrameWindowWrapper(@NotNull WindowWrapperBuilder builder) {
|
||||
assert builder.myMode == Mode.FRAME;
|
||||
if (builder.myOkAction != null || builder.myCancelAction != null) throw new IllegalArgumentException();
|
||||
|
||||
myProject = builder.myProject;
|
||||
myComponent = builder.myComponent;
|
||||
|
||||
Reference in New Issue
Block a user