2014-11-24 17:40:06 +01:00
|
|
|
/*
|
2016-10-17 11:47:25 +02:00
|
|
|
* Copyright 2015 dorkbox, llc
|
2014-11-24 17:40:06 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-02-12 01:51:15 +01:00
|
|
|
package dorkbox.systemTray;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2016-10-23 23:27:13 +02:00
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
import dorkbox.systemTray.peer.CheckboxPeer;
|
2016-10-31 11:32:49 +01:00
|
|
|
import dorkbox.util.SwingUtil;
|
2016-10-23 23:27:13 +02:00
|
|
|
|
2016-10-17 11:47:25 +02:00
|
|
|
/**
|
|
|
|
* This represents a common menu-checkbox entry, that is cross platform in nature
|
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
@SuppressWarnings("unused")
|
2015-06-28 01:47:02 +02:00
|
|
|
public
|
2016-10-23 23:27:13 +02:00
|
|
|
class Checkbox extends Entry {
|
2016-10-24 01:43:32 +02:00
|
|
|
private boolean isChecked = false;
|
|
|
|
private String text;
|
|
|
|
private ActionListener callback;
|
2016-10-23 23:27:13 +02:00
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
private boolean enabled = true;
|
|
|
|
private char mnemonicKey;
|
2016-10-23 23:27:13 +02:00
|
|
|
|
|
|
|
public
|
|
|
|
Checkbox() {
|
|
|
|
this(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public
|
|
|
|
Checkbox(final String text, final ActionListener callback) {
|
|
|
|
this.text = text;
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
public
|
|
|
|
Checkbox(final String text) {
|
|
|
|
this(text, null);
|
|
|
|
}
|
|
|
|
|
2016-10-23 23:27:13 +02:00
|
|
|
/**
|
2016-10-24 01:43:32 +02:00
|
|
|
* @param peer the platform specific implementation for all actions for this type
|
2016-10-23 23:27:13 +02:00
|
|
|
* @param parent the parent of this menu, null if the parent is the system tray
|
|
|
|
* @param systemTray the system tray (which is the object that sits in the system tray)
|
|
|
|
*/
|
|
|
|
public synchronized
|
2016-10-24 01:43:32 +02:00
|
|
|
void bind(final CheckboxPeer peer, final Menu parent, final SystemTray systemTray) {
|
|
|
|
super.bind(peer, parent, systemTray);
|
|
|
|
|
|
|
|
peer.setEnabled(this);
|
|
|
|
peer.setText(this);
|
|
|
|
peer.setCallback(this);
|
|
|
|
peer.setShortcut(this);
|
|
|
|
peer.setChecked(this);
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
/**
|
|
|
|
* @return true if this checkbox is selected, false if not.
|
|
|
|
*/
|
|
|
|
public synchronized
|
|
|
|
boolean getChecked() {
|
|
|
|
return isChecked;
|
|
|
|
}
|
2016-10-23 23:27:13 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-31 11:32:49 +01:00
|
|
|
* Sets the checked on/off status for this entry
|
2016-10-23 23:27:13 +02:00
|
|
|
*
|
2016-10-31 11:32:49 +01:00
|
|
|
* @param isChecked true to show the checkbox, false to hide it
|
2016-10-23 23:27:13 +02:00
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-31 11:32:49 +01:00
|
|
|
void setState(boolean isChecked) {
|
|
|
|
this.isChecked = isChecked;
|
2016-10-23 23:27:13 +02:00
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
if (peer != null) {
|
|
|
|
((CheckboxPeer) peer).setChecked(this);
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-24 01:43:32 +02:00
|
|
|
* Gets the callback assigned to this menu entry
|
2016-10-23 23:27:13 +02:00
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
|
|
|
ActionListener getCallback() {
|
|
|
|
return callback;
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-24 01:43:32 +02:00
|
|
|
* Sets a callback for a menu entry. This is the action that occurs when one clicks the menu entry
|
|
|
|
*
|
|
|
|
* @param callback the callback to set. If null, the callback is safely removed.
|
2016-10-23 23:27:13 +02:00
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
|
|
|
void setCallback(final ActionListener callback) {
|
|
|
|
this.callback = callback;
|
|
|
|
if (peer != null) {
|
|
|
|
((CheckboxPeer) peer).setCallback(this);
|
|
|
|
}
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:45:11 +01:00
|
|
|
/**
|
2016-10-23 23:27:13 +02:00
|
|
|
* @return true if this item is enabled, or false if it is disabled.
|
2015-11-09 19:45:11 +01:00
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-23 23:27:13 +02:00
|
|
|
boolean getEnabled() {
|
|
|
|
return this.enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables, or disables the entry.
|
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-23 23:27:13 +02:00
|
|
|
void setEnabled(final boolean enabled) {
|
|
|
|
this.enabled = enabled;
|
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
if (peer != null) {
|
|
|
|
((CheckboxPeer) peer).setEnabled(this);
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the text label that the menu entry has assigned
|
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-23 23:27:13 +02:00
|
|
|
String getText() {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specifies the new text to set for a menu entry
|
|
|
|
*
|
|
|
|
* @param text the new text to set
|
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-23 23:27:13 +02:00
|
|
|
void setText(final String text) {
|
|
|
|
this.text = text;
|
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
if (peer != null) {
|
|
|
|
((CheckboxPeer) peer).setText(this);
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the shortcut key for this menu entry (Mnemonic) which is what menu entry uses to be "selected" via the keyboard while the
|
|
|
|
* menu is displayed.
|
2016-10-24 01:43:32 +02:00
|
|
|
* <p>
|
2016-10-23 23:27:13 +02:00
|
|
|
* Mnemonics are case-insensitive, and if the character defined by the mnemonic is found within the text, the first occurrence
|
|
|
|
* of it will be underlined.
|
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-23 23:27:13 +02:00
|
|
|
char getShortcut() {
|
|
|
|
return this.mnemonicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a menu entry shortcut key (Mnemonic) so that menu entry can be "selected" via the keyboard while the menu is displayed.
|
2016-10-24 01:43:32 +02:00
|
|
|
* <p>
|
2016-10-23 23:27:13 +02:00
|
|
|
* Mnemonics are case-insensitive, and if the character defined by the mnemonic is found within the text, the first occurrence
|
|
|
|
* of it will be underlined.
|
|
|
|
*
|
|
|
|
* @param key this is the key to set as the mnemonic
|
|
|
|
*/
|
2016-10-24 01:43:32 +02:00
|
|
|
public synchronized
|
2016-10-23 23:27:13 +02:00
|
|
|
void setShortcut(final char key) {
|
|
|
|
this.mnemonicKey = key;
|
|
|
|
|
2016-10-24 01:43:32 +02:00
|
|
|
if (peer != null) {
|
|
|
|
((CheckboxPeer) peer).setShortcut(this);
|
2016-10-23 23:27:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 11:32:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a menu entry shortcut key (Mnemonic) so that menu entry can be "selected" via the keyboard while the menu is displayed.
|
|
|
|
*
|
|
|
|
* Mnemonics are case-insensitive, and if the character defined by the mnemonic is found within the text, the first occurrence
|
|
|
|
* of it will be underlined.
|
|
|
|
*
|
|
|
|
* @param key this is the VK key to set as the mnemonic
|
|
|
|
*/
|
|
|
|
public synchronized
|
|
|
|
void setShortcut(final int key) {
|
|
|
|
this.mnemonicKey = SwingUtil.getFromVirtualKey(key);
|
|
|
|
|
|
|
|
if (peer != null) {
|
|
|
|
((CheckboxPeer) peer).setShortcut(this);
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|