123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.modes;
-
- import com.dmdirc.addons.ui_swing.SwingController;
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.addons.ui_swing.components.text.TextLabel;
-
- import java.awt.Insets;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeSet;
-
- import javax.swing.BorderFactory;
- import javax.swing.JCheckBox;
- import javax.swing.JPanel;
- import javax.swing.UIManager;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Abstract panel to retrieve, show and set user and channel modes.
- */
- public abstract class ModesPane extends JPanel {
-
- /** Serial version UID. */
- private static final long serialVersionUID = 1;
- /** The checkboxes used for boolean modes. */
- private final Map<String, JCheckBox> modeCheckBoxes;
- /** The ParamModePanels used for parameter-requiring modes. */
- private final Map<String, ParamModePanel> modeInputs;
- /** Boolean modes panel. */
- private final JPanel booleanModesPanel;
- /** Param modes panel. */
- private final JPanel paramModesPanel;
- /** Modes set, used for layout. */
- private final Set<String> modes;
-
- public ModesPane() {
- super();
-
- setLayout(new MigLayout("fill, wmax 100%, wrap 1"));
- booleanModesPanel = new JPanel(new MigLayout("wrap 2"));
- paramModesPanel = new JPanel(new MigLayout("wrap 2"));
- modes = new TreeSet<String>(new ModesComparator());
- modeCheckBoxes = new HashMap<String, JCheckBox>();
- modeInputs = new HashMap<String, ParamModePanel>();
- this.setOpaque(UIUtilities.getTabbedPaneOpaque());
- }
-
- public Map<String, JCheckBox> getBooleanModes() {
- return modeCheckBoxes;
- }
-
- public Map<String, ParamModePanel> getParamModes() {
- return modeInputs;
- }
-
- /** Initialises the modes panel. */
- protected void initModesPanel() {
- getBooleanModes().clear();
- final String booleanModes = getAvailableBooleanModes();
- final String ourBooleanModes = getOurBooleanModes();
- // Lay out all the boolean mode checkboxes
- for (int i = 0; i < booleanModes.length(); i++) {
- final String mode = booleanModes.substring(i, i + 1);
- final boolean state = ourBooleanModes.split(" ")[0].contains(
- mode.subSequence(0, 1));
- final String text = getModeText(mode);
- final String tooltip = getModeTooltip(mode);
-
- final JCheckBox checkBox = new JCheckBox(text, state);
- checkBox.setMargin(new Insets(0, 0, 0, 0));
- checkBox.setToolTipText(tooltip);
- checkBox.setOpaque(false);
-
- getBooleanModes().put(mode, checkBox);
- if (isModeEnabled(mode)) {
- checkBox.setEnabled(true);
- } else if (!isModeSettable(mode)) {
- checkBox.setEnabled(false);
- }
- }
-
- getParamModes().clear();
- final String paramModes = getAllParamModes();
- // Lay out all the parameter-requiring modes
- for (int i = 0; i < paramModes.length(); i++) {
- final String mode = paramModes.substring(i, i + 1);
- final String value = getParamModeValue(mode);
- final boolean state = ourBooleanModes.split(" ")[0].contains(
- mode.subSequence(0, 1));
-
- final ParamModePanel panel = new ParamModePanel(mode, state, value,
- getSwingController());
-
- getParamModes().put(mode, panel);
- }
-
- layoutComponents();
- }
-
- /** Lays out the components. */
- protected void layoutComponents() {
- booleanModesPanel.removeAll();
- paramModesPanel.removeAll();
- modes.clear();
- modes.addAll(getBooleanModes().keySet());
- if (modes.isEmpty()) {
- booleanModesPanel.add(new TextLabel("No boolean modes."));
- }
- for (String mode : modes) {
- booleanModesPanel.add(getBooleanModes().get(mode), "growx, wmax 49%-rel*4");
- }
- modes.clear();
-
- modes.addAll(getParamModes().keySet());
- if (modes.isEmpty()) {
- paramModesPanel.add(new TextLabel("No parameter modes."));
- }
- for (String mode : modes) {
- final ParamModePanel modePanel = getParamModes().get(mode);
- paramModesPanel.add(modePanel.getCheckboxComponent());
- paramModesPanel.add(modePanel.getValueComponent(), "growx, pushx");
- }
-
- booleanModesPanel.setBorder(BorderFactory.createTitledBorder(
- UIManager.getBorder("TitledBorder.border"), "Boolean modes"));
- paramModesPanel.setBorder(BorderFactory.createTitledBorder(
- UIManager.getBorder("TitledBorder.border"), "Parameter modes"));
-
- booleanModesPanel.setOpaque(UIUtilities.getTabbedPaneOpaque());
- paramModesPanel.setOpaque(UIUtilities.getTabbedPaneOpaque());
-
- add(booleanModesPanel, "grow, wmax 100%");
- add(paramModesPanel, "grow, wmax 100%");
- }
-
- /** Updates the panel. */
- public void update() {
- setVisible(false);
- removeAll();
- initModesPanel();
- setVisible(true);
- }
-
- /**
- * Gets the description for a given mode
- *
- * @param mode Mode to be described
- *
- * @return Description of mode
- */
- private String getModeText(final String mode) {
- if (hasModeValue(mode)) {
- return getModeValue(mode) + " [+" + mode + "]";
- } else {
- return "Mode " + mode;
- }
- }
-
- /**
- * Gets a tooltip text for a mode, shows the description and mode value.
- *
- * @param mode Mode to be described
- *
- * @return Tooltip text for a mode
- */
- private String getModeTooltip(final String mode) {
- if (hasModeValue(mode)) {
- return "Mode " + mode + ": " + getModeValue(mode);
- } else {
- return "Mode " + mode + ": Unknown";
- }
- }
-
- /**
- * Processes the channel settings dialog and constructs a mode string for
- * changed modes, then sends this to the server.
- */
- public void save() {
- boolean changed = false;
- final String booleanModes = getAvailableBooleanModes();
- final String ourBooleanModes = getOurBooleanModes();
- final String paramModes = getAllParamModes();
-
- for (int i = 0; i < booleanModes.length(); i++) {
- final String mode = booleanModes.substring(i, i + 1);
- final boolean state = ourBooleanModes.split(" ")[0].contains(
- mode.subSequence(0, 1));
-
- if (getBooleanModes().get(mode) != null
- && state != getBooleanModes().get(mode).isSelected()) {
- changed = true;
- alterMode(getBooleanModes().get(mode).isSelected(), mode, "");
- }
- }
-
- for (int i = 0; i < paramModes.length(); i++) {
- final String mode = paramModes.substring(i, i + 1);
- final String value = getParamModeValue(mode);
- final boolean state =
- ourBooleanModes.split(" ")[0].contains(
- mode.subSequence(0, 1));
- final ParamModePanel paramModePanel = getParamModes().get(mode);
-
- if (state != paramModePanel.getState()
- || !value.equals(paramModePanel.getValue())) {
- changed = true;
- alterMode(paramModePanel.getState(), mode, paramModePanel.getValue());
- }
- }
- if (changed) {
- flushModes();
- }
- }
-
- /**
- * Returns the Swing controller to grab various objects from.
- *
- * @return Swing controller
- */
- public abstract SwingController getSwingController();
-
- /**
- * Checks whether there is a plain text description for this mode.
- *
- * @param mode Mode to check
- *
- * @return true iif there is a plain text description
- */
- public abstract boolean hasModeValue(final String mode);
-
- /**
- * Returns the plain text description for a mode.
- *
- * @param mode Mode to check
- *
- * @return Valid plain text description for a mode, or an empty string
- */
- public abstract String getModeValue(final String mode);
-
- /**
- * Is this mode enabled, are we allowed to set it?
- *
- * @param mode Mode to check
- *
- * @return true iif the mode is enabled
- */
- public abstract boolean isModeEnabled(final String mode);
-
- /**
- * Is this mode settable by us?
- *
- * @param mode Mode to check
- *
- * @return true iif the mode is settable
- */
- public abstract boolean isModeSettable(final String mode);
-
- /**
- * Returns all the available boolean modes.
- *
- * @return string containing all available boolean modes
- */
- public abstract String getAvailableBooleanModes();
-
- /**
- * Returns which boolean modes are set.
- *
- * @return string containing all set boolean modes
- */
- public abstract String getOurBooleanModes();
-
- /**
- * Returns all available param modes.
- *
- * @return string containing all available param modes
- */
- public abstract String getAllParamModes();
-
- /**
- * Returns the value of a given parameter mode.
- *
- * @param mode Mode to check
- *
- * @return value of a mode or an empty string
- */
- public abstract String getParamModeValue(final String mode);
-
- /**
- * Queues the specified mode change to be flushed with flushmodes.
- *
- * @param add Whether to add or remove the specified mode
- * @param mode The mode to be changed
- * @param parameter Optional parameter needed to make change
- */
- public abstract void alterMode(final boolean add, final String mode,
- final String parameter);
-
- /**
- * Sends the queued mode changes to the server.
- */
- public abstract void flushModes();
- }
|