Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
JFileChooserFactory.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.guiutils;
20 
21 import com.google.common.util.concurrent.ThreadFactoryBuilder;
22 import java.awt.Cursor;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.FutureTask;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import javax.swing.JFileChooser;
31 import org.openide.windows.WindowManager;
33 
51 public final class JFileChooserFactory {
52 
53  private static final Logger logger = Logger.getLogger(JFileChooserFactory.class.getName());
54 
55  private final FutureTask<JFileChooser> futureFileChooser;
56  private JFileChooser chooser;
57  private final ExecutorService executor;
58 
64  this(null);
65  }
66 
77  public JFileChooserFactory(Class<? extends JFileChooser> cls) {
78  if (cls == null) {
79  futureFileChooser = new FutureTask<>(JFileChooser::new);
80  } else {
81  futureFileChooser = new FutureTask<>(new ChooserCallable(cls));
82  }
83 
84  executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("JFileChooser-background-thread").build());
85  executor.execute(futureFileChooser);
86  }
87 
97  public JFileChooser getChooser() {
98  if (chooser == null) {
99  // In case this takes a moment show the wait cursor.
100  try {
101  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
102  try {
103  // get() will only return when the initilization of the
104  // JFileChooser has completed.
105  chooser = futureFileChooser.get();
106  } catch (InterruptedException | ExecutionException ex) {
107  // An exception is generally not expected. On the off chance
108  // one does occur save the situation by created a new
109  // instance in the EDT.
110  logger.log(Level.WARNING, "Failed to initialize JFileChooser in background thread.");
111  chooser = new JFileChooser();
112  }
113  } finally {
114  WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
115  executor.shutdown();
116  }
117  }
118 
119  return chooser;
120  }
121 
129  private class ChooserCallable implements Callable<JFileChooser> {
130 
131  private final Class<? extends JFileChooser> type;
132 
138  ChooserCallable(Class<? extends JFileChooser> type) {
139  this.type = type;
140  }
141 
142  @Override
143  public JFileChooser call() throws Exception {
144  return type.newInstance();
145  }
146  }
147 }
JFileChooserFactory(Class<?extends JFileChooser > cls)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.