Abstracts out startup window, added StartupWindowProvider doing runtime discovery and choosing the right window to use

This commit is contained in:
adam-m 2013-04-30 13:19:35 -04:00
parent 3df1e8d927
commit efe98d59d2
9 changed files with 155 additions and 27 deletions

View File

@ -816,7 +816,7 @@ public class Case {
* Invoke the creation of startup dialog window.
*/
static public void invokeStartupDialog() {
StartupWindow.getInstance().open();
StartupWindowProvider.getInstance().open();
}
/**

View File

@ -86,7 +86,7 @@ public final class CaseCloseAction extends CallableSystemAction implements Prese
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
StartupWindow.getInstance().open();
StartupWindowProvider.getInstance().open();
}
});
} catch (Exception ex) {

View File

@ -85,7 +85,7 @@ public final class CaseOpenAction implements ActionListener {
} else {
// try to close Startup window if there's one
try {
StartupWindow.getInstance().close();
StartupWindowProvider.getInstance().close();
} catch (Exception ex) {
// no need to show the error message to the user.
logger.log(Level.WARNING, "Error closing startup window.", ex);
@ -97,7 +97,7 @@ public final class CaseOpenAction implements ActionListener {
+ ": " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
logger.log(Level.WARNING, "Error opening case in folder " + path, ex);
StartupWindow.getInstance().open();
StartupWindowProvider.getInstance().open();
}
}
}

View File

@ -291,7 +291,7 @@ class NewCaseWizardPanel1 implements WizardDescriptor.ValidatingPanel<WizardDesc
createdDirectory = caseDirPath;
// try to close Startup window if there's one
try {
StartupWindow.getInstance().close();
StartupWindowProvider.getInstance().close();
} catch (Exception ex) {
logger.log(Level.WARNING, "Startup window didn't close as expected.", ex);

View File

@ -187,7 +187,7 @@ class OpenRecentCasePanel extends javax.swing.JPanel {
if (!casePath.equals("")) {
// Close the startup menu
try {
StartupWindow.getInstance().close();
StartupWindowProvider.getInstance().close();
CueBannerPanel.closeOpenRecentCasesWindow();
} catch (Exception ex) {
logger.log(Level.WARNING, "Error: couldn't open case: " + caseName, ex);
@ -200,7 +200,7 @@ class OpenRecentCasePanel extends javax.swing.JPanel {
//if case is not opened, open the start window
if (Case.isCaseOpen() == false) {
StartupWindow.getInstance().open();
StartupWindowProvider.getInstance().open();
}
} else {

View File

@ -65,7 +65,7 @@ class RecentItems implements ActionListener {
@Override
public void run() {
StartupWindow.getInstance().open();
StartupWindowProvider.getInstance().open();
}
});

View File

@ -26,40 +26,28 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.openide.util.lookup.ServiceProvider;
/**
* Displays
* The default implementation of the Autopsy startup window
*/
public final class StartupWindow extends JDialog {
@ServiceProvider(service=StartupWindowInterface.class)
public final class StartupWindow extends JDialog implements StartupWindowInterface {
private static StartupWindow instance;
private static final String TITLE = "Welcome";
private static Dimension DIMENSIONS = new Dimension(750, 400);
private StartupWindow(JFrame frame, String title, boolean isModal) {
super(frame, title, isModal);
public StartupWindow() {
super(new JFrame(TITLE), TITLE, true);
init();
}
/**
* Get the startup window
* @return the startup window singleton
*/
public static synchronized StartupWindow getInstance() {
if (StartupWindow.instance == null) {
JFrame frame = new JFrame(TITLE);
boolean isModal = true;
StartupWindow.instance = new StartupWindow(frame, TITLE, isModal);
}
return instance;
}
/**
* Shows the startup window.
*/
public void init() {
private void init() {
Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
@ -88,6 +76,7 @@ public final class StartupWindow extends JDialog {
}
@Override
public void open() {
setVisible(true);
}
@ -95,6 +84,7 @@ public final class StartupWindow extends JDialog {
/**
* Closes the startup window.
*/
@Override
public void close() {
this.setVisible(false);
}

View File

@ -0,0 +1,35 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2013 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* 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.sleuthkit.autopsy.casemodule;
/**
* Interface for startup window implementations
*/
public interface StartupWindowInterface {
/**
* Shows and makes active the startup window
*/
public void open();
/**
* Closes the startup window
*/
public void close();
}

View File

@ -0,0 +1,103 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2013 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* 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.sleuthkit.autopsy.casemodule;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import org.openide.util.Lookup;
import org.sleuthkit.autopsy.coreutils.Logger;
/**
* Provides the start up window to rest of the application. It may return the
* main / default startup window, or a custom one if it has been discovered.
*
* All that is required to create a custom startup window in a module and active it,
* is to implement StartupWindowInterface and register it with lookup as a ServiceProvider.
* The custom startup window is automatically chosen over the default one, given it is the only external module custom startup window.
*/
public class StartupWindowProvider implements StartupWindowInterface {
private static volatile StartupWindowProvider instance;
private static final Logger logger = Logger.getLogger(StartupWindowProvider.class.getName());
private volatile StartupWindowInterface startupWindowToUse;
public static StartupWindowProvider getInstance() {
if (instance == null) {
synchronized (StartupWindowProvider.class) {
if (instance == null) {
instance = new StartupWindowProvider();
instance.init();
}
}
}
return instance;
}
private void init() {
if (startupWindowToUse == null) {
//discover the registered windows
Collection<? extends StartupWindowInterface> startupWindows =
Lookup.getDefault().lookupAll(StartupWindowInterface.class);
int windowsCount = startupWindows.size();
if (windowsCount > 2) {
logger.log(Level.WARNING, "More than 2 (" + windowsCount + ") start up windows discovered, will use the first custom one");
} else if (windowsCount == 1) {
startupWindowToUse = startupWindows.iterator().next();
logger.log(Level.INFO, "Will use the default startup window: " + startupWindowToUse.toString());
} else {
//pick the non default one
Iterator<? extends StartupWindowInterface> it = startupWindows.iterator();
while (it.hasNext()) {
StartupWindowInterface window = it.next();
if (!org.sleuthkit.autopsy.casemodule.StartupWindow.class.isInstance(window)) {
startupWindowToUse = window;
logger.log(Level.INFO, "Will use the custom startup window: " + startupWindowToUse.toString());
break;
}
}
if (startupWindowToUse == null) {
logger.log(Level.SEVERE, "Unexpected error, no custom startup window found, using the default");
startupWindowToUse = new org.sleuthkit.autopsy.casemodule.StartupWindow();
}
}
}
}
@Override
public void open() {
if (startupWindowToUse != null) {
startupWindowToUse.open();
}
}
@Override
public void close() {
if (startupWindowToUse != null) {
startupWindowToUse.close();
}
}
}