Skip to content

Commit

Permalink
Some code cleanup (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Tamir Krispis <tamir.krispis@niceactimize.com>
  • Loading branch information
tamirkrispis and Tamir Krispis authored Dec 30, 2023
1 parent 86da929 commit f3cde8f
Show file tree
Hide file tree
Showing 80 changed files with 1,650 additions and 1,161 deletions.
6 changes: 3 additions & 3 deletions gui/src/main/java/com/keepaste/gui/DialogKeep.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Keepaste - The keep and paste program (http://www.keepaste.com)
* Copyright (C) 2023 Tamir Krispis
*
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down
6 changes: 3 additions & 3 deletions gui/src/main/java/com/keepaste/gui/Gui.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Keepaste - The keep and paste program (http://www.keepaste.com)
* Copyright (C) 2023 Tamir Krispis
*
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down
6 changes: 3 additions & 3 deletions gui/src/main/java/com/keepaste/gui/GuiMain.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Keepaste - The keep and paste program (http://www.keepaste.com)
* Copyright (C) 2023 Tamir Krispis
*
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down
185 changes: 98 additions & 87 deletions logic/src/main/java/com/keepaste/logic/Application.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
* Keepaste - The keep and paste program (http://www.keepaste.com)
* Copyright (C) 2023 Tamir Krispis
*
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down Expand Up @@ -55,7 +55,7 @@
import org.apache.maven.artifact.versioning.ComparableVersion;

/**
* This class holds the main entry point for the application and holds a reference to the {@link Context}.
* This class holds the main entry point for the application and holds a reference to the {@code Context}.
*/
@Log4j2
public final class Application {
Expand All @@ -76,98 +76,109 @@ private Application() {
*/
public static void main(String[] args) {
try {
// Load settings from disk
ModelSettings modelSettings = SettingsManager.loadSettingsFromFile();
// in case there is an issue loading the settings file, create a default one instead
if (modelSettings == null) {
SettingsManager.createDefaultSettingsFile();
modelSettings = SettingsManager.loadSettingsFromFile();
}
if (modelSettings == null) {
modelSettings = SettingsManager.getDefaultModelSettings();
}
// checking if there is a new release available
checkForNewRelease();

setInitialLookAndFeel(modelSettings);
// Load settings from disk or get default instance
ModelSettings modelSettings = SettingsManager.getModelSettings();

final Gui gui = new Gui();
// set look and feel based on settings
setInitialLookAndFeel(modelSettings);

if (OperatingSystemUtils.getOperatingSystemType() == OperatingSystemUtils.OperatingSystemType.WINDOWS) {
gui.setMinimumSize(new Dimension(APP_MIN_WIDTH, gui.getHeight()));
}
ModelActiveWindow modelActiveWindow = new ModelActiveWindow();
context = new Context(
gui,
getWindowManager(),
modelSettings,
new KeepsManager(),
new KeepExecutionManager(),
modelActiveWindow);
boolean isFirstTimeRunning = !new File(context.getKeepsManager().getKeepsFilePathString()).exists();
checkForNewRelease();
Image logoImage = ImagesUtils.getImage("/logo-white.png");

// Create and display the form
ModelSettings finalModelSettings = modelSettings;
EventQueue.invokeLater(() -> {
try {
gui.setIconImage(logoImage);
gui.labelBackground.setVisible(false);

// Intercepted window shown on status bar
ViewActiveWindow viewActiveWindow = new ViewActiveWindow(gui.labelTargetWindow, gui.labelTargetWindowTitle);
new ControllerActiveWindow(modelActiveWindow, viewActiveWindow);

// Top menu bar
ViewTopMenu viewTopMenu = new ViewTopMenu(gui.menuItemMain, gui.menuItemAbout, gui.menuItemHeart);
viewTopMenu.initUpperMenuBar();
new ControllerTopMenu(modelActiveWindow, viewActiveWindow, viewTopMenu.getLockingMenuItem());

// Tree
ViewTree viewTree = new ViewTree();
ModelTree modelTree = new ModelTree();
new ControllerTree(modelTree, viewTree);

// look and feel (theme) controller
ViewLookAndFeel viewLookAndFeel = new ViewLookAndFeel(viewTree);
new ControllerLookAndFeel(finalModelSettings, viewLookAndFeel);

final Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();

// do not change the order of these two next lines, setLocationRelativeTo sets the window in the center,
// but allows ctrl+f4 on Mac, while the second line set the window on a specific position.
// having those reversed will cause ctrl+f4 which is being used to switch between windows to stop working
// on always on top mode.
gui.setLocationRelativeTo(null);
gui.setLocation(
(int) screenSize.getWidth() - gui.getWidth() - LOCATION_MARGIN,
(int) screenSize.getHeight() - gui.getHeight() - LOCATION_MARGIN);

gui.setAlwaysOnTop(finalModelSettings.isAlwaysOnTop());

gui.setVisible(true);
finalModelSettings.setLookAndFeel(finalModelSettings.getTheme());
log.info("Gui initialized");

if (isFirstTimeRunning) {
// this is the first time keepaste runs on this machine, showing the welcome dialog
DialogWelcome dialogWelcome = new DialogWelcome(gui, true);
GuiUtils.initHyperlinkLabel(dialogWelcome.labelTutorial, "http://www.keepaste.com/tutorial.htm");
GuiUtils.initHyperlinkLabel(dialogWelcome.labelKeepsLibrary, "https://github.com/tamirkrispis/keeps-library");
GuiUtils.showDialogOnCenterScreen(dialogWelcome);
}
} catch (Exception ex) {
log.error("Failed to initialize gui", ex);
System.exit(1);
}
// generating app context to be used
context = generateContext(modelSettings);

context.startWindowInterceptorRunner();
});
// initializing and displaying the GUI
initGui();

} catch (Exception ex) {
log.error("Failed to start the application", ex);
}
}

private static void initGui() {
// Create and display the form
EventQueue.invokeLater(() -> {
try {
Gui gui = context.getGui();
// setting minimum size for windows
if (OperatingSystemUtils.getOperatingSystemType() == OperatingSystemUtils.OperatingSystemType.WINDOWS) {
gui.setMinimumSize(new Dimension(APP_MIN_WIDTH, gui.getHeight()));
}

gui.setIconImage(getLogoImage());
gui.labelBackground.setVisible(false);

// Intercepted window shown on status bar
ViewActiveWindow viewActiveWindow = new ViewActiveWindow(gui.labelTargetWindow, gui.labelTargetWindowTitle);
new ControllerActiveWindow(context.getModelActiveWindow(), viewActiveWindow);

// Top menu bar
ViewTopMenu viewTopMenu = new ViewTopMenu(gui.menuItemMain, gui.menuItemAbout, gui.menuItemHeart);
viewTopMenu.initUpperMenuBar();
new ControllerTopMenu(context.getModelActiveWindow(), viewActiveWindow, viewTopMenu.getLockingMenuItem());

// Tree
ViewTree viewTree = new ViewTree();
ModelTree modelTree = new ModelTree();
new ControllerTree(modelTree, viewTree);

// look and feel (theme) controller
ViewLookAndFeel viewLookAndFeel = new ViewLookAndFeel(viewTree);
new ControllerLookAndFeel(context.getModelSettings(), viewLookAndFeel);

final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();

// do not change the order of these two next lines, setLocationRelativeTo sets the window in the center,
// but allows ctrl+f4 on Mac, while the second line set the window on a specific position.
// having those reversed will cause ctrl+f4 which is being used to switch between windows to stop working
// on always on top mode.
gui.setLocationRelativeTo(null);
gui.setLocation(
(int) screenSize.getWidth() - gui.getWidth() - LOCATION_MARGIN,
(int) screenSize.getHeight() - gui.getHeight() - LOCATION_MARGIN);

gui.setAlwaysOnTop(context.getModelSettings().isAlwaysOnTop());

gui.setVisible(true);
context.getModelSettings().setLookAndFeel(context.getModelSettings().getTheme());
log.info("Gui initialized");

if (isFirstTimeRunning()) {
// this is the first time keepaste runs on this machine, showing the welcome dialog
DialogWelcome dialogWelcome = new DialogWelcome(gui, true);
GuiUtils.initHyperlinkLabel(dialogWelcome.labelTutorial, "https://www.keepaste.com/tutorial.htm");
GuiUtils.initHyperlinkLabel(dialogWelcome.labelKeepsLibrary, "https://github.com/tamirkrispis/keeps-library");
GuiUtils.showDialogOnCenterScreen(dialogWelcome);
}
} catch (Exception ex) {
log.error("Failed to initialize gui", ex);
System.exit(1);
}

context.startWindowInterceptorRunner();
});
}

private static Image getLogoImage() {
return ImagesUtils.getImage("/logo-white.png");
}

private static boolean isFirstTimeRunning() {
return !new File(context.getKeepsManager().getKeepsFilePathString()).exists();
}

private static Context generateContext(ModelSettings modelSettings) {
return new Context(
new Gui(),
getWindowManager(),
modelSettings,
new KeepsManager(),
new KeepExecutionManager(),
new ModelActiveWindow());
}

private static void setInitialLookAndFeel(ModelSettings modelSettings) {
try {
FlatLightLaf.setup();
Expand Down
15 changes: 7 additions & 8 deletions logic/src/main/java/com/keepaste/logic/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public class Context {
/**
* Constructor.
*
* @param gui the {@link Gui}
* @param windowManager the relevant {@link WindowManager} based on the OS
* @param modelSettings the {@link ModelSettings} to be used
* @param keepsManager the {@link KeepsManager} to be used
* @param keepExecutionManager the {@link KeepExecutionManager} to be used
* @param modelActiveWindow the {@link ModelActiveWindow} to be used
* @param gui the {@code Gui}
* @param windowManager the relevant {@code WindowManager} based on the OS
* @param modelSettings the {@code ModelSettings} to be used
* @param keepsManager the {@code KeepsManager} to be used
* @param keepExecutionManager the {@code KeepExecutionManager} to be used
* @param modelActiveWindow the {@code ModelActiveWindow} to be used
*/
public Context(@NonNull final Gui gui,
@NonNull final WindowManager windowManager,
Expand Down Expand Up @@ -91,9 +91,8 @@ public String getVersion() {
* Will start intercepting windows constantly.
*/
public void startWindowInterceptorRunner() {
log.info("Starting window interceptor");

if (modelSettings.isFocusOnWindowAndPaste()) {
log.info("Starting window interceptor");
Runnable windowInterceptorRunner = new WindowInterceptorRunner(windowManager, modelActiveWindow, INTERCEPT_INTERVAL_IN_MS);
windowInterceptorThread = new Thread(windowInterceptorRunner);
windowInterceptorThread.setDaemon(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
/**
* Keepaste - The keep and paste program (http://www.keepaste.com)
* Copyright (C) 2023 Tamir Krispis
*
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.keepaste.logic.actionlisteners.dialogkeep;

import com.keepaste.gui.DialogKeep;
import com.keepaste.logic.views.KeepParametersTableModel;
import lombok.extern.log4j.Log4j2;
import javax.swing.JTable;
import java.awt.event.ActionEvent;

/**
* This class is an ActionListener for adding a parameter to a Keep on the {@link com.keepaste.gui.DialogKeep}.
* This class is an ActionListener for adding a parameter to a Keep on the {@code com.keepaste.gui.DialogKeep}.
*/
@Log4j2
public class AddParamActionListener extends BaseDialogKeepActionListener {

/**
* Constructor.
*
* @param table the {@link JTable} to show the parameters
* @param dialogKeep a {@code DialogKeep}
*/
public AddParamActionListener(final JTable table) {
super(table);
public AddParamActionListener(final DialogKeep dialogKeep) {
super(dialogKeep);
}

@Override
public void actionPerformed(ActionEvent e) {
// Adding a new parameter to the parameters table on the dialog
log.debug("DialogKeep - Adding new keep parameter on the Dialog Command");
var table = getTable();
KeepParametersTableModel model = (KeepParametersTableModel) table.getModel();
var tableParams = dialogKeep.tableParams;
KeepParametersTableModel model = (KeepParametersTableModel) tableParams.getModel();
model.addRow(model.getRowCount(), "", "");
// Scroll to the added row
int row = model.getRowCount() - 1;
table.scrollRectToVisible(table.getCellRect(row, 0, true));
tableParams.scrollRectToVisible(tableParams.getCellRect(row, 0, true));

// Request focus on the added row's first cell
table.requestFocus();
table.changeSelection(row, 0, false, false);
table.editCellAt(row, 0);
table.getEditorComponent().requestFocus();
tableParams.requestFocus();
tableParams.changeSelection(row, 0, false, false);
tableParams.editCellAt(row, 0);
tableParams.getEditorComponent().requestFocus();

// updating the table to show the new row
getTable().updateUI();
tableParams.updateUI();
log.debug("DialogKeep - Added new keep parameter on the Dialog Command");
}
}
Loading

0 comments on commit f3cde8f

Please sign in to comment.