Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.jppf.ui.actions.AbstractUpdatableAction Maven / Gradle / Ivy
/*
* JPPF.
* Copyright (C) 2005-2019 JPPF Team.
* http://www.jppf.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.jppf.ui.actions;
import java.awt.Point;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import org.jppf.ui.options.OptionElement;
import org.jppf.ui.options.event.ValueChangeEvent;
import org.jppf.ui.options.factory.OptionsHandler;
import org.jppf.ui.utils.GuiUtils;
import org.jppf.utils.LocalizationUtils;
/**
* Abstract superclass for actions used in toolbars or popup menus.
* @author Laurent Cohen
*/
public abstract class AbstractUpdatableAction extends AbstractAction implements UpdatableAction {
/**
* The base location for internationalized messages.
*/
protected String BASE = null;
/**
* The list of selected elements.
*/
protected List selectedElements = new LinkedList<>();
/**
* Location at which to display any window or dialog created by this action.
*/
protected Point location = new Point(10, 10);
/**
* Get the location at which to display any window or dialog created by this action.
* @return a Point
instance.
*/
public Point getLocation() {
return location;
}
/**
* Set the location at which to display any window or dialog created by this action.
* @param location a Point
instance.
*/
public void setLocation(final Point location) {
this.location = location;
}
/**
* Update this action's enabled state based on a list of selected elements.
* @param selectedElements a list of objects.
*/
@Override
public void updateState(final List selectedElements) {
this.selectedElements = selectedElements;
}
/**
* Method called when the action is triggered.
* @param event the event encapsulating the source of the event.
*/
@Override
public void valueChanged(final ValueChangeEvent event) {
actionPerformed(new ActionEvent(event.getSource(), ActionEvent.ACTION_PERFORMED, ""));
}
/**
* Set the icon for this action using the specified image file name.
* @param name the name of the icon image file.
*/
protected void setupIcon(final String name) {
if (name != null) putValue(Action.SMALL_ICON, GuiUtils.loadIcon(name));
}
/**
* Set the action name and tooltip text.
* @param name the key to find the name and tooltip in the localized resource bundles.
*/
protected void setupNameAndTooltip(final String name) {
putValue(NAME, localize(name + ".label"));
putValue(SHORT_DESCRIPTION, localize(name + ".tooltip"));
}
/**
* Set the action tooltip text.
* @param name the key to find the tooltip in the localized resource bundles.
*/
protected void setupTooltip(final String name) {
putValue(SHORT_DESCRIPTION, localize(name + ".tooltip"));
}
/**
* Get a localized message given its unique name and the current locale.
* @param message the unique name of the localized message.
* @return a message in the current locale, or the default locale if the localization for the current locale is not found.
*/
protected String localize(final String message) {
return LocalizationUtils.getLocalized(BASE, message);
}
/**
* Get a localized message given its unique name and the current locale.
* @param message the unique name of the localized message.
* @param params optional parmeters for the message.
* @return a message in the current locale, or the default locale if the localization for the current locale is not found.
*/
protected String localize(final String message, final Object...params) {
return LocalizationUtils.getLocalized(BASE, message, message, Locale.getDefault(), params);
}
/**
* Execute the specified runnable in a new thread.
* The thread name is this.getClass().getSimpleName()
.
* @param r the Runnable
to execute.
*/
protected void runAction(final Runnable r) {
GuiUtils.runAction(r, getClass().getSimpleName());
}
/**
* Associate a keyboard virtual key with an action.
* @param comp the {@link JComponent} whose {@link InputMap} and {@link ActionMap} this method modifies.
* @param vkey the virtual key code to associate with the action, built from
* one of the values in {@link KeyEvent}, for instance: KeyEvent.VK_ENTER
.
* @param action the action to trigger upon pressing the keyboard key.
* @param actionKey a key to use int he {@link ActionMap}.
*/
protected static void setKeyAction(final JComponent comp, final KeyStroke vkey, final Action action, final Object actionKey) {
final InputMap inputMap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(vkey, actionKey);
final ActionMap map = comp.getActionMap();
map.put(actionKey, action);
}
/**
* Associate 'ok' and 'cancel' actions with 'Enter' and 'Esc' keys, repsectively.
* @param option contains the {@link JComponent} whose {@link InputMap} and {@link ActionMap} this method modifies.
* @param okAction the action to execute upon pressing 'Enter'.
* @param cancelAction the action to execute upon pressing 'Esc'.
*/
public static void setOkCancelKeys(final OptionElement option, final Action okAction, final Action cancelAction) {
setOkCancelKeys(option.getUIComponent(), okAction, cancelAction);
}
/**
* Associate 'ok' and 'cancel' actions with 'Enter' and 'Esc' keys, repsectively.
* @param option contains the {@link JComponent} whose {@link InputMap} and {@link ActionMap} this method modifies.
* @param okAction the action to execute upon pressing 'Enter'.
* @param cancelAction the action to execute upon pressing 'Esc'.
*/
public static void setOkCancelKeys(final JComponent option, final Action okAction, final Action cancelAction) {
if (okAction != null) setKeyAction(option, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), okAction, "ok");
if (cancelAction != null) setKeyAction(option, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelAction, "cancel");
}
/**
* Load the persisted preferences for the specified element and all of its descendants.
* @param xmlPath path to the xml resource to load.
* @return elt, for method chaining.
*/
protected OptionElement loadWithPreferences(final String xmlPath) {
return loadPreferences(OptionsHandler.loadPageFromXml(xmlPath));
}
/**
* Load the persisted preferences for the specified element and all of its descendants.
* @param elt the root of element for which preferences are loaded.
* @return elt, for method chaining.
*/
protected OptionElement loadPreferences(final OptionElement elt) {
final OptionsHandler.OptionNode optionNode = OptionsHandler.buildPersistenceGraph(elt);
OptionsHandler.loadPreferences(optionNode, OptionsHandler.getPreferences());
return elt;
}
/**
* Save the persisted preferences for the specified element and all of its descendants.
* @param elt the root element for which preferences are saved.
* @return elt, for method chaining.
*/
protected OptionElement savePreferences(final OptionElement elt) {
final OptionsHandler.OptionNode optionNode = OptionsHandler.buildPersistenceGraph(elt);
OptionsHandler.savePreferences(optionNode, OptionsHandler.getPreferences());
return elt;
}
/**
* Make the specified dialog ready for display.
* @param dialog the dialog to prepare.
* @param comp the dialog"'s content.
* @param location the dialog's location, may be {@code null}.
*/
protected void readyDialog(final JDialog dialog, final JComponent comp, final Point location) {
dialog.getContentPane().add(comp);
dialog.pack();
dialog.setLocationRelativeTo(null);
if (location != null) dialog.setLocation(location);
dialog.setVisible(true);
}
/**
* Dispose of the specified dialog.
* @param dialog the dialog to dispose of.
*/
protected void disposeDialog(final JDialog dialog) {
dialog.setVisible(false);
dialog.dispose();
}
}