Skip to content

Commit

Permalink
new features for neo4j desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
pgpv committed Jul 2, 2015
1 parent 3c9410d commit d1a6ae2
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 30 deletions.
Expand Up @@ -32,8 +32,9 @@
import static org.neo4j.desktop.ui.Components.alert; import static org.neo4j.desktop.ui.Components.alert;


/** /**
* The main class for starting the Neo4j desktop app window. The different components and wired up and started. * The main class for starting the Neo4j desktop app window. The different components are wired up and started.
*/ */

public final class Neo4jDesktop public final class Neo4jDesktop
{ {
public static void main( String[] args ) public static void main( String[] args )
Expand Down Expand Up @@ -82,6 +83,7 @@ private Installation getInstallation() throws Exception
case UNIX: case UNIX:
return new UnixInstallation(); return new UnixInstallation();
} }

return new UnixInstallation(); // This is the most generic one, presumably. return new UnixInstallation(); // This is the most generic one, presumably.
} }


Expand Down
Expand Up @@ -40,6 +40,7 @@
* Lifecycle actions for the Neo4j server living inside this JVM. Typically reacts to button presses * Lifecycle actions for the Neo4j server living inside this JVM. Typically reacts to button presses
* from {@link MainWindow}. * from {@link MainWindow}.
*/ */

