Home | History | Annotate | Download | only in control
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 package org.opensolaris.os.vp.panel.swing.control;
     28 
     29 import java.awt.event.*;
     30 import org.opensolaris.os.vp.panel.common.ClientContext;
     31 import org.opensolaris.os.vp.panel.common.action.*;
     32 import org.opensolaris.os.vp.panel.common.control.*;
     33 import org.opensolaris.os.vp.panel.common.model.PanelDescriptor;
     34 import org.opensolaris.os.vp.panel.swing.view.SettingsPanel;
     35 import org.opensolaris.os.vp.util.misc.ChangeableAggregator;
     36 
     37 /**
     38  * {@code SwingSettingsControl} extends class {@link SwingControl} to provide
     39  * support for {@link SettingsPanel}s.
     40  */
     41 public class SwingSettingsControl<P extends PanelDescriptor,
     42     C extends SettingsPanel> extends SwingControl<P, C> {
     43 
     44     //
     45     // Constructors
     46     //
     47 
     48     public SwingSettingsControl(String id, String name, ClientContext context) {
     49 	super(id, name, context);
     50     }
     51 
     52     public SwingSettingsControl(String id, String name, P descriptor) {
     53 	super(id, name, descriptor);
     54     }
     55 
     56     //
     57     // SwingControl methods
     58     //
     59 
     60     /**
     61      * Returns {@code getComponent().getChangeableAggregator()}.
     62      */
     63     @Override
     64     public ChangeableAggregator getChangeableAggregator() {
     65 	C comp = getComponent();
     66 	if (comp != null) {
     67 	    return comp.getChangeableAggregator();
     68 	}
     69 	return null;
     70     }
     71 
     72     //
     73     // SwingSettingsControl methods
     74     //
     75 
     76     /**
     77      * Adds a listener to the given {@link SettingsPanel}'s
     78      * <strong>Apply</strong> button to invoke this {@link Control}'s save
     79      * action.
     80      */
     81     protected void addDefaultApplyAction(SettingsPanel panel) {
     82 	panel.getButtonBar().getApplyButton().addActionListener(
     83 	    new ActionListener() {
     84 		@Override
     85 		public void actionPerformed(ActionEvent e) {
     86 		    getSaveAction().asyncInvoke();
     87 		}
     88 	    });
     89     }
     90 
     91     /**
     92      * Adds a listener to the given {@link SettingsPanel}'s
     93      * <strong>Back</strong> button to navigate up one level in the navigation
     94      * stack.
     95      */
     96     protected void addDefaultBackAction(SettingsPanel panel) {
     97 	panel.getButtonBar().getBackButton().addActionListener(
     98 	    new ActionListener() {
     99 		@Override
    100 		public void actionPerformed(ActionEvent e) {
    101 		    getNavigator().goToAsync(false, SwingSettingsControl.this,
    102 			Navigator.PARENT_NAVIGABLE);
    103 		}
    104 	    });
    105     }
    106 
    107     /**
    108      * Adds a listener to the given {@link SettingsPanel}'s
    109      * <strong>Cancel</strong> button to call {@link #doQuit}, if {@code quit}
    110      * is {@code true}, or {@link #doCancel}, if {@code quit} is {@code false}.
    111      */
    112     protected void addDefaultCancelAction(final SettingsPanel panel,
    113 	final boolean quit) {
    114 
    115 	panel.getButtonBar().getCancelButton().addActionListener(
    116 	    new ActionListener() {
    117 		@Override
    118 		public void actionPerformed(ActionEvent e) {
    119 		    if (quit) {
    120 			doQuit();
    121 		    } else {
    122 			doCancel();
    123 		    }
    124 		}
    125 	    });
    126     }
    127 
    128     /**
    129      * Adds a listener to the given {@link SettingsPanel}'s
    130      * <strong>Close</strong> button to call {@link #doSaveAndQuit}, if {@code
    131      * quit} is {@code true}, or {@link #doOkay}, if {@code quit} is {@code
    132      * false}.
    133      */
    134     protected void addDefaultCloseAction(final SettingsPanel panel,
    135 	final boolean quit) {
    136 
    137 	panel.getButtonBar().getCloseButton().addActionListener(
    138 	    new ActionListener() {
    139 		@Override
    140 		public void actionPerformed(ActionEvent e) {
    141 		    if (quit) {
    142 			doSaveAndQuit();
    143 		    } else {
    144 			doOkay();
    145 		    }
    146 		}
    147 	    });
    148     }
    149 
    150     /**
    151      * Adds a listener to the given {@link SettingsPanel}'s
    152      * <strong>Help</strong> button to {@link DefaultControl#loadHelpURL show}
    153      * this {@code DefaultControl}'s {@link DefaultControl#getHelpURL help}.
    154      */
    155     protected void addDefaultHelpAction(SettingsPanel panel) {
    156 	panel.getButtonBar().getHelpButton().addActionListener(
    157 	    new ActionListener() {
    158 		@Override
    159 		public void actionPerformed(ActionEvent e) {
    160 		    loadHelpURL();
    161 		}
    162 	    });
    163     }
    164 
    165     /**
    166      * Adds a listener to the given {@link SettingsPanel}'s
    167      * <strong>Okay</strong> button to call {@link #doSaveAndQuit}, if {@code
    168      * quit} is {@code true}, or {@link #doOkay}, if {@code quit} is {@code
    169      * false}.
    170      */
    171     protected void addDefaultOkayAction(final SettingsPanel panel,
    172 	final boolean quit) {
    173 
    174 	panel.getButtonBar().getOkayButton().addActionListener(
    175 	    new ActionListener() {
    176 		@Override
    177 		public void actionPerformed(ActionEvent e) {
    178 		    if (quit) {
    179 			doSaveAndQuit();
    180 		    } else {
    181 			doOkay();
    182 		    }
    183 		}
    184 	    });
    185     }
    186 
    187     /**
    188      * Adds a listener to the given {@link SettingsPanel}'s
    189      * <strong>Quit</strong> button to close this instance.
    190      */
    191     protected void addDefaultQuitAction(SettingsPanel panel) {
    192 	panel.getButtonBar().getQuitButton().addActionListener(
    193 	    new ActionListener() {
    194 		@Override
    195 		public void actionPerformed(ActionEvent e) {
    196 		    doQuit();
    197 		}
    198 	    });
    199     }
    200 
    201     /**
    202      * Adds a listener to the given {@link SettingsPanel}'s
    203      * <strong>Reset</strong> button to invoke this {@link Control}'s reset
    204      * action.
    205      */
    206     protected void addDefaultResetAction(SettingsPanel panel) {
    207 	panel.getButtonBar().getResetButton().addActionListener(
    208 	    new ActionListener() {
    209 		@Override
    210 		public void actionPerformed(ActionEvent e) {
    211 		    getResetAction().asyncInvoke();
    212 		}
    213 	    });
    214     }
    215 
    216     /**
    217      * Asynchronously closes this instance.
    218      */
    219     public void doQuit() {
    220 	final StructuredAction<?, ?, ?> resetAction = getResetAction();
    221 	resetAction.asyncExec(
    222 	    new Runnable() {
    223 		@Override
    224 		public void run() {
    225 		    try {
    226 			getClientContext().closeInstance(true);
    227 		    } catch (ActionException ignore) {
    228 		    }
    229 		}
    230 	    });
    231     }
    232 
    233     /**
    234      * Asynchronously invokes this {@link Control}'s save action, then closes
    235      * this instance.
    236      */
    237     public void doSaveAndQuit() {
    238 	final StructuredAction<?, ?, ?> saveAction = getSaveAction();
    239 	saveAction.asyncExec(
    240 	    new Runnable() {
    241 		@Override
    242 		public void run() {
    243 		    try {
    244 			saveAction.invoke();
    245 			getClientContext().closeInstance(false);
    246 		    } catch (ActionException ignore) {
    247 		    }
    248 		}
    249 	    });
    250     }
    251 }
    252