|
15 | 15 | */ |
16 | 16 | package org.exbin.bined.intellij.database; |
17 | 17 |
|
18 | | -//import com.intellij.database.datagrid.DataGrid; |
19 | | -//import com.intellij.database.datagrid.DataGridUtil; |
20 | | -//import com.intellij.database.run.actions.GridAction; |
| 18 | +import com.intellij.database.datagrid.DataGrid; |
| 19 | +import com.intellij.database.datagrid.DataGridUtil; |
| 20 | +import com.intellij.database.datagrid.GridColumn; |
| 21 | +import com.intellij.database.datagrid.GridModel; |
| 22 | +import com.intellij.database.datagrid.GridRow; |
| 23 | +import com.intellij.database.datagrid.SelectionModel; |
| 24 | +import com.intellij.database.extractors.TextInfo; |
| 25 | +import com.intellij.database.run.actions.GridAction; |
| 26 | +import com.intellij.database.run.ui.DataAccessType; |
| 27 | +import com.intellij.database.run.ui.grid.editors.CoreGridCellEditorHelper; |
21 | 28 | import com.intellij.openapi.actionSystem.AnAction; |
22 | 29 | import com.intellij.openapi.actionSystem.AnActionEvent; |
| 30 | +import com.intellij.openapi.actionSystem.Presentation; |
| 31 | +import com.intellij.openapi.application.ApplicationManager; |
23 | 32 | import com.intellij.openapi.project.DumbAware; |
24 | | -import org.jetbrains.annotations.NotNull; |
| 33 | +import com.intellij.openapi.project.Project; |
| 34 | +import com.intellij.openapi.ui.DialogWrapper; |
| 35 | +import com.intellij.util.ui.JBUI; |
| 36 | +import com.intellij.util.ui.components.BorderLayoutPanel; |
| 37 | +import org.exbin.auxiliary.paged_data.BinaryData; |
| 38 | +import org.exbin.auxiliary.paged_data.ByteArrayEditableData; |
| 39 | +import org.exbin.auxiliary.paged_data.EditableBinaryData; |
| 40 | +import org.exbin.bined.EditMode; |
| 41 | +import org.exbin.bined.intellij.BinEdPluginStartupActivity; |
| 42 | +import org.exbin.bined.intellij.data.ObjectValueConvertor; |
| 43 | +import org.exbin.bined.intellij.gui.BinEdComponentFileApi; |
| 44 | +import org.exbin.bined.intellij.gui.BinEdComponentPanel; |
| 45 | +import org.exbin.framework.bined.FileHandlingMode; |
25 | 46 |
|
| 47 | +import javax.annotation.Nonnull; |
| 48 | +import javax.annotation.Nullable; |
26 | 49 | import javax.annotation.ParametersAreNonnullByDefault; |
| 50 | +import javax.swing.Action; |
| 51 | +import javax.swing.JComponent; |
| 52 | +import java.util.Objects; |
| 53 | +import java.util.logging.Level; |
| 54 | +import java.util.logging.Logger; |
27 | 55 |
|
28 | 56 | /** |
29 | | - * Edit cell value as binary data action. |
| 57 | + * Edit database cell value as binary data action. |
30 | 58 | * |
31 | 59 | * @author ExBin Project (https://exbin.org) |
32 | 60 | */ |
33 | 61 | @ParametersAreNonnullByDefault |
34 | | -public class DbEditBinaryAction extends AnAction implements DumbAware { // , GridAction |
| 62 | +public class DbEditBinaryAction extends AnAction implements DumbAware, GridAction { |
35 | 63 |
|
36 | | - public void actionPerformed(AnActionEvent e) { |
37 | | -// DataGrid grid = DataGridUtil.getDataGrid(e.getDataContext()); |
38 | | -// if (grid != null) { |
39 | | -// grid.cancelEditing(); |
40 | | -// grid.setCells(grid.getSelectionModel().getSelectedRows(), grid.getSelectionModel().getSelectedColumns(), this.myValue); |
41 | | -// } |
| 64 | + private boolean actionVisible = true; |
| 65 | + private ObjectValueConvertor objectValueConvertor = new ObjectValueConvertor(); |
42 | 66 |
|
| 67 | + public DbEditBinaryAction() { |
| 68 | + BinEdPluginStartupActivity.addIntegrationOptionsListener(integrationOptions -> actionVisible = |
| 69 | + integrationOptions.isRegisterEditAsBinaryForDbColumn()); |
| 70 | + } |
| 71 | + |
| 72 | + public void actionPerformed(AnActionEvent event) { |
| 73 | + DataGrid grid = DataGridUtil.getDataGrid(event.getDataContext()); |
| 74 | + if (grid != null) { |
| 75 | + SelectionModel<GridRow, GridColumn> selectionModel = grid.getSelectionModel(); |
| 76 | + GridModel<GridRow, GridColumn> dataModel = grid.getDataModel(DataAccessType.DATA_WITH_MUTATIONS); |
| 77 | + GridColumn column = Objects.requireNonNull(dataModel.getColumn(selectionModel.getSelectedColumn())); |
| 78 | + GridRow row = Objects.requireNonNull(dataModel.getRow(selectionModel.getSelectedRow())); |
| 79 | + Object value = column.getValue(row); |
| 80 | + |
| 81 | + // TODO detect BLOB types somehow? column.getType() |
| 82 | +// switch (CoreGridCellEditorHelper.get(grid).guessJdbcTypeForEditing(grid, row, column)) { |
| 83 | +// case -4: |
| 84 | +// case -3: |
| 85 | +// case -2: |
| 86 | +// case 2004: |
| 87 | +// return 1; |
| 88 | +// default: |
| 89 | +// return 0; |
| 90 | +// } |
| 91 | + |
| 92 | + BinaryData binaryData; |
| 93 | + if (value instanceof byte[] || value == null) { |
| 94 | + binaryData = value == null ? new ByteArrayEditableData() : new ByteArrayEditableData((byte[]) value); |
| 95 | + } else if (value instanceof TextInfo) { |
| 96 | + binaryData = new ByteArrayEditableData(((TextInfo) value).bytes); |
| 97 | + } else { |
| 98 | + binaryData = objectValueConvertor.process(value).orElse(null); |
| 99 | + } |
| 100 | + |
| 101 | + if (binaryData != null) { |
| 102 | + Project project = grid.getProject(); |
| 103 | + ApplicationManager.getApplication().invokeLater(() -> { |
| 104 | + DataDialog dialog = new DataDialog(project, grid, binaryData); |
| 105 | + boolean editable = binaryData instanceof EditableBinaryData; |
| 106 | + dialog.setTitle(editable ? "Edit Binary Data" : "View Binary Data"); |
| 107 | + dialog.show(); |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void update(AnActionEvent event) { |
| 115 | + super.update(event); |
| 116 | + Presentation presentation = event.getPresentation(); |
| 117 | + presentation.setVisible(actionVisible); |
| 118 | + if (actionVisible) { |
| 119 | + boolean enabled = false; |
| 120 | + DataGrid grid = DataGridUtil.getDataGrid(event.getDataContext()); |
| 121 | + if (grid != null) { |
| 122 | + SelectionModel<GridRow, GridColumn> selectionModel = grid.getSelectionModel(); |
| 123 | + enabled = selectionModel.getSelectedColumnCount() == 1 && selectionModel.getSelectedRowCount() == 1; |
| 124 | + } |
| 125 | + presentation.setEnabled(enabled); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @ParametersAreNonnullByDefault |
| 130 | + private static class DataDialog extends DialogWrapper { |
| 131 | + |
| 132 | + private final BinEdComponentPanel viewPanel; |
| 133 | + private final DataGrid grid; |
| 134 | + |
| 135 | + private DataDialog(Project project, DataGrid grid, @Nullable BinaryData binaryData) { |
| 136 | + super(project, false); |
| 137 | + this.grid = grid; |
| 138 | + setModal(false); |
| 139 | + setCancelButtonText("Close"); |
| 140 | + setOKButtonText("Set"); |
| 141 | + boolean editable = binaryData instanceof EditableBinaryData; |
| 142 | + setOKActionEnabled(editable); |
| 143 | + setCrossClosesWindow(true); |
| 144 | + |
| 145 | + viewPanel = new BinEdComponentPanel(); |
| 146 | + viewPanel.setFileApi(new BinEdComponentFileApi() { |
| 147 | + @Override |
| 148 | + public boolean isSaveSupported() { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void saveDocument() { |
| 154 | + throw new IllegalStateException("Save not supported"); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void switchFileHandlingMode(FileHandlingMode newHandlingMode) { |
| 159 | + throw new IllegalStateException("Save not supported"); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void closeData() { |
| 164 | + // Ignore |
| 165 | + } |
| 166 | + }); |
| 167 | + viewPanel.setContentData(binaryData); |
| 168 | + if (!editable) { |
| 169 | + viewPanel.getCodeArea().setEditMode(EditMode.READ_ONLY); |
| 170 | + } |
| 171 | + init(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + protected void doOKAction() { |
| 176 | + super.doOKAction(); |
| 177 | + |
| 178 | + try { |
| 179 | + SelectionModel<GridRow, GridColumn> selectionModel = grid.getSelectionModel(); |
| 180 | + grid.cancelEditing(); |
| 181 | + BinaryData contentData = viewPanel.getContentData(); |
| 182 | + int size = contentData != null ? (int) contentData.getDataSize() : 0; |
| 183 | + byte[] resultData = new byte[size]; |
| 184 | + if (size > 0) { |
| 185 | + contentData.copyToArray(0, resultData, 0, size); |
| 186 | + } |
| 187 | + grid.setCells( |
| 188 | + selectionModel.getSelectedRows(), |
| 189 | + selectionModel.getSelectedColumns(), |
| 190 | + resultData |
| 191 | + ); |
| 192 | + } catch (Exception ex) { |
| 193 | + Logger.getLogger(DbEditBinaryAction.class.getName()).log(Level.SEVERE, "Unable to set value", ex); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Nonnull |
| 198 | + @Override |
| 199 | + protected Action[] createActions() { |
| 200 | + return new Action[] { getOKAction(), getCancelAction() }; |
| 201 | + } |
| 202 | + |
| 203 | + @Nullable |
| 204 | + @Override |
| 205 | + public JComponent getPreferredFocusedComponent() { |
| 206 | + return viewPanel; |
| 207 | + } |
| 208 | + |
| 209 | + @Nullable |
| 210 | + @Override |
| 211 | + protected String getDimensionServiceKey() { |
| 212 | + return "#org.exbin.bined.intellij.database.ViewBinaryAction"; |
| 213 | + } |
| 214 | + |
| 215 | + @Nullable |
| 216 | + @Override |
| 217 | + protected JComponent createCenterPanel() { |
| 218 | + BorderLayoutPanel panel = JBUI.Panels.simplePanel(viewPanel); |
| 219 | + panel.setPreferredSize(JBUI.size(600, 400)); |
| 220 | + return panel; |
| 221 | + } |
43 | 222 | } |
44 | 223 | } |
0 commit comments