public class DatabaseActions public class DatabaseActions
{ {
private final DesktopModel model; private final DesktopModel model;
Expand Down
Expand Up @@ -19,12 +19,15 @@
*/ */
package org.neo4j.desktop.ui; package org.neo4j.desktop.ui;


import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import javax.swing.JFileChooser; import java.io.FileWriter;
import javax.swing.JFrame; import java.io.IOException;
import javax.swing.JTextField; import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.*;


import static javax.swing.JFileChooser.APPROVE_OPTION; import static javax.swing.JFileChooser.APPROVE_OPTION;
import static javax.swing.JFileChooser.CUSTOM_DIALOG; import static javax.swing.JFileChooser.CUSTOM_DIALOG;
Expand All @@ -50,38 +53,94 @@ public BrowseForDatabaseActionListener( JFrame frame, JTextField directoryDispla
@Override @Override
public void actionPerformed( ActionEvent e ) public void actionPerformed( ActionEvent e )
{ {
JFileChooser jFileChooser = new JFileChooser(); File selectedFile = null;
jFileChooser.setFileSelectionMode( DIRECTORIES_ONLY ); boolean cancelled = false;
jFileChooser.setCurrentDirectory( new File( directoryDisplay.getText() ) ); boolean validLocation = false;
jFileChooser.setDialogTitle( "Select database" ); String os = System.getProperty( "os.name" );
jFileChooser.setDialogType( CUSTOM_DIALOG );


while ( true ) while ( !validLocation && !cancelled )
{ {
int choice = jFileChooser.showOpenDialog( frame ); if ( os.toLowerCase().contains( "mac" ) )

{
if ( choice != APPROVE_OPTION ) selectedFile = macFileSelection();
}
else
{ {
return; selectedFile = fileSelection();
} }


File selectedFile = jFileChooser.getSelectedFile();
try try
{ {
model.setDatabaseDirectory( selectedFile ); model.setDatabaseDirectory( selectedFile );
directoryDisplay.setText( model.getDatabaseDirectory().getAbsolutePath() ); directoryDisplay.setText( model.getDatabaseDirectory().getAbsolutePath() );
return;
validLocation = true;

FileWriter fileWriter = new FileWriter( new File( ".dblocation" ) );
fileWriter.write( selectedFile.getAbsolutePath() );
fileWriter.flush();
fileWriter.close();
} }
catch ( UnsuitableDirectoryException error ) catch ( UnsuitableDirectoryException ude )
{ {
int result = showWrappedConfirmDialog( int choice = showWrappedConfirmDialog(
frame, error.getMessage() + "\nPlease choose a different folder.", frame,
ude.getMessage() + "\nPlease choose a different folder.",
"Invalid folder selected", OK_CANCEL_OPTION, ERROR_MESSAGE ); "Invalid folder selected", OK_CANCEL_OPTION, ERROR_MESSAGE );
if ( result == CANCEL_OPTION )
if ( choice == CANCEL_OPTION )
{ {
return; cancelled = true;
} }
} }
catch ( IOException ioe )
{
System.out.println( "Error saving DB location" );
System.out.println( ioe );
}
} }
} }

private File fileSelection()
{
File selectedFile = null;

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( DIRECTORIES_ONLY );
fileChooser.setCurrentDirectory( new File( directoryDisplay.getText() ) );
fileChooser.setDialogTitle( "Select database" );
fileChooser.setDialogType( CUSTOM_DIALOG );

int choice = fileChooser.showOpenDialog( frame );

if ( choice == APPROVE_OPTION )
{
selectedFile = fileChooser.getSelectedFile();
}

return selectedFile;
}

private File macFileSelection()
{
File selectedFile = null;

System.setProperty( "apple.awt.fileDialogForDirectories", "true" );
FileDialog fileDialog = new FileDialog( frame );

fileDialog.setDirectory( directoryDisplay.getText() );
fileDialog.setVisible( true );

selectedFile = new File( fileDialog.getFile() );
System.setProperty( "apple.awt.fileDialogForDirectories", "false" );

return selectedFile;
}


public static void main( String[] args )
{
System.out.println( System.getProperty( "os.name" ) );
}

} }
Expand Up @@ -26,6 +26,11 @@
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
Expand Down Expand Up @@ -74,7 +79,7 @@ public class MainWindow


private DatabaseStatus databaseStatus; private DatabaseStatus databaseStatus;


public MainWindow( final DatabaseActions databaseActions, DesktopModel model ) public MainWindow( DatabaseActions databaseActions, DesktopModel model )
{ {
this.model = model; this.model = model;
this.debugWindow = new SystemOutDebugWindow(); this.debugWindow = new SystemOutDebugWindow();
Expand All @@ -84,7 +89,26 @@ public MainWindow( final DatabaseActions databaseActions, DesktopModel model )
this.frame.setIconImages( Graphics.loadIcons() ); this.frame.setIconImages( Graphics.loadIcons() );
this.sysTray = SysTray.install( new SysTrayActions(), frame ); this.sysTray = SysTray.install( new SysTrayActions(), frame );


this.directoryDisplay = createUnmodifiableTextField( model.getDatabaseDirectory().getAbsolutePath(), 35 ); String location = model.getDatabaseDirectory().getAbsolutePath();
File file = new File( ".dblocation" );

if( file.exists() && file.canRead() )
{
try
{
Scanner scanner = new Scanner( file );
if ( scanner.hasNextLine() )
{
location = scanner.nextLine();
}
}
catch ( FileNotFoundException e )
{
e.printStackTrace();
}
}

this.directoryDisplay = createUnmodifiableTextField( location, 35 );
this.browseButton = createBrowseButton(); this.browseButton = createBrowseButton();
this.statusPanelLayout = new CardLayout(); this.statusPanelLayout = new CardLayout();
this.statusPanel = createStatusPanel( statusPanelLayout ); this.statusPanel = createStatusPanel( statusPanelLayout );
Expand Down Expand Up @@ -248,5 +272,11 @@ public void clickCloseButton()
frame.setVisible( false ); frame.setVisible( false );
} }
} }

@Override
public void exit() { shutdown(); }

@Override
public void open() { frame.setVisible( true ); }
} }
} }
Expand Up @@ -19,9 +19,7 @@
*/ */
package org.neo4j.desktop.ui; package org.neo4j.desktop.ui;


import java.awt.AWTException; import java.awt.*;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
Expand All @@ -30,6 +28,8 @@
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import javax.swing.JFrame; import javax.swing.JFrame;


import org.neo4j.desktop.runtime.DatabaseActions;

import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE; import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;


import static org.neo4j.desktop.ui.DatabaseStatus.STOPPED; import static org.neo4j.desktop.ui.DatabaseStatus.STOPPED;
Expand All @@ -44,11 +44,17 @@ public abstract class SysTray
{ {
public static SysTray install( Actions actions, JFrame mainWindow ) public static SysTray install( Actions actions, JFrame mainWindow )
{ {
SysTray sysTray = null;

try try
{ {
if ( SystemTray.isSupported() ) if ( SystemTray.isSupported() )
{ {
return new SysTray.Enabled( Graphics.SYSTEM_TRAY_ICON, actions, mainWindow ); sysTray = new SysTray.Enabled( Graphics.SYSTEM_TRAY_ICON, actions, mainWindow );
}
else
{
sysTray = new SysTray.Disabled( actions, mainWindow );
} }
} }
catch ( AWTException e ) catch ( AWTException e )
Expand All @@ -58,7 +64,7 @@ public static SysTray install( Actions actions, JFrame mainWindow )
} }


// Fall back to still being able to function, but without the systray support. // Fall back to still being able to function, but without the systray support.
return new SysTray.Disabled( actions, mainWindow ); return sysTray;
} }


