use of java.util.prefs.Preferences in project jgnash by ccavanaugh.
the class JTextFieldEx method setSelectOnFocus.
public static void setSelectOnFocus(final boolean select) {
Preferences p = Preferences.userNodeForPackage(JTextFieldEx.class);
p.putBoolean(SELECT_ON_FOCUS, select);
JTextFieldEx.select = select;
}
use of java.util.prefs.Preferences in project jgnash by ccavanaugh.
the class RemoteConnectionDialog method initComponents.
private void initComponents() {
Preferences preferences = Preferences.userNodeForPackage(RemoteConnectionDialog.class);
setPort(preferences.getInt(LAST_PORT, JpaNetworkServer.DEFAULT_PORT));
setHost(preferences.get(LAST_HOST, "localhost"));
cancelButton = new JButton(rb.getString("Button.Cancel"));
okButton = new JButton(rb.getString("Button.Ok"));
cancelButton.addActionListener(this);
okButton.addActionListener(this);
}
use of java.util.prefs.Preferences in project jgnash by ccavanaugh.
the class DynamicJasperReportPanel method saveAction.
private void saveAction() {
Preferences p = Preferences.userNodeForPackage(DynamicJasperReportPanel.class);
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
saveContributors.forEach(fileChooser::addChoosableFileFilter);
// restore the last save format
if (p.get(LAST_CONTRIBUTOR, null) != null) {
String last = p.get(LAST_CONTRIBUTOR, null);
for (JRSaveContributor saveContributor : saveContributors) {
if (saveContributor.getDescription().equals(last)) {
fileChooser.setFileFilter(saveContributor);
break;
}
}
} else if (!saveContributors.isEmpty()) {
fileChooser.setFileFilter(saveContributors.get(0));
}
if (p.get(LAST_DIRECTORY, null) != null) {
fileChooser.setCurrentDirectory(new File(p.get(LAST_DIRECTORY, null)));
}
int retValue = fileChooser.showSaveDialog(this);
if (retValue == JFileChooser.APPROVE_OPTION) {
FileFilter fileFilter = fileChooser.getFileFilter();
File file = fileChooser.getSelectedFile();
p.put(LAST_DIRECTORY, file.getParent());
JRSaveContributor contributor = null;
if (fileFilter instanceof JRSaveContributor) {
// save format chosen from the list
contributor = (JRSaveContributor) fileFilter;
} else {
for (JRSaveContributor saveContributor : saveContributors) {
// need to determine the best match
if (saveContributor.accept(file)) {
contributor = saveContributor;
break;
}
}
if (contributor == null) {
JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
}
}
if (contributor != null) {
p.put(LAST_CONTRIBUTOR, contributor.getDescription());
try {
if (contributor instanceof JRSingleSheetXlsSaveContributor) {
LOG.info("Formatting for xls file");
JasperPrint print = report.createJasperPrint(true);
contributor.save(print, file);
} else if (contributor instanceof JRCsvSaveContributor) {
LOG.info("Formatting for csv file");
JasperPrint print = report.createJasperPrint(true);
contributor.save(print, file);
} else {
contributor.save(jasperPrint, file);
}
} catch (final JRException ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
}
}
}
}
use of java.util.prefs.Preferences in project jgnash by ccavanaugh.
the class DialogUtils method addBoundsListener.
/**
* Listens to a JDialog to save and restore windows bounds automatically.
* <p>
* {@code setVisible(false)} and {@code dispose()} must not be used
* to close the window. Instead, dispatch a window closing event.
* <PRE>
* dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
* </PRE>
* and the dialog must be set to dispose on close
* <PRE>
* setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
* </PRE>
*
* @param w {@code Window} to listen to
* @param prefNode String identifier to preference node to save and restore from
* @param key the key to save and restore from
*/
private static void addBoundsListener(final Window w, final String prefNode, final String key) {
String bounds = Preferences.userRoot().node(prefNode).get(key, null);
if (bounds != null) {
if (w instanceof JDialog) {
if (((JDialog) w).isResizable()) {
w.setBounds(decodeRectangle(bounds));
} else {
w.setLocation(decodeRectangle(bounds).getLocation());
}
} else {
w.setBounds(decodeRectangle(bounds));
}
Window owner = w.getOwner();
if (owner != null) {
w.setLocationRelativeTo(owner);
}
}
/* listen for a window closing event and deal with it */
w.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent evt) {
// save position and size
Preferences p = Preferences.userRoot().node(prefNode);
p.put(key, encodeRectangle(w.getBounds()));
// make GC easy
w.removeWindowListener(this);
}
});
if (w instanceof JDialog) {
addEscapeListener((JDialog) w);
}
}
use of java.util.prefs.Preferences in project jgnash by ccavanaugh.
the class EngineFactory method bootClientEngine.
public static synchronized Engine bootClientEngine(final String host, final int port, final char[] password, final String engineName) throws Exception {
if (engineMap.get(engineName) != null) {
throw new RuntimeException("A stale engine was found in the map");
}
final Preferences pref = Preferences.userNodeForPackage(EngineFactory.class);
Engine engine = null;
// start the client message bus
if (MessageBus.getInstance(engineName).setRemote(host, port + JpaNetworkServer.MESSAGE_SERVER_INCREMENT, password)) {
pref.putInt(LAST_PORT, port);
pref.put(LAST_HOST, host);
pref.putBoolean(LAST_REMOTE, true);
final MessageBus messageBus = MessageBus.getInstance(engineName);
// after starting the remote message bus, it should receive the path on the server
final String remoteDataBasePath = messageBus.getRemoteDataBasePath();
final DataStoreType dataStoreType = messageBus.getRemoteDataStoreType();
if (remoteDataBasePath == null || remoteDataBasePath.isEmpty() || dataStoreType == null) {
throw new Exception("Invalid connection wih the message bus");
}
logger.log(Level.INFO, "Remote path was {0}", remoteDataBasePath);
logger.log(Level.INFO, "Remote data store was {0}", dataStoreType.name());
logger.log(Level.INFO, "Engine name was {0}", engineName);
DataStore dataStore = dataStoreType.getDataStore();
// connect to the remote server
engine = dataStore.getClientEngine(host, port, password, remoteDataBasePath);
if (engine != null) {
logger.info(ResourceUtils.getString("Message.EngineStart"));
engineMap.put(engineName, engine);
dataStoreMap.put(engineName, dataStore);
// remember if the user used a password for the last session
pref.putBoolean(USED_PASSWORD, password.length > 0);
final Message message = new Message(MessageChannel.SYSTEM, ChannelEvent.FILE_LOAD_SUCCESS, engine);
MessageBus.getInstance(engineName).fireEvent(message);
}
}
return engine;
}
Aggregations