Skip to content

Commit

Permalink
Updates for file compare
Browse files Browse the repository at this point in the history
  • Loading branch information
hajdam committed Nov 2, 2021
1 parent adadf86 commit bb2bb5f
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 51 deletions.
Binary file modified lib/bined-operation-swing-0.2.1-SNAPSHOT.jar
Binary file not shown.
123 changes: 123 additions & 0 deletions src/org/exbin/bined/intellij/action/CompareFilesAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (C) ExBin Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.exbin.bined.intellij.action;

import com.intellij.ide.highlighter.ArchiveFileType;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.newvfs.ArchiveFileSystem;
import org.exbin.auxiliary.paged_data.BinaryData;
import org.exbin.auxiliary.paged_data.PagedData;
import org.exbin.bined.swing.extended.ExtCodeArea;
import org.exbin.framework.bined.gui.CompareFilesPanel;
import org.exbin.framework.gui.utils.WindowUtils;
import org.exbin.framework.gui.utils.gui.CloseControlPanel;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Compare files action.
*
* @version 0.2.5 2021/11/02
* @author ExBin Project (http://exbin.org)
*/
@ParametersAreNonnullByDefault
public class CompareFilesAction implements ActionListener {

private final ExtCodeArea codeArea;

public CompareFilesAction(ExtCodeArea codeArea) {
this.codeArea = Objects.requireNonNull(codeArea);
}

@Override
public void actionPerformed(ActionEvent event) {
final CompareFilesPanel compareFilesPanel = new CompareFilesPanel();
ResourceBundle panelResourceBundle = compareFilesPanel.getResourceBundle();
CloseControlPanel controlPanel = new CloseControlPanel(panelResourceBundle);
JPanel dialogPanel = WindowUtils.createDialogPanel(compareFilesPanel, controlPanel);
Dimension preferredSize = dialogPanel.getPreferredSize();
dialogPanel.setPreferredSize(new Dimension(preferredSize.width, preferredSize.height + 450));
final WindowUtils.DialogWrapper dialog = WindowUtils.createDialog(dialogPanel, (Component) event.getSource(), panelResourceBundle.getString("dialog.title"), Dialog.ModalityType.APPLICATION_MODAL);
controlPanel.setHandler(dialog::close);

List<String> availableFiles = new ArrayList<>();
availableFiles.add("Current File");
compareFilesPanel.setControl(new CompareFilesPanel.Control() {
@Nullable
@Override
public CompareFilesPanel.FileRecord openFile() {
Project project = ProjectManager.getInstance().getDefaultProject();

FileChooserDescriptor chooserDescriptor = new FileChooserDescriptor(true, false, true, false, false, false);
VirtualFile virtualFile = FileChooser.chooseFile(chooserDescriptor, project, null);
boolean isValid = virtualFile != null && virtualFile.isValid();
if (isValid && virtualFile.isDirectory()) {
isValid = false;
if (virtualFile.getFileType() instanceof ArchiveFileType) {
if (virtualFile.getFileSystem() instanceof JarFileSystem) {
virtualFile = ((JarFileSystem) virtualFile.getFileSystem()).getVirtualFileForJar(virtualFile);
isValid = virtualFile != null && virtualFile.isValid();
} else {
virtualFile = ((ArchiveFileSystem) virtualFile.getFileSystem()).getLocalByEntry(virtualFile);
isValid = virtualFile != null && virtualFile.isValid();
}
}
}
if (isValid) {
try (InputStream stream = virtualFile.getInputStream()) {
PagedData pagedData = new PagedData();
pagedData.loadFromStream(stream);
return new CompareFilesPanel.FileRecord(virtualFile.getName(), pagedData);
} catch (IOException ex) {
Logger.getLogger(CompareFilesAction.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
JOptionPane.showMessageDialog(null, "File reported as invalid", "Unable to open file", JOptionPane.ERROR_MESSAGE);
}

return null;
}

@Nonnull
@Override
public BinaryData getFileData(int index) {
return codeArea.getContentData();
}
});
compareFilesPanel.setAvailableFiles(availableFiles);
compareFilesPanel.setLeftIndex(1);
dialog.showCentered((Component) event.getSource());
}
}
18 changes: 9 additions & 9 deletions src/org/exbin/bined/intellij/action/InsertDataAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@
/**
* Insert data action.
*
* @version 0.2.1 2019/07/21
* @version 0.2.5 2021/11/02
* @author ExBin Project (http://exbin.org)
*/
@ParametersAreNonnullByDefault
public class InsertDataAction implements ActionListener {

private final ResourceBundle resourceBundle = LanguageUtils.getResourceBundleByClass(GoToBinaryPanel.class);
private final ExtCodeArea codeArea;
private final BinaryDataUndoHandler undoHandler;
private BinaryDataUndoHandler undoHandler;

public InsertDataAction(ExtCodeArea codeArea, BinaryDataUndoHandler undoHandler) {
public InsertDataAction(ExtCodeArea codeArea) {
this.codeArea = Objects.requireNonNull(codeArea);
}

public void setUndoHandler(BinaryDataUndoHandler undoHandler) {
this.undoHandler = undoHandler;
}

Expand All @@ -71,7 +73,7 @@ public void actionPerformed(ActionEvent event) {
final InsertDataPanel insertDataPanel = new InsertDataPanel();
DefaultControlPanel controlPanel = new DefaultControlPanel(insertDataPanel.getResourceBundle());
JPanel dialogPanel = WindowUtils.createDialogPanel(insertDataPanel, controlPanel);
final DialogWrapper dialog = WindowUtils.createDialog(dialogPanel, (Component) event.getSource(), "", Dialog.ModalityType.APPLICATION_MODAL);
final DialogWrapper dialog = WindowUtils.createDialog(dialogPanel, (Component) event.getSource(), "Insert Data", Dialog.ModalityType.APPLICATION_MODAL);
insertDataPanel.setControl(() -> {
final BinaryMultilinePanel multilinePanel = new BinaryMultilinePanel();
SearchCondition searchCondition = new SearchCondition();
Expand All @@ -82,8 +84,7 @@ public void actionPerformed(ActionEvent event) {
multilinePanel.setCondition(searchCondition);
DefaultControlPanel controlPanel1 = new DefaultControlPanel();
JPanel dialogPanel1 = WindowUtils.createDialogPanel(multilinePanel, controlPanel1);
final DialogWrapper multilineDialog = WindowUtils.createDialog(dialog.getWindow(), Dialog.ModalityType.APPLICATION_MODAL, dialogPanel1);
//WindowUtils.setDialogTitle(multilineDialog, multilinePanel.getResourceBundle());
final DialogWrapper multilineDialog = WindowUtils.createDialog(dialogPanel1, dialog.getWindow(), "Multiline Data", Dialog.ModalityType.APPLICATION_MODAL);
controlPanel1.setHandler((DefaultControlHandler.ControlActionType actionType) -> {
if (actionType == DefaultControlHandler.ControlActionType.OK) {
SearchCondition condition = multilinePanel.getCondition();
Expand All @@ -102,8 +103,7 @@ public void actionPerformed(ActionEvent event) {
multilineDialog.showCentered(dialog.getWindow());
// multilinePanel.detachMenu();
});
WindowUtils.addHeaderPanel(dialog.getWindow(), insertDataPanel.getClass(), insertDataPanel.getResourceBundle());
// frameModule.setDialogTitle(dialog, insertDataPanel.getResourceBundle());
//WindowUtils.addHeaderPanel(dialog.getWindow(), insertDataPanel.getClass(), insertDataPanel.getResourceBundle());
controlPanel.setHandler((DefaultControlHandler.ControlActionType actionType) -> {
if (actionType == DefaultControlHandler.ControlActionType.OK) {
insertDataPanel.acceptInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.exbin.bined.intellij;
package org.exbin.bined.intellij.action;

import org.exbin.bined.DefaultCodeAreaCaretPosition;
import org.exbin.bined.highlight.swing.extended.ExtendedHighlightCodeAreaPainter;
Expand Down
27 changes: 21 additions & 6 deletions src/org/exbin/bined/intellij/gui/BinEdComponentPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
import org.exbin.bined.highlight.swing.extended.ExtendedHighlightNonAsciiCodeAreaPainter;
import org.exbin.bined.intellij.BinEdApplyOptions;
import org.exbin.bined.intellij.BinEdIntelliJPlugin;
import org.exbin.bined.intellij.action.CompareFilesAction;
import org.exbin.bined.intellij.action.GoToPositionAction;
import org.exbin.bined.intellij.IntelliJPreferencesWrapper;
import org.exbin.bined.intellij.SearchAction;
import org.exbin.bined.intellij.action.SearchAction;
import org.exbin.bined.intellij.action.InsertDataAction;
import org.exbin.bined.operation.BinaryDataCommand;
import org.exbin.bined.operation.swing.CodeAreaOperationCommandHandler;
Expand Down Expand Up @@ -102,6 +103,7 @@ public class BinEdComponentPanel extends JBPanel implements DumbAware {
private ModifiedStateListener modifiedChangeListener = null;
private final GoToPositionAction goToRowAction;
private final InsertDataAction insertDataAction;
private final CompareFilesAction compareFilesAction;
private final AbstractAction showHeaderAction;
private final AbstractAction showRowNumbersAction;
private final SearchAction searchAction;
Expand Down Expand Up @@ -159,7 +161,8 @@ public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
statusPanel = new BinaryStatusPanel();

goToRowAction = new GoToPositionAction(codeArea);
insertDataAction = new InsertDataAction(codeArea, undoHandler);
insertDataAction = new InsertDataAction(codeArea);
compareFilesAction = new CompareFilesAction(codeArea);
showHeaderAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down Expand Up @@ -614,6 +617,9 @@ private void createContextMenu(final JPopupMenu menu, int x, int y) {
}
}

JMenuItem compareFilesMenuItem = createCompareFilesMenuItem();
menu.add(compareFilesMenuItem);

final JMenuItem optionsMenuItem = new JMenuItem("Options...");
optionsMenuItem.setIcon(new ImageIcon(getClass().getResource("/org/exbin/framework/gui/options/resources/icons/Preferences16.gif")));
optionsMenuItem.addActionListener(createOptionsAction());
Expand Down Expand Up @@ -704,10 +710,17 @@ private JMenuItem createGoToMenuItem() {

@Nonnull
private JMenuItem createInsertDataMenuItem() {
final JMenuItem insertDataActionItem = new JMenuItem("Insert Data...");
insertDataActionItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionUtils.getMetaMask()));
insertDataActionItem.addActionListener(insertDataAction);
return insertDataActionItem;
final JMenuItem insertDataMenuItem = new JMenuItem("Insert Data...");
insertDataMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionUtils.getMetaMask()));
insertDataMenuItem.addActionListener(insertDataAction);
return insertDataMenuItem;
}

@Nonnull
private JMenuItem createCompareFilesMenuItem() {
final JMenuItem compareFilesMenuItem = new JMenuItem("Compare Files...");
compareFilesMenuItem.addActionListener(compareFilesAction);
return compareFilesMenuItem;
}

@Nonnull
Expand Down Expand Up @@ -925,6 +938,7 @@ public void setUndoHandler(BinaryDataUndoHandler undoHandler) {
if (valuesPanel != null) {
valuesPanel.setCodeArea(codeArea, undoHandler);
}
insertDataAction.setUndoHandler(undoHandler);
// TODO set ENTER KEY mode in apply options

undoHandler.addUndoUpdateListener(new BinaryDataUndoUpdateListener() {
Expand Down Expand Up @@ -963,6 +977,7 @@ public void setContentData(BinaryData data) {
// codeArea.setCharset(charset);
}

@Nonnull
public static PropertiesComponent getPreferences() {
return PropertiesComponent.getInstance();
}
Expand Down
Loading

0 comments on commit bb2bb5f

Please sign in to comment.