public abstract void changeStatus( DatabaseStatus status ); public abstract void changeStatus( DatabaseStatus status );
Expand All @@ -67,10 +73,11 @@ private static class Enabled extends SysTray
{ {
private final TrayIcon trayIcon; private final TrayIcon trayIcon;
private final String iconResourceBaseName; private final String iconResourceBaseName;

Enabled( String iconResourceBaseName, Actions actions, JFrame mainWindow ) throws AWTException Enabled( String iconResourceBaseName, Actions actions, JFrame mainWindow ) throws AWTException
{ {
this.iconResourceBaseName = iconResourceBaseName; this.iconResourceBaseName = iconResourceBaseName;

this.trayIcon = init( actions, mainWindow ); this.trayIcon = init( actions, mainWindow );
} }


Expand All @@ -91,6 +98,33 @@ private TrayIcon init( final Actions actions, JFrame mainWindow )
throws AWTException throws AWTException
{ {
TrayIcon trayIcon = new TrayIcon( loadImage( tryStatusSpecific( STOPPED ) ), title( STOPPED ) ); TrayIcon trayIcon = new TrayIcon( loadImage( tryStatusSpecific( STOPPED ) ), title( STOPPED ) );

PopupMenu popUpMenu = new PopupMenu( );

MenuItem menuItemOpen = new MenuItem( "Open" );
menuItemOpen.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent actionEvent )
{
actions.open();
}
} );
popUpMenu.add( menuItemOpen );

MenuItem menuItemExit = new MenuItem( "Exit" );
menuItemExit.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent actionEvent )
{
actions.exit();
}
} );

popUpMenu.add( menuItemExit );
trayIcon.setPopupMenu( popUpMenu );

trayIcon.addActionListener( new ActionListener() trayIcon.addActionListener( new ActionListener()
{ {
@Override @Override
Expand All @@ -107,6 +141,7 @@ public void mouseClicked( MouseEvent e )
actions.clickSysTray(); actions.clickSysTray();
} }
} ); } );

mainWindow.setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); mainWindow.setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
mainWindow.addWindowListener( new WindowAdapter() mainWindow.addWindowListener( new WindowAdapter()
{ {
Expand All @@ -116,7 +151,9 @@ public void windowClosing( WindowEvent e )
actions.clickCloseButton(); actions.clickCloseButton();
} }
} ); } );

SystemTray.getSystemTray().add( trayIcon ); SystemTray.getSystemTray().add( trayIcon );

return trayIcon; return trayIcon;
} }


Expand Down Expand Up @@ -155,5 +192,9 @@ public interface Actions
void clickSysTray(); void clickSysTray();


void closeForReal(); void closeForReal();

void open();

void exit();
} }
} }
2 changes: 1 addition & 1 deletion packaging/neo4j-desktop/updates.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<updateDescriptor baseUrl=""> <updateDescriptor baseUrl="">
<entry targetMediaFileId="444" updatableVersionMin="" updatableVersionMax="" fileName="neo4j-community_macos_@NEO4J_VERSION@.dmg" newVersion="@NEO4J_VERSION@" newMediaFileId="444" fileSize="51211267" bundledJre="macosx-amd64-1.7.0_80_unpacked" archive="true"> <entry targetMediaFileId="444" updatableVersionMin="" updatableVersionMax="" fileName="neo4j-community_macos_@NEO4J_VERSION@.dmg" newVersion="@NEO4J_VERSION@" newMediaFileId="444" fileSize="108730958" bundledJre="macosx-amd64-1.7.0_80_unpacked" archive="true">
<comment language="en" /> <comment language="en" />
</entry> </entry>
</updateDescriptor> </updateDescriptor>

0 comments on commit d1a6ae2

Please sign in to comment.