Extract method openEditor(Entity item) for CreateAction and EditAction #PL-5993

This commit is contained in:
Yuriy Artamonov 2015-10-02 10:37:18 +00:00
parent ff337dcfe7
commit 5622527fa6
2 changed files with 50 additions and 43 deletions

View File

@ -19,7 +19,7 @@ import com.haulmont.cuba.security.entity.EntityAttrAccess;
import com.haulmont.cuba.security.entity.EntityOp; import com.haulmont.cuba.security.entity.EntityOp;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.Collections;
import java.util.Map; import java.util.Map;
/** /**
@ -194,33 +194,37 @@ public class CreateAction extends BaseAction implements Action.HasOpenType {
parentDs = datasource; parentDs = datasource;
} }
} }
final Datasource pDs = parentDs;
Map<String, Object> params = getWindowParams(); Map<String, Object> params = getWindowParams();
if (params == null) if (params == null) {
params = new HashMap<>(); params = Collections.emptyMap();
}
Window.Editor window = target.getFrame().openEditor(getWindowId(), item, getOpenType(), params, parentDs); openEditor(datasource, item, parentDs, params);
}
protected void openEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
Window.Editor window = target.getFrame().openEditor(getWindowId(), newItem, getOpenType(), params, parentDs);
window.addCloseListener(actionId -> { window.addCloseListener(actionId -> {
// move focus to owner
target.requestFocus();
if (Window.COMMIT_ACTION_ID.equals(actionId)) { if (Window.COMMIT_ACTION_ID.equals(actionId)) {
Entity item1 = window.getItem(); Entity editedItem = window.getItem();
if (item1 != null) { if (editedItem != null) {
if (pDs == null) { if (parentDs == null) {
boolean modified = datasource.isModified(); boolean modified = datasource.isModified();
datasource.addItem(item1); datasource.addItem(editedItem);
((DatasourceImplementation) datasource).setModified(modified); ((DatasourceImplementation) datasource).setModified(modified);
} }
target.setSelected(item1); target.setSelected(editedItem);
afterCommit(item1); afterCommit(editedItem);
if (afterCommitHandler != null) { if (afterCommitHandler != null) {
afterCommitHandler.handle(item1); afterCommitHandler.handle(editedItem);
} }
} }
} }
// move focus to owner
target.requestFocus();
afterWindowClosed(window); afterWindowClosed(window);
if (afterWindowClosedHandler != null) { if (afterWindowClosedHandler != null) {
afterWindowClosedHandler.handle(window); afterWindowClosedHandler.handle(window);

View File

@ -24,7 +24,7 @@ import com.haulmont.cuba.gui.data.PropertyDatasource;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager; import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.security.entity.EntityOp; import com.haulmont.cuba.security.entity.EntityOp;
import java.util.HashMap; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -158,8 +158,6 @@ public class EditAction extends BaseAction implements Action.HasOpenType {
public void actionPerform(Component component) { public void actionPerform(Component component) {
final Set selected = target.getSelected(); final Set selected = target.getSelected();
if (selected.size() == 1) { if (selected.size() == 1) {
String windowID = getWindowId();
Datasource parentDs = null; Datasource parentDs = null;
final CollectionDatasource datasource = target.getDatasource(); final CollectionDatasource datasource = target.getDatasource();
if (datasource instanceof PropertyDatasource) { if (datasource instanceof PropertyDatasource) {
@ -168,37 +166,42 @@ public class EditAction extends BaseAction implements Action.HasOpenType {
parentDs = datasource; parentDs = datasource;
} }
} }
final Datasource pDs = parentDs;
Map<String, Object> params = getWindowParams(); Map<String, Object> params = getWindowParams();
if (params == null) if (params == null) {
params = new HashMap<>(); params = Collections.emptyMap();
}
Window.Editor window = target.getFrame().openEditor(windowID, datasource.getItem(), getOpenType(), params, parentDs); openEditor(parentDs, datasource, params);
window.addCloseListener(actionId -> { }
if (Window.COMMIT_ACTION_ID.equals(actionId)) { }
Entity item = window.getItem();
if (item != null) { protected void openEditor(Datasource parentDs, CollectionDatasource datasource, Map<String, Object> params) {
if (pDs == null) { Window.Editor window = target.getFrame().openEditor(getWindowId(), datasource.getItem(),
//noinspection unchecked getOpenType(), params, parentDs);
datasource.updateItem(item); window.addCloseListener(actionId -> {
} // move focus to owner
afterCommit(item); target.requestFocus();
if (afterCommitHandler != null) {
afterCommitHandler.handle(item); if (Window.COMMIT_ACTION_ID.equals(actionId)) {
} Entity editedItem = window.getItem();
if (editedItem != null) {
if (parentDs == null) {
//noinspection unchecked
datasource.updateItem(editedItem);
}
afterCommit(editedItem);
if (afterCommitHandler != null) {
afterCommitHandler.handle(editedItem);
} }
} }
}
// move focus to owner afterWindowClosed(window);
target.requestFocus(); if (afterWindowClosedHandler != null) {
afterWindowClosedHandler.handle(window);
afterWindowClosed(window); }
if (afterWindowClosedHandler != null) { });
afterWindowClosedHandler.handle(window);
}
});
}
} }
/** /**